Database driver constants
This commit is contained in:
parent
444de26ce3
commit
6f501a6e74
19 changed files with 53 additions and 49 deletions
|
@ -110,7 +110,7 @@ function do_install()
|
|||
{ // {{{
|
||||
if (file_exists("data/config/auto_install.conf.php")) {
|
||||
require_once "data/config/auto_install.conf.php";
|
||||
} elseif (@$_POST["database_type"] == "sqlite") {
|
||||
} elseif (@$_POST["database_type"] == Database::SQLITE_DRIVER) {
|
||||
$id = bin2hex(random_bytes(5));
|
||||
define('DATABASE_DSN', "sqlite:data/shimmie.{$id}.sqlite");
|
||||
} elseif (isset($_POST['database_type']) && isset($_POST['database_host']) && isset($_POST['database_user']) && isset($_POST['database_name'])) {
|
||||
|
@ -153,9 +153,9 @@ function ask_questions()
|
|||
|
||||
$drivers = PDO::getAvailableDrivers();
|
||||
if (
|
||||
!in_array("mysql", $drivers) &&
|
||||
!in_array("pgsql", $drivers) &&
|
||||
!in_array("sqlite", $drivers)
|
||||
!in_array(Database::MYSQL_DRIVER, $drivers) &&
|
||||
!in_array(Database::PGSQL_DRIVER, $drivers) &&
|
||||
!in_array(Database::SQLITE_DRIVER, $drivers)
|
||||
) {
|
||||
$errors[] = "
|
||||
No database connection library could be found; shimmie needs
|
||||
|
@ -163,9 +163,9 @@ function ask_questions()
|
|||
";
|
||||
}
|
||||
|
||||
$db_m = in_array("mysql", $drivers) ? '<option value="mysql">MySQL</option>' : "";
|
||||
$db_p = in_array("pgsql", $drivers) ? '<option value="pgsql">PostgreSQL</option>' : "";
|
||||
$db_s = in_array("sqlite", $drivers) ? '<option value="sqlite">SQLite</option>' : "";
|
||||
$db_m = in_array(Database::MYSQL_DRIVER, $drivers) ? '<option value="'.Database::MYSQL_DRIVER.'">MySQL</option>' : "";
|
||||
$db_p = in_array(Database::PGSQL_DRIVER, $drivers) ? '<option value="'.Database::PGSQL_DRIVER.'">PostgreSQL</option>' : "";
|
||||
$db_s = in_array(Database::SQLITE_DRIVER, $drivers) ? '<option value="'.Database::SQLITE_DRIVER.'">SQLite</option>' : "";
|
||||
|
||||
$warn_msg = $warnings ? "<h3>Warnings</h3>".implode("\n<p>", $warnings) : "";
|
||||
$err_msg = $errors ? "<h3>Errors</h3>".implode("\n<p>", $errors) : "";
|
||||
|
|
|
@ -4,6 +4,10 @@
|
|||
*/
|
||||
class Database
|
||||
{
|
||||
const MYSQL_DRIVER = "mysql";
|
||||
const PGSQL_DRIVER = "pgsql";
|
||||
const SQLITE_DRIVER = "sqlite";
|
||||
|
||||
/**
|
||||
* The PDO database connection object, for anyone who wants direct access.
|
||||
* @var null|PDO
|
||||
|
@ -72,7 +76,7 @@ class Database
|
|||
|
||||
// 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") {
|
||||
if (version_compare(PHP_VERSION, "6.9.9") == 1 && $this->get_driver_name() == self::SQLITE_DRIVER) {
|
||||
$ka = false;
|
||||
}
|
||||
|
||||
|
@ -96,11 +100,11 @@ class Database
|
|||
throw new SCoreException("Can't figure out database engine");
|
||||
}
|
||||
|
||||
if ($db_proto === "mysql") {
|
||||
if ($db_proto === self::MYSQL_DRIVER) {
|
||||
$this->engine = new MySQL();
|
||||
} elseif ($db_proto === "pgsql") {
|
||||
} elseif ($db_proto === self::PGSQL_DRIVER) {
|
||||
$this->engine = new PostgreSQL();
|
||||
} elseif ($db_proto === "sqlite") {
|
||||
} elseif ($db_proto === self::SQLITE_DRIVER) {
|
||||
$this->engine = new SQLite();
|
||||
} else {
|
||||
die('Unknown PDO driver: '.$db_proto);
|
||||
|
@ -224,7 +228,7 @@ class Database
|
|||
}
|
||||
return $stmt;
|
||||
} catch (PDOException $pdoe) {
|
||||
throw new SCoreException($pdoe->getMessage()."<p><b>Query:</b> ".$query);
|
||||
throw new SCoreException($pdoe->getMessage()."<p><b>Query:</b> ".$query, $pdoe->getCode(), $pdoe);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -296,7 +300,7 @@ class Database
|
|||
*/
|
||||
public function get_last_insert_id(string $seq): int
|
||||
{
|
||||
if ($this->engine->name == "pgsql") {
|
||||
if ($this->engine->name == self::PGSQL_DRIVER) {
|
||||
return $this->db->lastInsertId($seq);
|
||||
} else {
|
||||
return $this->db->lastInsertId();
|
||||
|
@ -326,15 +330,15 @@ class Database
|
|||
$this->connect_db();
|
||||
}
|
||||
|
||||
if ($this->engine->name === "mysql") {
|
||||
if ($this->engine->name === self::MYSQL_DRIVER) {
|
||||
return count(
|
||||
$this->get_all("SHOW TABLES")
|
||||
);
|
||||
} elseif ($this->engine->name === "pgsql") {
|
||||
} elseif ($this->engine->name === self::PGSQL_DRIVER) {
|
||||
return count(
|
||||
$this->get_all("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'")
|
||||
);
|
||||
} elseif ($this->engine->name === "sqlite") {
|
||||
} elseif ($this->engine->name === self::SQLITE_DRIVER) {
|
||||
return count(
|
||||
$this->get_all("SELECT name FROM sqlite_master WHERE type = 'table'")
|
||||
);
|
||||
|
|
|
@ -22,7 +22,7 @@ class DBEngine
|
|||
class MySQL extends DBEngine
|
||||
{
|
||||
/** @var string */
|
||||
public $name = "mysql";
|
||||
public $name = Database::MYSQL_DRIVER;
|
||||
|
||||
public function init(PDO $db)
|
||||
{
|
||||
|
@ -54,7 +54,7 @@ class MySQL extends DBEngine
|
|||
class PostgreSQL extends DBEngine
|
||||
{
|
||||
/** @var string */
|
||||
public $name = "pgsql";
|
||||
public $name = Database::PGSQL_DRIVER;
|
||||
|
||||
public function init(PDO $db)
|
||||
{
|
||||
|
@ -136,7 +136,7 @@ function _ln($n)
|
|||
class SQLite extends DBEngine
|
||||
{
|
||||
/** @var string */
|
||||
public $name = "sqlite";
|
||||
public $name = Database::SQLITE_DRIVER;
|
||||
|
||||
public function init(PDO $db)
|
||||
{
|
||||
|
|
|
@ -590,7 +590,7 @@ class Image
|
|||
public function delete_tags_from_image(): void
|
||||
{
|
||||
global $database;
|
||||
if ($database->get_driver_name() == "mysql") {
|
||||
if ($database->get_driver_name() == Database::MYSQL_DRIVER) {
|
||||
//mysql < 5.6 has terrible subquery optimization, using EXISTS / JOIN fixes this
|
||||
$database->execute(
|
||||
"
|
||||
|
@ -907,7 +907,7 @@ class Image
|
|||
|
||||
// more than one positive tag, or more than zero negative tags
|
||||
else {
|
||||
if ($database->get_driver_name() === "mysql") {
|
||||
if ($database->get_driver_name() === Database::MYSQL_DRIVER) {
|
||||
$query = Image::build_ugly_search_querylet($tag_querylets);
|
||||
} else {
|
||||
$query = Image::build_accurate_search_querylet($tag_querylets);
|
||||
|
|
|
@ -69,7 +69,7 @@ class User
|
|||
global $config, $database;
|
||||
$row = $database->cache->get("user-session:$name-$session");
|
||||
if (!$row) {
|
||||
if ($database->get_driver_name() === "mysql") {
|
||||
if ($database->get_driver_name() === Database::MYSQL_DRIVER) {
|
||||
$query = "SELECT * FROM users WHERE name = :name AND md5(concat(pass, :ip)) = :sess";
|
||||
} else {
|
||||
$query = "SELECT * FROM users WHERE name = :name AND md5(pass || :ip) = :sess";
|
||||
|
|
|
@ -201,14 +201,14 @@ class AdminPage extends Extension
|
|||
$database = $matches['dbname'];
|
||||
|
||||
switch ($software) {
|
||||
case 'mysql':
|
||||
case Database::MYSQL_DRIVER:
|
||||
$cmd = "mysqldump -h$hostname -u$username -p$password $database";
|
||||
break;
|
||||
case 'pgsql':
|
||||
case Database::PGSQL_DRIVER:
|
||||
putenv("PGPASSWORD=$password");
|
||||
$cmd = "pg_dump -h $hostname -U $username $database";
|
||||
break;
|
||||
case 'sqlite':
|
||||
case Database::SQLITE_DRIVER:
|
||||
$cmd = "sqlite3 $database .dump";
|
||||
break;
|
||||
default:
|
||||
|
@ -257,7 +257,7 @@ class AdminPage extends Extension
|
|||
//TODO: Update score_log (Having an optional ID column for score_log would be nice..)
|
||||
preg_match("#^(?P<proto>\w+)\:(?:user=(?P<user>\w+)(?:;|$)|password=(?P<password>\w*)(?:;|$)|host=(?P<host>[\w\.\-]+)(?:;|$)|dbname=(?P<dbname>[\w_]+)(?:;|$))+#", DATABASE_DSN, $matches);
|
||||
|
||||
if ($matches['proto'] == "mysql") {
|
||||
if ($matches['proto'] == Database::MYSQL_DRIVER) {
|
||||
$tables = $database->get_col("SELECT TABLE_NAME
|
||||
FROM information_schema.KEY_COLUMN_USAGE
|
||||
WHERE TABLE_SCHEMA = :db
|
||||
|
@ -280,9 +280,9 @@ class AdminPage extends Extension
|
|||
$i++;
|
||||
}
|
||||
$database->execute("ALTER TABLE images AUTO_INCREMENT=".(count($ids) + 1));
|
||||
} elseif ($matches['proto'] == "pgsql") {
|
||||
} elseif ($matches['proto'] == Database::PGSQL_DRIVER) {
|
||||
//TODO: Make this work with PostgreSQL
|
||||
} elseif ($matches['proto'] == "sqlite") {
|
||||
} elseif ($matches['proto'] == Database::SQLITE_DRIVER) {
|
||||
//TODO: Make this work with SQLite
|
||||
}
|
||||
return true;
|
||||
|
|
|
@ -45,7 +45,7 @@ class AdminPageTheme extends Themelet
|
|||
$html .= $this->button("Download all images", "download_all_images", false);
|
||||
}
|
||||
$html .= $this->button("Download database contents", "database_dump", false);
|
||||
if ($database->get_driver_name() == "mysql") {
|
||||
if ($database->get_driver_name() == Database::MYSQL_DRIVER) {
|
||||
$html .= $this->button("Reset image IDs", "reset_image_ids", true);
|
||||
}
|
||||
$page->add_block(new Block("Misc Admin Tools", $html));
|
||||
|
|
|
@ -480,14 +480,14 @@ class CommentList extends Extension
|
|||
global $config, $database;
|
||||
|
||||
// sqlite fails at intervals
|
||||
if ($database->get_driver_name() === "sqlite") {
|
||||
if ($database->get_driver_name() === Database::SQLITE_DRIVER) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$window = int_escape($config->get_int('comment_window'));
|
||||
$max = int_escape($config->get_int('comment_limit'));
|
||||
|
||||
if ($database->get_driver_name() == "mysql") {
|
||||
if ($database->get_driver_name() == Database::MYSQL_DRIVER) {
|
||||
$window_sql = "interval $window minute";
|
||||
} else {
|
||||
$window_sql = "interval '$window minute'";
|
||||
|
|
|
@ -157,7 +157,7 @@ class IndexTest extends ShimmiePHPUnitTestCase
|
|||
|
||||
global $database;
|
||||
$db = $database->get_driver_name();
|
||||
if ($db == "pgsql" || $db == "sqlite") {
|
||||
if ($db == Database::PGSQL_DRIVER || $db == Database::SQLITE_DRIVER) {
|
||||
$this->markTestIncomplete();
|
||||
}
|
||||
|
||||
|
|
|
@ -235,7 +235,7 @@ class IPBan extends Extension
|
|||
{
|
||||
global $config, $database;
|
||||
|
||||
$prefix = ($database->get_driver_name() == "sqlite" ? "bans." : "");
|
||||
$prefix = ($database->get_driver_name() == Database::SQLITE_DRIVER ? "bans." : "");
|
||||
|
||||
$bans = $this->get_active_bans();
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ class IPBanTheme extends Themelet
|
|||
{
|
||||
global $database, $user;
|
||||
$h_bans = "";
|
||||
$prefix = ($database->get_driver_name() == "sqlite" ? "bans." : "");
|
||||
$prefix = ($database->get_driver_name() == Database::SQLITE_DRIVER ? "bans." : "");
|
||||
foreach ($bans as $ban) {
|
||||
$end_human = date('Y-m-d', $ban[$prefix.'end_timestamp']);
|
||||
$h_bans .= "
|
||||
|
|
|
@ -68,7 +68,7 @@ class LogDatabase extends Extension
|
|||
$args["module"] = $_GET["module"];
|
||||
}
|
||||
if (!empty($_GET["user"])) {
|
||||
if ($database->get_driver_name() == "pgsql") {
|
||||
if ($database->get_driver_name() == Database::PGSQL_DRIVER) {
|
||||
if (preg_match("#\d+\.\d+\.\d+\.\d+(/\d+)?#", $_GET["user"])) {
|
||||
$wheres[] = "(username = :user1 OR text(address) = :user2)";
|
||||
$args["user1"] = $_GET["user"];
|
||||
|
|
|
@ -37,7 +37,7 @@ class RatingSetEvent extends Event
|
|||
|
||||
class Ratings extends Extension
|
||||
{
|
||||
protected $db_support = ['mysql','pgsql']; // ?
|
||||
protected $db_support = [Database::MYSQL_DRIVER,Database::PGSQL_DRIVER];
|
||||
|
||||
public function get_priority(): int
|
||||
{
|
||||
|
@ -331,10 +331,10 @@ class Ratings extends Extension
|
|||
if ($config->get_int("ext_ratings2_version") < 3) {
|
||||
$database->Execute("UPDATE images SET rating = 'u' WHERE rating is null");
|
||||
switch ($database->get_driver_name()) {
|
||||
case "mysql":
|
||||
case Database::MYSQL_DRIVER:
|
||||
$database->Execute("ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT 'u'");
|
||||
break;
|
||||
case "pgsql":
|
||||
case Database::PGSQL_DRIVER:
|
||||
$database->Execute("ALTER TABLE images ALTER COLUMN rating SET DEFAULT 'u'");
|
||||
$database->Execute("ALTER TABLE images ALTER COLUMN rating SET NOT NULL");
|
||||
break;
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
class Relationships extends Extension
|
||||
{
|
||||
protected $db_support = ['mysql', 'pgsql'];
|
||||
protected $db_support = [Database::MYSQL_DRIVER, Database::PGSQL_DRIVER];
|
||||
|
||||
public function onInitExt(InitExtEvent $event)
|
||||
{
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
class RSS_Comments extends Extension
|
||||
{
|
||||
protected $db_support = ['mysql', 'sqlite']; // pgsql has no UNIX_TIMESTAMP
|
||||
protected $db_support = [Database::MYSQL_DRIVER, Database::SQLITE_DRIVER]; // pgsql has no UNIX_TIMESTAMP
|
||||
|
||||
public function onPostListBuilding(PostListBuildingEvent $event)
|
||||
{
|
||||
|
|
|
@ -19,7 +19,7 @@ if ( // kill these glitched requests immediately
|
|||
|
||||
class Rule34 extends Extension
|
||||
{
|
||||
protected $db_support = ['pgsql']; # Only PG has the NOTIFY pubsub system
|
||||
protected $db_support = [Database::PGSQL_DRIVER]; # Only PG has the NOTIFY pubsub system
|
||||
|
||||
public function onImageDeletion(ImageDeletionEvent $event)
|
||||
{
|
||||
|
|
|
@ -19,7 +19,7 @@ class Rule34Theme extends Themelet
|
|||
{
|
||||
global $database, $user;
|
||||
$h_bans = "";
|
||||
$prefix = ($database->get_driver_name() == "sqlite" ? "bans." : "");
|
||||
$prefix = ($database->get_driver_name() == Database::SQLITE_DRIVER ? "bans." : "");
|
||||
foreach ($bans as $ban) {
|
||||
$h_bans .= "
|
||||
<tr>
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
class Tips extends Extension
|
||||
{
|
||||
protected $db_support = ['mysql', 'sqlite']; // rand() ?
|
||||
protected $db_support = [Database::MYSQL_DRIVER, Database::SQLITE_DRIVER]; // rand() ?
|
||||
|
||||
public function onInitExt(InitExtEvent $event)
|
||||
{
|
||||
|
|
|
@ -44,7 +44,7 @@ class Upgrade extends Extension
|
|||
$config->set_bool("in_upgrade", true);
|
||||
$config->set_int("db_version", 9);
|
||||
|
||||
if ($database->get_driver_name() == 'mysql') {
|
||||
if ($database->get_driver_name() == Database::MYSQL_DRIVER) {
|
||||
$tables = $database->get_col("SHOW TABLES");
|
||||
foreach ($tables as $table) {
|
||||
log_info("upgrade", "converting $table to innodb");
|
||||
|
@ -84,7 +84,7 @@ class Upgrade extends Extension
|
|||
$config->set_bool("in_upgrade", true);
|
||||
$config->set_int("db_version", 12);
|
||||
|
||||
if ($database->get_driver_name() == 'pgsql') {
|
||||
if ($database->get_driver_name() == Database::PGSQL_DRIVER) {
|
||||
log_info("upgrade", "Changing ext column to VARCHAR");
|
||||
$database->execute("ALTER TABLE images ALTER COLUMN ext SET DATA TYPE VARCHAR(4)");
|
||||
}
|
||||
|
@ -101,9 +101,9 @@ class Upgrade extends Extension
|
|||
$config->set_int("db_version", 13);
|
||||
|
||||
log_info("upgrade", "Changing password column to VARCHAR(250)");
|
||||
if ($database->get_driver_name() == 'pgsql') {
|
||||
if ($database->get_driver_name() == Database::PGSQL_DRIVER) {
|
||||
$database->execute("ALTER TABLE users ALTER COLUMN pass SET DATA TYPE VARCHAR(250)");
|
||||
} elseif ($database->get_driver_name() == 'mysql') {
|
||||
} elseif ($database->get_driver_name() == Database::MYSQL_DRIVER) {
|
||||
$database->execute("ALTER TABLE users CHANGE pass pass VARCHAR(250)");
|
||||
}
|
||||
|
||||
|
@ -116,11 +116,11 @@ class Upgrade extends Extension
|
|||
$config->set_int("db_version", 14);
|
||||
|
||||
log_info("upgrade", "Changing tag column to VARCHAR(255)");
|
||||
if ($database->get_driver_name() == 'pgsql') {
|
||||
if ($database->get_driver_name() == Database::PGSQL_DRIVER) {
|
||||
$database->execute('ALTER TABLE tags ALTER COLUMN tag SET DATA TYPE VARCHAR(255)');
|
||||
$database->execute('ALTER TABLE aliases ALTER COLUMN oldtag SET DATA TYPE VARCHAR(255)');
|
||||
$database->execute('ALTER TABLE aliases ALTER COLUMN newtag SET DATA TYPE VARCHAR(255)');
|
||||
} elseif ($database->get_driver_name() == 'mysql') {
|
||||
} elseif ($database->get_driver_name() == Database::MYSQL_DRIVER) {
|
||||
$database->execute('ALTER TABLE tags MODIFY COLUMN tag VARCHAR(255) NOT NULL');
|
||||
$database->execute('ALTER TABLE aliases MODIFY COLUMN oldtag VARCHAR(255) NOT NULL');
|
||||
$database->execute('ALTER TABLE aliases MODIFY COLUMN newtag VARCHAR(255) NOT NULL');
|
||||
|
|
Reference in a new issue