class _pdo extends PDO {
	var $query;
	private $type;
	private $values;
	
	function __construct($db){
        try{
            parent::__construct($db['type'].':host='.$db['server'].';dbname='.$db['db_name'].';'.$db['charset'],$db['user'],$db['pass'],array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
            $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('_pdo_statement'));
			$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
			$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
			$this->setAttribute(PDO::MYSQL_ATTR_FOUND_ROWS, true);
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } 
        catch(PDOException $e){                
            die('Error: '. $e->getMessage());
        }
    }

	public function insert_id(){
		return $this->lastInsertId();
	}

	public function find($table, $id){
		$columns = $this->column($table);
		return $this->select($table)->where($columns['Field'] ." = ".$id)->limit(1)->result();
	}

	public function select($table){		
		$this->query = "SELECT * FROM ".$table." ";		
		return $this;		
	}

	public function left($condition){
		$this->query .= "LEFT JOIN ".$condition." ";
		return $this;
	}

	public function insert($table){
		$this->type = 'insert';
		
		$this->query = "INSERT INTO ".$table." ";
		return $this;
	}

	public function replace($table){
		$this->type = 'insert';
		
		$this->query = "REPLACE INTO ".$table." ";
		return $this;
	}

	public function update($table){
		$this->type = 'update';
		
		$this->query = "UPDATE ".$table." SET ";
		return $this;
	}

	public function values($values){
		$this->values = $values;
		
		$keys = array_keys($values);
		$vals = array_values($values);

		if ($this->type == 'insert'){
			$row = '(';
			for ($i = 0; $i<count($values); $i++){
				$row .= $keys[$i];
 
				if ($i != count($values) - 1 ){
					$row .= ', ';
				}
				else{
					$row .= ') VALUES (';
				}
			}	
			for ($i = 0; $i<count($values); $i++){
				$row .= ':'.$keys[$i];
				
				if ($i != count($values) - 1 ){
					$row .= ', ';
				}
				else{
					$row .= ')';
				}
			}	
			$this->query .= $row;
			$query = $this->prepare($this->query);
			$query->execute($values);
		}

		elseif ($this->type == 'update'){
			for ($i = 0; $i<count($values); $i++){
				$this->query .= $keys[$i].' = :'.$keys[$i].' '; 
				if ($i != count($values) - 1 ){
					$this->query .= ', ';	
				}
			} 
			return $this;
		}		
	}

	public function delete($table, $key = '')
	{
		if (empty($key)){
			$this->query = "DELETE FROM ".$table." ";		
			return $this;
		}
		else {
			$columns = $this->column($table);
			$this->delete($table)->where(''.$columns['Field'] .' = "'.$key.'"')->limit(1)->run();
		}
	}

	public function where($condition){
		$this->query .= " WHERE ".$condition;
		
		if ($this->type == 'update'){
			$query = $this->prepare($this->query);
			$query->execute($this->values);
			return $this;
		}	
		else{
			return $this;
		}
	}

	public function which($condition){
		$this->query = str_replace('*', $condition, $this->query);
		return $this;
	}

	public function group($condition){
		$this->query .= " GROUP BY ".$condition;
		return $this;		
	}

	public function have($condition){
		$this->query .= " HAVING ".$condition;
		return $this;		
	}

	public function order($condition){
		$this->query .= " ORDER BY ".$condition;
		return $this;
	}

	public function limit($limit = 3000){
		$this->query .= " LIMIT ".$limit." ";
		return $this;
	}

	public function offset($offset = 3000){
		$this->query .= " OFFSET ".$offset." ";
		return $this;
	}

	public function column($table){
		$query = $this->query("SHOW COLUMNS FROM ".$table);
		return $query->fetch();
	}

	public final function write(){
		echo $this->query;	
	}

	public final function run($return = false){
		if ($return){
			return $this->query($this->query);
		}
		
		$this->query($this->query);
	}

	public final function result($key = ''){
		$query = $this->run(true);
		
		if (!$key){
			return $query->fetch();
		}
		else{
			$result = $query->fetch();
			return $result[$key];	
		}
	}
	
	public final function results(){
		$query = $this->run(true);
		
		$query = $this->query($this->query);
		return $query->fetch_array();
	}

	public final function results_pairs($key, $values = '') {
		$results = $this->results();
		
		foreach ($results AS $result){
			foreach ($values AS $value){
				$res[$result[$key]][$value] = $result[$value];
			}
		}
			
		return $res;
	}
}

class _pdo_statement extends PDOStatement 
{
    public function fetch_array(){
        return $this->fetchAll(PDO::FETCH_ASSOC);
    }

	public function fetch_assoc($result){
		return $this->fetchAll(PDO::FETCH_ASSOC);
	}

	public function num_rows(){
		return $this->rowcount();
	}

	public function affected_rows(){
		return $this->rowcount();
	}
}

function select($table){
	global $pdo;	
	return $pdo->select($table);
}

function insert($table){
	global $pdo;	
	return $pdo->insert($table);
}

function replace($table){
	global $pdo;	
	return $pdo->replace($table);
}

function update($table){
	global $pdo;	
	return $pdo->update($table);
}

function delete($table, $key = ''){
	global $pdo;	
	return $pdo->delete($table, $key);
}

Comments

Immurce için bir cevap yazın Cevabı iptal et

E-posta hesabınız yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>