Skip to content

Instantly share code, notes, and snippets.

@nerdsrescueme
Created October 7, 2011 18:48
Show Gist options
  • Save nerdsrescueme/1271062 to your computer and use it in GitHub Desktop.
Save nerdsrescueme/1271062 to your computer and use it in GitHub Desktop.
PDODB
<?php
use \Atom\DB\Connection as Conn;
class DB {
protected $connections = array();
public static function connection($id = 'default', $config = null)
{
if(!isset(static::$connections[$id]))
{
static::$connections[$id] = new static($config);
}
}
private $_connection;
private $config;
private $_quote_char;
private $_data = array();
private $_dirty = false;
private $_new = false;
private $_dirty_data = array();
private $_values = array();
private $_offset, $_limit;
public $_id = 'id';
protected __construct($config)
{
// Get $config values from DB.config.
// Load driver defaults
switch($this->connection->getAttribute(Conn::ATTR_DRIVER_NAME))
{
case 'pgsql':
case 'sqlsrv':
case 'dblib':
case 'mssql':
case 'sybase':
$this->_quote_char = '"';
case 'mysql':
case 'sqlite':
case 'sqlite2':
default:
$this->_quote_char = '`';
}
return $this;
}
public function get_connection()
{
return $this->_connection;
}
public function group_by($column)
{
$this->_group_by[] = $this->quote($column);
return $this;
}
public function limit($limit)
{
$this->_limit = $limit;
return $this;
}
public function offset($offset)
{
$this->_offset = $offset;
return $this;
}
public function order_by($column, $direction = 'ASC')
{
$this->_order_by[] = $this->quote($column).' '.$direction;
return $this;
}
public function paginate($offset, $limit)
{
$this->offset($offset);
$this->limit($limit);
return $this;
}
public function quote($quotable)
{
return $this->_quote_char.$quotable.$this->_quote_char;
}
public function where($column, $seperator, $value)
{
$this->_where[] = array($this->quote($column).$seperator.'?', $value);
return $this;
}
// IS NULL, IS NOT NULL
public function where_expr($column, $expression)
{
$this->_where[] = array($this->quote($column).' '.$expression);
return $this;
}
private function _where()
{
if(count($this->_where) === 0) return '';
$conditions = array();
foreach($this->_where as $where)
{
$conditions[] = $where[0];
if(isset($where[1])) $this->_values = array_merge($this->_values, $where[1]);
}
return 'WHERE '.join(' AND ', $conditions);
}
private function _group_by()
{
return ($count($this->_group_by) > 0) ? 'ORDER BY '.join(', ', $this->_group_by) : '';
}
private function _order_by()
{
return ($count($this->_order_by) > 0) ? 'ORDER BY '.join(', ', $this->_order_by) : '';
}
private function _limit()
{
return ($this->_limit !== null) ? 'LIMIT '.$this->_offset : '';
}
private function _offset()
{
return ($this->_offset !== null) ? 'OFFSET '.$this->_offset : '';
}
public function __set($property, $value)
{
$this->_data = $value;
$this->_dirty_data[$property] = $value;
$this->_dirty = true;
}
public function __get($property)
{
return (isset($this->_data[$property])) ? $this->_data[$property] : null;
}
public function __isset($property)
{
return isset($this->_data[$property]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment