function db_init() { global $cfg; if (!isset($GLOBALS['$db'])) { $db = new Database($cfg); $db->connect(); // store db $GLOBALS['$db'] = $db; } return $GLOBALS['$db']; }
/** * Remove. * @param any $params * @return int * @throws Oppa\InvalidValueException */ public final function remove($params) : int { $params = [$params]; if (empty($params)) { throw new InvalidValueException('You need to pass a parameter to make a query!'); } $return = $this->db->getLink()->getAgent()->delete($this->table, "{$this->tablePrimary} IN(?)", $params); if (method_exists($this, 'onDelete')) { $this->onDelete(); } return $return; }
<?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);
<?php include 'inc.php'; $autoload = (require __DIR__ . '/../src/Autoload.php'); $autoload->register(); use Oppa\Database; use Oppa\Config; use Oppa\Query\Builder as QueryBuilder; $cfg = ['agent' => 'mysqli', 'profiling' => true, 'database' => ['fetch_type' => 'object', 'charset' => 'utf8', 'timezone' => '+00:00', 'host' => 'localhost', 'name' => 'test', 'username' => 'test', 'password' => '********']]; $db = new Database($cfg); $db->connect(); $qb = new QueryBuilder($db->getLink()); $qb->setTable('users'); // $qb->select('id,name'); // $qb->select('id,name')->where('id=?', [1]); // $qb->select('id,name')->where('id=?', [1])->limit(1); // $qb->select('id,name')->whereLike('name LIKE ?', ['%Ker"em%']); // $qb->select('id,name')->whereLike('(id LIKE ? OR name LIKE ?)', ['2%', '%Ke_rem%']); // $qb->select('id,name') // ->where('id=?', [1]) // ->where('(name=? OR name=? OR old BETWEEN %d AND %d)', ['Kerem', 'Murat', 30, 40], $qb::OP_AND) // ; // $qb->select()->aggregate('count'); // pre($qb->get()); $qb->setTable('users u'); $qb->select('u.*, us.score, ul.login')->aggregate('sum', 'us.score', 'sum_score')->join('users_score us', 'us.user_id = u.id')->joinLeft('users_login ul', 'ul.user_id = u.id')->whereIn('u.id', [1, 2, 3])->whereBetween('u.old', [30, 50])->whereNotNull('ul.login')->groupBy('u.id')->orderBy('old')->having('sum_score <= ?', [30])->limit(0, 10); pre($qb->toString()); pre($qb->get()); // pre($qb->getAll()); // pre($qb->execute()); // insert
<?php include 'inc.php'; $autoload = (require __DIR__ . '/../src/Autoload.php'); $autoload->register(); use Oppa\Database; use Oppa\Config; /*** single ***/ $cfg = ['agent' => 'mysqli', 'database' => ['fetch_type' => 'object', 'charset' => 'utf8', 'timezone' => '+00:00', 'port' => 3306, 'host' => 'localhost', 'name' => 'test', 'username' => 'test', 'password' => '********', 'profile' => true, 'query_log' => true, 'query_log_level' => Oppa\Logger::ALL, 'query_log_directory' => __DIR__ . '/../.logs']]; $db = new Database($cfg); $db->connect(); // pre($db); pre($db->getLink()->getAgent()->query("select * from users")); // pre($db->getLink()); // pre($db->getLink('localhost')); // // $db->disconnect(); // $db->disconnect('localhost'); // pre($db->getLink('localhost')); // err! /*** sharding ***/ // $cfg = [ // 'agent' => 'mysqli', // 'sharding' => true, // 'database' => [ // 'fetch_type' => 'object', // 'charset' => 'utf8', // 'timezone' => '+00:00', // 'port' => 3306, // 'username' => 'test', // 'password' => '********', // 'master' => ['host' => 'master.mysql.local', 'name' => 'test', 'port' => 3307], // 'slaves' => [