Database driver constants

This commit is contained in:
Matthew Barbour
2019-06-14 13:17:03 -05:00
committed by matthew
parent 444de26ce3
commit 6f501a6e74
19 changed files with 53 additions and 49 deletions

View File

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