2019-01-12 17:43:06 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2021-04-14 15:17:01 +00:00
|
|
|
|
2019-08-25 17:19:11 +00:00
|
|
|
namespace JKingWeb\Arsse\Test\DatabaseDrivers;
|
2019-01-12 17:43:06 +00:00
|
|
|
|
|
|
|
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;
|
2019-01-14 04:17:19 +00:00
|
|
|
protected static $stringOutput = true;
|
2020-03-01 20:16:50 +00:00
|
|
|
|
2019-01-12 17:43:06 +00:00
|
|
|
public static function dbInterface() {
|
2020-10-30 16:16:03 +00:00
|
|
|
if (!class_exists("mysqli")) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-03-02 16:04:21 +00:00
|
|
|
$d = mysqli_init();
|
|
|
|
$d->options(\MYSQLI_OPT_INT_AND_FLOAT_NATIVE, false);
|
|
|
|
$d->options(\MYSQLI_SET_CHARSET_NAME, "utf8mb4");
|
|
|
|
@$d->real_connect(Arsse::$conf->dbMySQLHost, Arsse::$conf->dbMySQLUser, Arsse::$conf->dbMySQLPass, Arsse::$conf->dbMySQLDb, Arsse::$conf->dbMySQLPort);
|
2019-01-14 04:17:19 +00:00
|
|
|
if ($d->connect_errno) {
|
2020-10-30 16:16:03 +00:00
|
|
|
return null;
|
2019-01-14 04:17:19 +00:00
|
|
|
}
|
|
|
|
$d->set_charset("utf8mb4");
|
|
|
|
foreach (\JKingWeb\Arsse\Db\MySQL\PDODriver::makeSetupQueries() as $q) {
|
|
|
|
$d->query($q);
|
|
|
|
}
|
|
|
|
return $d;
|
2019-01-12 17:43:06 +00:00
|
|
|
}
|
2020-03-01 20:16:50 +00:00
|
|
|
|
2019-01-12 17:43:06 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
public static function dbTruncate($db, array $afterStatements = []): void {
|
2019-01-12 17:43:06 +00:00
|
|
|
// rollback any pending transaction
|
|
|
|
try {
|
|
|
|
$db->query("UNLOCK TABLES; ROLLBACK");
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
}
|
2019-12-01 20:00:28 +00:00
|
|
|
$db->query("SET FOREIGN_KEY_CHECKS=0");
|
2019-01-12 17:43:06 +00:00
|
|
|
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");
|
|
|
|
}
|
2019-12-01 20:00:28 +00:00
|
|
|
$db->query("SET FOREIGN_KEY_CHECKS=1");
|
2019-01-12 17:43:06 +00:00
|
|
|
foreach ($afterStatements as $st) {
|
|
|
|
$db->query($st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:03 +00:00
|
|
|
public static function dbRaze($db, array $afterStatements = []): void {
|
2019-01-12 17:43:06 +00:00
|
|
|
// rollback any pending transaction
|
|
|
|
try {
|
|
|
|
$db->query("UNLOCK TABLES; ROLLBACK");
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
}
|
2019-12-01 20:00:28 +00:00
|
|
|
$db->query("SET FOREIGN_KEY_CHECKS=0");
|
2019-01-12 17:43:06 +00:00
|
|
|
foreach (self::dbTableList($db) as $table) {
|
|
|
|
$db->query("DROP TABLE IF EXISTS $table");
|
|
|
|
}
|
2019-12-01 20:00:28 +00:00
|
|
|
$db->query("SET FOREIGN_KEY_CHECKS=1");
|
2019-01-12 17:43:06 +00:00
|
|
|
foreach ($afterStatements as $st) {
|
|
|
|
$db->query($st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|