Skip to content

Commit fb49581

Browse files
committed
feat: add DEFAULT helper support for SQLite
- Add testDefaultHelper() test for SQLite to match other dialects - Handle DEFAULT keyword in RawValueResolver for SQLite (replace with NULL) - Update buildRawValueExpression() in SqliteDialect to handle DEFAULT in UPSERT operations - SQLite doesn't support DEFAULT in UPDATE, so we use NULL as equivalent
1 parent 3cdf9ba commit fb49581

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/dialects/SqliteDialect.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,13 @@ protected function buildIncrementExpression(string $colSql, array $expr, string
319319
*/
320320
protected function buildRawValueExpression(string $colSql, RawValue $expr, string $tableName, string $col): string
321321
{
322-
return "{$colSql} = {$expr->getValue()}";
322+
// SQLite doesn't support DEFAULT keyword in UPDATE statements
323+
// Replace DEFAULT with NULL (closest equivalent behavior)
324+
$value = $expr->getValue();
325+
if (trim($value) === 'DEFAULT') {
326+
return "{$colSql} = NULL";
327+
}
328+
return "{$colSql} = {$value}";
323329
}
324330

325331
/**

src/query/RawValueResolver.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use tommyknocker\pdodb\connection\ConnectionInterface;
88
use tommyknocker\pdodb\dialects\DialectInterface;
9+
use tommyknocker\pdodb\dialects\SqliteDialect;
910
use tommyknocker\pdodb\helpers\values\ConcatValue;
1011
use tommyknocker\pdodb\helpers\values\ConfigValue;
1112
use tommyknocker\pdodb\helpers\values\CurDateValue;
@@ -186,6 +187,12 @@ public function resolveRawValue(string|RawValue $value): string
186187
$sql = $value->getValue();
187188
$params = $value->getParams();
188189

190+
// SQLite doesn't support DEFAULT keyword in UPDATE statements
191+
// Replace DEFAULT with NULL (closest equivalent behavior)
192+
if (trim($sql) === 'DEFAULT' && $this->dialect instanceof SqliteDialect) {
193+
return 'NULL';
194+
}
195+
189196
if (empty($params)) {
190197
return $sql;
191198
}

tests/sqlite/HelpersTests.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,30 @@ public function testIsNullIsNotNullHelpers(): void
130130
$this->assertEquals('Helen', $notNullResults[0]['name']);
131131
}
132132

133+
public function testDefaultHelper(): void
134+
{
135+
$db = self::$db;
136+
137+
$userId = $db->find()->table('users')->insert([
138+
'name' => 'Nina',
139+
'company' => 'DefaultCorp',
140+
'age' => 31,
141+
'status' => 'active',
142+
]);
143+
144+
$db->find()
145+
->table('users')
146+
->where('id', $userId)
147+
->update(['status' => Db::default()]);
148+
149+
$result = $db->find()
150+
->from('users')
151+
->where('id', $userId)
152+
->getOne();
153+
154+
$this->assertNull($result['status']);
155+
}
156+
133157
public function testCaseInSelect(): void
134158
{
135159
$db = self::$db;

0 commit comments

Comments
 (0)