class SqlQuery {
protected $sql = null;
protected $query = null;
protected $params = null;
protected $preparedQuery = null;
function __construct ($sql, $query = null, $params = null) {
$this -> sql = $sql;
$this -> query = $query;
$this -> params = $params;
}
function replace_m ($m) {
$res = array ();
foreach ($m as $k => $v) {
$k = str_replace (array ('`', '\\'), '', $k); //пофиксил
$res [] = "`$k` = '" . $this -> sql -> escapeString ($v) . "'";
}
return join (',', $res);
}
function replace_a ($a) {
return "'" . join ("','", array_map (array ($this -> sql, 'escapeString'), $a)) . "'";
}
protected function replace ($m) {
if (isset ($m [1])) {
if ($m [1] == '_') return $this -> sql -> getTablePreffix ();
$f = 'replace_' . $m [1];
if (method_exists ($this, $f))
return $this -> $f (array_shift ($this -> params));
}
return $this -> sql -> escapeString (array_shift ($this -> params));
}
function setQuery ($q) {
$this -> preparedQuery = null;
$this -> query = $q;
}
function setParams ($p) {
$this -> preparedQuery = null;
$this -> params = $p;
}
function __toString () {
if ($this -> preparedQuery == null)
$this -> preparedQuery = preg_replace_callback ('~\?(m|a|_)?~', array ($this, 'replace'), $this -> query);
return $this -> preparedQuery;
}
}
class Sql extends mysqli {
protected static $instances = array ();
static $config;
protected $myConf;
protected function __construct ($n) {
$c = self::$config [$n];
parent::__construct ($c ['host'], $c ['user'], $c ['password'], $c ['name']);
$this -> myConf = $c;
}
function getTablePreffix () {
return isset ($this -> myConf ['prefix']) ? $this -> myConf ['prefix'] : '';
}
static function getInstance ($c = 'default') {
if (!isset (self::$instances [$c])) self::$instances [$c] = new self ($c);
return self::$instances [$c];
}
function insert_id () {
return $this -> insert_id;
}
function escapeString ($s) {
return $this -> real_escape_string ($s);
}
function query ($q) {
$args = func_get_args ();
if (isset ($args [1])) {
$o = new SqlQuery ($this, array_shift ($args), $args);
$q = $o -> __toString ();
}
$qe = $this -> real_query ($q);
return new SqlResult ($this, !$qe);
}
function run ($q) {
$args = func_get_args ();
if (isset ($args [1])) {
$o = new SqlQuery ($this, array_shift ($args), $args);
$q = $o -> __toString ();
}
return parent::query ($q);
}
}
class SqlResult extends mysqli_result {
protected $_queryError;
function __construct ($sql, $queryError) {
$this -> _queryError = $queryError;
parent::__construct ($sql);
}
function num_rows () {
if ($this -> _queryError) return 0;
return $this -> num_rows;
}
function getRow () {
if ($this -> num_rows () == 0) return false;
$row = $this -> fetch_assoc ();
$this -> free_result ();
return $row;
}
function getArray () {
if ($this -> num_rows () == 0) return false;
$d = array ();
while ($r = $this -> fetch_assoc ())
$d [] = $r;
$this -> free_result ();
return $d;
}
function getMap ($key) {
if ($this -> num_rows () == 0) return false;
$keys = func_get_args ();
if (is_array ($keys [0])) $keys = $keys [0];
$d = array ();
while ($row = $this -> fetch_assoc ()) {
$ptr = &$d;
foreach ($keys as $key) {
if (!isset ($row [$key])) {
$bt = debug_backtrace ();
trigger_error ('Undefined index ' . $key . ' in result row: in ' . $bt [2] ['file'] . ' on line ' . $bt [2] ['line'] . "\n", E_USER_NOTICE);
}
if (!isset ($ptr [$row [$key]]))
$ptr [$row [$key]] = null;
$ptr = &$ptr [$row [$key]];
}
$ptr = $row;
}
$this -> free_result ();
return $d;
}
function getCell () {
if ($this -> num_rows () == 0) return false;
$row = $this -> fetch_row ();
$this -> free_result ();
return $row [0];
}
function getColl () {
if ($this -> num_rows () == 0) return false;
$d = array ();
while ($row = $this -> fetch_row ())
$d [] = $row [0];
return $d;
}
}
error_reporting (E_ALL);
Sql::$config = array (
'default' => array ('host' => 'localhost', 'user' => 'root', 'password' => '', 'name' => 'DEMOVB', 'prefix' => 'vb_'),
);
$sql = Sql::getInstance ();
print_r ($sql -> query ("SELECT * FROM ?_User where ID = '?'", 1) -> getRow ());
print_r ($sql -> query ("SELECT Name FROM ?_User where ID IN(?a)", array (1, 2, 3)) -> getColl ());
print_r ($sql -> query ("SELECT * FROM ?_User limit ?, ?", 10, 10) -> getMap ('Name', 'ID'));
$sql -> run ("INSERT INTO vb_User SET ?m, InsertDate = NOW()", array ('Name' => 'vasia', 'Surname' => 'Pupkin'));