Skip to content

Commit ea225a1

Browse files
authored
now() throws error when using SQLite3
1 parent 81e6289 commit ea225a1

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

system/Session/Handlers/DatabaseHandler.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function write($id, $data): bool
196196
'data' => $this->prepareData($data),
197197
];
198198

199-
if (! $this->db->table($this->table)->set('timestamp', 'now()', false)->insert($insertData)) {
199+
if (! $this->db->table($this->table)->set('timestamp', $this->now(), false)->insert($insertData)) {
200200
return $this->fail();
201201
}
202202

@@ -218,7 +218,7 @@ public function write($id, $data): bool
218218
$updateData['data'] = $this->prepareData($data);
219219
}
220220

221-
if (! $builder->set('timestamp', 'now()', false)->update($updateData)) {
221+
if (! $builder->set('timestamp', $this->now(), false)->update($updateData)) {
222222
return $this->fail();
223223
}
224224

@@ -287,7 +287,7 @@ public function gc($max_lifetime)
287287

288288
return $this->db->table($this->table)->where(
289289
'timestamp <',
290-
"now() - INTERVAL {$interval}",
290+
$this->now($interval),
291291
false
292292
)->delete() ? 1 : $this->fail();
293293
}
@@ -304,4 +304,25 @@ protected function releaseLock(): bool
304304
// Unsupported DB? Let the parent handle the simple version.
305305
return parent::releaseLock();
306306
}
307+
308+
/**
309+
* Determines which NOW function to use based on DBDriver
310+
*
311+
* @param string $interval Amount of time to subtract from NOW
312+
*/
313+
private function now($interval = null): string
314+
{
315+
$DBDriver = service('settings')->get("Database.{$this->DBGroup}")['DBDriver'];
316+
return !is_null($interval)
317+
? match($DBDriver)
318+
{
319+
'SQLite3' => "datetime('now', '-{$interval}')",
320+
default => "now() - INTERVAL {$interval}",
321+
}
322+
: match($DBDriver)
323+
{
324+
'SQLite3' => "datetime('now')",
325+
default => "now()",
326+
};
327+
}
307328
}

0 commit comments

Comments
 (0)