First cut of ID generation need detection

This commit is contained in:
2024-06-10 21:40:47 -04:00
parent c892689eb6
commit 9e617e7e23
3 changed files with 37 additions and 32 deletions

View File

@@ -50,22 +50,23 @@ class Query
* Create an `INSERT` statement for a document
*
* @param string $tableName The name of the table into which the document will be inserted
* @param AutoId|null $autoId The version of automatic ID query to generate (optional, defaults to None)
* @return string The `INSERT` statement to insert a document
* @throws DocumentException If the database mode is not set
*/
public static function insert(string $tableName): string
public static function insert(string $tableName, ?AutoId $autoId = null): string
{
try {
$id = Configuration::$idField;
$values = match (Configuration::$mode) {
Mode::SQLite => match (Configuration::$autoId) {
Mode::SQLite => match ($autoId ?? AutoId::None) {
AutoId::None => ':data',
AutoId::Number => "json_set(:data, '$.$id', "
. "(SELECT coalesce(max(data->>'$id'), 0) + 1 FROM $tableName))",
AutoId::UUID => "json_set(:data, '$.$id', '" . AutoId::generateUUID() . "')",
AutoId::RandomString => "json_set(:data, '$.$id', '" . AutoId::generateRandom() ."')"
},
Mode::PgSQL => match (Configuration::$autoId) {
Mode::PgSQL => match ($autoId ?? AutoId::None) {
AutoId::None => ':data',
AutoId::Number => ":data || ('{\"$id\":' || "
. "(SELECT COALESCE(MAX(data->>'$id'), 0) + 1 FROM $tableName) || '}')",