Skip to content

Commit

Permalink
connect to db before setting db timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Jun 30, 2023
1 parent 0f162fe commit 340b9da
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions core/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ public function __construct(string $dsn)
$this->dsn = $dsn;
}

private function connect_db(): void
private function get_db(): PDO
{
$this->db = new PDO($this->dsn);
$this->connect_engine();
$this->get_engine()->init($this->db);
$this->begin_transaction();
if(is_null($this->db)) {
$this->db = new PDO($this->dsn);
$this->connect_engine();
$this->get_engine()->init($this->db);
$this->begin_transaction();
}
return $this->db;
}

private function connect_engine(): void
Expand Down Expand Up @@ -76,7 +79,7 @@ private function connect_engine(): void
public function begin_transaction(): void
{
if ($this->is_transaction_open() === false) {
$this->db->beginTransaction();
$this->get_db()->beginTransaction();
}
}

Expand All @@ -88,7 +91,7 @@ public function is_transaction_open(): bool
public function commit(): bool
{
if ($this->is_transaction_open()) {
return $this->db->commit();
return $this->get_db()->commit();
} else {
throw new SCoreException("Unable to call commit() as there is no transaction currently open.");
}
Expand All @@ -97,7 +100,7 @@ public function commit(): bool
public function rollback(): bool
{
if ($this->is_transaction_open()) {
return $this->db->rollback();
return $this->get_db()->rollback();
} else {
throw new SCoreException("Unable to call rollback() as there is no transaction currently open.");
}
Expand All @@ -123,7 +126,7 @@ public function get_driver_id(): DatabaseDriverID

public function get_version(): string
{
return $this->get_engine()->get_version($this->db);
return $this->get_engine()->get_version($this->get_db());
}

private function count_time(string $method, float $start, string $query, ?array $args): void
Expand All @@ -144,21 +147,18 @@ private function count_time(string $method, float $start, string $query, ?array

public function set_timeout(?int $time): void
{
$this->get_engine()->set_timeout($this->db, $time);
$this->get_engine()->set_timeout($this->get_db(), $time);
}

public function notify(string $channel, ?string $data=null): void
{
$this->get_engine()->notify($this->db, $channel, $data);
$this->get_engine()->notify($this->get_db(), $channel, $data);
}

public function _execute(string $query, array $args = []): PDOStatement
{
try {
if (is_null($this->db)) {
$this->connect_db();
}
$ret = $this->db->execute(
$ret = $this->get_db()->execute(
"-- " . str_replace("%2F", "/", urlencode($_GET['q'] ?? '')). "\n" .
$query,
$args
Expand Down Expand Up @@ -297,9 +297,9 @@ public function exists(string $query, array $args = []): bool
public function get_last_insert_id(string $seq): int
{
if ($this->get_engine()->id == DatabaseDriverID::PGSQL) {
$id = $this->db->lastInsertId($seq);
$id = $this->get_db()->lastInsertId($seq);
} else {
$id = $this->db->lastInsertId();
$id = $this->get_db()->lastInsertId();
}
assert(is_numeric($id));
return (int)$id;
Expand All @@ -324,10 +324,6 @@ public function create_table(string $name, string $data): void
*/
public function count_tables(): int
{
if (is_null($this->db) || is_null($this->engine)) {
$this->connect_db();
}

if ($this->get_engine()->id === DatabaseDriverID::MYSQL) {
return count(
$this->get_all("SHOW TABLES")
Expand All @@ -347,10 +343,7 @@ public function count_tables(): int

public function raw_db(): PDO
{
if (is_null($this->db)) {
$this->connect_db();
}
return $this->db;
return $this->get_db();
}

public function standardise_boolean(string $table, string $column, bool $include_postgres=false): void
Expand Down

0 comments on commit 340b9da

Please sign in to comment.