First cut of ID generation need detection
This commit is contained in:
parent
c892689eb6
commit
9e617e7e23
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
namespace BitBadger\PDODocument;
|
namespace BitBadger\PDODocument;
|
||||||
|
|
||||||
use BitBadger\PDODocument\Query\Insert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Functions that apply at a whole document level
|
* Functions that apply at a whole document level
|
||||||
*/
|
*/
|
||||||
|
@ -18,7 +16,25 @@ class Document
|
||||||
*/
|
*/
|
||||||
public static function insert(string $tableName, array|object $document): void
|
public static function insert(string $tableName, array|object $document): void
|
||||||
{
|
{
|
||||||
Custom::nonQuery(Query::insert($tableName), Parameters::json(':data', $document));
|
$doInsert = fn() => Custom::nonQuery(Query::insert($tableName), Parameters::json(':data', $document));
|
||||||
|
|
||||||
|
if (Configuration::$autoId == AutoId::None) {
|
||||||
|
$doInsert();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$id = Configuration::$idField;
|
||||||
|
$idProvided =
|
||||||
|
(is_array( $document) && is_int( $document[$id]) && $document[$id] <> 0)
|
||||||
|
|| (is_array( $document) && is_string($document[$id]) && $document[$id] <> '')
|
||||||
|
|| (is_object($document) && is_int( $document->{$id}) && $document->{$id} <> 0)
|
||||||
|
|| (is_object($document) && is_string($document->{$id}) && $document->{$id} <> '');
|
||||||
|
|
||||||
|
if ($idProvided) {
|
||||||
|
$doInsert();
|
||||||
|
} else {
|
||||||
|
Custom::nonQuery(Query::insert($tableName, Configuration::$autoId), Parameters::json(':data', $document));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -50,22 +50,23 @@ class Query
|
||||||
* Create an `INSERT` statement for a document
|
* Create an `INSERT` statement for a document
|
||||||
*
|
*
|
||||||
* @param string $tableName The name of the table into which the document will be inserted
|
* @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
|
* @return string The `INSERT` statement to insert a document
|
||||||
* @throws DocumentException If the database mode is not set
|
* @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 {
|
try {
|
||||||
$id = Configuration::$idField;
|
$id = Configuration::$idField;
|
||||||
$values = match (Configuration::$mode) {
|
$values = match (Configuration::$mode) {
|
||||||
Mode::SQLite => match (Configuration::$autoId) {
|
Mode::SQLite => match ($autoId ?? AutoId::None) {
|
||||||
AutoId::None => ':data',
|
AutoId::None => ':data',
|
||||||
AutoId::Number => "json_set(:data, '$.$id', "
|
AutoId::Number => "json_set(:data, '$.$id', "
|
||||||
. "(SELECT coalesce(max(data->>'$id'), 0) + 1 FROM $tableName))",
|
. "(SELECT coalesce(max(data->>'$id'), 0) + 1 FROM $tableName))",
|
||||||
AutoId::UUID => "json_set(:data, '$.$id', '" . AutoId::generateUUID() . "')",
|
AutoId::UUID => "json_set(:data, '$.$id', '" . AutoId::generateUUID() . "')",
|
||||||
AutoId::RandomString => "json_set(:data, '$.$id', '" . AutoId::generateRandom() ."')"
|
AutoId::RandomString => "json_set(:data, '$.$id', '" . AutoId::generateRandom() ."')"
|
||||||
},
|
},
|
||||||
Mode::PgSQL => match (Configuration::$autoId) {
|
Mode::PgSQL => match ($autoId ?? AutoId::None) {
|
||||||
AutoId::None => ':data',
|
AutoId::None => ':data',
|
||||||
AutoId::Number => ":data || ('{\"$id\":' || "
|
AutoId::Number => ":data || ('{\"$id\":' || "
|
||||||
. "(SELECT COALESCE(MAX(data->>'$id'), 0) + 1 FROM $tableName) || '}')",
|
. "(SELECT COALESCE(MAX(data->>'$id'), 0) + 1 FROM $tableName) || '}')",
|
||||||
|
|
|
@ -87,64 +87,56 @@ class QueryTest extends TestCase
|
||||||
#[TestDox('Insert succeeds with auto numeric ID for PostgreSQL')]
|
#[TestDox('Insert succeeds with auto numeric ID for PostgreSQL')]
|
||||||
public function testInsertSucceedsWithAutoNumericIdForPostgreSQL(): void
|
public function testInsertSucceedsWithAutoNumericIdForPostgreSQL(): void
|
||||||
{
|
{
|
||||||
Configuration::$mode = Mode::PgSQL;
|
Configuration::$mode = Mode::PgSQL;
|
||||||
Configuration::$autoId = AutoId::Number;
|
|
||||||
try {
|
try {
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"INSERT INTO test_tbl VALUES (:data || ('{\"id\":' "
|
"INSERT INTO test_tbl VALUES (:data || ('{\"id\":' "
|
||||||
. "|| (SELECT COALESCE(MAX(data->>'id'), 0) + 1 FROM test_tbl) || '}'))",
|
. "|| (SELECT COALESCE(MAX(data->>'id'), 0) + 1 FROM test_tbl) || '}'))",
|
||||||
Query::insert('test_tbl'), 'INSERT statement not constructed correctly');
|
Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly');
|
||||||
} finally {
|
} finally {
|
||||||
Configuration::$mode = null;
|
Configuration::$mode = null;
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto numeric ID for SQLite')]
|
#[TestDox('Insert succeeds with auto numeric ID for SQLite')]
|
||||||
public function testInsertSucceedsWithAutoNumericIdForSQLite(): void
|
public function testInsertSucceedsWithAutoNumericIdForSQLite(): void
|
||||||
{
|
{
|
||||||
Configuration::$mode = Mode::SQLite;
|
Configuration::$mode = Mode::SQLite;
|
||||||
Configuration::$autoId = AutoId::Number;
|
|
||||||
try {
|
try {
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
"INSERT INTO test_tbl VALUES (json_set(:data, '$.id', "
|
"INSERT INTO test_tbl VALUES (json_set(:data, '$.id', "
|
||||||
. "(SELECT coalesce(max(data->>'id'), 0) + 1 FROM test_tbl)))",
|
. "(SELECT coalesce(max(data->>'id'), 0) + 1 FROM test_tbl)))",
|
||||||
Query::insert('test_tbl'), 'INSERT statement not constructed correctly');
|
Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly');
|
||||||
} finally {
|
} finally {
|
||||||
Configuration::$mode = null;
|
Configuration::$mode = null;
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto UUID for PostgreSQL')]
|
#[TestDox('Insert succeeds with auto UUID for PostgreSQL')]
|
||||||
public function testInsertSucceedsWithAutoUuidForPostgreSQL(): void
|
public function testInsertSucceedsWithAutoUuidForPostgreSQL(): void
|
||||||
{
|
{
|
||||||
Configuration::$mode = Mode::PgSQL;
|
Configuration::$mode = Mode::PgSQL;
|
||||||
Configuration::$autoId = AutoId::UUID;
|
|
||||||
try {
|
try {
|
||||||
$query = Query::insert('test_tbl');
|
$query = Query::insert('test_tbl', AutoId::UUID);
|
||||||
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", $query,
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", $query,
|
||||||
'INSERT statement not constructed correctly');
|
'INSERT statement not constructed correctly');
|
||||||
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
||||||
} finally {
|
} finally {
|
||||||
Configuration::$mode = null;
|
Configuration::$mode = null;
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto UUID for SQLite')]
|
#[TestDox('Insert succeeds with auto UUID for SQLite')]
|
||||||
public function testInsertSucceedsWithAutoUuidForSQLite(): void
|
public function testInsertSucceedsWithAutoUuidForSQLite(): void
|
||||||
{
|
{
|
||||||
Configuration::$mode = Mode::SQLite;
|
Configuration::$mode = Mode::SQLite;
|
||||||
Configuration::$autoId = AutoId::UUID;
|
|
||||||
try {
|
try {
|
||||||
$query = Query::insert('test_tbl');
|
$query = Query::insert('test_tbl', AutoId::UUID);
|
||||||
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query,
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query,
|
||||||
'INSERT statement not constructed correctly');
|
'INSERT statement not constructed correctly');
|
||||||
$this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly');
|
$this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly');
|
||||||
} finally {
|
} finally {
|
||||||
Configuration::$mode = null;
|
Configuration::$mode = null;
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,10 +144,9 @@ class QueryTest extends TestCase
|
||||||
public function testInsertSucceedsWithAutoRandomStringForPostgreSQL(): void
|
public function testInsertSucceedsWithAutoRandomStringForPostgreSQL(): void
|
||||||
{
|
{
|
||||||
Configuration::$mode = Mode::PgSQL;
|
Configuration::$mode = Mode::PgSQL;
|
||||||
Configuration::$autoId = AutoId::RandomString;
|
|
||||||
Configuration::$idStringLength = 8;
|
Configuration::$idStringLength = 8;
|
||||||
try {
|
try {
|
||||||
$query = Query::insert('test_tbl');
|
$query = Query::insert('test_tbl', AutoId::RandomString);
|
||||||
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", $query,
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", $query,
|
||||||
'INSERT statement not constructed correctly');
|
'INSERT statement not constructed correctly');
|
||||||
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
||||||
|
@ -163,7 +154,6 @@ class QueryTest extends TestCase
|
||||||
$this->assertEquals(8, strlen($id), "Generated ID [$id] should have been 8 characters long");
|
$this->assertEquals(8, strlen($id), "Generated ID [$id] should have been 8 characters long");
|
||||||
} finally {
|
} finally {
|
||||||
Configuration::$mode = null;
|
Configuration::$mode = null;
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
Configuration::$idStringLength = 16;
|
Configuration::$idStringLength = 16;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,9 +162,8 @@ class QueryTest extends TestCase
|
||||||
public function testInsertSucceedsWithAutoRandomStringForSQLite(): void
|
public function testInsertSucceedsWithAutoRandomStringForSQLite(): void
|
||||||
{
|
{
|
||||||
Configuration::$mode = Mode::SQLite;
|
Configuration::$mode = Mode::SQLite;
|
||||||
Configuration::$autoId = AutoId::RandomString;
|
|
||||||
try {
|
try {
|
||||||
$query = Query::insert('test_tbl');
|
$query = Query::insert('test_tbl', AutoId::RandomString);
|
||||||
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query,
|
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query,
|
||||||
'INSERT statement not constructed correctly');
|
'INSERT statement not constructed correctly');
|
||||||
$this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly');
|
$this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly');
|
||||||
|
@ -182,7 +171,6 @@ class QueryTest extends TestCase
|
||||||
$this->assertEquals(16, strlen($id), "Generated ID [$id] should have been 16 characters long");
|
$this->assertEquals(16, strlen($id), "Generated ID [$id] should have been 16 characters long");
|
||||||
} finally {
|
} finally {
|
||||||
Configuration::$mode = null;
|
Configuration::$mode = null;
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user