Added set_timeout to database and engine

This commit is contained in:
Matthew Barbour 2019-10-14 13:31:12 -05:00 committed by Shish
parent 702f098ea6
commit 3efa76c6a2
6 changed files with 31 additions and 14 deletions

View file

@ -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 {

View file

@ -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
}
}

View file

@ -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.

View file

@ -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' ]);

View file

@ -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();

View file

@ -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)');