<?PHP

class DB {
	
	private $database;
	private $conn;
	
	function __construct($db = null){ 
		if($db == null) {
			$this->database = DB_MASTER;
		} else {
			$this->database = $db;
		}
	}
	
	function db_connect() {
		try{
			$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, $this->database);
			if ($this->conn->connect_errno) {
				printf("Connect failed: %s\n", $this->conn->connect_error);
				exit();
			}
		} catch(Exception $e) {
			throw new Exception($e->getMessage());
		}
	}
	
	function db_close() {
		$this->conn->close();
	}
	
	function real_escape_string($string) {
		$this->db_connect();
		return $this->conn->real_escape_string($string);
		$this->db_close();
	}
	
	function insert($table, $params = array(), $multi = false){
		if(!empty($params)) {
			if($multi) {
				$col_string = '';
				$val_string = '';
				$j = 1;
				foreach($params as $row) {
					$col_string = '';
					$val_string .= '(';
					$i = 1;
					foreach($row as $col=>$value) {
						$col_string .= sprintf("%s", $col);
						$val_string .= sprintf("'%s'", $value);
						
						if($i < count($row)) {
							$col_string .= ', ';
							$val_string .= ', ';
						}
						$i++;
					}
					
					$val_string .= ')';
					if($j < count($params)) {
						$val_string .= ', ';
					}
					$j++;
				}
				
				$query = sprintf("INSERT INTO %s (%s) VALUES %s", $table, $col_string, $val_string);
			} else {
				$col_string = '';
				$val_string = '';
				$i = 1;
				foreach($params as $col=>$value) {
					$col_string .= sprintf("%s", $col);
					$val_string .= sprintf("'%s'", $value);
					if($i < count($params)) {
						$col_string .= ', ';
						$val_string .= ', ';
					}
					$i++;
				}
				
				$query = sprintf("INSERT INTO %s (%s) VALUES (%s)", $table, $col_string, $val_string);
			}
		}
		
		
		if(DEBUG) {
			echo $query;
		}
		
		$this->db_connect();
		$res = $this->conn->query($query);
		$res = $this->conn->insert_id;
		$this->db_close();
		
		return $res;
	}
	
	function update($table, $params = array()){
		if(!empty($params)) {
			$set_string = '';
			$where_string = '';
			if(isset($params['where'])) {
				$i = 1;
				foreach($params['where'] as $col=>$value) {
					$where_string .= sprintf(" %s='%s'", $col, $value);
					if($i < count($params['where'])) {
						$where_string .= ' AND';
					}
					$i++;
				}
				unset($params['where']);
				$i = 1;
				
				foreach($params as $col=>$value) {
					$set_string .= sprintf(" %s='%s'", $col, $value);
					if($i < count($params)) {
						$set_string .= ' ,';
					}
					$i++;
				}
			}
		}
		
		$query = sprintf("UPDATE %s SET%s WHERE %s", $table, $set_string, $where_string);
		
		if(DEBUG) {
			echo $query;
		}
		
		$this->db_connect();
		
		$res = $this->conn->query($query);
			
		$this->db_close();
		
		return $res;
	}	
	
	function remove($table, $params = array()){
		
		if(!empty($params)) {
			$where_string = '';
			$i = 1;
			foreach($params as $col=>$value) {
				$where_string .= sprintf(" %s='%s'", $col, $value);
				if($i < count($params['where'])) {
					$where_string .= ' AND';
				}
				$i++;
			}
		}
		
		$query = sprintf("DELETE FROM %s WHERE %s", $table, $where_string);
		
		if(DEBUG) {
			echo $query;
		}
		
		$this->db_connect();
		
		$res = $this->conn->query($query);
			
		$this->db_close();
		
		return $res;
	}
	
	function query($query, $single_row=false){
		if(DEBUG) {
			echo $query;
		}
		$this->db_connect();
		
		$res = $this->conn->query($query);
		if($res) {
			if(is_object($res)) {
				if($res->num_rows > 0) {
					while ($row = $res->fetch_assoc()) {
						$result[] = $row;
					}
				} else {
					$result = array();
				}
				$res->close();
			} else {
				$result = array($res);
			}
		}
		
		$this->db_close();
		
		if(isset($result)){
			return new Result($result, $single_row);
		}
		return false;
	}
}

class Result {
	public $num_rows = 0;
	public $result = null;
	
	function __construct($result, $single_row=false){
		if(!empty($result)) {
			$this->num_rows = count($result);
			if($single_row) {
				$this->result = $result[0];
			} else {
				$this->result = $result;
			}
		}
	}
}
