1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

Remove the DatabaseInformation class in tests and use traits instead

This commit is contained in:
J. King 2019-01-12 12:43:06 -05:00
parent 81acba90dc
commit e501fbdc87
38 changed files with 452 additions and 398 deletions

View file

@ -27,8 +27,6 @@ abstract class Base extends \JKingWeb\Arsse\Test\AbstractTest {
use SeriesLabel;
use SeriesCleanup;
/** @var \JKingWeb\Arsse\Test\DatabaseInformation */
protected static $dbInfo;
/** @var \JKingWeb\Arsse\Db\Driver */
protected static $drv;
protected static $failureReason = "";
@ -52,16 +50,15 @@ abstract class Base extends \JKingWeb\Arsse\Test\AbstractTest {
// perform an initial connection to the database to reset its version to zero
// in the case of SQLite this will always be the case (we use a memory database),
// but other engines should clean up from potentially interrupted prior tests
static::$dbInfo = new DatabaseInformation(static::$implementation);
static::setConf();
try {
static::$drv = new static::$dbInfo->driverClass;
static::$drv = new static::$dbDriverClass;
} catch (\JKingWeb\Arsse\Db\Exception $e) {
static::$failureReason = $e->getMessage();
return;
}
// wipe the database absolutely clean
(static::$dbInfo->razeFunction)(static::$drv);
static::dbRaze(static::$drv);
// create the database interface with the suitable driver and apply the latest schema
Arsse::$db = new Database(static::$drv);
Arsse::$db->driverSchemaUpdate();
@ -97,17 +94,16 @@ abstract class Base extends \JKingWeb\Arsse\Test\AbstractTest {
// clean up
$this->primed = false;
// call the database-specific table cleanup function
(static::$dbInfo->truncateFunction)(static::$drv);
static::dbTruncate(static::$drv);
// clear state
static::clearData();
}
public static function tearDownAfterClass() {
// wipe the database absolutely clean
(static::$dbInfo->razeFunction)(static::$drv);
static::dbRaze(static::$drv);
// clean up
static::$drv = null;
static::$dbInfo = null;
static::$failureReason = "";
static::clearData();
}

View file

@ -12,7 +12,7 @@ use JKingWeb\Arsse\Database;
trait SeriesMiscellany {
protected function setUpSeriesMiscellany() {
static::setConf([
'dbDriver' => static::$dbInfo->driverClass,
'dbDriver' => static::$dbDriverClass,
]);
}
@ -27,13 +27,13 @@ trait SeriesMiscellany {
}
public function testInitializeDatabase() {
(static::$dbInfo->razeFunction)(static::$drv);
static::dbRaze(static::$drv);
$d = new Database(true);
$this->assertSame(Database::SCHEMA_VERSION, $d->driverSchemaVersion());
}
public function testManuallyInitializeDatabase() {
(static::$dbInfo->razeFunction)(static::$drv);
static::dbRaze(static::$drv);
$d = new Database(false);
$this->assertSame(0, $d->driverSchemaVersion());
$this->assertTrue($d->driverSchemaUpdate());

View file

@ -0,0 +1,68 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\DatabaseDrivers;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\Driver;
trait MySQL {
protected static $implementation = "MySQL";
protected static $backend = "MySQL";
protected static $dbResultClass = \JKingWeb\Arsse\Db\MySQL\Result::class;
protected static $dbStatementClass = \JKingWeb\Arsse\Db\MySQL\Statement::class;
protected static $dbDriverClass = \JKingWeb\Arsse\Db\MySQL\Driver::class;
protected static $stringOutput = false;
public static function dbInterface() {
}
public static function dbTableList($db): array {
$listTables = "SELECT table_name as name from information_schema.tables where table_schema = database() and table_name like 'arsse_%'";
if ($db instanceof Driver) {
$tables = $db->query($listTables)->getAll();
} elseif ($db instanceof \PDO) {
$tables = $db->query($listTables)->fetchAll(\PDO::FETCH_ASSOC);
} else {
$tables = $db->query($listTables)->fetch_all(\MYSQLI_ASSOC);
}
$tables = sizeof($tables) ? array_column($tables, "name") : [];
return $tables;
}
public static function dbTruncate($db, array $afterStatements = []) {
// rollback any pending transaction
try {
$db->query("UNLOCK TABLES; ROLLBACK");
} catch (\Throwable $e) {
}
foreach (self::dbTableList($db) as $table) {
if ($table === "arsse_meta") {
$db->query("DELETE FROM $table where `key` <> 'schema_version'");
} else {
$db->query("DELETE FROM $table");
}
$db->query("ALTER TABLE $table auto_increment = 1");
}
foreach ($afterStatements as $st) {
$db->query($st);
}
}
public static function dbRaze($db, array $afterStatements = []) {
// rollback any pending transaction
try {
$db->query("UNLOCK TABLES; ROLLBACK");
} catch (\Throwable $e) {
}
foreach (self::dbTableList($db) as $table) {
$db->query("DROP TABLE IF EXISTS $table");
}
foreach ($afterStatements as $st) {
$db->query($st);
}
}
}

View file

@ -0,0 +1,50 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\DatabaseDrivers;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\Driver;
trait MySQLPDO {
protected static $implementation = "PDO MySQL";
protected static $backend = "MySQL";
protected static $dbResultClass = \JKingWeb\Arsse\Db\PDOResult::class;
protected static $dbStatementClass = \JKingWeb\Arsse\Db\MySQL\PDOStatement::class;
protected static $dbDriverClass = \JKingWeb\Arsse\Db\MySQL\PDODriver::class;
protected static $stringOutput = true;
public static function dbInterface() {
try {
$dsn = [];
$params = [
'charset' => "utf8mb4",
'host' => Arsse::$conf->dbMySQLHost,
'port' => Arsse::$conf->dbMySQLPort,
'dbname' => Arsse::$conf->dbMySQLDb,
];
foreach ($params as $k => $v) {
$dsn[] = "$k=$v";
}
$dsn = "mysql:".implode(";", $dsn);
return new \PDO($dsn, Arsse::$conf->dbMySQLUser, Arsse::$conf->dbMySQLPass, [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::MYSQL_ATTR_MULTI_STATEMENTS => false, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET sql_mode = '".\JKingWeb\Arsse\Db\MySQL\PDODriver::SQL_MODE."'",]);
} catch (\Throwable $e) {
return;
}
}
public static function dbTableList($db): array {
return MySQL::dbTableList($db);
}
public static function dbTruncate($db, array $afterStatements = []) {
MySQL::dbTruncate($db, $afterStatements);
}
public static function dbRaze($db, array $afterStatements = []) {
MySQL::dbRaze($db, $afterStatements);
}
}

View file

@ -0,0 +1,88 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\DatabaseDrivers;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\Driver;
trait PostgreSQL {
protected static $implementation = "PostgreSQL";
protected static $backend = "PostgreSQL";
protected static $dbResultClass = \JKingWeb\Arsse\Db\PostgreSQL\Result::class;
protected static $dbStatementClass = \JKingWeb\Arsse\Db\PostgreSQL\Statement::class;
protected static $dbDriverClass = \JKingWeb\Arsse\Db\PostgreSQL\Driver::class;
protected static $stringOutput = true;
public static function dbInterface() {
$connString = \JKingWeb\Arsse\Db\PostgreSQL\Driver::makeConnectionString(false, Arsse::$conf->dbPostgreSQLUser, Arsse::$conf->dbPostgreSQLPass, Arsse::$conf->dbPostgreSQLDb, Arsse::$conf->dbPostgreSQLHost, Arsse::$conf->dbPostgreSQLPort, "");
if ($d = @pg_connect($connString, \PGSQL_CONNECT_FORCE_NEW)) {
foreach (\JKingWeb\Arsse\Db\PostgreSQL\Driver::makeSetupQueries(Arsse::$conf->dbPostgreSQLSchema) as $q) {
pg_query($d, $q);
}
return $d;
} else {
return;
}
}
public static function dbExec($db, $q) {
if ($db instanceof Driver) {
$db->exec($q);
} elseif ($db instanceof \PDO) {
$db->exec($q);
} else {
pg_query($db, $q);
}
}
public static function dbTableList($db): array {
$listObjects = "SELECT table_name as name, 'TABLE' as type from information_schema.tables where table_schema = current_schema() and table_name like 'arsse_%' union SELECT collation_name as name, 'COLLATION' as type from information_schema.collations where collation_schema = current_schema()";
if ($db instanceof Driver) {
return $db->query($listObjects)->getAll();
} elseif ($db instanceof \PDO) {
return $db->query($listObjects)->fetchAll(\PDO::FETCH_ASSOC);
} else {
$r = @pg_query($db, $listObjects);
$out = $r ? pg_fetch_all($r) : false;
return $out ? $out : [];
}
}
public static function dbTruncate($db, array $afterStatements = []) {
// rollback any pending transaction
try {
@self::dbExec($db, "ROLLBACK");
} catch (\Throwable $e) {
}
foreach (self::dbTableList($db) as $obj) {
if ($obj['type'] !== "TABLE") {
continue;
} elseif ($obj['name'] === "arsse_meta") {
self::dbExec($db, "DELETE FROM {$obj['name']} where key <> 'schema_version'");
} else {
self::dbExec($db, "TRUNCATE TABLE {$obj['name']} restart identity cascade");
}
}
foreach ($afterStatements as $st) {
self::dbExec($db, $st);
}
}
public static function dbRaze($db, array $afterStatements = []) {
// rollback any pending transaction
try {
@self::dbExec($db, "ROLLBACK");
} catch (\Throwable $e) {
}
foreach (self::dbTableList($db) as $obj) {
self::dbExec($db, "DROP {$obj['type']} IF EXISTS {$obj['name']} cascade");
}
foreach ($afterStatements as $st) {
self::dbExec($db, $st);
}
}
}

View file

@ -0,0 +1,44 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\DatabaseDrivers;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\Driver;
trait PostgreSQLPDO {
protected static $implementation = "PDO PostgreSQL";
protected static $backend = "PostgreSQL";
protected static $dbResultClass = \JKingWeb\Arsse\Db\PDOResult::class;
protected static $dbStatementClass = \JKingWeb\Arsse\Db\PostgreSQL\PDOStatement::class;
protected static $dbDriverClass = \JKingWeb\Arsse\Db\PostgreSQL\PDODriver::class;
protected static $stringOutput = false;
public static function dbInterface() {
$connString = \JKingWeb\Arsse\Db\PostgreSQL\Driver::makeConnectionString(true, Arsse::$conf->dbPostgreSQLUser, Arsse::$conf->dbPostgreSQLPass, Arsse::$conf->dbPostgreSQLDb, Arsse::$conf->dbPostgreSQLHost, Arsse::$conf->dbPostgreSQLPort, "");
try {
$d = new \PDO("pgsql:".$connString, Arsse::$conf->dbPostgreSQLUser, Arsse::$conf->dbPostgreSQLPass, [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
} catch (\Throwable $e) {
return;
}
foreach (\JKingWeb\Arsse\Db\PostgreSQL\PDODriver::makeSetupQueries(Arsse::$conf->dbPostgreSQLSchema) as $q) {
$d->exec($q);
}
return $d;
}
public static function dbTableList($db): array {
return PostgreSQL::dbTableList($db);
}
public static function dbTruncate($db, array $afterStatements = []) {
PostgreSQL::dbTruncate($db, $afterStatements);
}
public static function dbRaze($db, array $afterStatements = []) {
PostgreSQL::dbRaze($db, $afterStatements);
}
}

View file

@ -0,0 +1,88 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\DatabaseDrivers;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\Driver;
trait SQLite3 {
protected static $implementation = "SQLite 3";
protected static $backend = "SQLite 3";
protected static $dbResultClass = \JKingWeb\Arsse\Db\SQLite3\Result::class;
protected static $dbStatementClass = \JKingWeb\Arsse\Db\SQLite3\Statement::class;
protected static $dbDriverClass = \JKingWeb\Arsse\Db\SQLite3\Driver::class;
protected static $stringOutput = false;
protected static function dbInterface() {
try {
$d = new \SQLite3(Arsse::$conf->dbSQLite3File);
} catch (\Throwable $e) {
return;
}
$d->enableExceptions(true);
return $d;
}
public static function dbTableList($db): array {
$listTables = "SELECT name from sqlite_master where type = 'table' and name like 'arsse_%'";
if ($db instanceof Driver) {
$tables = $db->query($listTables)->getAll();
$tables = sizeof($tables) ? array_column($tables, "name") : [];
} elseif ($db instanceof \PDO) {
retry:
try {
$tables = $db->query($listTables)->fetchAll(\PDO::FETCH_ASSOC);
} catch (\PDOException $e) {
goto retry;
}
$tables = sizeof($tables) ? array_column($tables, "name") : [];
} else {
$tables = [];
$result = $db->query($listTables);
while ($r = $result->fetchArray(\SQLITE3_ASSOC)) {
$tables[] = $r['name'];
}
$result->finalize();
}
return $tables;
}
public static function dbTruncate($db, array $afterStatements = []) {
// rollback any pending transaction
try {
$db->exec("ROLLBACK");
} catch (\Throwable $e) {
}
foreach (self::dbTableList($db) as $table) {
if ($table === "arsse_meta") {
$db->exec("DELETE FROM $table where key <> 'schema_version'");
} else {
$db->exec("DELETE FROM $table");
}
}
foreach ($afterStatements as $st) {
$db->exec($st);
}
}
public static function dbRaze($db, array $afterStatements = []) {
// rollback any pending transaction
try {
$db->exec("ROLLBACK");
} catch (\Throwable $e) {
}
$db->exec("PRAGMA foreign_keys=0");
foreach (self::dbTableList($db) as $table) {
$db->exec("DROP TABLE IF EXISTS $table");
}
$db->exec("PRAGMA user_version=0");
$db->exec("PRAGMA foreign_keys=1");
foreach ($afterStatements as $st) {
$db->exec($st);
}
}
}

View file

@ -0,0 +1,41 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\DatabaseDrivers;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\Driver;
trait SQLite3PDO {
protected static $implementation = "PDO SQLite 3";
protected static $backend = "SQLite 3";
protected static $dbResultClass = \JKingWeb\Arsse\Db\PDOResult::class;
protected static $dbStatementClass = \JKingWeb\Arsse\Db\SQLite3\PDOStatement::class;
protected static $dbDriverClass = \JKingWeb\Arsse\Db\SQLite3\PDODriver::class;
protected static $stringOutput = true;
public static function dbInterface() {
try {
$d = new \PDO("sqlite:".Arsse::$conf->dbSQLite3File, "", "", [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
$d->exec("PRAGMA busy_timeout=0");
return $d;
} catch (\Throwable $e) {
return;
}
}
public static function dbTableList($db): array {
return SQLite3::dbTableList($db);
}
public static function dbTruncate($db, array $afterStatements = []) {
SQLite3::dbTruncate($db, $afterStatements);
}
public static function dbRaze($db, array $afterStatements = []) {
SQLite3::dbRaze($db, $afterStatements);
}
}

View file

@ -12,7 +12,6 @@ use JKingWeb\Arsse\Test\DatabaseInformation;
abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest {
protected static $insertDefaultValues = "INSERT INTO arsse_test default values";
protected static $dbInfo;
protected static $interface;
protected $drv;
protected $create;
@ -27,9 +26,8 @@ abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest {
public static function setUpBeforeClass() {
// establish a clean baseline
static::clearData();
static::$dbInfo = new DatabaseInformation(static::$implementation);
static::setConf(static::$conf);
static::$interface = (static::$dbInfo->interfaceConstructor)();
static::$interface = static::dbInterface();
}
public function setUp() {
@ -39,12 +37,12 @@ abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest {
$this->markTestSkipped(static::$implementation." database driver not available");
}
// completely clear the database and ensure the schema version can easily be altered
(static::$dbInfo->razeFunction)(static::$interface, [
static::dbRaze(static::$interface, [
"CREATE TABLE arsse_meta(\"key\" varchar(255) primary key not null, value text)",
"INSERT INTO arsse_meta(\"key\",value) values('schema_version','0')",
]);
// construct a fresh driver for each test
$this->drv = new static::$dbInfo->driverClass;
$this->drv = new static::$dbDriverClass;
}
public function tearDown() {
@ -56,10 +54,9 @@ abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest {
public static function tearDownAfterClass() {
if (static::$interface) {
// completely clear the database
(static::$dbInfo->razeFunction)(static::$interface);
static::dbRaze(static::$interface);
}
static::$interface = null;
static::$dbInfo = null;
self::clearData();
}
@ -359,7 +356,7 @@ abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest {
// SQLite is unaffected by the removal of the metadata table; other backends are
// in neither case should a query for the schema version produce an error, however
$this->exec("DROP TABLE IF EXISTS arsse_meta");
$exp = (static::$dbInfo->backend === "SQLite 3") ? 2 : 0;
$exp = (static::$backend === "SQLite 3") ? 2 : 0;
$this->assertSame($exp, $this->drv->schemaVersion());
}

View file

@ -11,19 +11,16 @@ use JKingWeb\Arsse\Test\DatabaseInformation;
abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest {
protected static $insertDefault = "INSERT INTO arsse_test default values";
protected static $dbInfo;
protected static $interface;
protected $resultClass;
protected $stringOutput;
abstract protected function makeResult(string $q): array;
public static function setUpBeforeClass() {
// establish a clean baseline
static::clearData();
static::$dbInfo = new DatabaseInformation(static::$implementation);
static::setConf();
static::$interface = (static::$dbInfo->interfaceConstructor)();
static::$interface = static::dbInterface();
}
public function setUp() {
@ -33,9 +30,8 @@ abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest {
$this->markTestSkipped(static::$implementation." database driver not available");
}
// completely clear the database
(static::$dbInfo->razeFunction)(static::$interface);
$this->resultClass = static::$dbInfo->resultClass;
$this->stringOutput = static::$dbInfo->stringOutput;
static::dbRaze(static::$interface);
$this->resultClass = static::$dbResultClass;
}
public function tearDown() {
@ -45,10 +41,9 @@ abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest {
public static function tearDownAfterClass() {
if (static::$interface) {
// completely clear the database
(static::$dbInfo->razeFunction)(static::$interface);
static::dbRaze(static::$interface);
}
static::$interface = null;
static::$dbInfo = null;
self::clearData();
}
@ -75,7 +70,7 @@ abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest {
public function testIterateOverResults() {
$exp = [0 => 1, 1 => 2, 2 => 3];
$exp = $this->stringOutput ? $this->stringify($exp) : $exp;
$exp = static::$stringOutput ? $this->stringify($exp) : $exp;
foreach (new $this->resultClass(...$this->makeResult("SELECT 1 as col union select 2 as col union select 3 as col")) as $index => $row) {
$rows[$index] = $row['col'];
}
@ -84,7 +79,7 @@ abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest {
public function testIterateOverResultsTwice() {
$exp = [0 => 1, 1 => 2, 2 => 3];
$exp = $this->stringOutput ? $this->stringify($exp) : $exp;
$exp = static::$stringOutput ? $this->stringify($exp) : $exp;
$result = new $this->resultClass(...$this->makeResult("SELECT 1 as col union select 2 as col union select 3 as col"));
foreach ($result as $index => $row) {
$rows[$index] = $row['col'];
@ -98,7 +93,7 @@ abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest {
public function testGetSingleValues() {
$exp = [1867, 1970, 2112];
$exp = $this->stringOutput ? $this->stringify($exp) : $exp;
$exp = static::$stringOutput ? $this->stringify($exp) : $exp;
$test = new $this->resultClass(...$this->makeResult("SELECT 1867 as year union all select 1970 as year union all select 2112 as year"));
$this->assertSame($exp[0], $test->getValue());
$this->assertSame($exp[1], $test->getValue());
@ -108,7 +103,7 @@ abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest {
public function testGetFirstValuesOnly() {
$exp = [1867, 1970, 2112];
$exp = $this->stringOutput ? $this->stringify($exp) : $exp;
$exp = static::$stringOutput ? $this->stringify($exp) : $exp;
$test = new $this->resultClass(...$this->makeResult("SELECT 1867 as year, 19 as century union all select 1970 as year, 20 as century union all select 2112 as year, 22 as century"));
$this->assertSame($exp[0], $test->getValue());
$this->assertSame($exp[1], $test->getValue());

View file

@ -10,10 +10,8 @@ use JKingWeb\Arsse\Db\Statement;
use JKingWeb\Arsse\Test\DatabaseInformation;
abstract class BaseStatement extends \JKingWeb\Arsse\Test\AbstractTest {
protected static $dbInfo;
protected static $interface;
protected $statementClass;
protected $stringOutput;
abstract protected function makeStatement(string $q, array $types = []): array;
abstract protected function decorateTypeSyntax(string $value, string $type): string;
@ -21,9 +19,8 @@ abstract class BaseStatement extends \JKingWeb\Arsse\Test\AbstractTest {
public static function setUpBeforeClass() {
// establish a clean baseline
static::clearData();
static::$dbInfo = new DatabaseInformation(static::$implementation);
static::setConf();
static::$interface = (static::$dbInfo->interfaceConstructor)();
static::$interface = static::dbInterface();
}
public function setUp() {
@ -33,9 +30,8 @@ abstract class BaseStatement extends \JKingWeb\Arsse\Test\AbstractTest {
$this->markTestSkipped(static::$implementation." database driver not available");
}
// completely clear the database
(static::$dbInfo->razeFunction)(static::$interface);
$this->statementClass = static::$dbInfo->statementClass;
$this->stringOutput = static::$dbInfo->stringOutput;
static::dbRaze(static::$interface);
$this->statementClass = static::$dbStatementClass;
}
public function tearDown() {
@ -45,10 +41,9 @@ abstract class BaseStatement extends \JKingWeb\Arsse\Test\AbstractTest {
public static function tearDownAfterClass() {
if (static::$interface) {
// completely clear the database
(static::$dbInfo->razeFunction)(static::$interface);
static::dbRaze(static::$interface);
}
static::$interface = null;
static::$dbInfo = null;
self::clearData();
}
@ -98,7 +93,7 @@ abstract class BaseStatement extends \JKingWeb\Arsse\Test\AbstractTest {
'one' => 1,
'two' => 2,
];
$exp = $this->stringOutput ? $this->stringify($exp) : $exp;
$exp = static::$stringOutput ? $this->stringify($exp) : $exp;
$s = new $this->statementClass(...$this->makeStatement("SELECT ? as one, ? as two", ["int", "int"]));
$val = $s->runArray([1,2])->getRow();
$this->assertSame($exp, $val);
@ -111,7 +106,7 @@ abstract class BaseStatement extends \JKingWeb\Arsse\Test\AbstractTest {
'three' => 3,
'four' => 4,
];
$exp = $this->stringOutput ? $this->stringify($exp) : $exp;
$exp = static::$stringOutput ? $this->stringify($exp) : $exp;
$s = new $this->statementClass(...$this->makeStatement("SELECT ? as one, ? as two, ? as three, ? as four", ["int", ["int", "int"], "int"]));
$val = $s->runArray([1, [2, 3], 4])->getRow();
$this->assertSame($exp, $val);

View file

@ -13,7 +13,6 @@ use JKingWeb\Arsse\Test\DatabaseInformation;
use org\bovigo\vfs\vfsStream;
class BaseUpdate extends \JKingWeb\Arsse\Test\AbstractTest {
protected static $dbInfo;
protected static $interface;
protected $drv;
protected $vfs;
@ -23,9 +22,8 @@ class BaseUpdate extends \JKingWeb\Arsse\Test\AbstractTest {
public static function setUpBeforeClass() {
// establish a clean baseline
static::clearData();
static::$dbInfo = new DatabaseInformation(static::$implementation);
static::setConf();
static::$interface = (static::$dbInfo->interfaceConstructor)();
static::$interface = static::dbInterface();
}
public function setUp() {
@ -35,14 +33,14 @@ class BaseUpdate extends \JKingWeb\Arsse\Test\AbstractTest {
self::clearData();
self::setConf();
// construct a fresh driver for each test
$this->drv = new static::$dbInfo->driverClass;
$this->drv = new static::$dbDriverClass;
$schemaId = (get_class($this->drv))::schemaID();
// set up a virtual filesystem for schema files
$this->vfs = vfsStream::setup("schemata", null, [$schemaId => []]);
$this->base = $this->vfs->url();
$this->path = $this->base."/$schemaId/";
// completely clear the database
(static::$dbInfo->razeFunction)(static::$interface);
static::dbRaze(static::$interface);
}
public function tearDown() {
@ -55,10 +53,9 @@ class BaseUpdate extends \JKingWeb\Arsse\Test\AbstractTest {
public static function tearDownAfterClass() {
if (static::$interface) {
// completely clear the database
(static::$dbInfo->razeFunction)(static::$interface);
static::dbRaze(static::$interface);
}
static::$interface = null;
static::$dbInfo = null;
self::clearData();
}

View file

@ -13,7 +13,7 @@ namespace JKingWeb\Arsse\TestCase\Db\MySQLPDO;
* @covers \JKingWeb\Arsse\Misc\Query<extended>
*/
class TestDatabase extends \JKingWeb\Arsse\TestCase\Database\Base {
protected static $implementation = "PDO MySQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\MySQLPDO;
protected function nextID(string $table): int {
return (int) (static::$drv->query("SELECT auto_increment from information_schema.tables where table_name = '$table'")->getValue() ?? 1);

View file

@ -12,7 +12,8 @@ namespace JKingWeb\Arsse\TestCase\Db\MySQLPDO;
* @covers \JKingWeb\Arsse\Db\PDODriver
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestDriver extends \JKingWeb\Arsse\TestCase\Db\BaseDriver {
protected static $implementation = "PDO MySQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\MySQLPDO;
protected $create = "CREATE TABLE arsse_test(id bigint auto_increment primary key)";
protected $lock = ["SET lock_wait_timeout = 1", "LOCK TABLES arsse_meta WRITE"];
protected $setVersion = "UPDATE arsse_meta set value = '#' where `key` = 'schema_version'";

View file

@ -13,7 +13,8 @@ use JKingWeb\Arsse\Test\DatabaseInformation;
* @covers \JKingWeb\Arsse\Db\PDOResult<extended>
*/
class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult {
protected static $implementation = "PDO MySQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\MySQLPDO;
protected static $createMeta = "CREATE TABLE arsse_meta(`key` varchar(255) primary key not null, value text)";
protected static $createTest = "CREATE TABLE arsse_test(id bigint auto_increment primary key)";
protected static $insertDefault = "INSERT INTO arsse_test(id) values(default)";

View file

@ -11,7 +11,7 @@ namespace JKingWeb\Arsse\TestCase\Db\MySQLPDO;
* @covers \JKingWeb\Arsse\Db\MySQL\PDOStatement<extended>
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestStatement extends \JKingWeb\Arsse\TestCase\Db\BaseStatement {
protected static $implementation = "PDO MySQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\MySQLPDO;
protected function makeStatement(string $q, array $types = []): array {
return [static::$interface, $q, $types];

View file

@ -11,7 +11,8 @@ namespace JKingWeb\Arsse\TestCase\Db\MySQLPDO;
* @covers \JKingWeb\Arsse\Db\MySQL\PDODriver<extended>
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestUpdate extends \JKingWeb\Arsse\TestCase\Db\BaseUpdate {
protected static $implementation = "PDO MySQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\MySQLPDO;
protected static $minimal1 = "CREATE TABLE arsse_meta(`key` varchar(255) primary key, value text); INSERT INTO arsse_meta(`key`,value) values('schema_version','1');";
protected static $minimal2 = "UPDATE arsse_meta set value = '2' where `key` = 'schema_version';";
}

View file

@ -13,7 +13,7 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQL;
* @covers \JKingWeb\Arsse\Misc\Query<extended>
*/
class TestDatabase extends \JKingWeb\Arsse\TestCase\Database\Base {
protected static $implementation = "PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQL;
protected function nextID(string $table): int {
return (int) static::$drv->query("SELECT coalesce(last_value, (select max(id) from $table)) + 1 from pg_sequences where sequencename = '{$table}_id_seq'")->getValue();

View file

@ -11,7 +11,8 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQL;
* @covers \JKingWeb\Arsse\Db\PostgreSQL\Driver<extended>
* @covers \JKingWeb\Arsse\Db\PostgreSQL\Dispatch<extended> */
class TestDriver extends \JKingWeb\Arsse\TestCase\Db\BaseDriver {
protected static $implementation = "PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQL;
protected $create = "CREATE TABLE arsse_test(id bigserial primary key)";
protected $lock = ["BEGIN", "LOCK TABLE arsse_meta IN EXCLUSIVE MODE NOWAIT"];
protected $setVersion = "UPDATE arsse_meta set value = '#' where key = 'schema_version'";
@ -26,7 +27,7 @@ class TestDriver extends \JKingWeb\Arsse\TestCase\Db\BaseDriver {
public static function tearDownAfterClass() {
if (static::$interface) {
(static::$dbInfo->razeFunction)(static::$interface);
static::dbRaze(static::$interface);
@pg_close(static::$interface);
static::$interface = null;
}

View file

@ -13,7 +13,8 @@ use JKingWeb\Arsse\Test\DatabaseInformation;
* @covers \JKingWeb\Arsse\Db\PostgreSQL\Result<extended>
*/
class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult {
protected static $implementation = "PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQL;
protected static $createMeta = "CREATE TABLE arsse_meta(key text primary key not null, value text)";
protected static $createTest = "CREATE TABLE arsse_test(id bigserial primary key)";
@ -24,7 +25,7 @@ class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult {
public static function tearDownAfterClass() {
if (static::$interface) {
(static::$dbInfo->razeFunction)(static::$interface);
static::dbRaze(static::$interface);
@pg_close(static::$interface);
static::$interface = null;
}

View file

@ -11,7 +11,7 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQL;
* @covers \JKingWeb\Arsse\Db\PostgreSQL\Statement<extended>
* @covers \JKingWeb\Arsse\Db\PostgreSQL\Dispatch<extended> */
class TestStatement extends \JKingWeb\Arsse\TestCase\Db\BaseStatement {
protected static $implementation = "PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQL;
protected function makeStatement(string $q, array $types = []): array {
return [static::$interface, $q, $types];
@ -33,7 +33,7 @@ class TestStatement extends \JKingWeb\Arsse\TestCase\Db\BaseStatement {
public static function tearDownAfterClass() {
if (static::$interface) {
(static::$dbInfo->razeFunction)(static::$interface);
static::dbRaze(static::$interface);
@pg_close(static::$interface);
static::$interface = null;
}

View file

@ -10,7 +10,8 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQL;
* @group slow
* @covers \JKingWeb\Arsse\Db\PostgreSQL\Driver<extended> */
class TestUpdate extends \JKingWeb\Arsse\TestCase\Db\BaseUpdate {
protected static $implementation = "PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQL;
protected static $minimal1 = "CREATE TABLE arsse_meta(key text primary key, value text); INSERT INTO arsse_meta(key,value) values('schema_version','1');";
protected static $minimal2 = "UPDATE arsse_meta set value = '2' where key = 'schema_version';";
}

View file

@ -14,7 +14,7 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQLPDO;
* @covers \JKingWeb\Arsse\Misc\Query<extended>
*/
class TestDatabase extends \JKingWeb\Arsse\TestCase\Database\Base {
protected static $implementation = "PDO PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQLPDO;
protected function nextID(string $table): int {
return (int) static::$drv->query("SELECT coalesce(last_value, (select max(id) from $table)) + 1 from pg_sequences where sequencename = '{$table}_id_seq'")->getValue();

View file

@ -12,7 +12,8 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQLPDO;
* @covers \JKingWeb\Arsse\Db\PDODriver
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestDriver extends \JKingWeb\Arsse\TestCase\Db\BaseDriver {
protected static $implementation = "PDO PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQLPDO;
protected $create = "CREATE TABLE arsse_test(id bigserial primary key)";
protected $lock = ["BEGIN", "LOCK TABLE arsse_meta IN EXCLUSIVE MODE NOWAIT"];
protected $setVersion = "UPDATE arsse_meta set value = '#' where key = 'schema_version'";

View file

@ -13,7 +13,8 @@ use JKingWeb\Arsse\Test\DatabaseInformation;
* @covers \JKingWeb\Arsse\Db\PDOResult<extended>
*/
class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult {
protected static $implementation = "PDO PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQLPDO;
protected static $createMeta = "CREATE TABLE arsse_meta(key text primary key not null, value text)";
protected static $createTest = "CREATE TABLE arsse_test(id bigserial primary key)";

View file

@ -11,7 +11,7 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQLPDO;
* @covers \JKingWeb\Arsse\Db\PostgreSQL\PDOStatement<extended>
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestStatement extends \JKingWeb\Arsse\TestCase\Db\BaseStatement {
protected static $implementation = "PDO PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQLPDO;
protected function makeStatement(string $q, array $types = []): array {
return [static::$interface, $q, $types];

View file

@ -11,7 +11,8 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQLPDO;
* @covers \JKingWeb\Arsse\Db\PostgreSQL\PDODriver<extended>
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestUpdate extends \JKingWeb\Arsse\TestCase\Db\BaseUpdate {
protected static $implementation = "PDO PostgreSQL";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\PostgreSQLPDO;
protected static $minimal1 = "CREATE TABLE arsse_meta(key text primary key, value text); INSERT INTO arsse_meta(key,value) values('schema_version','1');";
protected static $minimal2 = "UPDATE arsse_meta set value = '2' where key = 'schema_version';";
}

View file

@ -12,7 +12,7 @@ namespace JKingWeb\Arsse\TestCase\Db\SQLite3;
* @covers \JKingWeb\Arsse\Misc\Query<extended>
*/
class TestDatabase extends \JKingWeb\Arsse\TestCase\Database\Base {
protected static $implementation = "SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3;
protected function nextID(string $table): int {
return static::$drv->query("SELECT (case when max(id) then max(id) else 0 end)+1 from $table")->getValue();

View file

@ -10,7 +10,8 @@ namespace JKingWeb\Arsse\TestCase\Db\SQLite3;
* @covers \JKingWeb\Arsse\Db\SQLite3\Driver<extended>
* @covers \JKingWeb\Arsse\Db\SQLite3\ExceptionBuilder */
class TestDriver extends \JKingWeb\Arsse\TestCase\Db\BaseDriver {
protected static $implementation = "SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3;
protected $create = "CREATE TABLE arsse_test(id integer primary key)";
protected $lock = "BEGIN EXCLUSIVE TRANSACTION";
protected $setVersion = "PRAGMA user_version=#";

View file

@ -12,7 +12,8 @@ use JKingWeb\Arsse\Test\DatabaseInformation;
* @covers \JKingWeb\Arsse\Db\SQLite3\Result<extended>
*/
class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult {
protected static $implementation = "SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3;
protected static $createMeta = "CREATE TABLE arsse_meta(key text primary key not null, value text) without rowid";
protected static $createTest = "CREATE TABLE arsse_test(id integer primary key)";

View file

@ -10,7 +10,7 @@ namespace JKingWeb\Arsse\TestCase\Db\SQLite3;
* @covers \JKingWeb\Arsse\Db\SQLite3\Statement<extended>
* @covers \JKingWeb\Arsse\Db\SQLite3\ExceptionBuilder */
class TestStatement extends \JKingWeb\Arsse\TestCase\Db\BaseStatement {
protected static $implementation = "SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3;
public static function tearDownAfterClass() {
static::$interface->close();

View file

@ -10,7 +10,8 @@ namespace JKingWeb\Arsse\TestCase\Db\SQLite3;
* @covers \JKingWeb\Arsse\Db\SQLite3\Driver<extended>
* @covers \JKingWeb\Arsse\Db\SQLite3\ExceptionBuilder */
class TestUpdate extends \JKingWeb\Arsse\TestCase\Db\BaseUpdate {
protected static $implementation = "SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3;
protected static $minimal1 = "create table arsse_meta(key text primary key not null, value text); pragma user_version=1";
protected static $minimal2 = "pragma user_version=2";

View file

@ -11,7 +11,7 @@ namespace JKingWeb\Arsse\TestCase\Db\SQLite3PDO;
* @covers \JKingWeb\Arsse\Misc\Query<extended>
*/
class TestDatabase extends \JKingWeb\Arsse\TestCase\Database\Base {
protected static $implementation = "PDO SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3PDO;
protected function nextID(string $table): int {
return (int) static::$drv->query("SELECT (case when max(id) then max(id) else 0 end)+1 from $table")->getValue();

View file

@ -11,7 +11,8 @@ namespace JKingWeb\Arsse\TestCase\Db\SQLite3PDO;
* @covers \JKingWeb\Arsse\Db\PDODriver
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestDriver extends \JKingWeb\Arsse\TestCase\Db\BaseDriver {
protected static $implementation = "PDO SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3PDO;
protected $create = "CREATE TABLE arsse_test(id integer primary key)";
protected $lock = "BEGIN EXCLUSIVE TRANSACTION";
protected $setVersion = "PRAGMA user_version=#";

View file

@ -12,7 +12,8 @@ use JKingWeb\Arsse\Test\DatabaseInformation;
* @covers \JKingWeb\Arsse\Db\PDOResult<extended>
*/
class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult {
protected static $implementation = "PDO SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3PDO;
protected static $createMeta = "CREATE TABLE arsse_meta(key text primary key not null, value text) without rowid";
protected static $createTest = "CREATE TABLE arsse_test(id integer primary key)";

View file

@ -10,7 +10,7 @@ namespace JKingWeb\Arsse\TestCase\Db\SQLite3PDO;
* @covers \JKingWeb\Arsse\Db\PDOStatement<extended>
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestStatement extends \JKingWeb\Arsse\TestCase\Db\BaseStatement {
protected static $implementation = "PDO SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3PDO;
protected function makeStatement(string $q, array $types = []): array {
return [static::$interface, $q, $types];

View file

@ -10,7 +10,8 @@ namespace JKingWeb\Arsse\TestCase\Db\SQLite3PDO;
* @covers \JKingWeb\Arsse\Db\SQLite3\PDODriver<extended>
* @covers \JKingWeb\Arsse\Db\PDOError */
class TestUpdate extends \JKingWeb\Arsse\TestCase\Db\BaseUpdate {
protected static $implementation = "PDO SQLite 3";
use \JKingWeb\Arsse\TestCase\DatabaseDrivers\SQLite3PDO;
protected static $minimal1 = "create table arsse_meta(key text primary key not null, value text); pragma user_version=1";
protected static $minimal2 = "pragma user_version=2";
}

View file

@ -1,320 +0,0 @@
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\Test;
use JKingWeb\Arsse\Arsse;
use JKingWeb\Arsse\Db\Driver;
class DatabaseInformation {
public $name;
public $backend;
public $pdo;
public $resultClass;
public $statementClass;
public $driverClass;
public $stringOutput;
public $interfaceConstructor;
public $truncateFunction;
public $razeFunction;
protected static $data;
public function __construct(string $name) {
if (!isset(self::$data)) {
self::$data = self::getData();
}
if (!array_key_exists($name, self::$data)) {
throw new \Exception("Invalid database driver name");
}
$this->name = $name;
foreach (self::$data[$name] as $key => $value) {
$this->$key = $value;
}
}
public static function list(): array {
if (!isset(self::$data)) {
self::$data = self::getData();
}
return array_keys(self::$data);
}
public static function listPDO(): array {
if (!isset(self::$data)) {
self::$data = self::getData();
}
return array_values(array_filter(array_keys(self::$data), function($k) {
return self::$data[$k]['pdo'];
}));
}
protected static function getData() {
$sqlite3TableList = function($db): array {
$listTables = "SELECT name from sqlite_master where type = 'table' and name like 'arsse_%'";
if ($db instanceof Driver) {
$tables = $db->query($listTables)->getAll();
$tables = sizeof($tables) ? array_column($tables, "name") : [];
} elseif ($db instanceof \PDO) {
retry:
try {
$tables = $db->query($listTables)->fetchAll(\PDO::FETCH_ASSOC);
} catch (\PDOException $e) {
goto retry;
}
$tables = sizeof($tables) ? array_column($tables, "name") : [];
} else {
$tables = [];
$result = $db->query($listTables);
while ($r = $result->fetchArray(\SQLITE3_ASSOC)) {
$tables[] = $r['name'];
}
$result->finalize();
}
return $tables;
};
$sqlite3TruncateFunction = function($db, array $afterStatements = []) use ($sqlite3TableList) {
// rollback any pending transaction
try {
$db->exec("ROLLBACK");
} catch (\Throwable $e) {
}
foreach ($sqlite3TableList($db) as $table) {
if ($table === "arsse_meta") {
$db->exec("DELETE FROM $table where key <> 'schema_version'");
} else {
$db->exec("DELETE FROM $table");
}
}
foreach ($afterStatements as $st) {
$db->exec($st);
}
};
$sqlite3RazeFunction = function($db, array $afterStatements = []) use ($sqlite3TableList) {
// rollback any pending transaction
try {
$db->exec("ROLLBACK");
} catch (\Throwable $e) {
}
$db->exec("PRAGMA foreign_keys=0");
foreach ($sqlite3TableList($db) as $table) {
$db->exec("DROP TABLE IF EXISTS $table");
}
$db->exec("PRAGMA user_version=0");
$db->exec("PRAGMA foreign_keys=1");
foreach ($afterStatements as $st) {
$db->exec($st);
}
};
$pgObjectList = function($db): array {
$listObjects = "SELECT table_name as name, 'TABLE' as type from information_schema.tables where table_schema = current_schema() and table_name like 'arsse_%' union SELECT collation_name as name, 'COLLATION' as type from information_schema.collations where collation_schema = current_schema()";
if ($db instanceof Driver) {
return $db->query($listObjects)->getAll();
} elseif ($db instanceof \PDO) {
return $db->query($listObjects)->fetchAll(\PDO::FETCH_ASSOC);
} else {
$r = @pg_query($db, $listObjects);
$out = $r ? pg_fetch_all($r) : false;
return $out ? $out : [];
}
};
$pgExecFunction = function($db, $q) {
if ($db instanceof Driver) {
$db->exec($q);
} elseif ($db instanceof \PDO) {
$db->exec($q);
} else {
pg_query($db, $q);
}
};
$pgTruncateFunction = function($db, array $afterStatements = []) use ($pgObjectList, $pgExecFunction) {
// rollback any pending transaction
try {
@$pgExecFunction($db, "ROLLBACK");
} catch (\Throwable $e) {
}
foreach ($pgObjectList($db) as $obj) {
if ($obj['type'] !== "TABLE") {
continue;
} elseif ($obj['name'] === "arsse_meta") {
$pgExecFunction($db, "DELETE FROM {$obj['name']} where key <> 'schema_version'");
} else {
$pgExecFunction($db, "TRUNCATE TABLE {$obj['name']} restart identity cascade");
}
}
foreach ($afterStatements as $st) {
$pgExecFunction($db, $st);
}
};
$pgRazeFunction = function($db, array $afterStatements = []) use ($pgObjectList, $pgExecFunction) {
// rollback any pending transaction
try {
$pgExecFunction($db, "ROLLBACK");
} catch (\Throwable $e) {
}
foreach ($pgObjectList($db) as $obj) {
$pgExecFunction($db, "DROP {$obj['type']} IF EXISTS {$obj['name']} cascade");
}
foreach ($afterStatements as $st) {
$pgExecFunction($db, $st);
}
};
$mysqlTableList = function($db): array {
$listTables = "SELECT table_name as name from information_schema.tables where table_schema = database() and table_name like 'arsse_%'";
if ($db instanceof Driver) {
$tables = $db->query($listTables)->getAll();
} elseif ($db instanceof \PDO) {
$tables = $db->query($listTables)->fetchAll(\PDO::FETCH_ASSOC);
} else {
$tables = $db->query($listTables)->fetch_all(\MYSQLI_ASSOC);
}
$tables = sizeof($tables) ? array_column($tables, "name") : [];
return $tables;
};
$mysqlTruncateFunction = function($db, array $afterStatements = []) use ($mysqlTableList) {
// rollback any pending transaction
try {
$db->query("UNLOCK TABLES; ROLLBACK");
} catch (\Throwable $e) {
}
foreach ($mysqlTableList($db) as $table) {
if ($table === "arsse_meta") {
$db->query("DELETE FROM $table where `key` <> 'schema_version'");
} else {
$db->query("DELETE FROM $table");
}
$db->query("ALTER TABLE $table auto_increment = 1");
}
foreach ($afterStatements as $st) {
$db->query($st);
}
};
$mysqlRazeFunction = function($db, array $afterStatements = []) use ($mysqlTableList) {
// rollback any pending transaction
try {
$db->query("UNLOCK TABLES; ROLLBACK");
} catch (\Throwable $e) {
}
foreach ($mysqlTableList($db) as $table) {
$db->query("DROP TABLE IF EXISTS $table");
}
foreach ($afterStatements as $st) {
$db->query($st);
}
};
return [
'SQLite 3' => [
'pdo' => false,
'backend' => "SQLite 3",
'statementClass' => \JKingWeb\Arsse\Db\SQLite3\Statement::class,
'resultClass' => \JKingWeb\Arsse\Db\SQLite3\Result::class,
'driverClass' => \JKingWeb\Arsse\Db\SQLite3\Driver::class,
'stringOutput' => false,
'interfaceConstructor' => function() {
try {
$d = new \SQLite3(Arsse::$conf->dbSQLite3File);
} catch (\Throwable $e) {
return;
}
$d->enableExceptions(true);
return $d;
},
'truncateFunction' => $sqlite3TruncateFunction,
'razeFunction' => $sqlite3RazeFunction,
],
'PDO SQLite 3' => [
'pdo' => true,
'backend' => "SQLite 3",
'statementClass' => \JKingWeb\Arsse\Db\SQLite3\PDOStatement::class,
'resultClass' => \JKingWeb\Arsse\Db\PDOResult::class,
'driverClass' => \JKingWeb\Arsse\Db\SQLite3\PDODriver::class,
'stringOutput' => true,
'interfaceConstructor' => function() {
try {
$d = new \PDO("sqlite:".Arsse::$conf->dbSQLite3File, "", "", [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
$d->exec("PRAGMA busy_timeout=0");
return $d;
} catch (\Throwable $e) {
return;
}
},
'truncateFunction' => $sqlite3TruncateFunction,
'razeFunction' => $sqlite3RazeFunction,
],
'PostgreSQL' => [
'pdo' => false,
'backend' => "PostgreSQL",
'statementClass' => \JKingWeb\Arsse\Db\PostgreSQL\Statement::class,
'resultClass' => \JKingWeb\Arsse\Db\PostgreSQL\Result::class,
'driverClass' => \JKingWeb\Arsse\Db\PostgreSQL\Driver::class,
'stringOutput' => true,
'interfaceConstructor' => function() {
$connString = \JKingWeb\Arsse\Db\PostgreSQL\Driver::makeConnectionString(false, Arsse::$conf->dbPostgreSQLUser, Arsse::$conf->dbPostgreSQLPass, Arsse::$conf->dbPostgreSQLDb, Arsse::$conf->dbPostgreSQLHost, Arsse::$conf->dbPostgreSQLPort, "");
if ($d = @pg_connect($connString, \PGSQL_CONNECT_FORCE_NEW)) {
foreach (\JKingWeb\Arsse\Db\PostgreSQL\Driver::makeSetupQueries(Arsse::$conf->dbPostgreSQLSchema) as $q) {
pg_query($d, $q);
}
return $d;
} else {
return;
}
},
'truncateFunction' => $pgTruncateFunction,
'razeFunction' => $pgRazeFunction,
],
'PDO PostgreSQL' => [
'pdo' => true,
'backend' => "PostgreSQL",
'statementClass' => \JKingWeb\Arsse\Db\PostgreSQL\PDOStatement::class,
'resultClass' => \JKingWeb\Arsse\Db\PDOResult::class,
'driverClass' => \JKingWeb\Arsse\Db\PostgreSQL\PDODriver::class,
'stringOutput' => false,
'interfaceConstructor' => function() {
$connString = \JKingWeb\Arsse\Db\PostgreSQL\Driver::makeConnectionString(true, Arsse::$conf->dbPostgreSQLUser, Arsse::$conf->dbPostgreSQLPass, Arsse::$conf->dbPostgreSQLDb, Arsse::$conf->dbPostgreSQLHost, Arsse::$conf->dbPostgreSQLPort, "");
try {
$d = new \PDO("pgsql:".$connString, Arsse::$conf->dbPostgreSQLUser, Arsse::$conf->dbPostgreSQLPass, [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
} catch (\Throwable $e) {
return;
}
foreach (\JKingWeb\Arsse\Db\PostgreSQL\PDODriver::makeSetupQueries(Arsse::$conf->dbPostgreSQLSchema) as $q) {
$d->exec($q);
}
return $d;
},
'truncateFunction' => $pgTruncateFunction,
'razeFunction' => $pgRazeFunction,
],
'PDO MySQL' => [
'pdo' => true,
'backend' => "MySQL",
'statementClass' => \JKingWeb\Arsse\Db\MySQL\PDOStatement::class,
'resultClass' => \JKingWeb\Arsse\Db\PDOResult::class,
'driverClass' => \JKingWeb\Arsse\Db\MySQL\PDODriver::class,
'stringOutput' => true,
'interfaceConstructor' => function() {
try {
$dsn = [];
$params = [
'charset' => "utf8mb4",
'host' => Arsse::$conf->dbMySQLHost,
'port' => Arsse::$conf->dbMySQLPort,
'dbname' => Arsse::$conf->dbMySQLDb,
];
foreach ($params as $k => $v) {
$dsn[] = "$k=$v";
}
$dsn = "mysql:".implode(";", $dsn);
return new \PDO($dsn, Arsse::$conf->dbMySQLUser, Arsse::$conf->dbMySQLPass, [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::MYSQL_ATTR_MULTI_STATEMENTS => false, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET sql_mode = '".\JKingWeb\Arsse\Db\MySQL\PDODriver::SQL_MODE."'",]);
} catch (\Throwable $e) {
return;
}
},
'truncateFunction' => $mysqlTruncateFunction,
'razeFunction' => $mysqlRazeFunction,
],
];
}
}