diff --git a/core/database.php b/core/database.php index c6ae69ab..a68acdd3 100644 --- a/core/database.php +++ b/core/database.php @@ -178,6 +178,11 @@ class Database $this->dbtime += $dur; } + public function set_timeout(int $time): void + { + $this->engine->set_timeout($this->db, $time); + } + public function execute(string $query, array $args=[], bool $scoreql = false): PDOStatement { try { diff --git a/core/dbengine.php b/core/dbengine.php index 03b4b851..e72c734b 100644 --- a/core/dbengine.php +++ b/core/dbengine.php @@ -12,7 +12,7 @@ abstract class SCORE const ILIKE = "SCORE_ILIKE"; } -class DBEngine +abstract class DBEngine { /** @var null|string */ public $name = null; @@ -33,6 +33,8 @@ class DBEngine { return 'CREATE TABLE '.$name.' ('.$data.')'; } + + public abstract function set_timeout(PDO $db, int $time); } class MySQL extends DBEngine @@ -68,6 +70,13 @@ class MySQL extends DBEngine $ctes = "ENGINE=InnoDB DEFAULT CHARSET='utf8'"; return 'CREATE TABLE '.$name.' ('.$data.') '.$ctes; } + + public function set_timeout(PDO $db, int $time): void + { + // These only apply to read-only queries, which appears to be the best we can to mysql-wise + $db->exec("SET SESSION MAX_EXECUTION_TIME=".$time.";"); + } + } class PostgreSQL extends DBEngine @@ -87,7 +96,7 @@ class PostgreSQL extends DBEngine } else { $db->exec("SET application_name TO 'shimmie [local]';"); } - $db->exec("SET statement_timeout TO ".DATABASE_TIMEOUT.";"); + $this->set_timeout($db, DATABASE_TIMEOUT); } public function scoreql_to_sql(string $data): string @@ -109,6 +118,12 @@ class PostgreSQL extends DBEngine $data = $this->scoreql_to_sql($data); return "CREATE TABLE $name ($data)"; } + + public function set_timeout(PDO $db, int $time): void + { + $db->exec("SET statement_timeout TO ".$time.";"); + } + } // shimmie functions for export to sqlite @@ -213,4 +228,9 @@ class SQLite extends DBEngine $cols_redone = implode(", ", $cols); return "CREATE TABLE $name ($cols_redone); $extras"; } + + public function set_timeout(PDO $db, int $time): void + { + // There doesn't seem to be such a thing for SQLite, so it does nothing + } } diff --git a/ext/media/main.php b/ext/media/main.php index 1a9d674b..9c1eb5c0 100644 --- a/ext/media/main.php +++ b/ext/media/main.php @@ -1049,9 +1049,7 @@ class Media extends Extension break; } - if ($database->get_driver_name()==DatabaseDriver::PGSQL) { // These updates can take a little bit - $database->execute("SET statement_timeout TO 300000;"); - } + $database->set_timeout(300000); // These updates can take a little bit if ($database->transaction === true) { $database->commit(); // Each of these commands could hit a lot of data, combining them into one big transaction would not be a good idea. diff --git a/ext/rating/main.php b/ext/rating/main.php index aa18cabd..66778d91 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -578,9 +578,7 @@ class Ratings extends Extension break; } - if ($database->get_driver_name()==DatabaseDriver::PGSQL) { // These updates can take a little bit - $database->execute("SET statement_timeout TO 300000;"); - } + $database->set_timeout(300000); // These updates can take a little bit $database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>'?', "old"=>'u' ]); diff --git a/ext/rule34/main.php b/ext/rule34/main.php index 1aa80d7d..f7e5a343 100644 --- a/ext/rule34/main.php +++ b/ext/rule34/main.php @@ -71,9 +71,7 @@ class Rule34 extends Extension { global $database, $page, $user; - if ($user->can(Permissions::DELETE_USER)) { // deleting users can take a while - $database->execute("SET statement_timeout TO ".(DATABASE_TIMEOUT+15000).";"); - } + $database->set_timeout(DATABASE_TIMEOUT+15000); // deleting users can take a while if (function_exists("sd_notify_watchdog")) { sd_notify_watchdog(); diff --git a/ext/upgrade/main.php b/ext/upgrade/main.php index 491cb183..0424f269 100644 --- a/ext/upgrade/main.php +++ b/ext/upgrade/main.php @@ -191,9 +191,7 @@ class Upgrade extends Extension break; } - if ($database->get_driver_name()==DatabaseDriver::PGSQL) { // These updates can take a little bit - $database->execute("SET statement_timeout TO 300000;"); - } + $database->set_timeout(300000); // These updates can take a little bit log_info("upgrade", "Setting index for ext column"); $database->execute('CREATE INDEX images_ext_idx ON images(ext)');