<?php
/**
 * Project:     astat - simple site engine
 * File:        /core/database/mysqli.class.php
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @link http://www.astat.org SVN: $URL: http://svn.astat.org/astat/trunk/core/database/mysqli.class.php $
 * @copyright 2025 becast.at
 * @author Bernhard Jaud <bernhard at becast dot at>
 * @package astat core
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version $Id: mysqli.class.php 126 2010-02-21 21:07:52Z genuineparts $
*/
 
$module["db"]["name"]="Database Class (mysqli)";
$module["db"]["ver"]="1.0.1";
class db {
	var $mysqli;
	var $host="";
	var $user="root";
	var $password="";
	var $db="astat";
	var $encoding;
	var $logger;
	var $logging;
	var $abfragen=0;
	var $exception;
	public const ASSOC = 1;
	public const NUM = 2;
	public const BOTH = 3;
	
	function __construct($host,$user,$password,$db,$encoding="utf8",$logging=false,$exception=false) {
		global $log,$config,$root,$logger;
		$this->mysqli;
		$this->host=$host;
		$this->user=$user;
		$this->password=$password;
		$this->db=$db;
		$this->logging=$logging;
		$this->exception=$exception;
		if($this->logging!=FALSE || DEBUG!=FALSE){
			$this->logger=new logger("file",dirname(dirname(dirname(__FILE__))).'/logs/mysql_debug.log',5);
		}
		$this->encoding=$encoding;

		if($this->logging)
			$this->logger->write("mySQLi Klasse instanziert", 5,__LINE__,__FILE__);
			
			if(!is_object($this->mysqli)){
				if($this->logging)
					$this->logger->write("Connection zu mySQL Server besteht nicht.", 5,__LINE__,__FILE__);
					
					$this->connect($this->host,$this->user,$this->password,$this->db);
				}else{
					if($this->logging)
						$this->logger->write("Connection zu mySQL Server besteht.", 5,__LINE__,__FILE__);
				}
			if($this->encoding)
			$this->mysqli->set_charset($this->encoding);
	}

	function __destruct() {
	$this->disconnect();
	}

	function connect($host, $user, $password, $db){
	    $this->mysqli=@new mysqli($host, $user, $password,$db);
		if($this->logging)
			$this->logger->write("Connect to Mysql Server", 5,__LINE__,__FILE__);

	   	if (mysqli_connect_errno()) {
			if($this->exception){
				 throw new Exception('mySQLi Connect failed');
			}else{
				if($this->logging)
					$this->logger->write("Connect Failed ". mysqli_connect_error(), 2,__LINE__,__FILE__);

				trigger_error("mySQLi Connect failed",E_USER_ERROR);
				die();
			}
		}
	  }


	function disconnect(){
		if($this->logging)
			$this->logger->write("Closing Mysqli Connection.", 5,__LINE__,__FILE__);

   		$this->mysqli->close();
   	}

	function query($query){
		global $abfragen;

		if($this->logging)
			$this->logger->write($query, 5);

	    $this->abfragen++;

	    $res = $this->mysqli->query($query);
		if($this->mysqli->error && $this->logging){
			$this->logger->write("SQL ERROR: ".$this->mysqli->error,2);
		}
	    return $res;
    	}

	function querys(){
	    return $this->abfragen;
	    }
	
	function num_rows($result){
	    $rows = $result->num_rows;
	    return $rows;
	    }

	function fetch_row($result){
	    $row = $result->fetch_row();
	    return $row;
	    }

	function fetch_array($result){

	    $row = array();
	    $row = $result->fetch_array(MYSQLI_ASSOC);
	    return $row;
	    }

	function fetch_array_num($result){

	    $row = array();
	    $row = $result->fetch_array(MYSQLI_NUM );
	    return $row;
	    }

	function escape($string){
	    $return = $this->mysqli->real_escape_string($string);
	    return $return;
	}
	
	function escape_binary($string){
		return "X'".$this->escape(bin2hex($string))."'";
	}
	
	function fetch_object($result){
	    $row = $result->fetch_object();
	    return $row;
	}

	function error(){
	    $error = $this->mysqli->error;
	    return $error;
	}
	
	function errno(){
	    $errno = $this->mysqli->errno;
	    return $errno ;
	}

	function free_result($result){
	    $result->free_result();
	}
	
	function last_id(){
	    $id = $this->mysqli->insert_id;
	    return $id;
	}
}
?>
