Skip to content

Instantly share code, notes, and snippets.

@lbehm
Created May 24, 2012 07:42
Show Gist options
  • Save lbehm/2780067 to your computer and use it in GitHub Desktop.
Save lbehm/2780067 to your computer and use it in GitHub Desktop.
draft for new database access layer: object based query generation
<?php
class db_driver_mySQL{
private $connection;
private $handle=false;
public function __construct($connection){
if(is_string($connection)){
if(preg_match('/mySQL:\/\/\w+(:\w+)?@\w+(:\d+)?(\/\w+)?/',$connection)){
$connection=@parse_url($connection);
}
}
if(is_array($connection)){
$this->connection=array(
'scheme'=> 'mySQL',
'host'=> ((isset($connection['host']))? rawurldecode($connection['host']):'localhost'),
'port'=> ((isset($connection['port']))? rawurldecode($connection['port']):'3306'),
'user'=> ((isset($connection['user']))? rawurldecode($connection['user']):'root'),
'pass'=> ((isset($connection['pass']))? rawurldecode($connection['pass']):''),
'db'=> ((isset($connection['path']))? rawurldecode($connection['path']):'')
);
if(isset($connection['query'])){
parse_str($connection['query'],$options);
foreach($options as $k=>$v){
$this->connection['options'][$k]=((mb_strtoupper($v)=='TRUE')?true:((mb_strtoupper($v)=='FALSE')?false:$v));
}
unset($options);
}
unset($connection);
$this->connection['options']['flags']=(isset($this->connection['options']['flags']))?$this->connection['options']['flags']:0;
$this->handle=@mysql_connect($this->connection['host'].':'.$this->connection['port'],$this->connection['user'],$this->connection['pass'],false,$this->connection['options']['flags']);
if($this->handle){
if(isset($this->connection['options']['charset']))
$this->set_charset($this->connection['options']['charset']);
if($this->connection['db']!='')
$this->select($this->connection['db']);
return;
}
}
throw new Exception("Connection failed");
}
public function getHandle(){
return $this->handle;
}
public function select($db){}
public function __get($db){
if(!$this->handle)
return false;
return new db_driver_mySQL_db($this,$db);
}
public function close(){
if($this->handle){
@mysql_close($this->handle);
$this->handle=false;
}
}
public function set_charset($charset){
if(!$this->handle)
return false;
$this->connection['options']['charset']=$charset;
return @mysql_set_charset($charset, $this->handle);
}
}
/* *********************************** */
class db_driver_mySQL_db{
private $connection=false;
private $parent=false;
private $name;
public function getConnection(){
return $this->connection;
}
public function getName(){
return $this->name;
}
public function __construct($parent,$db){
$this->connection=$parent->getHandle();
$this->parent=$parent;
$this->name=$db;
@mysql_select_db($db,$this->connection);
}
public function __get($table){
if(!$this->connection)
return false;
return new db_driver_mySQL_table($this,$table);
}
}
/* *********************************** */
class db_driver_mySQL_table{
private $connection=false;
private $parent=false;
private $name;
private $query_type;
private $select=array();
private $limit_max=false;
private $limit_beginn=false;
public function getConnection(){
return $this->connection;
}
public function getName(){
return $this->name;
}
public function __construct($parent,$table){
$this->parent=$parent;
$this->connection=$parent->getConnection();
$this->name=$table;
}
public function select($field='*'){
$this->query_type='select';
if($field=='*')
$this->select=array();
else
$this->select[]=$field;
return $this;
}
public function limit($arg1=false,$arg2=false){
if($arg2!==false){
if($arg1!==false)
$this->limit_beginn=$arg1;
$this->limit_max=$arg2;
}
elseif($arg1!==false)
$this->limit_max=$arg1;
else{
$this->limit_max=false;
$this->limit_beginn=false;
}
return $this;
}
public function query(){
$query='';
switch($this->query_type){
case'select':
$query="SELECT ".((count($this->select))?implode(', ',$this->select).' ':'* ').'FROM '.$this->parent->getName().'.'.$this->name.' '.((($this->limit_beginn!==false && $this->limit_max!==false) || $this->limit_max!==false)?('LIMIT '.(($this->limit_beginn!==false)?$this->limit_beginn.', ':'').$this->limit_max.' '):'');
}
if($query!=''){
return new db_driver_mySQL_query($this,$query.';');
}
}
}
/* *********************************** */
class db_driver_mySQL_query{
private $connection=false;
private $parent=false;
private $handle;
private $query;
private $data=array();
public function getConnection(){
return $this->connection;
}
public function getHandle(){
return $this->handle;
}
public function __construct($parent,$query){
$this->parent=$parent;
$this->connection=$parent->getConnection();
$this->query=$query;
$this->handle=@mysql_query($query,$this->connection);
while($row=mysql_fetch_assoc($this->handle))
{
$this->data[]=$row;
}
mysql_data_seek($this->handle,0);
}
public function getData(){
return $this->data;
}
}
?>
<?php
require_once("db_driver_mysql.php");
$conn=new db_driver_mySQL("mySQL://root@localhost");
$data=$conn->db->table->select('column')->select('column2')->limit(50,100)->query()->getData();
var_dump($data);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment