2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-07-21 06:36:12 +00:00
|
|
|
/** @privatesection */
|
2009-07-19 07:38:13 +00:00
|
|
|
// Querylet {{{
|
2007-12-06 11:01:18 +00:00
|
|
|
class Querylet {
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var string */
|
|
|
|
public $sql;
|
|
|
|
/** @var array */
|
|
|
|
public $variables;
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $sql, array $variables=array()) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql = $sql;
|
|
|
|
$this->variables = $variables;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function append(Querylet $querylet) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql .= $querylet->sql;
|
|
|
|
$this->variables = array_merge($this->variables, $querylet->variables);
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function append_sql(string $sql) {
|
2007-04-16 11:58:25 +00:00
|
|
|
$this->sql .= $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_variable($var) {
|
|
|
|
$this->variables[] = $var;
|
|
|
|
}
|
2008-05-19 15:59:58 +00:00
|
|
|
}
|
2014-04-27 22:59:01 +00:00
|
|
|
|
2008-05-19 15:59:58 +00:00
|
|
|
class TagQuerylet {
|
2014-04-29 00:37:31 +00:00
|
|
|
/** @var string */
|
|
|
|
public $tag;
|
|
|
|
/** @var bool */
|
|
|
|
public $positive;
|
2008-05-19 15:59:58 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $tag, bool $positive) {
|
2008-05-19 15:59:58 +00:00
|
|
|
$this->tag = $tag;
|
|
|
|
$this->positive = $positive;
|
|
|
|
}
|
|
|
|
}
|
2014-04-27 22:59:01 +00:00
|
|
|
|
2008-05-19 15:59:58 +00:00
|
|
|
class ImgQuerylet {
|
2014-04-29 00:37:31 +00:00
|
|
|
/** @var \Querylet */
|
|
|
|
public $qlet;
|
|
|
|
/** @var bool */
|
|
|
|
public $positive;
|
2008-05-19 15:59:58 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(Querylet $qlet, bool $positive) {
|
2008-05-19 15:59:58 +00:00
|
|
|
$this->qlet = $qlet;
|
|
|
|
$this->positive = $positive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// }}}
|
2009-01-19 18:18:23 +00:00
|
|
|
// {{{ db engines
|
2008-05-18 02:14:51 +00:00
|
|
|
class DBEngine {
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var null|string */
|
2014-04-24 08:36:05 +00:00
|
|
|
public $name = null;
|
2009-01-22 12:05:55 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function init(PDO $db) {}
|
2009-01-22 13:53:30 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function scoreql_to_sql(string $scoreql): string {
|
2009-12-30 08:54:04 +00:00
|
|
|
return $scoreql;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function create_table_sql(string $name, string $data): string {
|
2012-01-11 20:57:00 +00:00
|
|
|
return 'CREATE TABLE '.$name.' ('.$data.')';
|
2009-01-22 12:05:55 +00:00
|
|
|
}
|
2008-05-18 02:14:51 +00:00
|
|
|
}
|
|
|
|
class MySQL extends DBEngine {
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var string */
|
2014-04-24 08:36:05 +00:00
|
|
|
public $name = "mysql";
|
2008-08-11 20:27:24 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function init(PDO $db) {
|
2012-06-26 22:46:38 +00:00
|
|
|
$db->exec("SET NAMES utf8;");
|
2008-08-11 20:27:24 +00:00
|
|
|
}
|
2009-01-22 12:05:55 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function scoreql_to_sql(string $data): string {
|
2009-01-22 12:11:43 +00:00
|
|
|
$data = str_replace("SCORE_AIPK", "INTEGER PRIMARY KEY auto_increment", $data);
|
2012-07-28 10:57:22 +00:00
|
|
|
$data = str_replace("SCORE_INET", "VARCHAR(45)", $data);
|
2009-01-22 12:11:43 +00:00
|
|
|
$data = str_replace("SCORE_BOOL_Y", "'Y'", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_N", "'N'", $data);
|
2009-01-22 12:14:38 +00:00
|
|
|
$data = str_replace("SCORE_BOOL", "ENUM('Y', 'N')", $data);
|
2009-07-15 22:29:14 +00:00
|
|
|
$data = str_replace("SCORE_DATETIME", "DATETIME", $data);
|
2009-01-22 12:12:15 +00:00
|
|
|
$data = str_replace("SCORE_NOW", "\"1970-01-01\"", $data);
|
2010-02-02 02:13:45 +00:00
|
|
|
$data = str_replace("SCORE_STRNORM", "", $data);
|
2012-01-01 16:54:44 +00:00
|
|
|
$data = str_replace("SCORE_ILIKE", "LIKE", $data);
|
2009-12-30 08:54:04 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function create_table_sql(string $name, string $data): string {
|
2009-12-30 08:54:04 +00:00
|
|
|
$data = $this->scoreql_to_sql($data);
|
2011-07-14 09:12:39 +00:00
|
|
|
$ctes = "ENGINE=InnoDB DEFAULT CHARSET='utf8'";
|
2012-01-11 20:57:00 +00:00
|
|
|
return 'CREATE TABLE '.$name.' ('.$data.') '.$ctes;
|
2009-01-22 12:05:55 +00:00
|
|
|
}
|
2008-05-18 02:14:51 +00:00
|
|
|
}
|
|
|
|
class PostgreSQL extends DBEngine {
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var string */
|
2014-04-24 08:36:05 +00:00
|
|
|
public $name = "pgsql";
|
2008-08-11 20:27:24 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function init(PDO $db) {
|
2015-08-09 11:15:46 +00:00
|
|
|
if(array_key_exists('REMOTE_ADDR', $_SERVER)) {
|
|
|
|
$db->exec("SET application_name TO 'shimmie [{$_SERVER['REMOTE_ADDR']}]';");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$db->exec("SET application_name TO 'shimmie [local]';");
|
|
|
|
}
|
2014-12-31 13:16:00 +00:00
|
|
|
$db->exec("SET statement_timeout TO 10000;");
|
2012-01-31 12:16:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function scoreql_to_sql(string $data): string {
|
2009-01-22 12:11:43 +00:00
|
|
|
$data = str_replace("SCORE_AIPK", "SERIAL PRIMARY KEY", $data);
|
|
|
|
$data = str_replace("SCORE_INET", "INET", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_Y", "'t'", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_N", "'f'", $data);
|
2009-01-22 12:14:38 +00:00
|
|
|
$data = str_replace("SCORE_BOOL", "BOOL", $data);
|
2009-07-15 22:29:14 +00:00
|
|
|
$data = str_replace("SCORE_DATETIME", "TIMESTAMP", $data);
|
2012-07-28 11:07:46 +00:00
|
|
|
$data = str_replace("SCORE_NOW", "current_timestamp", $data);
|
2010-02-02 02:13:45 +00:00
|
|
|
$data = str_replace("SCORE_STRNORM", "lower", $data);
|
2012-01-01 16:54:44 +00:00
|
|
|
$data = str_replace("SCORE_ILIKE", "ILIKE", $data);
|
2009-12-30 08:54:04 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function create_table_sql(string $name, string $data): string {
|
2009-12-30 08:54:04 +00:00
|
|
|
$data = $this->scoreql_to_sql($data);
|
2015-08-09 11:15:46 +00:00
|
|
|
return "CREATE TABLE $name ($data)";
|
2009-01-22 13:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-25 12:10:10 +00:00
|
|
|
|
|
|
|
// shimmie functions for export to sqlite
|
|
|
|
function _unix_timestamp($date) { return strtotime($date); }
|
|
|
|
function _now() { return date("Y-m-d h:i:s"); }
|
|
|
|
function _floor($a) { return floor($a); }
|
2009-07-17 00:55:07 +00:00
|
|
|
function _log($a, $b=null) {
|
|
|
|
if(is_null($b)) return log($a);
|
|
|
|
else return log($a, $b);
|
|
|
|
}
|
2009-01-25 12:10:10 +00:00
|
|
|
function _isnull($a) { return is_null($a); }
|
|
|
|
function _md5($a) { return md5($a); }
|
|
|
|
function _concat($a, $b) { return $a . $b; }
|
|
|
|
function _lower($a) { return strtolower($a); }
|
2015-08-08 21:11:41 +00:00
|
|
|
function _rand() { return rand(); }
|
|
|
|
function _ln($n) { return log($n); }
|
2009-01-25 12:10:10 +00:00
|
|
|
|
2009-01-22 13:53:30 +00:00
|
|
|
class SQLite extends DBEngine {
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var string */
|
2014-04-24 08:36:05 +00:00
|
|
|
public $name = "sqlite";
|
2009-01-22 13:53:30 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function init(PDO $db) {
|
2009-01-25 12:10:10 +00:00
|
|
|
ini_set('sqlite.assoc_case', 0);
|
2012-06-26 22:46:38 +00:00
|
|
|
$db->exec("PRAGMA foreign_keys = ON;");
|
|
|
|
$db->sqliteCreateFunction('UNIX_TIMESTAMP', '_unix_timestamp', 1);
|
|
|
|
$db->sqliteCreateFunction('now', '_now', 0);
|
|
|
|
$db->sqliteCreateFunction('floor', '_floor', 1);
|
|
|
|
$db->sqliteCreateFunction('log', '_log');
|
|
|
|
$db->sqliteCreateFunction('isnull', '_isnull', 1);
|
|
|
|
$db->sqliteCreateFunction('md5', '_md5', 1);
|
|
|
|
$db->sqliteCreateFunction('concat', '_concat', 2);
|
|
|
|
$db->sqliteCreateFunction('lower', '_lower', 1);
|
2015-08-08 21:11:41 +00:00
|
|
|
$db->sqliteCreateFunction('rand', '_rand', 0);
|
|
|
|
$db->sqliteCreateFunction('ln', '_ln', 1);
|
2009-01-22 15:08:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function scoreql_to_sql(string $data): string {
|
2009-01-22 13:53:30 +00:00
|
|
|
$data = str_replace("SCORE_AIPK", "INTEGER PRIMARY KEY", $data);
|
2012-04-01 14:44:43 +00:00
|
|
|
$data = str_replace("SCORE_INET", "VARCHAR(45)", $data);
|
2009-01-22 13:53:30 +00:00
|
|
|
$data = str_replace("SCORE_BOOL_Y", "'Y'", $data);
|
|
|
|
$data = str_replace("SCORE_BOOL_N", "'N'", $data);
|
2009-01-22 15:08:37 +00:00
|
|
|
$data = str_replace("SCORE_BOOL", "CHAR(1)", $data);
|
|
|
|
$data = str_replace("SCORE_NOW", "\"1970-01-01\"", $data);
|
2013-06-13 09:34:47 +00:00
|
|
|
$data = str_replace("SCORE_STRNORM", "lower", $data);
|
2012-01-01 16:54:44 +00:00
|
|
|
$data = str_replace("SCORE_ILIKE", "LIKE", $data);
|
2013-01-31 08:07:19 +00:00
|
|
|
return $data;
|
2012-06-26 23:08:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function create_table_sql(string $name, string $data): string {
|
2012-06-26 23:08:51 +00:00
|
|
|
$data = $this->scoreql_to_sql($data);
|
2009-01-22 15:08:37 +00:00
|
|
|
$cols = array();
|
|
|
|
$extras = "";
|
|
|
|
foreach(explode(",", $data) as $bit) {
|
|
|
|
$matches = array();
|
2015-08-08 16:17:44 +00:00
|
|
|
if(preg_match("/(UNIQUE)? ?INDEX\s*\((.*)\)/", $bit, $matches)) {
|
|
|
|
$uni = $matches[1];
|
|
|
|
$col = $matches[2];
|
|
|
|
$extras .= "CREATE $uni INDEX {$name}_{$col} ON {$name}({$col});";
|
2009-01-22 15:08:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$cols[] = $bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$cols_redone = implode(", ", $cols);
|
2012-06-26 22:46:38 +00:00
|
|
|
return "CREATE TABLE $name ($cols_redone); $extras";
|
2008-08-11 20:27:24 +00:00
|
|
|
}
|
2008-05-18 02:14:51 +00:00
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
// }}}
|
|
|
|
// {{{ cache engines
|
|
|
|
interface CacheEngine {
|
2017-03-09 09:27:52 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get(string $key);
|
|
|
|
public function set(string $key, $val, int $time=0);
|
|
|
|
public function delete(string $key);
|
|
|
|
public function get_hits(): int;
|
|
|
|
public function get_misses(): int;
|
2009-01-20 10:47:20 +00:00
|
|
|
}
|
|
|
|
class NoCache implements CacheEngine {
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get(string $key) {return false;}
|
|
|
|
public function set(string $key, $val, int $time=0) {}
|
|
|
|
public function delete(string $key) {}
|
2009-01-20 11:54:43 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_hits(): int {return 0;}
|
|
|
|
public function get_misses(): int {return 0;}
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
2010-02-01 16:17:00 +00:00
|
|
|
class MemcacheCache implements CacheEngine {
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var \Memcache|null */
|
|
|
|
public $memcache=null;
|
|
|
|
/** @var int */
|
|
|
|
private $hits=0;
|
|
|
|
/** @var int */
|
|
|
|
private $misses=0;
|
2009-01-20 11:54:43 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $args) {
|
2010-07-30 20:28:31 +00:00
|
|
|
$hp = explode(":", $args);
|
2017-05-29 09:18:11 +00:00
|
|
|
$this->memcache = new Memcache;
|
|
|
|
@$this->memcache->pconnect($hp[0], $hp[1]);
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get(string $key) {
|
2014-11-26 13:07:30 +00:00
|
|
|
$val = $this->memcache->get($key);
|
2013-07-05 21:19:12 +00:00
|
|
|
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
|
2014-11-26 13:07:30 +00:00
|
|
|
$hit = $val === false ? "miss" : "hit";
|
|
|
|
file_put_contents("data/cache.log", "Cache $hit: $key\n", FILE_APPEND);
|
2013-07-05 21:19:12 +00:00
|
|
|
}
|
2012-06-23 22:17:17 +00:00
|
|
|
if($val !== false) {
|
2009-01-20 10:47:20 +00:00
|
|
|
$this->hits++;
|
2009-01-19 18:18:23 +00:00
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
else {
|
2009-01-20 10:47:20 +00:00
|
|
|
$this->misses++;
|
2009-01-19 18:18:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function set(string $key, $val, int $time=0) {
|
2009-01-20 10:47:20 +00:00
|
|
|
$this->memcache->set($key, $val, false, $time);
|
2014-12-06 23:50:56 +00:00
|
|
|
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
|
|
|
|
file_put_contents("data/cache.log", "Cache set: $key ($time)\n", FILE_APPEND);
|
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function delete(string $key) {
|
2009-01-20 10:47:20 +00:00
|
|
|
$this->memcache->delete($key);
|
2014-12-06 23:50:56 +00:00
|
|
|
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
|
|
|
|
file_put_contents("data/cache.log", "Cache delete: $key\n", FILE_APPEND);
|
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
2009-01-20 11:54:43 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_hits(): int {return $this->hits;}
|
|
|
|
public function get_misses(): int {return $this->misses;}
|
2009-01-19 18:18:23 +00:00
|
|
|
}
|
2017-05-29 09:18:11 +00:00
|
|
|
class MemcachedCache implements CacheEngine {
|
|
|
|
/** @var \Memcached|null */
|
|
|
|
public $memcache=null;
|
|
|
|
/** @var int */
|
|
|
|
private $hits=0;
|
|
|
|
/** @var int */
|
|
|
|
private $misses=0;
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $args) {
|
2017-05-29 09:18:11 +00:00
|
|
|
$hp = explode(":", $args);
|
|
|
|
$this->memcache = new Memcached;
|
|
|
|
#$this->memcache->setOption(Memcached::OPT_COMPRESSION, False);
|
|
|
|
#$this->memcache->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP);
|
|
|
|
#$this->memcache->setOption(Memcached::OPT_PREFIX_KEY, phpversion());
|
|
|
|
$this->memcache->addServer($hp[0], $hp[1]);
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get(string $key) {
|
2017-06-08 08:37:38 +00:00
|
|
|
$key = urlencode($key);
|
2017-05-29 09:18:11 +00:00
|
|
|
|
|
|
|
$val = $this->memcache->get($key);
|
|
|
|
$res = $this->memcache->getResultCode();
|
|
|
|
|
|
|
|
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
|
2017-08-22 00:04:33 +00:00
|
|
|
$hit = $res == Memcached::RES_SUCCESS ? "hit" : "miss";
|
2017-05-29 09:18:11 +00:00
|
|
|
file_put_contents("data/cache.log", "Cache $hit: $key\n", FILE_APPEND);
|
|
|
|
}
|
|
|
|
if($res == Memcached::RES_SUCCESS) {
|
|
|
|
$this->hits++;
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
else if($res == Memcached::RES_NOTFOUND) {
|
|
|
|
$this->misses++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
2017-06-08 08:37:38 +00:00
|
|
|
error_log("Memcached error during get($key): $res");
|
2017-09-19 17:55:43 +00:00
|
|
|
return false;
|
2017-05-29 09:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function set(string $key, $val, int $time=0) {
|
2017-06-08 08:37:38 +00:00
|
|
|
$key = urlencode($key);
|
2017-05-29 09:18:11 +00:00
|
|
|
|
|
|
|
$this->memcache->set($key, $val, $time);
|
2017-06-08 08:37:38 +00:00
|
|
|
$res = $this->memcache->getResultCode();
|
2017-05-29 09:18:11 +00:00
|
|
|
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
|
|
|
|
file_put_contents("data/cache.log", "Cache set: $key ($time)\n", FILE_APPEND);
|
|
|
|
}
|
2017-06-08 08:37:38 +00:00
|
|
|
if($res != Memcached::RES_SUCCESS) {
|
|
|
|
error_log("Memcached error during set($key): $res");
|
|
|
|
}
|
2017-05-29 09:18:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function delete(string $key) {
|
2017-06-08 08:37:38 +00:00
|
|
|
$key = urlencode($key);
|
2017-05-29 09:18:11 +00:00
|
|
|
|
|
|
|
$this->memcache->delete($key);
|
2017-06-08 08:37:38 +00:00
|
|
|
$res = $this->memcache->getResultCode();
|
2017-05-29 09:18:11 +00:00
|
|
|
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
|
|
|
|
file_put_contents("data/cache.log", "Cache delete: $key\n", FILE_APPEND);
|
|
|
|
}
|
2017-06-08 08:37:38 +00:00
|
|
|
if($res != Memcached::RES_SUCCESS && $res != Memcached::RES_NOTFOUND) {
|
|
|
|
error_log("Memcached error during delete($key): $res");
|
|
|
|
}
|
2017-05-29 09:18:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_hits(): int {return $this->hits;}
|
|
|
|
public function get_misses(): int {return $this->misses;}
|
2017-05-29 09:18:11 +00:00
|
|
|
}
|
2014-04-27 22:59:01 +00:00
|
|
|
|
2010-02-02 17:12:40 +00:00
|
|
|
class APCCache implements CacheEngine {
|
2016-06-19 22:05:57 +00:00
|
|
|
public $hits=0, $misses=0;
|
2010-02-02 17:12:40 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(string $args) {
|
2014-04-19 08:17:58 +00:00
|
|
|
// $args is not used, but is passed in when APC cache is created.
|
|
|
|
}
|
2010-02-02 17:12:40 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get(string $key) {
|
2010-02-02 17:12:40 +00:00
|
|
|
$val = apc_fetch($key);
|
|
|
|
if($val) {
|
|
|
|
$this->hits++;
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->misses++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function set(string $key, $val, int $time=0) {
|
2010-02-02 17:12:40 +00:00
|
|
|
apc_store($key, $val, $time);
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function delete(string $key) {
|
2010-02-02 17:12:40 +00:00
|
|
|
apc_delete($key);
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_hits(): int {return $this->hits;}
|
|
|
|
public function get_misses(): int {return $this->misses;}
|
2010-02-02 17:12:40 +00:00
|
|
|
}
|
2017-10-28 19:28:31 +00:00
|
|
|
|
|
|
|
class RedisCache implements CacheEngine {
|
|
|
|
public $hits=0, $misses=0;
|
|
|
|
private $redis=null;
|
|
|
|
|
|
|
|
public function __construct(string $args) {
|
|
|
|
$this->redis = new Redis();
|
|
|
|
$hp = explode(":", $args);
|
|
|
|
$this->redis->pconnect($hp[0], $hp[1]);
|
|
|
|
$this->redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
|
|
|
|
$this->redis->setOption(Redis::OPT_PREFIX, 'shm:');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get(string $key) {
|
|
|
|
$val = $this->redis->get($key);
|
|
|
|
if($val !== false) {
|
|
|
|
$this->hits++;
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->misses++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function set(string $key, $val, int $time=0) {
|
|
|
|
if($time > 0) {
|
|
|
|
$this->redis->setEx($key, $time, $val);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->redis->set($key, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(string $key) {
|
|
|
|
$this->redis->delete($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_hits(): int {return $this->hits;}
|
|
|
|
public function get_misses(): int {return $this->misses;}
|
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
// }}}
|
2009-07-21 06:36:12 +00:00
|
|
|
/** @publicsection */
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-01-03 20:32:57 +00:00
|
|
|
* A class for controlled database access
|
2007-12-06 11:01:18 +00:00
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
class Database {
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* The PDO database connection object, for anyone who wants direct access.
|
|
|
|
* @var null|PDO
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2012-06-23 23:50:13 +00:00
|
|
|
private $db = null;
|
2017-03-10 05:58:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
2014-11-26 13:09:22 +00:00
|
|
|
public $dbtime = 0.0;
|
2009-07-19 07:38:13 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* Meta info about the database engine.
|
|
|
|
* @var DBEngine|null
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2012-06-23 23:50:13 +00:00
|
|
|
private $engine = null;
|
2009-07-19 07:38:13 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* The currently active cache engine.
|
|
|
|
* @var CacheEngine|null
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2012-06-23 23:57:55 +00:00
|
|
|
public $cache = null;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2014-02-19 07:12:56 +00:00
|
|
|
/**
|
|
|
|
* A boolean flag to track if we already have an active transaction.
|
|
|
|
* (ie: True if beginTransaction() already called)
|
2014-04-27 22:59:01 +00:00
|
|
|
*
|
|
|
|
* @var bool
|
2014-02-19 07:12:56 +00:00
|
|
|
*/
|
|
|
|
public $transaction = false;
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2015-08-02 20:31:55 +00:00
|
|
|
/**
|
|
|
|
* How many queries this DB object has run
|
|
|
|
*/
|
|
|
|
public $query_count = 0;
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2012-06-23 23:57:55 +00:00
|
|
|
* For now, only connect to the cache, as we will pretty much certainly
|
|
|
|
* need it. There are some pages where all the data is in cache, so the
|
|
|
|
* DB connection is on-demand.
|
2007-12-11 18:37:11 +00:00
|
|
|
*/
|
2014-03-22 09:00:59 +00:00
|
|
|
public function __construct() {
|
2012-06-23 23:57:55 +00:00
|
|
|
$this->connect_cache();
|
2012-06-23 23:50:13 +00:00
|
|
|
}
|
2012-06-23 22:57:34 +00:00
|
|
|
|
2012-06-23 23:50:13 +00:00
|
|
|
private function connect_cache() {
|
2012-06-23 22:57:34 +00:00
|
|
|
$matches = array();
|
2017-10-28 19:28:31 +00:00
|
|
|
if(defined("CACHE_DSN") && CACHE_DSN && preg_match("#(.*)://(.*)#", CACHE_DSN, $matches)) {
|
2012-06-23 22:57:34 +00:00
|
|
|
if($matches[1] == "memcache") {
|
2017-05-30 01:13:11 +00:00
|
|
|
$this->cache = new MemcacheCache($matches[2]);
|
2017-05-29 09:18:11 +00:00
|
|
|
}
|
|
|
|
else if($matches[1] == "memcached") {
|
2017-05-30 01:13:11 +00:00
|
|
|
$this->cache = new MemcachedCache($matches[2]);
|
2012-06-23 22:57:34 +00:00
|
|
|
}
|
|
|
|
else if($matches[1] == "apc") {
|
|
|
|
$this->cache = new APCCache($matches[2]);
|
|
|
|
}
|
2017-10-28 19:28:31 +00:00
|
|
|
else if($matches[1] == "redis") {
|
|
|
|
$this->cache = new RedisCache($matches[2]);
|
|
|
|
}
|
2012-06-23 22:57:34 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->cache = new NoCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function connect_db() {
|
2011-01-01 16:59:10 +00:00
|
|
|
# FIXME: detect ADODB URI, automatically translate PDO DSN
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Why does the abstraction layer act differently depending on the
|
|
|
|
* back-end? Because PHP is deliberately retarded.
|
|
|
|
*
|
|
|
|
* http://stackoverflow.com/questions/237367
|
|
|
|
*/
|
|
|
|
$matches = array(); $db_user=null; $db_pass=null;
|
2012-01-20 03:15:58 +00:00
|
|
|
if(preg_match("/user=([^;]*)/", DATABASE_DSN, $matches)) $db_user=$matches[1];
|
|
|
|
if(preg_match("/password=([^;]*)/", DATABASE_DSN, $matches)) $db_pass=$matches[1];
|
2011-01-01 16:59:10 +00:00
|
|
|
|
2015-08-09 14:39:00 +00:00
|
|
|
// https://bugs.php.net/bug.php?id=70221
|
|
|
|
$ka = DATABASE_KA;
|
|
|
|
if(version_compare(PHP_VERSION, "6.9.9") == 1 && $this->get_driver_name() == "sqlite") {
|
|
|
|
$ka = false;
|
|
|
|
}
|
|
|
|
|
2012-02-02 07:43:43 +00:00
|
|
|
$db_params = array(
|
2015-08-09 14:39:00 +00:00
|
|
|
PDO::ATTR_PERSISTENT => $ka,
|
2012-01-26 17:24:56 +00:00
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
|
2012-02-02 07:43:43 +00:00
|
|
|
);
|
2014-12-07 01:10:12 +00:00
|
|
|
$this->db = new PDO(DATABASE_DSN, $db_user, $db_pass, $db_params);
|
2010-12-31 19:29:15 +00:00
|
|
|
|
2012-06-23 23:50:13 +00:00
|
|
|
$this->connect_engine();
|
|
|
|
$this->engine->init($this->db);
|
|
|
|
|
2014-02-19 07:12:56 +00:00
|
|
|
$this->beginTransaction();
|
2012-06-23 23:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function connect_engine() {
|
|
|
|
if(preg_match("/^([^:]*)/", DATABASE_DSN, $matches)) $db_proto=$matches[1];
|
|
|
|
else throw new SCoreException("Can't figure out database engine");
|
|
|
|
|
2012-01-11 20:08:27 +00:00
|
|
|
if($db_proto === "mysql") {
|
2009-08-09 12:10:59 +00:00
|
|
|
$this->engine = new MySQL();
|
|
|
|
}
|
2012-01-11 20:08:27 +00:00
|
|
|
else if($db_proto === "pgsql") {
|
2009-08-09 12:10:59 +00:00
|
|
|
$this->engine = new PostgreSQL();
|
|
|
|
}
|
2012-01-11 20:08:27 +00:00
|
|
|
else if($db_proto === "sqlite") {
|
2009-08-09 12:10:59 +00:00
|
|
|
$this->engine = new SQLite();
|
|
|
|
}
|
2011-01-01 16:59:10 +00:00
|
|
|
else {
|
2012-01-11 20:08:27 +00:00
|
|
|
die('Unknown PDO driver: '.$db_proto);
|
2011-01-01 16:59:10 +00:00
|
|
|
}
|
2012-06-23 23:50:13 +00:00
|
|
|
}
|
2009-01-19 18:18:23 +00:00
|
|
|
|
2014-02-19 07:12:56 +00:00
|
|
|
public function beginTransaction() {
|
|
|
|
if ($this->transaction === false) {
|
|
|
|
$this->db->beginTransaction();
|
2014-02-19 07:50:10 +00:00
|
|
|
$this->transaction = true;
|
2014-02-19 07:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function commit(): bool {
|
2014-02-22 05:40:14 +00:00
|
|
|
if(!is_null($this->db)) {
|
|
|
|
if ($this->transaction === true) {
|
|
|
|
$this->transaction = false;
|
|
|
|
return $this->db->commit();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new SCoreException("<p><b>Database Transaction Error:</b> Unable to call commit() as there is no transaction currently open.");
|
|
|
|
}
|
2014-02-19 07:12:56 +00:00
|
|
|
}
|
2017-09-19 17:55:43 +00:00
|
|
|
else {
|
|
|
|
throw new SCoreException("<p><b>Database Transaction Error:</b> Unable to call commit() as there is no connection currently open.");
|
|
|
|
}
|
2012-06-23 23:50:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function rollback(): bool {
|
2014-02-22 05:40:14 +00:00
|
|
|
if(!is_null($this->db)) {
|
|
|
|
if ($this->transaction === true) {
|
|
|
|
$this->transaction = false;
|
|
|
|
return $this->db->rollback();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new SCoreException("<p><b>Database Transaction Error:</b> Unable to call rollback() as there is no transaction currently open.");
|
|
|
|
}
|
2014-02-19 07:17:23 +00:00
|
|
|
}
|
2017-09-19 17:55:43 +00:00
|
|
|
else {
|
|
|
|
throw new SCoreException("<p><b>Database Transaction Error:</b> Unable to call rollback() as there is no connection currently open.");
|
|
|
|
}
|
2012-06-23 23:50:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function escape(string $input): string {
|
2012-06-23 23:50:13 +00:00
|
|
|
if(is_null($this->db)) $this->connect_db();
|
2012-06-23 23:57:55 +00:00
|
|
|
return $this->db->Quote($input);
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function scoreql_to_sql(string $input): string {
|
2012-06-23 23:57:55 +00:00
|
|
|
if(is_null($this->engine)) $this->connect_engine();
|
|
|
|
return $this->engine->scoreql_to_sql($input);
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_driver_name(): string {
|
2012-06-23 23:57:55 +00:00
|
|
|
if(is_null($this->engine)) $this->connect_engine();
|
|
|
|
return $this->engine->name;
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
private function count_execs(string $sql, array $inputarray) {
|
2017-08-22 00:04:33 +00:00
|
|
|
if((DEBUG_SQL === true) || (is_null(DEBUG_SQL) && @$_GET['DEBUG_SQL'])) {
|
2017-05-30 01:13:11 +00:00
|
|
|
$sql = trim(preg_replace('/\s+/msi', ' ', $sql));
|
|
|
|
if(isset($inputarray) && is_array($inputarray) && !empty($inputarray)) {
|
|
|
|
$text = $sql." -- ".join(", ", $inputarray)."\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$text = $sql."\n";
|
2015-08-02 20:31:55 +00:00
|
|
|
}
|
2017-05-30 01:13:11 +00:00
|
|
|
file_put_contents("data/sql.log", $text, FILE_APPEND);
|
2015-08-02 20:31:55 +00:00
|
|
|
}
|
|
|
|
if(!is_array($inputarray)) $this->query_count++;
|
|
|
|
# handle 2-dimensional input arrays
|
|
|
|
else if(is_array(reset($inputarray))) $this->query_count += sizeof($inputarray);
|
|
|
|
else $this->query_count++;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
private function count_time(string $method, float $start) {
|
2017-08-22 00:04:33 +00:00
|
|
|
if((DEBUG_SQL === true) || (is_null(DEBUG_SQL) && @$_GET['DEBUG_SQL'])) {
|
2017-05-30 01:13:11 +00:00
|
|
|
$text = $method.":".(microtime(true) - $start)."\n";
|
|
|
|
file_put_contents("data/sql.log", $text, FILE_APPEND);
|
2017-05-29 09:19:11 +00:00
|
|
|
}
|
|
|
|
$this->dbtime += microtime(true) - $start;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function execute(string $query, array $args=array()): PDOStatement {
|
2014-02-19 21:16:01 +00:00
|
|
|
try {
|
2012-06-23 23:50:13 +00:00
|
|
|
if(is_null($this->db)) $this->connect_db();
|
2017-09-19 17:55:43 +00:00
|
|
|
$this->count_execs($query, $args);
|
2011-01-01 15:27:24 +00:00
|
|
|
$stmt = $this->db->prepare($query);
|
2011-03-05 01:11:29 +00:00
|
|
|
if (!array_key_exists(0, $args)) {
|
|
|
|
foreach($args as $name=>$value) {
|
|
|
|
if(is_numeric($value)) {
|
2012-01-11 20:57:00 +00:00
|
|
|
$stmt->bindValue(':'.$name, $value, PDO::PARAM_INT);
|
2011-03-05 01:11:29 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-01-11 20:57:00 +00:00
|
|
|
$stmt->bindValue(':'.$name, $value, PDO::PARAM_STR);
|
2011-03-05 01:11:29 +00:00
|
|
|
}
|
2011-01-01 15:27:24 +00:00
|
|
|
}
|
2011-03-05 01:11:29 +00:00
|
|
|
$stmt->execute();
|
2014-02-22 20:42:09 +00:00
|
|
|
}
|
2011-03-05 01:11:29 +00:00
|
|
|
else {
|
|
|
|
$stmt->execute($args);
|
2011-01-01 15:27:24 +00:00
|
|
|
}
|
|
|
|
return $stmt;
|
2014-02-19 21:16:01 +00:00
|
|
|
}
|
2011-01-01 15:27:24 +00:00
|
|
|
catch(PDOException $pdoe) {
|
2012-03-31 16:07:11 +00:00
|
|
|
throw new SCoreException($pdoe->getMessage()."<p><b>Query:</b> ".$query);
|
2014-02-19 21:16:01 +00:00
|
|
|
}
|
2007-08-08 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* Execute an SQL query and return a 2D array.
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_all(string $query, array $args=array()): array {
|
2014-11-26 13:09:22 +00:00
|
|
|
$_start = microtime(true);
|
|
|
|
$data = $this->execute($query, $args)->fetchAll();
|
2017-05-29 09:19:11 +00:00
|
|
|
$this->count_time("get_all", $_start);
|
2014-11-26 13:09:22 +00:00
|
|
|
return $data;
|
2008-01-05 00:22:19 +00:00
|
|
|
}
|
2009-01-04 16:15:00 +00:00
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* Execute an SQL query and return a single row.
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $args
|
2015-09-27 00:03:58 +00:00
|
|
|
* @return array|null
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_row(string $query, array $args=array()) {
|
2014-11-26 13:09:22 +00:00
|
|
|
$_start = microtime(true);
|
2011-03-24 14:27:11 +00:00
|
|
|
$row = $this->execute($query, $args)->fetch();
|
2017-05-29 09:19:11 +00:00
|
|
|
$this->count_time("get_row", $_start);
|
2011-03-24 14:27:11 +00:00
|
|
|
return $row ? $row : null;
|
2011-01-01 15:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* Execute an SQL query and return the first column of each row.
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2011-01-01 15:27:24 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_col(string $query, array $args=array()): array {
|
2014-11-26 13:09:22 +00:00
|
|
|
$_start = microtime(true);
|
2011-01-01 15:27:24 +00:00
|
|
|
$stmt = $this->execute($query, $args);
|
|
|
|
$res = array();
|
|
|
|
foreach($stmt as $row) {
|
|
|
|
$res[] = $row[0];
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
2017-05-29 09:19:11 +00:00
|
|
|
$this->count_time("get_col", $_start);
|
2011-01-01 15:27:24 +00:00
|
|
|
return $res;
|
2008-08-23 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
2011-01-01 16:27:56 +00:00
|
|
|
/**
|
2017-09-19 17:55:43 +00:00
|
|
|
* Execute an SQL query and return the the first row => the second row.
|
2014-04-27 22:59:01 +00:00
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2011-01-01 16:27:56 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_pairs(string $query, array $args=array()): array {
|
2014-11-26 13:09:22 +00:00
|
|
|
$_start = microtime(true);
|
2011-01-01 16:27:56 +00:00
|
|
|
$stmt = $this->execute($query, $args);
|
|
|
|
$res = array();
|
|
|
|
foreach($stmt as $row) {
|
|
|
|
$res[$row[0]] = $row[1];
|
|
|
|
}
|
2017-05-29 09:19:11 +00:00
|
|
|
$this->count_time("get_pairs", $_start);
|
2011-01-01 16:27:56 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2011-01-01 15:27:24 +00:00
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* Execute an SQL query and return a single value.
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param array $args
|
2017-09-19 17:55:43 +00:00
|
|
|
* @return mixed|null
|
2011-01-01 15:27:24 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_one(string $query, array $args=array()) {
|
2014-11-26 13:09:22 +00:00
|
|
|
$_start = microtime(true);
|
2011-01-01 15:27:24 +00:00
|
|
|
$row = $this->execute($query, $args)->fetch();
|
2017-05-29 09:19:11 +00:00
|
|
|
$this->count_time("get_one", $_start);
|
2011-01-01 15:27:24 +00:00
|
|
|
return $row[0];
|
|
|
|
}
|
|
|
|
|
2011-01-03 15:18:24 +00:00
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* Get the ID of the last inserted row.
|
|
|
|
*
|
|
|
|
* @param string|null $seq
|
2015-09-27 00:03:58 +00:00
|
|
|
* @return int
|
2011-01-03 15:18:24 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_last_insert_id(string $seq): int {
|
2012-03-08 02:55:04 +00:00
|
|
|
if($this->engine->name == "pgsql") {
|
|
|
|
return $this->db->lastInsertId($seq);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $this->db->lastInsertId();
|
|
|
|
}
|
2011-01-03 15:18:24 +00:00
|
|
|
}
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2014-04-27 22:59:01 +00:00
|
|
|
* Create a table from pseudo-SQL.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $data
|
2009-07-19 07:38:13 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function create_table(string $name, string $data) {
|
2014-04-27 22:59:01 +00:00
|
|
|
if(is_null($this->engine)) { $this->connect_engine(); }
|
2015-07-02 11:38:33 +00:00
|
|
|
$data = trim($data, ", \t\n\r\0\x0B"); // mysql doesn't like trailing commas
|
2009-01-22 12:05:55 +00:00
|
|
|
$this->execute($this->engine->create_table_sql($name, $data));
|
2009-01-22 11:22:55 +00:00
|
|
|
}
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2012-04-18 04:55:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of tables present in the current database.
|
2014-04-27 22:59:01 +00:00
|
|
|
*
|
2017-09-19 17:55:43 +00:00
|
|
|
* @return int
|
|
|
|
* @throws SCoreException
|
2012-04-18 04:55:39 +00:00
|
|
|
*/
|
2017-09-19 17:55:43 +00:00
|
|
|
public function count_tables(): int {
|
2013-11-28 05:31:09 +00:00
|
|
|
if(is_null($this->db) || is_null($this->engine)) $this->connect_db();
|
2014-02-22 20:42:09 +00:00
|
|
|
|
2012-04-18 04:55:39 +00:00
|
|
|
if($this->engine->name === "mysql") {
|
|
|
|
return count(
|
2017-09-19 17:55:43 +00:00
|
|
|
$this->get_all("SHOW TABLES")
|
|
|
|
);
|
2012-04-18 04:55:39 +00:00
|
|
|
} else if ($this->engine->name === "pgsql") {
|
|
|
|
return count(
|
2017-09-19 17:55:43 +00:00
|
|
|
$this->get_all("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'")
|
|
|
|
);
|
2012-04-18 04:55:39 +00:00
|
|
|
} else if ($this->engine->name === "sqlite") {
|
|
|
|
return count(
|
2017-09-19 17:55:43 +00:00
|
|
|
$this->get_all("SELECT name FROM sqlite_master WHERE type = 'table'")
|
|
|
|
);
|
2012-04-18 04:55:39 +00:00
|
|
|
} else {
|
2017-09-19 17:55:43 +00:00
|
|
|
throw new SCoreException("Can't count tables for database type {$this->engine->name}");
|
2012-04-18 04:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-16 21:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MockDatabase extends Database {
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var int */
|
2016-06-19 22:05:57 +00:00
|
|
|
private $query_id = 0;
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var array */
|
2016-06-19 22:05:57 +00:00
|
|
|
private $responses = array();
|
2014-04-27 22:59:01 +00:00
|
|
|
/** @var \NoCache|null */
|
2016-06-19 22:05:57 +00:00
|
|
|
public $cache = null;
|
2012-10-16 21:58:47 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function __construct(array $responses = array()) {
|
2012-10-16 21:58:47 +00:00
|
|
|
$this->cache = new NoCache();
|
|
|
|
$this->responses = $responses;
|
|
|
|
}
|
2014-04-27 22:59:01 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function execute(string $query, array $params=array()): PDOStatement {
|
|
|
|
log_debug("mock-database",
|
|
|
|
"QUERY: " . $query .
|
|
|
|
"\nARGS: " . var_export($params, true) .
|
|
|
|
"\nRETURN: " . var_export($this->responses[$this->query_id], true)
|
|
|
|
);
|
|
|
|
return $this->responses[$this->query_id++];
|
|
|
|
}
|
|
|
|
public function _execute(string $query, array $params=array()) {
|
2012-10-16 21:58:47 +00:00
|
|
|
log_debug("mock-database",
|
|
|
|
"QUERY: " . $query .
|
|
|
|
"\nARGS: " . var_export($params, true) .
|
|
|
|
"\nRETURN: " . var_export($this->responses[$this->query_id], true)
|
|
|
|
);
|
|
|
|
return $this->responses[$this->query_id++];
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_all(string $query, array $args=array()): array {return $this->_execute($query, $args);}
|
|
|
|
public function get_row(string $query, array $args=array()) {return $this->_execute($query, $args);}
|
|
|
|
public function get_col(string $query, array $args=array()): array {return $this->_execute($query, $args);}
|
|
|
|
public function get_pairs(string $query, array $args=array()): array {return $this->_execute($query, $args);}
|
|
|
|
public function get_one(string $query, array $args=array()) {return $this->_execute($query, $args);}
|
2014-04-29 05:33:03 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function get_last_insert_id(string $seq): int {return $this->query_id;}
|
2012-10-16 21:58:47 +00:00
|
|
|
|
2017-09-19 17:55:43 +00:00
|
|
|
public function scoreql_to_sql(string $sql): string {return $sql;}
|
|
|
|
public function create_table(string $name, string $def) {}
|
2012-10-16 21:58:47 +00:00
|
|
|
public function connect_engine() {}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2014-04-24 23:01:47 +00:00
|
|
|
|