Created
May 24, 2012 21:16
-
-
Save swvitaliy/2784282 to your computer and use it in GitHub Desktop.
mysql db wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class db { | |
/** | |
* @var null|array | |
*/ | |
protected static $config = null; | |
/** | |
* @var null|db | |
*/ | |
protected static $_inst = NULL; | |
public static function config ($values) { | |
return self::$config = $values; | |
} | |
/** | |
* @static | |
* @return null|db | |
*/ | |
public static function inst () { | |
if (is_null(self::$_inst)) { | |
self::$_inst = db::ctor(); | |
} | |
return self::$_inst; | |
} | |
public static function ctor ($config = null) { | |
if (!is_null($config)) | |
self::config($config); | |
return new self(true); | |
} | |
public static function connect () { return self::inst()->connection(); } | |
public static function qry ($sql) { return self::inst()->query($sql); } | |
public static function esc ($txt) { return mysql_real_escape_string($txt, db::connect()); } | |
protected $connect = null; | |
protected $result = null; | |
protected $options = array(); | |
public function __construct ($connect = false) { | |
if ($connect) { | |
$this->connection(); | |
} | |
} | |
public function __destruct() { | |
$this->disconnect(); | |
} | |
public function connection () { | |
if (is_null($this->connect)) { | |
if (is_null(self::$config)) { | |
self::$config = include realpath(dirname(__FILE__) . '/../config') . '/db.dorgen.php'; | |
} | |
$this->connect = (self::$config['persistent']) ? | |
mysql_connect (self::$config['host'], self::$config['user'], self::$config['password'], false) : | |
mysql_pconnect(self::$config['host'], self::$config['user'], self::$config['password']); | |
if (!$this->connect) | |
throw new Exception("Could not connect : " . mysql_error()); | |
if (!mysql_select_db(self::$config['name'])) | |
throw new Exception("Could not select database"); | |
} | |
return $this->connect; | |
} | |
public function disconnect () { | |
if ( !is_null($this->connect) ) { | |
mysql_close($this->connect); | |
$this->connect = null; | |
} | |
} | |
public function query ($sql) { | |
if (!($this->result = mysql_query($sql, $this->connection())) || mysql_errno() != 0) | |
throw new Exception("sql query \"{$sql}\" failed with message: ".mysql_error()); | |
return $this; | |
} | |
public function insert_id () { return mysql_insert_id($this->connect); } | |
public function options (array $value) { | |
$this->options = $value; | |
return $this; | |
} | |
public function fetch_one (array $options = array()) { | |
$row = mysql_fetch_array($this->result, MYSQL_ASSOC); | |
$options = array_merge($this->options, $options); | |
$this->options = array(); | |
if (isset($options['expect'])&&$options['expect']) { | |
if (!$row) { | |
throw new Exception("expected not empty response"); | |
} | |
} | |
return $row; | |
} | |
public function fetch_all (array $options = array()) { | |
$rows = array(); | |
while ( $row = mysql_fetch_array($this->result, MYSQL_ASSOC) ) $rows [] = $row; | |
$options = array_merge($this->options, $options); | |
$this->options = array(); | |
if (isset($options['at_least'])) { | |
$at_least = $options['at_least']; | |
if (count($rows)<$at_least) { | |
throw new Exception("expected at least {$at_least} rows"); | |
} | |
} | |
return $rows; | |
} | |
public function begin_transaction () { | |
return $this->query("START TRANSACTION"); | |
} | |
public function commit () { | |
return $this->query("COMMIT"); | |
} | |
public function rollback () { | |
return $this->query("ROLLBACK"); | |
} | |
public static function condition_text ($condition) | |
{ | |
$result = ''; | |
foreach ($condition as $_field => $_value) | |
{ | |
if ( is_array($_value) ) | |
$result .= '(' . self::condition_text($_value) . ')'; | |
elseif ( is_int($_field) && in_array(strtolower($_value), array('and', 'or')) ) | |
$result .= ' ' . strtoupper($_value) . ' '; | |
elseif ( is_string($_field) ) | |
$result .= '(' . Text::pair($_field, $_value, '=') . ')'; | |
else | |
return false; | |
} | |
return $result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment