pg support of duplicate key update

This commit is contained in:
xiaomlove
2026-04-25 03:22:38 +07:00
parent dc77ab7b40
commit 7d18a7f76a
14 changed files with 62 additions and 29 deletions
+35
View File
@@ -522,4 +522,39 @@ class NexusDB
}
}
public static function fromUnixTimestampField(int $timestamp): string
{
if (self::isMysql()) {
return sprintf("FROM_UNIXTIME(%d)", $timestamp);
} elseif (self::isPgsql()) {
return sprintf("to_timestamp(%d)", $timestamp);
} else {
throw new \RuntimeException('Not supported database.');
}
}
public static function upsertField(array $uniqueFields, array $updateFields): string
{
if (self::isMysql()) {
$updates = [];
foreach ($updateFields ?: ['id'] as $field) {
$updates[] = "`$field` = VALUES(`$field`)";
}
return sprintf("ON DUPLICATE KEY UPDATE %s", implode(', ', $updates));
} elseif (self::isPgsql()) {
if (empty($updateFields)) {
$updateStr = "NOTHING";
} else {
$updates = [];
foreach ($updateFields as $field) {
$updates[] = "$field = EXCLUDED.$field";
}
$updateStr = "UPDATE SET " . implode(', ', $updates);
}
return sprintf("ON CONFLICT (%s) DO %s", implode(', ', $uniqueFields), $updateStr);
} else {
throw new \RuntimeException('Not supported database.');
}
}
}