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;
 }
 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());
 }
Example #4
0
 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);
 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;
 }
Example #7
0
#!/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";