Exemplo n.º 1
0
<?php

use Gaia\DB;
use Gaia\Test\Tap;
include __DIR__ . '/../lib/setup.php';
include __DIR__ . '/../../assert/pdo_installed.php';
include __DIR__ . '/../../assert/pdo_sqlite_installed.php';
DB\Connection::load(array('test' => function () {
    $db = new DB\Driver\PDO('sqlite:/tmp/souk.db');
    $cb = array('start' => function () {
        $i = \Gaia\DB\Transaction::internals();
        Tap::debug('TXN: start ' . $i['depth']);
    }, 'commit' => function () {
        $i = \Gaia\DB\Transaction::internals();
        Tap::debug('TXN: commit ' . $i['depth']);
    }, 'rollback' => function () {
        $i = \Gaia\DB\Transaction::internals();
        Tap::debug('TXN: rollback ' . $i['depth']);
    }, 'query' => function ($args) {
        $query = array_shift($args);
        $query = \Gaia\DB\Query::format($query, $args);
        Tap::debug('QUERY: ' . $query);
    });
    //$db = new DB\Observe( $db, $cb);
    return $db;
}));
Exemplo n.º 2
0
 public static function closures(\CI_DB_driver $db)
 {
     $_ = array();
     $lastquery = '';
     $_['__tostring'] = function () use($db, &$lastquery) {
         $errmsg = $db->_error_message();
         $error = $errmsg ? $db->_error_number() . ': ' . $errmsg : '';
         $res = '(Gaia\\DB\\CI object - ' . "\n" . '  [driver] => ' . $db->dbdriver . "\n" . '  [connection] => ' . $db->hostname . "\n" . '  [error] => ' . $error . "\n" . '  [lastquery] => ' . $lastquery . "\n" . ')';
         return $res;
     };
     $_['prep_args'] = $format_args = function ($query, array $args) use($db, &$lastquery) {
         if (!$args || count($args) < 1) {
             return $query;
         }
         return \Gaia\DB\Query::prepare($query, $args, function ($v) use($db) {
             return "'" . $db->escape_str($v) . "'";
         });
     };
     $_['execute'] = function ($query) use($db, &$lastquery) {
         $lastquery = $query;
         if (strlen($lastquery) > 500) {
             $lastquery = substr($lastquery, 0, 485) . ' ...[trucated]';
         }
         try {
             $res = $db->query($query);
             if (!$res) {
                 return FALSE;
             }
             $_ = array();
             if (is_object($res)) {
                 $_['fetch'] = function () use($res) {
                     return $res->_fetch_assoc();
                 };
                 $_['free'] = function () use($res) {
                     $res->free_result();
                 };
             }
             $_['affected'] = $db->affected_rows();
             if (preg_match('#^insert#i', $query)) {
                 $_['insertid'] = $db->insert_id();
             }
             return new Result($_);
         } catch (Exception $e) {
             return FALSE;
         }
     };
     $_['start'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->start($auth);
         }
         $db->trans_start();
         return TRUE;
     };
     $_['commit'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->commit($auth);
         }
         $db->trans_complete();
         return TRUE;
     };
     $_['rollback'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->rollback($auth);
         }
         $db->trans_rollback($auth);
         return TRUE;
     };
     $_['error'] = function () use($db) {
         return $db->_error_message();
     };
     $_['errorcode'] = function () use($db) {
         return $db->_error_number();
     };
     $_['isa'] = function ($name) use($db) {
         if ($db instanceof $name) {
             return TRUE;
         }
         if ($db->dbdriver == $name) {
             return TRUE;
         }
         return FALSE;
     };
     $_['hash'] = function () use($db) {
         if ($db instanceof Iface) {
             return $db->hash();
         }
         return spl_object_hash($db);
     };
     return $_;
 }
Exemplo n.º 3
0
 public function __construct($dsn)
 {
     $args = func_get_args();
     $args = func_get_args();
     for ($i = 0; $i < 4; $i++) {
         if (!isset($args[$i])) {
             $args[$i] = NULL;
         }
     }
     if ($args[0] instanceof \PDO) {
         $db = $args[0];
     } else {
         $db = new \PDO($args[0], $args[1], $args[2], $args[3]);
     }
     $callbacks = array();
     $wrapper = $this;
     $db->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array('Gaia\\DB\\Driver\\PDOStatement', array($wrapper)));
     $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
     $callbacks['__tostring'] = function () use($db) {
         list($errstate, $errcode, $errmsg) = $db->errorInfo();
         @($res = '(Gaia\\DB\\PDO object - ' . "\n" . '  [driver] => ' . $db->getAttribute(\PDO::ATTR_DRIVER_NAME) . "\n" . '  [connection] => ' . $db->getAttribute(\PDO::ATTR_CONNECTION_STATUS) . "\n" . '  [version] => ' . $db->getAttribute(\PDO::ATTR_SERVER_VERSION) . "\n" . '  [info] => ' . $db->getAttribute(\PDO::ATTR_SERVER_INFO) . "\n" . '  [error] => ' . $errmsg . "\n" . '  [insert_id] => ' . $db->lastInsertId() . "\n" . ')');
         return $res;
     };
     $callbacks['prep_query_args'] = $prep_args = function ($query, array $args) use($db) {
         if (!$args || count($args) < 1) {
             return $query;
         }
         return \Gaia\DB\Query::prepare($query, $args, function ($v) use($db) {
             return $db->quote($v);
         });
     };
     $callbacks['isa'] = function ($name) use($wrapper, $db) {
         if ($wrapper instanceof $name) {
             return TRUE;
         }
         if ($db instanceof $name) {
             return TRUE;
         }
         $name = strtolower($name);
         if ($name == 'pdo') {
             return TRUE;
         }
         return FALSE;
     };
     $callbacks['execute'] = function ($query) use($db, $prep_args) {
         $args = func_get_args();
         array_shift($args);
         $rs = $db->query($sql = $prep_args($query, $args));
         return $rs;
     };
     $callbacks['query'] = function ($query) use($db, $wrapper) {
         if ($wrapper->lock) {
             return FALSE;
         }
         $args = func_get_args();
         $res = call_user_func_array(array($db, 'query'), $args);
         if ($res) {
             return $res;
         }
         if ($wrapper->txn) {
             Transaction::block();
             $wrapper->lock = TRUE;
         }
         return $res;
     };
     $callbacks['exec'] = function ($query) use($db, $wrapper) {
         if ($wrapper->lock) {
             return FALSE;
         }
         $res = $db->exec($query);
         if ($res) {
             return $res;
         }
         if ($wrapper->txn) {
             Transaction::block();
             $wrapper->lock = TRUE;
         }
         return $res;
     };
     $callbacks['start'] = function () use($db) {
         return $db->beginTransaction();
     };
     $callbacks['beginTransaction'] = function () use($wrapper) {
         return $wrapper->start();
     };
     $callbacks['commit'] = function () use($db) {
         return $db->commit();
     };
     $callbacks['rollback'] = function () use($db) {
         return $db->rollback();
     };
     $callbacks['hash'] = function () use($db) {
         return spl_object_hash($db);
     };
     $callbacks['__get'] = function ($k) use($db) {
         return $db->{$k};
     };
     $callbacks['__call'] = function ($method, array $args) use($db) {
         return call_user_func_array(array($db, $method), $args);
     };
     parent::__construct($callbacks);
 }
Exemplo n.º 4
0
 public function prep_args($query, array $args)
 {
     if (!$args || count($args) < 1) {
         return $query;
     }
     $conn = $this;
     return \Gaia\DB\Query::prepare($query, $args, function ($v) use($conn) {
         return $conn->quote($v);
     });
 }
Exemplo n.º 5
0
 public static function closures(\MySQLi $db)
 {
     $_ = array();
     $lastquery = '';
     $_['__tostring'] = function () use($db, &$lastquery) {
         if (method_exists($db, '__toString')) {
             return $db->__toString();
         }
         if ($db->connect_error) {
             $error = $db->connect_error ? $db->connect_errno . ': connection error ... ' . $db->connect_error : '';
         } else {
             $error = $db->error ? $db->errno . ': ' . $db->error : '';
         }
         @($res = '(Gaia\\DB\\MySQLi object - ' . "\n" . '  [connection] => ' . $db->host_info . "\n" . '  [error] => ' . $error . "\n" . '  [lastquery] => ' . $lastquery . "\n" . ')');
         return $res;
     };
     $_['prep_args'] = $format_args = function ($query, array $args) use($db) {
         if (!$args || count($args) < 1) {
             return $query;
         }
         return \Gaia\DB\Query::prepare($query, $args, function ($v) use($db) {
             return "'" . $db->real_escape_string($v) . "'";
         });
     };
     $_['execute'] = function ($query) use($db, &$lastquery) {
         $lastquery = $query;
         if (strlen($lastquery) > 500) {
             $lastquery = substr($lastquery, 0, 485) . ' ...[trucated]';
         }
         $res = $db->query($query);
         if (!$res) {
             return FALSE;
         }
         $_ = array();
         if (is_object($res)) {
             $_['fetch'] = function () use($res) {
                 return $res->fetch_assoc();
             };
             $_['free'] = function () use($res) {
                 $res->free_result();
             };
         }
         $_['affected'] = $db->affected_rows;
         $_['insertid'] = $db->insert_id;
         return new Result($_);
     };
     $_['start'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->start($auth);
         }
         return $db->query('START TRANSACTION');
     };
     $_['rollback'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->rollback($auth);
         }
         return $db->query('ROLLBACK');
     };
     $_['commit'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->commit($auth);
         }
         return $db->query('COMMIT');
     };
     $_['error'] = function () use($db) {
         return $db->error;
     };
     $_['errorcode'] = function () use($db) {
         return $db->errno;
     };
     $_['isa'] = function ($name) use($db) {
         if ($db instanceof $name) {
             return TRUE;
         }
         if ($name == 'mysql') {
             return TRUE;
         }
         return FALSE;
     };
     $_['hash'] = function () use($db) {
         if ($db instanceof Iface) {
             return $db->hash();
         }
         return spl_object_hash($db);
     };
     return $_;
 }
Exemplo n.º 6
0
 public static function closures(\PDO $db)
 {
     $_ = array();
     $lastquery = '';
     $_['__tostring'] = function () use($db, &$lastquery) {
         list($errstate, $errcode, $errmsg) = $db->errorInfo();
         $error = $errmsg ? $errcode . ': ' . $errmsg : '';
         $res = '(Gaia\\DB\\PDO object - ' . "\n" . '  [driver] => ' . $db->getAttribute(\PDO::ATTR_DRIVER_NAME) . "\n" . '  [connection] => ' . @$db->getAttribute(\PDO::ATTR_CONNECTION_STATUS) . "\n" . '  [error] => ' . $error . "\n" . '  [lastquery] => ' . $lastquery . "\n" . ')';
         return $res;
     };
     $_['prep_args'] = $format_args = function ($query, array $args) use($db) {
         if (!$args || count($args) < 1) {
             return $query;
         }
         return \Gaia\DB\Query::prepare($query, $args, function ($v) use($db) {
             return $db->quote($v);
         });
     };
     $_['execute'] = function ($query) use($db, &$lastquery) {
         try {
             $lastquery = $query;
             if (strlen($lastquery) > 500) {
                 $lastquery = substr($lastquery, 0, 485) . ' ...[trucated]';
             }
             $res = $db->query($query);
             if (!$res) {
                 return FALSE;
             }
             $_ = array();
             $_['fetch'] = function () use($res) {
                 return $res->fetch(\PDO::FETCH_ASSOC);
             };
             $_['free'] = function () use($res) {
                 $res->closeCursor();
             };
             $_['affected'] = $res->rowCount();
             $_['insertid'] = $db->lastInsertId();
             return new Result($_);
         } catch (\Exception $e) {
             return FALSE;
         }
     };
     $_['start'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->start($auth);
         }
         return $db->beginTransaction();
     };
     $_['commit'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->commit($auth);
         }
         return $db->commit();
     };
     $_['rollback'] = function ($auth = NULL) use($db) {
         if ($db instanceof Iface) {
             return $db->rollback($auth);
         }
         return $db->rollback();
     };
     $_['error'] = function () use($db) {
         $info = $db->errorInfo();
         return $info[2];
     };
     $_['errorcode'] = function () use($db) {
         $info = $db->errorInfo();
         return $info[1];
     };
     $_['isa'] = function ($name) use($db) {
         if ($db instanceof $name) {
             return TRUE;
         }
         if ($name == $db->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
             return TRUE;
         }
         return FALSE;
     };
     $_['hash'] = function () use($db) {
         if ($db instanceof Iface) {
             return $db->hash();
         }
         return spl_object_hash($db);
     };
     return $_;
 }
Exemplo n.º 7
0
 public function prep_args($query, array $args)
 {
     if (!$args || count($args) < 1) {
         return $query;
     }
     $conn = $this;
     return \Gaia\DB\Query::prepare($query, $args, function ($v) use($conn) {
         return "'" . $conn->real_escape_string($v) . "'";
     });
 }
Exemplo n.º 8
0
 public function __construct()
 {
     $args = func_get_args();
     for ($i = 0; $i < 6; $i++) {
         if (!isset($args[$i])) {
             $args[$i] = NULL;
         }
     }
     if ($args[0] instanceof \MySQLi) {
         $mysqli = $args[0];
     } else {
         $mysqli = new \Mysqli($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
     }
     $callbacks = array();
     $wrapper = $this;
     $callbacks['__tostring'] = function () use($mysqli) {
         @($res = '(Gaia\\DB\\MySQLi object - ' . "\n" . '  [affected_rows] => ' . $mysqli->affected_rows . "\n" . '  [client_info] => ' . $mysqli->client_info . "\n" . '  [client_version] => ' . $mysqli->client_version . "\n" . '  [connect_errno] => ' . $mysqli->connect_errno . "\n" . '  [connect_error] => ' . $mysqli->connect_error . "\n" . '  [errno] => ' . $mysqli->errno . "\n" . '  [error] => ' . $mysqli->error . "\n" . '  [field_count] => ' . $mysqli->field_count . "\n" . '  [host_info] => ' . $mysqli->host_info . "\n" . '  [info] => ' . $mysqli->info . "\n" . '  [insert_id] => ' . $mysqli->insert_id . "\n" . '  [server_info] => ' . $mysqli->server_info . "\n" . '  [server_version] => ' . $mysqli->server_version . "\n" . '  [sqlstate] => ' . $mysqli->sqlstate . "\n" . '  [protocol_version] => ' . $mysqli->protocol_version . "\n" . '  [thread_id] => ' . $mysqli->thread_id . "\n" . '  [warning_count] => ' . $mysqli->warning_count . "\n" . ')');
         return $res;
     };
     $callbacks['prep_args'] = $prep_args = function ($query, array $args) use($mysqli) {
         if (!$args || count($args) < 1) {
             return $query;
         }
         return \Gaia\DB\Query::prepare($query, $args, function ($v) use($mysqli) {
             return "'" . $mysqli->real_escape_string($v) . "'";
         });
     };
     $callbacks['execute'] = function ($query) use($mysqli, $prep_args) {
         $args = func_get_args();
         array_shift($args);
         return $mysqli->query($prep_args($query, $args));
     };
     $callbacks['start'] = function () use($mysqli) {
         return $mysqli->query('START TRANSACTION');
     };
     $callbacks['autocommit'] = function ($mode) use($wrapper) {
         return $mode ? $wrapper->commit() : $wrapper->start();
     };
     $callbacks['hash'] = function () use($mysqli) {
         return spl_object_hash($mysqli);
     };
     $callbacks['rollback'] = function () use($mysqli) {
         return $mysqli->query('ROLLBACK');
     };
     $callbacks['commit'] = function () use($mysqli) {
         return $mysqli->query('COMMIT');
     };
     $callbacks['prepare'] = function ($query) {
         trigger_error('unsupported method ' . __CLASS__ . '::' . __FUNCTION__, E_USER_ERROR);
         exit;
     };
     $callbacks['close'] = function () use($mysqli, $wrapper) {
         Connection::remove($wrapper);
         if ($wrapper->lock) {
             return FALSE;
         }
         $rs = $mysqli->close();
         $wrapper->lock = TRUE;
         return $rs;
     };
     $callbacks['__get'] = function ($k) use($mysqli) {
         return $mysqli->{$k};
     };
     $callbacks['__call'] = function ($method, array $args) use($mysqli) {
         return call_user_func_array(array($mysqli, $method), $args);
     };
     $callbacks['query'] = function ($query, $mode = MYSQLI_STORE_RESULT) use($mysqli, $wrapper) {
         if ($wrapper->lock) {
             return FALSE;
         }
         $res = $mysqli->query($query, $mode);
         if ($res) {
             return $res;
         }
         if ($wrapper->txn) {
             Transaction::block();
             $wrapper->lock = TRUE;
         }
         return $res;
     };
     $callbacks['multi_query'] = function ($query) use($mysqli, $wrapper) {
         if ($wrapper->lock) {
             return FALSE;
         }
         $res = $mysqli->multi_query($query);
         if ($res) {
             return $res;
         }
         if ($wrapper->txn) {
             Transaction::block();
             $wrapper->lock = TRUE;
         }
         return $res;
     };
     $callbacks['real_query'] = function ($query) use($mysqli, $wrapper) {
         if ($wrapper->lock) {
             return FALSE;
         }
         $res = $mysqli->real_query($query);
         if ($res) {
             return $res;
         }
         if ($wrapper->txn) {
             Transaction::block();
             $wrapper->lock = TRUE;
         }
         return $res;
     };
     $callbacks['isa'] = function ($name) use($wrapper, $mysqli) {
         if ($wrapper instanceof $name) {
             return TRUE;
         }
         if ($mysqli instanceof $name) {
             return TRUE;
         }
         $name = strtolower($name);
         if ($name == 'mysqli') {
             return TRUE;
         }
         //if( strpos($name, 'mysqli') !== FALSE ) return TRUE;
         return FALSE;
     };
     parent::__construct($callbacks);
 }