public function __construct($db, $table_prefix) { if (!is_scalar($table_prefix) || !preg_match('#^[a-z0-9_]+$#', $table_prefix)) { throw new Exception('invalid table name'); } $this->table_prefix = $table_prefix; $this->db = function () use($db) { static $object; if (isset($object)) { return $object; } if (is_scalar($db)) { $db = DB\Connection::instance($db); } if (!$db instanceof DB\Iface) { throw new Exception('invalid db object'); } if (!$db->isA('sqlite')) { throw new Exception('db object not sqlite'); } if (!$db->isa('gaia\\db\\extendediface')) { throw new Exception('invalid db object'); } if (!$db->isA('Gaia\\DB\\Except')) { $db = new DB\Except($db); } return $object = $db; }; }
protected static function loadDefault(Iface $stockpile, $name, $dsn) { $db = Connection::instance($dsn); if (!$db instanceof \Gaia\DB\ExtendedIface) { $db = new \Gaia\DB($db); } if ($db->isa('mysql')) { $classname = 'Gaia\\Stockpile\\Storage\\MySQL\\' . $name; } elseif ($db->isa('sqlite')) { $classname = 'Gaia\\Stockpile\\Storage\\SQLite\\' . $name; } else { throw new Exception('invalid db driver', $db); } $table = $stockpile->app() . '_stockpile_' . constant($classname . '::TABLE'); $object = new $classname($db, $table, $stockpile->user()); if (!\Gaia\Stockpile\Storage::isAutoSchemaEnabled()) { return $object; } $cache = new Store\Gate(new Store\Apc()); $key = 'stockpile/storage/__create/' . md5($dsn . '/' . Connection::version() . '/' . $table . '/' . $classname); if ($cache->get($key)) { return $object; } if (!$cache->add($key, 1, 60)) { return $object; } $object->create(); return $object; }
public function close() { Connection::remove($this); if ($this->lock) { return FALSE; } $rs = parent::close(); $this->lock = TRUE; return $rs; }
protected static function loadDefault(Iface $souk, $dsn) { $db = Connection::instance($dsn); if (!$db instanceof \Gaia\DB\Iface) { throw new Exception('invalid db driver', $db); } if (!$db instanceof \Gaia\DB\ExtendedIface) { $db = new \Gaia\DB($db); } if ($db->isa('mysql')) { $classname = 'Gaia\\Souk\\Storage\\MySQL'; } elseif ($db->isa('sqlite')) { $classname = 'Gaia\\Souk\\Storage\\SQLite'; } else { throw new Exception('invalid db driver', $db); } return new $classname($db, $souk->app(), $souk->user(), $dsn . '.' . Connection::version()); }
protected function db() { if ($this->db instanceof \Closure) { $mapper = $this->db; $db = $mapper($this->table()); } elseif (is_scalar($this->db)) { $db = DB\Connection::instance($this->db); } else { $db = $this->db; } if (!$db instanceof DB\Iface) { throw new Exception('invalid db'); } if (!$db->isa('mysql')) { throw new Exception('invalid db'); } if (!$db->isa('gaia\\db\\extendediface')) { throw new Exception('invalid db'); } if (!$db->isa('Gaia\\DB\\Except')) { $db = new DB\Except($db); } if (!Transaction::atStart()) { Transaction::add($db); } return $db; }
Tap::is($rs->fetch(), array('test' => '1112.122445543333333333'), 'query execute works injecting big float in'); $rs = $db->execute('SELECT %f as test', 'dummy'); Tap::is($rs->fetch(), array('test' => '0'), 'query execute sanitizes non float'); $query = $db->prep('%s', array('dummy', 'rummy')); Tap::is($query, "'dummy', 'rummy'", 'format query handles arrays of strings'); $query = $db->prep('%i', array(1, 2, 3)); Tap::is($query, '1, 2, 3', 'format query handles arrays of integers'); $query = $db->prep('%f', array(1.545, 2.2, 3)); Tap::is($query, '1.545, 2.2, 3', 'format query handles arrays of floats'); $query = $db->prep('test %%s ?, (?,?)', array(1, 2), 3, 4); Tap::is($query, "test %s '1', '2', ('3','4')", 'format query question mark as string'); $rs = $db->execute('err'); Tap::cmp_ok($rs, '===', FALSE, 'db returns false on query error'); Tap::like($db->error(), '/you have an error in your sql syntax/i', '$db->error() returns error message'); Tap::is($db->errorcode(), 1064, 'returns expected mysql error code'); $db = new DB\Except(DB\Connection::instance('test')); $err = NULL; try { $db->execute('err'); } catch (Exception $e) { $err = (string) $e; } Tap::like($err, '/database error/i', 'When a bad query is run using execute() the except wrapper tosses an exception'); Tap::is($db->isa(get_class($original)), TRUE, 'isa returns true for original object'); Tap::is($db->isa('gaia\\db'), TRUE, 'isa returns true for gaia\\db'); $rs = $db->execute("SELECT 'test' as r1"); Tap::is($rs->affected(), 1, 'selected a row, affected rows is one'); $newconn = function () use($instance) { return new DB($instance()); }; $table = 'test_' . time() . '_' . mt_rand(10000, 99999);
<?php include __DIR__ . '/../common.php'; use Gaia\Souk; use Gaia\DB; DB\Connection::load(array('test' => function () { return new DB\Driver\PDO('sqlite:/tmp/souk.db'); })); Gaia\Souk\Storage::attach(function (Gaia\Souk\Iface $souk) { return 'test'; }); Gaia\Souk\Storage::enableAutoSchema(); try { $app = 'test1'; $seller_id = mt_rand(1, 1000000000); $buyer_id = mt_rand(1, 1000000000); $souk = new Souk($app, $seller_id); $listing = $souk->auction(array('price' => 10, 'item_id' => 1, 'quantity' => 1, 'bid' => 2, 'step' => 1)); print "\nNew auction:\n"; print_r($listing->export()); $souk = new Souk($app, $buyer_id); $listing = $souk->bid($listing->id, 10); print "\nAfter first bid:\n"; print_r($souk->get($listing->id)->export()); $souk = new Souk($app); $listing = $souk->close($listing->id); print "\nAfter closing:\n"; print_r($listing->export()); $souk = new Souk($app, $seller_id); $listing = $souk->auction(array('item_id' => 1, 'quantity' => 1, 'price' => 10)); print "\nNew buy-only auction:\n";
protected function db() { $db = DB\Connection::instance($this->dsn); if (!$db->isa('mysql')) { throw new Exception('invalid db'); } if (!$db->isa('gaia\\db\\extendediface')) { throw new Exception('invalid db'); } if (!$db->isa('Gaia\\DB\\Except')) { $db = new DB\Except($db); } return $db; }
DB\Transaction::reset(); DB\Connection::reset(); $listing = $souk->buy($listing->id); Tap::is($listing->closed, 1, 'buy works fine, tho'); $souk = Souk($app, $seller_id); $listing = $souk->auction(array('bid' => 0, 'item_id' => $item_id)); $souk = Souk($app, $buyer_id); $e = NULL; try { $listing = $souk->buy($listing->id); } catch (Exception $e) { $e = $e->getMessage(); } Tap::is($e, 'bid only', 'when buying bid-only, fails'); DB\Transaction::reset(); DB\Connection::reset(); $listing = $souk->bid($listing->id, 1); Tap::is($listing->bid, 1, 'bid works fine, tho'); $listing = Souk($app, $seller_id)->auction(array('bid' => 0, 'reserve' => 5, 'item_id' => $item_id)); $listing = Souk($app, $buyer_id)->bid($listing->id, 5); Tap::is($listing->bid, 5, 'without enable_proxy, bid is set, not stepped'); $listing = Souk($app)->close($listing->id); Tap::is($listing->buyer, 0, 'when reserve isnt met, bidder doesnt win listing'); Tap::is($listing->closed, 1, 'even tho reserve wasnt met, closing still ends the bidding.'); unset($seller_id); unset($buyer_id); unset($item_id); Time::offset(86400 * 30); $id = 0; $ct = 0; while ($listings = Souk($app)->pending(0, 5, $id)) {
<?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; }));
<?php use Gaia\Identifier; use Gaia\DB; use Gaia\Test\Tap; include __DIR__ . '/../common.php'; include __DIR__ . '/../assert/mysqli_installed.php'; include __DIR__ . '/../assert/mysql_running.php'; $db = new MySQLi('127.0.0.1', NULL, NULL, 'test', '3306'); if ($db->connect_error) { Tap::plan('skip_all', 'mysqli: ' . $db->connect_error); } $create_identifier = function ($table = NULL) use($db) { if ($table === NULL) { $table = 'test_identifier'; } $db = new DB($db); DB\Connection::add('test', $db); $identifier = new Identifier\MySQL(function () use($db) { return $db; }, $table); $schema = $identifier->schema(); $schema = str_replace('CREATE TABLE', 'CREATE TEMPORARY TABLE', $schema); $db->execute($schema); return $identifier; };
<?php use Gaia\DB; use Gaia\Test\Tap; include __DIR__ . '/../lib/setup.php'; include __DIR__ . '/../../assert/mysqli_installed.php'; include __DIR__ . '/../../assert/mysql_running.php'; DB\Connection::load(array('test' => function () { $db = new DB\Driver\MySQLi($host = '127.0.0.1', $user = NULL, $pass = NULL, $db = 'test', '3306'); $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; }));
<?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 () { return new DB(new \PDO('sqlite:/tmp/stockpile.db')); }));
public function rollback($auth = NULL) { if ($auth != Transaction::SIGNATURE) { return Transaction::rollback(); } if (!$this->txn) { return parent::rollback(); } if ($this->lock) { return TRUE; } Connection::remove($this); $rs = parent::rollback(); $this->lock = TRUE; return $rs; }
<?php use Gaia\DB; use Gaia\Test\Tap; include __DIR__ . '/../lib/setup.php'; include __DIR__ . '/../../assert/mysqli_installed.php'; include __DIR__ . '/../../assert/mysql_running.php'; DB\Connection::load(array('test' => function () { $db = new DB(new \MySQLi($host = '127.0.0.1', $user = NULL, $pass = NULL, $db = 'test', '3306')); return $db; }));
#!/usr/bin/env php <?php use Gaia\Skein; use Gaia\Container; use Gaia\DB; include __DIR__ . '/../common.php'; $thread = 5184; $cache = new Container(); DB\Connection::load(array('test' => function () { return new DB\Except(new Gaia\DB(new MySQLi('127.0.0.1', NULL, NULL, 'test', '3306'))); })); $callback = function ($table) use($cache) { $db = DB\Connection::instance('test'); if (!$cache->add($table, 1, 60)) { return $db; } $sql = substr($table, -5) == 'index' ? Skein\MySQL::indexSchema($table) : Skein\MySQL::dataSchema($table); $db->execute($sql); return $db; }; $skein = new Skein\MySQL($thread, $callback, $app_prefix = 'test002'); $data = 'the time is ' . date('Y/m/d H:i:s'); $id = $skein->add($data); print "\nID: {$id} \n"; $data = $skein->get($id); print "\nDATA: " . print_r($data, TRUE) . "\n"; $skein->store($id, array('foo' => $data)); $data = $skein->get($id); print "\nDATA: " . print_r($data, TRUE) . "\n"; $data = $skein->get($skein->descending()); print "\nDESCENDING ORDER: " . print_r($data, TRUE) . "\n";
#!/usr/bin/env php <?php include __DIR__ . '/../common.php'; use Gaia\Test\Tap; use Gaia\Store; use Gaia\DB; $db = new DB\Except(new Gaia\DB(new MySQLi('127.0.0.1', NULL, NULL, 'test', '3306'))); DB\Connection::load(array('test' => function () use($db) { return $db; })); $table = 'mysql_loadtest'; $store = new Store\Prefix(new Store\MySQL('test', $table), mt_rand(1, 100000000) . '/'); $schema = $store->schema(); $db->execute($schema); include __DIR__ . '/.load_test.php';