/** * Constructor. * @param Oppa\Database $db * @throws Oppa\InvalidValueException */ public function __construct(Database $db) { // check for table, primary key if (!isset($this->table, $this->tablePrimary)) { throw new InvalidValueException("You need to specify both 'table' and 'tablePrimary' properties!"); } $this->db = $db; $this->db->connect(); // set table info for once if (empty(self::$tableInfo)) { $result = $this->db->getLink()->getAgent()->getAll("SHOW COLUMNS FROM {$this->table}"); foreach ($result as $result) { self::$tableInfo[$result->Field] = []; } // set field names as shorcut self::$tableInfo['fields'] = array_keys(self::$tableInfo); } // methods to bind to the entities $className = get_class($this); $reflection = new \ReflectionClass($className); foreach ($reflection->getMethods() as $method) { if ($method->class == $className) { $methodName = strtolower($method->name); $methodPrefix = substr($methodName, 0, 2); // skip magics and on* methods if ($methodPrefix == '__' || $methodPrefix == 'on') { continue; } $this->bindMethods[$methodName] = $reflection->getMethod($methodName)->getClosure($this); } } }
function db_init() { global $cfg; if (!isset($GLOBALS['$db'])) { $db = new Database($cfg); $db->connect(); // store db $GLOBALS['$db'] = $db; } return $GLOBALS['$db']; }
<?php include 'inc.php'; $autoload = (require __DIR__ . '/../src/Autoload.php'); $autoload->register(); use Oppa\Logger; use Oppa\Database; $cfg = ['agent' => 'mysqli', 'query_log' => true, 'query_log_level' => Logger::FAIL, 'query_log_directory' => __DIR__ . '/../.logs', 'query_log_filename_format' => 'Y-m-d', 'database' => ['fetch_type' => 'object', 'charset' => 'utf8', 'timezone' => '+00:00', 'host' => 'localhost', 'name' => 'test', 'username' => 'test', 'password' => '********']]; $db = new Database($cfg); $db->connect(); // @tmp // $db->getLink()->getAgent()->query('delete from users where id > 10'); $batch = $db->getLink()->getAgent()->getBatch(); // set autocommit=1 $batch->lock(); try { $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1, 100)]); // $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1,100)]); // $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1,100)]); // $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1,100)]); // $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1,100)]); // $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1,100)]); // $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1,100)]); // $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1,100)]); // $batch->queue('insert into users (name,old) values (?,?)', ['John Doe', rand(1,100)]); // $batch->queue('insert into userssssss (name,old) values (?,?)', ['John Doe', rand(1,100)]); // commit $batch->run(); } catch (\Throwable $e) { print $e->getMessage(); // rollback & set autocommit=1
<?php include 'inc.php'; $autoload = (require __DIR__ . '/../src/Autoload.php'); $autoload->register(); use Oppa\Database; $cfg = ['agent' => 'mysqli', 'database' => ['host' => 'localhost', 'name' => 'test', 'username' => 'test', 'password' => '********', 'charset' => 'utf8', 'timezone' => '+00:00']]; $db = new Database($cfg); $agent = $db->connect()->getLink()->getAgent(); $s = $agent->prepare('sid = :sid, pid = :pid, a = ?, tid = :tid, b = %d', ['pid' => 2, 'sid' => 1, 'aaa', '9000', 'tid' => 3]); pre($s);