2020-01-26 13:19:35 +00:00
|
|
|
<?php declare(strict_types=1);
|
2019-11-26 10:05:52 +00:00
|
|
|
use FFSPHP\PDO;
|
|
|
|
|
2019-06-20 15:42:32 +00:00
|
|
|
abstract class DatabaseDriver
|
|
|
|
{
|
|
|
|
public const MYSQL = "mysql";
|
|
|
|
public const PGSQL = "pgsql";
|
|
|
|
public const SQLITE = "sqlite";
|
|
|
|
}
|
|
|
|
|
2018-11-05 22:30:18 +00:00
|
|
|
/**
|
|
|
|
* A class for controlled database access
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class Database
|
|
|
|
{
|
2020-01-27 18:24:11 +00:00
|
|
|
/** @var string */
|
|
|
|
private $dsn;
|
2019-06-14 18:17:03 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* The PDO database connection object, for anyone who wants direct access.
|
|
|
|
* @var null|PDO
|
|
|
|
*/
|
|
|
|
private $db = null;
|
2019-11-11 16:43:04 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
public $dbtime = 0.0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Meta info about the database engine.
|
|
|
|
* @var DBEngine|null
|
|
|
|
*/
|
|
|
|
private $engine = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A boolean flag to track if we already have an active transaction.
|
|
|
|
* (ie: True if beginTransaction() already called)
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $transaction = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How many queries this DB object has run
|
|
|
|
*/
|
|
|
|
public $query_count = 0;
|
|
|
|
|
2020-01-29 00:49:21 +00:00
|
|
|
public function __construct(string $dsn)
|
|
|
|
{
|
2020-01-27 18:24:11 +00:00
|
|
|
$this->dsn = $dsn;
|
|
|
|
}
|
|
|
|
|
2019-05-29 17:23:29 +00:00
|
|
|
private function connect_db(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-27 18:24:11 +00:00
|
|
|
$this->db = new PDO($this->dsn, [
|
2019-09-30 09:19:47 +00:00
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
2019-11-26 10:05:52 +00:00
|
|
|
]);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$this->connect_engine();
|
|
|
|
$this->engine->init($this->db);
|
|
|
|
|
|
|
|
$this->beginTransaction();
|
|
|
|
}
|
|
|
|
|
2019-05-29 17:23:29 +00:00
|
|
|
private function connect_engine(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-01-27 19:28:58 +00:00
|
|
|
if (preg_match("/^([^:]*)/", $this->dsn, $matches)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$db_proto=$matches[1];
|
|
|
|
} else {
|
|
|
|
throw new SCoreException("Can't figure out database engine");
|
|
|
|
}
|
|
|
|
|
2019-06-20 15:42:32 +00:00
|
|
|
if ($db_proto === DatabaseDriver::MYSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->engine = new MySQL();
|
2019-06-20 15:42:32 +00:00
|
|
|
} elseif ($db_proto === DatabaseDriver::PGSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->engine = new PostgreSQL();
|
2019-06-20 15:42:32 +00:00
|
|
|
} elseif ($db_proto === DatabaseDriver::SQLITE) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->engine = new SQLite();
|
|
|
|
} else {
|
|
|
|
die('Unknown PDO driver: '.$db_proto);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 17:23:29 +00:00
|
|
|
public function beginTransaction(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if ($this->transaction === false) {
|
|
|
|
$this->db->beginTransaction();
|
|
|
|
$this->transaction = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function commit(): bool
|
|
|
|
{
|
2019-11-11 16:43:04 +00:00
|
|
|
if (!is_null($this->db) && $this->transaction === true) {
|
|
|
|
$this->transaction = false;
|
|
|
|
return $this->db->commit();
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2019-11-11 16:43:04 +00:00
|
|
|
throw new SCoreException("Unable to call commit() as there is no transaction currently open.");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rollback(): bool
|
|
|
|
{
|
2019-11-11 16:43:04 +00:00
|
|
|
if (!is_null($this->db) && $this->transaction === true) {
|
|
|
|
$this->transaction = false;
|
|
|
|
return $this->db->rollback();
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2019-11-11 16:43:04 +00:00
|
|
|
throw new SCoreException("Unable to call rollback() as there is no transaction currently open.");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scoreql_to_sql(string $input): string
|
|
|
|
{
|
|
|
|
if (is_null($this->engine)) {
|
|
|
|
$this->connect_engine();
|
|
|
|
}
|
|
|
|
return $this->engine->scoreql_to_sql($input);
|
|
|
|
}
|
|
|
|
|
2019-06-25 18:58:50 +00:00
|
|
|
public function scoresql_value_prepare($input)
|
|
|
|
{
|
|
|
|
if (is_null($this->engine)) {
|
|
|
|
$this->connect_engine();
|
|
|
|
}
|
2019-09-29 13:30:55 +00:00
|
|
|
if ($input===true) {
|
2019-06-25 18:58:50 +00:00
|
|
|
return $this->engine->BOOL_Y;
|
2019-09-29 13:30:55 +00:00
|
|
|
} elseif ($input===false) {
|
2019-06-25 18:58:50 +00:00
|
|
|
return $this->engine->BOOL_N;
|
|
|
|
}
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
public function get_driver_name(): string
|
|
|
|
{
|
|
|
|
if (is_null($this->engine)) {
|
|
|
|
$this->connect_engine();
|
|
|
|
}
|
|
|
|
return $this->engine->name;
|
|
|
|
}
|
|
|
|
|
2019-07-06 20:41:48 +00:00
|
|
|
private function count_time(string $method, float $start, string $query, ?array $args): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-09-29 13:30:55 +00:00
|
|
|
global $_tracer, $tracer_enabled;
|
|
|
|
$dur = microtime(true) - $start;
|
|
|
|
if ($tracer_enabled) {
|
2019-07-31 13:52:17 +00:00
|
|
|
$query = trim(preg_replace('/^[\t ]+/m', '', $query)); // trim leading whitespace
|
|
|
|
$_tracer->complete($start * 1000000, $dur * 1000000, "DB Query", ["query"=>$query, "args"=>$args, "method"=>$method]);
|
|
|
|
}
|
2019-09-29 13:30:55 +00:00
|
|
|
$this->query_count++;
|
2019-07-06 22:01:22 +00:00
|
|
|
$this->dbtime += $dur;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 18:31:12 +00:00
|
|
|
public function set_timeout(int $time): void
|
|
|
|
{
|
|
|
|
$this->engine->set_timeout($this->db, $time);
|
|
|
|
}
|
|
|
|
|
2020-02-01 22:51:30 +00:00
|
|
|
public function execute(string $query, array $args = []): PDOStatement
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (is_null($this->db)) {
|
|
|
|
$this->connect_db();
|
|
|
|
}
|
2019-11-26 10:05:52 +00:00
|
|
|
return $this->db->execute(
|
2020-01-26 13:19:35 +00:00
|
|
|
"-- " . str_replace("%2F", "/", urlencode($_GET['q'] ?? '')). "\n" .
|
2019-11-26 10:05:52 +00:00
|
|
|
$query,
|
|
|
|
$args
|
2019-10-02 08:05:48 +00:00
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
} catch (PDOException $pdoe) {
|
2019-11-11 16:43:04 +00:00
|
|
|
throw new SCoreException($pdoe->getMessage(), $query);
|
2019-09-29 13:30:55 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an SQL query and return a 2D array.
|
|
|
|
*/
|
2020-02-01 22:51:30 +00:00
|
|
|
public function get_all(string $query, array $args = []): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$_start = microtime(true);
|
|
|
|
$data = $this->execute($query, $args)->fetchAll();
|
2019-07-06 20:41:48 +00:00
|
|
|
$this->count_time("get_all", $_start, $query, $args);
|
2019-05-28 16:59:38 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-07-05 15:24:46 +00:00
|
|
|
/**
|
|
|
|
* Execute an SQL query and return a iterable object for use with generators.
|
|
|
|
*/
|
2020-02-01 22:51:30 +00:00
|
|
|
public function get_all_iterable(string $query, array $args = []): PDOStatement
|
2019-07-05 15:24:46 +00:00
|
|
|
{
|
|
|
|
$_start = microtime(true);
|
|
|
|
$data = $this->execute($query, $args);
|
2019-07-07 10:11:27 +00:00
|
|
|
$this->count_time("get_all_iterable", $_start, $query, $args);
|
2019-07-05 15:24:46 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
/**
|
|
|
|
* Execute an SQL query and return a single row.
|
|
|
|
*/
|
2020-02-01 22:51:30 +00:00
|
|
|
public function get_row(string $query, array $args = []): ?array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$_start = microtime(true);
|
|
|
|
$row = $this->execute($query, $args)->fetch();
|
2019-07-06 20:41:48 +00:00
|
|
|
$this->count_time("get_row", $_start, $query, $args);
|
2019-05-28 16:59:38 +00:00
|
|
|
return $row ? $row : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an SQL query and return the first column of each row.
|
|
|
|
*/
|
2020-02-01 22:51:30 +00:00
|
|
|
public function get_col(string $query, array $args = []): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$_start = microtime(true);
|
2019-07-07 14:42:19 +00:00
|
|
|
$res = $this->execute($query, $args)->fetchAll(PDO::FETCH_COLUMN);
|
2019-07-06 20:41:48 +00:00
|
|
|
$this->count_time("get_col", $_start, $query, $args);
|
2019-05-28 16:59:38 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-05 15:24:46 +00:00
|
|
|
* Execute an SQL query and return the first column of each row as a single iterable object.
|
|
|
|
*/
|
2020-02-01 22:51:30 +00:00
|
|
|
public function get_col_iterable(string $query, array $args = []): Generator
|
2019-07-05 15:24:46 +00:00
|
|
|
{
|
|
|
|
$_start = microtime(true);
|
|
|
|
$stmt = $this->execute($query, $args);
|
2019-07-07 10:11:27 +00:00
|
|
|
$this->count_time("get_col_iterable", $_start, $query, $args);
|
2019-07-05 15:24:46 +00:00
|
|
|
foreach ($stmt as $row) {
|
|
|
|
yield $row[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an SQL query and return the the first column => the second column.
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
2020-02-01 22:51:30 +00:00
|
|
|
public function get_pairs(string $query, array $args = []): array
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$_start = microtime(true);
|
2019-07-07 14:42:19 +00:00
|
|
|
$res = $this->execute($query, $args)->fetchAll(PDO::FETCH_KEY_PAIR);
|
2019-07-06 20:41:48 +00:00
|
|
|
$this->count_time("get_pairs", $_start, $query, $args);
|
2019-05-28 16:59:38 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-27 22:22:07 +00:00
|
|
|
* Execute an SQL query and return a single value, or null.
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
2020-02-01 22:51:30 +00:00
|
|
|
public function get_one(string $query, array $args = [])
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$_start = microtime(true);
|
|
|
|
$row = $this->execute($query, $args)->fetch();
|
2019-07-06 20:41:48 +00:00
|
|
|
$this->count_time("get_one", $_start, $query, $args);
|
2020-01-27 22:22:07 +00:00
|
|
|
return $row ? $row[0] : null;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of the last inserted row.
|
|
|
|
*/
|
|
|
|
public function get_last_insert_id(string $seq): int
|
|
|
|
{
|
2019-06-20 15:42:32 +00:00
|
|
|
if ($this->engine->name == DatabaseDriver::PGSQL) {
|
2019-10-02 08:03:14 +00:00
|
|
|
$id = $this->db->lastInsertId($seq);
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2019-10-02 08:03:14 +00:00
|
|
|
$id = $this->db->lastInsertId();
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-10-02 08:05:48 +00:00
|
|
|
assert(is_numeric($id));
|
|
|
|
return (int)$id;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a table from pseudo-SQL.
|
|
|
|
*/
|
|
|
|
public function create_table(string $name, string $data): void
|
|
|
|
{
|
|
|
|
if (is_null($this->engine)) {
|
|
|
|
$this->connect_engine();
|
|
|
|
}
|
|
|
|
$data = trim($data, ", \t\n\r\0\x0B"); // mysql doesn't like trailing commas
|
|
|
|
$this->execute($this->engine->create_table_sql($name, $data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of tables present in the current database.
|
|
|
|
*
|
|
|
|
* @throws SCoreException
|
|
|
|
*/
|
|
|
|
public function count_tables(): int
|
|
|
|
{
|
|
|
|
if (is_null($this->db) || is_null($this->engine)) {
|
|
|
|
$this->connect_db();
|
|
|
|
}
|
|
|
|
|
2019-06-20 15:42:32 +00:00
|
|
|
if ($this->engine->name === DatabaseDriver::MYSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return count(
|
|
|
|
$this->get_all("SHOW TABLES")
|
|
|
|
);
|
2019-06-20 15:42:32 +00:00
|
|
|
} elseif ($this->engine->name === DatabaseDriver::PGSQL) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return count(
|
|
|
|
$this->get_all("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'")
|
|
|
|
);
|
2019-06-20 15:42:32 +00:00
|
|
|
} elseif ($this->engine->name === DatabaseDriver::SQLITE) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return count(
|
|
|
|
$this->get_all("SELECT name FROM sqlite_master WHERE type = 'table'")
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw new SCoreException("Can't count tables for database type {$this->engine->name}");
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 15:59:14 +00:00
|
|
|
|
2019-11-27 12:13:04 +00:00
|
|
|
public function raw_db(): PDO
|
|
|
|
{
|
2019-11-24 15:59:14 +00:00
|
|
|
return $this->db;
|
|
|
|
}
|
2018-11-05 22:30:18 +00:00
|
|
|
}
|