From ea225a1c578fa2eeeeb10c7b4e5d153c46b9f418 Mon Sep 17 00:00:00 2001 From: grimpirate Date: Wed, 16 Oct 2024 02:02:01 -0400 Subject: [PATCH] now() throws error when using SQLite3 --- system/Session/Handlers/DatabaseHandler.php | 27 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/system/Session/Handlers/DatabaseHandler.php b/system/Session/Handlers/DatabaseHandler.php index 2d1d2fbbe97e..59bb5c2e7fda 100644 --- a/system/Session/Handlers/DatabaseHandler.php +++ b/system/Session/Handlers/DatabaseHandler.php @@ -196,7 +196,7 @@ public function write($id, $data): bool 'data' => $this->prepareData($data), ]; - if (! $this->db->table($this->table)->set('timestamp', 'now()', false)->insert($insertData)) { + if (! $this->db->table($this->table)->set('timestamp', $this->now(), false)->insert($insertData)) { return $this->fail(); } @@ -218,7 +218,7 @@ public function write($id, $data): bool $updateData['data'] = $this->prepareData($data); } - if (! $builder->set('timestamp', 'now()', false)->update($updateData)) { + if (! $builder->set('timestamp', $this->now(), false)->update($updateData)) { return $this->fail(); } @@ -287,7 +287,7 @@ public function gc($max_lifetime) return $this->db->table($this->table)->where( 'timestamp <', - "now() - INTERVAL {$interval}", + $this->now($interval), false )->delete() ? 1 : $this->fail(); } @@ -304,4 +304,25 @@ protected function releaseLock(): bool // Unsupported DB? Let the parent handle the simple version. return parent::releaseLock(); } + + /** + * Determines which NOW function to use based on DBDriver + * + * @param string $interval Amount of time to subtract from NOW + */ + private function now($interval = null): string + { + $DBDriver = service('settings')->get("Database.{$this->DBGroup}")['DBDriver']; + return !is_null($interval) + ? match($DBDriver) + { + 'SQLite3' => "datetime('now', '-{$interval}')", + default => "now() - INTERVAL {$interval}", + } + : match($DBDriver) + { + 'SQLite3' => "datetime('now')", + default => "now()", + }; + } }