database notification API

This commit is contained in:
Shish 2020-10-03 12:50:37 +00:00
parent 2ac695c135
commit e696357c06
2 changed files with 24 additions and 0 deletions

View file

@ -169,6 +169,11 @@ class Database
$this->engine->set_timeout($this->db, $time); $this->engine->set_timeout($this->db, $time);
} }
public function notify(string $channel, ?string $data=null): void
{
$this->engine->notify($this->db, $channel, $data);
}
public function execute(string $query, array $args = []): PDOStatement public function execute(string $query, array $args = []): PDOStatement
{ {
try { try {

View file

@ -33,6 +33,8 @@ abstract class DBEngine
abstract public function set_timeout(PDO $db, int $time); abstract public function set_timeout(PDO $db, int $time);
abstract public function get_version(PDO $db): string; abstract public function get_version(PDO $db): string;
abstract public function notify(PDO $db, string $channel, ?string $data=null): void;
} }
class MySQL extends DBEngine class MySQL extends DBEngine
@ -71,6 +73,10 @@ class MySQL extends DBEngine
// $db->exec("SET SESSION MAX_EXECUTION_TIME=".$time.";"); // $db->exec("SET SESSION MAX_EXECUTION_TIME=".$time.";");
} }
public function notify(PDO $db, string $channel, ?string $data=null): void
{
}
public function get_version(PDO $db): string public function get_version(PDO $db): string
{ {
return $db->query('select version()')->fetch()[0]; return $db->query('select version()')->fetch()[0];
@ -118,6 +124,15 @@ class PostgreSQL extends DBEngine
$db->exec("SET statement_timeout TO ".$time.";"); $db->exec("SET statement_timeout TO ".$time.";");
} }
public function notify(PDO $db, string $channel, ?string $data=null): void
{
if ($data) {
$db->exec("NOTIFY $channel, '$data';");
} else {
$db->exec("NOTIFY $channel;");
}
}
public function get_version(PDO $db): string public function get_version(PDO $db): string
{ {
return $db->query('select version()')->fetch()[0]; return $db->query('select version()')->fetch()[0];
@ -229,6 +244,10 @@ class SQLite extends DBEngine
// There doesn't seem to be such a thing for SQLite, so it does nothing // There doesn't seem to be such a thing for SQLite, so it does nothing
} }
public function notify(PDO $db, string $channel, ?string $data=null): void
{
}
public function get_version(PDO $db): string public function get_version(PDO $db): string
{ {
return $db->query('select sqlite_version()')->fetch()[0]; return $db->query('select sqlite_version()')->fetch()[0];