Compare commits
23 Commits
v1.0.0-alp
...
a10ecbb1cd
| Author | SHA1 | Date | |
|---|---|---|---|
| a10ecbb1cd | |||
| 2d8f8b6e87 | |||
| a96fdf9d14 | |||
| 9fba3781d6 | |||
| 1ab961e35a | |||
| d9ffc36fe6 | |||
| bcca9f5ace | |||
| 1e7c41eaa2 | |||
| 50b4104e02 | |||
| 85786fa113 | |||
| 71ae46fac7 | |||
| 259247464a | |||
| b871d3e03f | |||
| 4342d1ac50 | |||
| 3d45bbcabc | |||
| 7390ae0f61 | |||
| cecbb51414 | |||
| c2dc111dce | |||
| afc5d80095 | |||
| b705130624 | |||
| 98bfceb7c9 | |||
| ecc13a30cf | |||
| 1164dc7cc5 |
4
.gitattributes
vendored
4
.gitattributes
vendored
@@ -1,4 +0,0 @@
|
|||||||
/.gitignore export-ignore
|
|
||||||
/.gitattributes export-ignore
|
|
||||||
/composer.lock export-ignore
|
|
||||||
/tests/**/* export-ignore
|
|
||||||
33
README.md
33
README.md
@@ -1,32 +1,3 @@
|
|||||||
# PDODocument
|
# pdo-document
|
||||||
|
|
||||||
This library allows SQLite (and, by v1.0.0-beta1, PostgreSQL) to be treated as a document database. It is a PHP implementation of the .NET [BitBadger.Documents](https://git.bitbadger.solutions/bit-badger/BitBadger.Documents) library.
|
Bit Badger Documents PHP implementation with PDO
|
||||||
|
|
||||||
## Add via Composer
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
`composer require bit-badger/pdo-document`
|
|
||||||
|
|
||||||
## Configuration
|
|
||||||
|
|
||||||
### Connection Details
|
|
||||||
|
|
||||||
The static variable `Configuration::$pdoDSN` must be set to the [PDO data source name](https://www.php.net/manual/en/pdo.construct.php#refsect1-pdo.construct-parameters) for your database. `Configuration` also has `$username`, `$password`, and `$options` variables that will be used to construct the PDO object it will use for data access.
|
|
||||||
|
|
||||||
### Document Identifiers
|
|
||||||
|
|
||||||
Each document must have a unique identifier. By default, the library assumes that this is a property or array key named `id`, but this can be controlled by setting `Configuration::$idField`. Once documents exist, this should not be changed.
|
|
||||||
|
|
||||||
IDs can be generated automatically on insert. The `AutoId` enumeration has 4 values:
|
|
||||||
|
|
||||||
- `AutoId::None` is the default; no IDs will be generated
|
|
||||||
- `AutoId::Number` will assign max-ID-plus-one to documents with an ID of 0
|
|
||||||
- `AutoId::UUID` will generate a v4 <abbr title="Universally Unique Identifier">UUID</abbr> for documents with an empty `string` ID
|
|
||||||
- `AutoId::RandomString` will generate a string of letters and numbers for documents with an empty `string` ID; `Configuration::$idStringLength` controls the length of the generated string, and defaults to 16 characters
|
|
||||||
|
|
||||||
In all generated scenarios, if the ID value is not 0 or blank, that ID will be used instead of a generated one.
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Documentation for this library is not complete; however, its structure is very similar to the .NET version, so [its documentation will help](https://bitbadger.solutions/open-source/relational-documents/basic-usage.html) until its project specific documentation is developed. Things like `Count.All()` become `Count::all`, and all the `byField` operations are named `byFields` and take an array of fields.
|
|
||||||
@@ -1,23 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "bit-badger/pdo-document",
|
"name": "bit-badger/pdo-document",
|
||||||
"description": "Treat SQLite (and soon PostgreSQL) as a document store",
|
|
||||||
"keywords": ["database", "document", "sqlite", "pdo"],
|
|
||||||
"license": "MIT",
|
|
||||||
"authors": [
|
|
||||||
{
|
|
||||||
"name": "Daniel J. Summers",
|
|
||||||
"email": "daniel@bitbadger.solutions",
|
|
||||||
"homepage": "https://bitbadger.solutions",
|
|
||||||
"role": "Developer"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"support": {
|
|
||||||
"email": "daniel@bitbadger.solutions",
|
|
||||||
"source": "https://git.bitbadger.solutions/bit-badger/pdo-document",
|
|
||||||
"rss": "https://git.bitbadger.solutions/bit-badger/pdo-document.rss"
|
|
||||||
},
|
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.2",
|
|
||||||
"netresearch/jsonmapper": "^4",
|
"netresearch/jsonmapper": "^4",
|
||||||
"ext-pdo": "*"
|
"ext-pdo": "*"
|
||||||
},
|
},
|
||||||
@@ -37,8 +20,5 @@
|
|||||||
"Test\\Integration\\": "./tests/integration",
|
"Test\\Integration\\": "./tests/integration",
|
||||||
"Test\\Integration\\SQLite\\": "./tests/integration/sqlite"
|
"Test\\Integration\\SQLite\\": "./tests/integration/sqlite"
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"archive": {
|
|
||||||
"exclude": [ "/tests", "/.gitattributes", "/.gitignore", "/.git", "/composer.lock" ]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
3
composer.lock
generated
3
composer.lock
generated
@@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "20bf0d96304e429b431535d05ff4585a",
|
"content-hash": "eada4b7eb6f976e0aaf0b54b92e2ca32",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "netresearch/jsonmapper",
|
"name": "netresearch/jsonmapper",
|
||||||
@@ -1697,7 +1697,6 @@
|
|||||||
"prefer-stable": false,
|
"prefer-stable": false,
|
||||||
"prefer-lowest": false,
|
"prefer-lowest": false,
|
||||||
"platform": {
|
"platform": {
|
||||||
"php": ">=8.2",
|
|
||||||
"ext-pdo": "*"
|
"ext-pdo": "*"
|
||||||
},
|
},
|
||||||
"platform-dev": [],
|
"platform-dev": [],
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace BitBadger\PDODocument;
|
|
||||||
|
|
||||||
use Random\RandomException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* How automatic ID generation should be performed
|
|
||||||
*/
|
|
||||||
enum AutoId
|
|
||||||
{
|
|
||||||
/** Do not automatically generate IDs */
|
|
||||||
case None;
|
|
||||||
|
|
||||||
/** New documents with a 0 ID should receive max ID plus one */
|
|
||||||
case Number;
|
|
||||||
|
|
||||||
/** New documents with a blank ID should receive a v4 UUID (Universally Unique Identifier) */
|
|
||||||
case UUID;
|
|
||||||
|
|
||||||
/** New documents with a blank ID should receive a random string (set `Configuration::$idStringLength`) */
|
|
||||||
case RandomString;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a v4 UUID
|
|
||||||
*
|
|
||||||
* @return string The v4 UUID
|
|
||||||
* @throws RandomException If an appropriate source of randomness cannot be found
|
|
||||||
*/
|
|
||||||
public static function generateUUID(): string
|
|
||||||
{
|
|
||||||
// hat tip: https://stackoverflow.com/a/15875555/276707
|
|
||||||
$bytes = random_bytes(16);
|
|
||||||
|
|
||||||
$bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40); // set version to 0100
|
|
||||||
$bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80); // set bits 6-7 to 10
|
|
||||||
|
|
||||||
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a random string ID
|
|
||||||
*
|
|
||||||
* @return string A string filled with the hexadecimal representation of random bytes
|
|
||||||
* @throws RandomException If an appropriate source of randomness cannot be found
|
|
||||||
*/
|
|
||||||
public static function generateRandom(): string
|
|
||||||
{
|
|
||||||
return bin2hex(random_bytes(Configuration::$idStringLength / 2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,14 +12,6 @@ class Configuration
|
|||||||
/** @var string The name of the ID field used in the database (will be treated as the primary key) */
|
/** @var string The name of the ID field used in the database (will be treated as the primary key) */
|
||||||
public static string $idField = 'id';
|
public static string $idField = 'id';
|
||||||
|
|
||||||
/** @var AutoId The automatic ID generation process to use */
|
|
||||||
public static AutoId $autoId = AutoId::None;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int The number of characters a string generated by `AutoId::RandomString` will have (must be an even number)
|
|
||||||
*/
|
|
||||||
public static int $idStringLength = 16;
|
|
||||||
|
|
||||||
/** @var string The data source name (DSN) of the connection string */
|
/** @var string The data source name (DSN) of the connection string */
|
||||||
public static string $pdoDSN = '';
|
public static string $pdoDSN = '';
|
||||||
|
|
||||||
@@ -67,9 +59,7 @@ class Configuration
|
|||||||
return self::$_pdo;
|
return self::$_pdo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Clear the current PDO instance
|
|
||||||
*/
|
|
||||||
public static function resetPDO(): void
|
public static function resetPDO(): void
|
||||||
{
|
{
|
||||||
self::$_pdo = null;
|
self::$_pdo = null;
|
||||||
|
|||||||
@@ -26,14 +26,14 @@ class Count
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table in which documents should be counted
|
* @param string $tableName The name of the table in which documents should be counted
|
||||||
* @param array|Field[] $fields The field comparison to match
|
* @param array|Field[] $fields The field comparison to match
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return int The count of documents matching the field comparison
|
* @return int The count of documents matching the field comparison
|
||||||
* @throws DocumentException If one is encountered
|
* @throws DocumentException If one is encountered
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): int
|
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): int
|
||||||
{
|
{
|
||||||
$namedFields = Parameters::nameFields($fields);
|
$namedFields = Parameters::nameFields($fields);
|
||||||
return Custom::scalar(Query\Count::byFields($tableName, $namedFields, $match),
|
return Custom::scalar(Query\Count::byFields($tableName, $namedFields, $conjunction),
|
||||||
Parameters::addFields($namedFields, []), new CountMapper());
|
Parameters::addFields($namedFields, []), new CountMapper());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,13 +24,13 @@ class Delete
|
|||||||
*
|
*
|
||||||
* @param string $tableName The table from which documents should be deleted
|
* @param string $tableName The table from which documents should be deleted
|
||||||
* @param array|Field[] $fields The field comparison to match
|
* @param array|Field[] $fields The field comparison to match
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @throws DocumentException If any is encountered
|
* @throws DocumentException If any is encountered
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): void
|
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): void
|
||||||
{
|
{
|
||||||
$namedFields = Parameters::nameFields($fields);
|
$namedFields = Parameters::nameFields($fields);
|
||||||
Custom::nonQuery(Query\Delete::byFields($tableName, $namedFields, $match),
|
Custom::nonQuery(Query\Delete::byFields($tableName, $namedFields, $conjunction),
|
||||||
Parameters::addFields($namedFields, []));
|
Parameters::addFields($namedFields, []));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,25 +16,7 @@ class Document
|
|||||||
*/
|
*/
|
||||||
public static function insert(string $tableName, array|object $document): void
|
public static function insert(string $tableName, array|object $document): void
|
||||||
{
|
{
|
||||||
$doInsert = fn() => Custom::nonQuery(Query::insert($tableName), Parameters::json(':data', $document));
|
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -27,14 +27,14 @@ class Exists
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table in which document existence should be determined
|
* @param string $tableName The name of the table in which document existence should be determined
|
||||||
* @param Field[] $fields The field comparison to match
|
* @param Field[] $fields The field comparison to match
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return bool True if any documents match the field comparison, false if not
|
* @return bool True if any documents match the field comparison, false if not
|
||||||
* @throws DocumentException If any is encountered
|
* @throws DocumentException If any is encountered
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): bool
|
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): bool
|
||||||
{
|
{
|
||||||
$namedFields = Parameters::nameFields($fields);
|
$namedFields = Parameters::nameFields($fields);
|
||||||
return Custom::scalar(Query\Exists::byFields($tableName, $namedFields, $match),
|
return Custom::scalar(Query\Exists::byFields($tableName, $namedFields, $conjunction),
|
||||||
Parameters::addFields($namedFields, []), new ExistsMapper());
|
Parameters::addFields($namedFields, []), new ExistsMapper());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace BitBadger\PDODocument;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* How multiple fields should be matched
|
|
||||||
*/
|
|
||||||
enum FieldMatch
|
|
||||||
{
|
|
||||||
/** Match all provided fields (`AND`) */
|
|
||||||
case All;
|
|
||||||
|
|
||||||
/** Match any provided fields (`OR`) */
|
|
||||||
case Any;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the SQL keyword for this enumeration value
|
|
||||||
*
|
|
||||||
* @return string The SQL keyword for this enumeration value
|
|
||||||
*/
|
|
||||||
public function toString(): string
|
|
||||||
{
|
|
||||||
return match ($this) {
|
|
||||||
FieldMatch::All => 'AND',
|
|
||||||
FieldMatch::Any => 'OR'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
12
src/Find.php
12
src/Find.php
@@ -45,15 +45,15 @@ class Find
|
|||||||
* @param string $tableName The table from which documents should be retrieved
|
* @param string $tableName The table from which documents should be retrieved
|
||||||
* @param array|Field[] $fields The field comparison to match
|
* @param array|Field[] $fields The field comparison to match
|
||||||
* @param class-string<TDoc> $className The name of the class to be retrieved
|
* @param class-string<TDoc> $className The name of the class to be retrieved
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return DocumentList<TDoc> A list of documents matching the given field comparison
|
* @return DocumentList<TDoc> A list of documents matching the given field comparison
|
||||||
* @throws DocumentException If any is encountered
|
* @throws DocumentException If any is encountered
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, string $className,
|
public static function byFields(string $tableName, array $fields, string $className,
|
||||||
?FieldMatch $match = null): DocumentList
|
string $conjunction = 'AND'): DocumentList
|
||||||
{
|
{
|
||||||
$namedFields = Parameters::nameFields($fields);
|
$namedFields = Parameters::nameFields($fields);
|
||||||
return Custom::list(Query\Find::byFields($tableName, $namedFields, $match),
|
return Custom::list(Query\Find::byFields($tableName, $namedFields, $conjunction),
|
||||||
Parameters::addFields($namedFields, []), new DocumentMapper($className));
|
Parameters::addFields($namedFields, []), new DocumentMapper($className));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,15 +64,15 @@ class Find
|
|||||||
* @param string $tableName The table from which the document should be retrieved
|
* @param string $tableName The table from which the document should be retrieved
|
||||||
* @param array|Field[] $fields The field comparison to match
|
* @param array|Field[] $fields The field comparison to match
|
||||||
* @param class-string<TDoc> $className The name of the class to be retrieved
|
* @param class-string<TDoc> $className The name of the class to be retrieved
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return false|TDoc The first document if any matches are found, false otherwise
|
* @return false|TDoc The first document if any matches are found, false otherwise
|
||||||
* @throws DocumentException If any is encountered
|
* @throws DocumentException If any is encountered
|
||||||
*/
|
*/
|
||||||
public static function firstByFields(string $tableName, array $fields, string $className,
|
public static function firstByFields(string $tableName, array $fields, string $className,
|
||||||
?FieldMatch $match = null): mixed
|
string $conjunction = 'AND'): mixed
|
||||||
{
|
{
|
||||||
$namedFields = Parameters::nameFields($fields);
|
$namedFields = Parameters::nameFields($fields);
|
||||||
return Custom::single(Query\Find::byFields($tableName, $namedFields, $match),
|
return Custom::single(Query\Find::byFields($tableName, $namedFields, $conjunction),
|
||||||
Parameters::addFields($namedFields, []), new DocumentMapper($className));
|
Parameters::addFields($namedFields, []), new DocumentMapper($className));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,14 +27,14 @@ class Patch
|
|||||||
* @param string $tableName The table in which documents should be patched
|
* @param string $tableName The table in which documents should be patched
|
||||||
* @param array|Field[] $fields The field comparison to match
|
* @param array|Field[] $fields The field comparison to match
|
||||||
* @param array|object $patch The object with which the documents should be patched (will be JSON-encoded)
|
* @param array|object $patch The object with which the documents should be patched (will be JSON-encoded)
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @throws DocumentException If any is encountered
|
* @throws DocumentException If any is encountered
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, array|object $patch,
|
public static function byFields(string $tableName, array $fields, array|object $patch,
|
||||||
?FieldMatch $match = null): void
|
string $conjunction = 'AND'): void
|
||||||
{
|
{
|
||||||
$namedFields = Parameters::nameFields($fields);
|
$namedFields = Parameters::nameFields($fields);
|
||||||
Custom::nonQuery(Query\Patch::byFields($tableName, $namedFields, $match),
|
Custom::nonQuery(Query\Patch::byFields($tableName, $namedFields, $conjunction),
|
||||||
Parameters::addFields($namedFields, Parameters::json(':data', $patch)));
|
Parameters::addFields($namedFields, Parameters::json(':data', $patch)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace BitBadger\PDODocument;
|
namespace BitBadger\PDODocument;
|
||||||
|
|
||||||
use Random\RandomException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query construction functions
|
* Query construction functions
|
||||||
*/
|
*/
|
||||||
@@ -24,14 +22,12 @@ class Query
|
|||||||
* Create a WHERE clause fragment to implement a comparison on fields in a JSON document
|
* Create a WHERE clause fragment to implement a comparison on fields in a JSON document
|
||||||
*
|
*
|
||||||
* @param Field[] $fields The field comparison to generate
|
* @param Field[] $fields The field comparison to generate
|
||||||
* @param FieldMatch|null $match How to join multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to join multiple conditions (optional; defaults to AND)
|
||||||
* @return string The WHERE clause fragment matching the given fields and parameter
|
* @return string The WHERE clause fragment matching the given fields and parameter
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function whereByFields(array $fields, ?FieldMatch $match = null): string
|
public static function whereByFields(array $fields, string $conjunction = 'AND'): string
|
||||||
{
|
{
|
||||||
return implode(' ' . ($match ?? FieldMatch::All)->toString() . ' ',
|
return implode(" $conjunction ", array_map(fn($it) => $it->toWhere(), $fields));
|
||||||
array_map(fn($it) => $it->toWhere(), $fields));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +35,6 @@ class Query
|
|||||||
*
|
*
|
||||||
* @param string $paramName The parameter name where the value of the ID will be provided (optional; default @id)
|
* @param string $paramName The parameter name where the value of the ID will be provided (optional; default @id)
|
||||||
* @return string The WHERE clause fragment to match by ID
|
* @return string The WHERE clause fragment to match by ID
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function whereById(string $paramName = ':id'): string
|
public static function whereById(string $paramName = ':id'): string
|
||||||
{
|
{
|
||||||
@@ -47,39 +42,14 @@ class Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an `INSERT` statement for a document
|
* Query to insert 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 a document should be inserted
|
||||||
* @param AutoId|null $autoId The version of automatic ID query to generate (optional, defaults to None)
|
* @return string The INSERT statement for the given table
|
||||||
* @return string The `INSERT` statement to insert a document
|
|
||||||
* @throws DocumentException If the database mode is not set
|
|
||||||
*/
|
*/
|
||||||
public static function insert(string $tableName, ?AutoId $autoId = null): string
|
public static function insert(string $tableName): string
|
||||||
{
|
{
|
||||||
try {
|
return "INSERT INTO $tableName VALUES (:data)";
|
||||||
$id = Configuration::$idField;
|
|
||||||
$values = match (Configuration::$mode) {
|
|
||||||
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 ($autoId ?? AutoId::None) {
|
|
||||||
AutoId::None => ':data',
|
|
||||||
AutoId::Number => ":data || ('{\"$id\":' || "
|
|
||||||
. "(SELECT COALESCE(MAX(data->>'$id'), 0) + 1 FROM $tableName) || '}')",
|
|
||||||
AutoId::UUID => ":data || '{\"$id\":\"" . AutoId::generateUUID() . "\"}'",
|
|
||||||
AutoId::RandomString => ":data || '{\"$id\":\"" . AutoId::generateRandom() . "\"}'",
|
|
||||||
},
|
|
||||||
default =>
|
|
||||||
throw new DocumentException('Database mode not set; cannot generate auto-ID INSERT statement'),
|
|
||||||
};
|
|
||||||
return "INSERT INTO $tableName VALUES ($values)";
|
|
||||||
} catch (RandomException $ex) {
|
|
||||||
throw new DocumentException('Unable to generate ID: ' . $ex->getMessage(), previous: $ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,8 +60,8 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public static function save(string $tableName): string
|
public static function save(string $tableName): string
|
||||||
{
|
{
|
||||||
$id = Configuration::$idField;
|
return self::insert($tableName)
|
||||||
return "INSERT INTO $tableName VALUES (:data) ON CONFLICT ((data->>'$id')) DO UPDATE SET data = EXCLUDED.data";
|
. " ON CONFLICT ((data->>'" . Configuration::$idField . "')) DO UPDATE SET data = EXCLUDED.data";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,7 +69,6 @@ class Query
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table in which the document should be updated
|
* @param string $tableName The name of the table in which the document should be updated
|
||||||
* @return string The UPDATE query for the document
|
* @return string The UPDATE query for the document
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function update(string $tableName): string
|
public static function update(string $tableName): string
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace BitBadger\PDODocument\Query;
|
namespace BitBadger\PDODocument\Query;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{DocumentException, Field, FieldMatch, Query};
|
use BitBadger\PDODocument\{Field, Query};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries for counting documents
|
* Queries for counting documents
|
||||||
@@ -25,12 +25,11 @@ class Count
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table in which documents should be counted
|
* @param string $tableName The name of the table in which documents should be counted
|
||||||
* @param Field[] $fields The field comparison to match
|
* @param Field[] $fields The field comparison to match
|
||||||
* @param FieldMatch|null $match How to join multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return string The query to count documents using a field comparison
|
* @return string The query to count documents using a field comparison
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): string
|
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): string
|
||||||
{
|
{
|
||||||
return self::all($tableName) . ' WHERE ' . Query::whereByFields($fields, $match);
|
return self::all($tableName) . ' WHERE ' . Query::whereByFields($fields, $conjunction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace BitBadger\PDODocument\Query;
|
namespace BitBadger\PDODocument\Query;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{DocumentException, Field, FieldMatch, Query};
|
use BitBadger\PDODocument\{Field, Query};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries to delete documents
|
* Queries to delete documents
|
||||||
@@ -14,7 +14,6 @@ class Delete
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table from which a document should be deleted
|
* @param string $tableName The name of the table from which a document should be deleted
|
||||||
* @return string The DELETE statement to delete a document by its ID
|
* @return string The DELETE statement to delete a document by its ID
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function byId(string $tableName): string
|
public static function byId(string $tableName): string
|
||||||
{
|
{
|
||||||
@@ -26,12 +25,11 @@ class Delete
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table from which documents should be deleted
|
* @param string $tableName The name of the table from which documents should be deleted
|
||||||
* @param Field[] $fields The field comparison to match
|
* @param Field[] $fields The field comparison to match
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return string The DELETE statement to delete documents via field comparison
|
* @return string The DELETE statement to delete documents via field comparison
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): string
|
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): string
|
||||||
{
|
{
|
||||||
return "DELETE FROM $tableName WHERE " . Query::whereByFields($fields, $match);
|
return "DELETE FROM $tableName WHERE " . Query::whereByFields($fields, $conjunction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace BitBadger\PDODocument\Query;
|
namespace BitBadger\PDODocument\Query;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{DocumentException, Field, FieldMatch, Query};
|
use BitBadger\PDODocument\{Field, Query};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries to determine document existence
|
* Queries to determine document existence
|
||||||
@@ -26,7 +26,6 @@ class Exists
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table in which document existence should be checked
|
* @param string $tableName The name of the table in which document existence should be checked
|
||||||
* @return string The query to determine document existence by ID
|
* @return string The query to determine document existence by ID
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function byId(string $tableName): string
|
public static function byId(string $tableName): string
|
||||||
{
|
{
|
||||||
@@ -38,12 +37,11 @@ class Exists
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table in which document existence should be checked
|
* @param string $tableName The name of the table in which document existence should be checked
|
||||||
* @param Field[] $fields The field comparison to match
|
* @param Field[] $fields The field comparison to match
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return string The query to determine document existence by field comparison
|
* @return string The query to determine document existence by field comparison
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): string
|
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): string
|
||||||
{
|
{
|
||||||
return self::query($tableName, Query::whereByFields($fields, $match));
|
return self::query($tableName, Query::whereByFields($fields, $conjunction));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace BitBadger\PDODocument\Query;
|
namespace BitBadger\PDODocument\Query;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{DocumentException, Field, FieldMatch, Query};
|
use BitBadger\PDODocument\{Field, Query};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries for retrieving documents
|
* Queries for retrieving documents
|
||||||
@@ -14,7 +14,6 @@ class Find
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table from which a document should be retrieved
|
* @param string $tableName The name of the table from which a document should be retrieved
|
||||||
* @return string The SELECT statement to retrieve a document by its ID
|
* @return string The SELECT statement to retrieve a document by its ID
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function byId(string $tableName): string
|
public static function byId(string $tableName): string
|
||||||
{
|
{
|
||||||
@@ -26,12 +25,11 @@ class Find
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table from which documents should be retrieved
|
* @param string $tableName The name of the table from which documents should be retrieved
|
||||||
* @param Field[] $fields The field comparison to match
|
* @param Field[] $fields The field comparison to match
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return string The SELECT statement to retrieve documents by field comparison
|
* @return string The SELECT statement to retrieve documents by field comparison
|
||||||
* @throws DocumentException If the database mode has not been set
|
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): string
|
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): string
|
||||||
{
|
{
|
||||||
return Query::selectFromTable($tableName) . ' WHERE ' . Query::whereByFields($fields, $match);
|
return Query::selectFromTable($tableName) . ' WHERE ' . Query::whereByFields($fields, $conjunction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace BitBadger\PDODocument\Query;
|
namespace BitBadger\PDODocument\Query;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode, Query};
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Query};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries to perform partial updates on documents
|
* Queries to perform partial updates on documents
|
||||||
@@ -44,12 +44,12 @@ class Patch
|
|||||||
*
|
*
|
||||||
* @param string $tableName The name of the table in which documents should be patched
|
* @param string $tableName The name of the table in which documents should be patched
|
||||||
* @param array|Field[] $field The field comparison to match
|
* @param array|Field[] $field The field comparison to match
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return string The query to patch documents via field comparison
|
* @return string The query to patch documents via field comparison
|
||||||
* @throws DocumentException If the database mode has not been set
|
* @throws DocumentException If the database mode has not been set
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $field, ?FieldMatch $match = null): string
|
public static function byFields(string $tableName, array $field, string $conjunction = 'AND'): string
|
||||||
{
|
{
|
||||||
return self::update($tableName, Query::whereByFields($field, $match));
|
return self::update($tableName, Query::whereByFields($field, $conjunction));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace BitBadger\PDODocument\Query;
|
namespace BitBadger\PDODocument\Query;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{Configuration, DocumentException, Field, FieldMatch, Mode, Query};
|
use BitBadger\PDODocument\{Configuration, DocumentException, Field, Mode, Query};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries to remove fields from documents
|
* Queries to remove fields from documents
|
||||||
@@ -54,13 +54,13 @@ class RemoveFields
|
|||||||
* @param string $tableName The name of the table in which documents should be manipulated
|
* @param string $tableName The name of the table in which documents should be manipulated
|
||||||
* @param array|Field[] $fields The field comparison to match
|
* @param array|Field[] $fields The field comparison to match
|
||||||
* @param array $parameters The parameter list for the query
|
* @param array $parameters The parameter list for the query
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @return string The UPDATE statement to remove fields from documents via field comparison
|
* @return string The UPDATE statement to remove fields from documents via field comparison
|
||||||
* @throws DocumentException If the database mode has not been set
|
* @throws DocumentException If the database mode has not been set
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, array $parameters,
|
public static function byFields(string $tableName, array $fields, array $parameters,
|
||||||
?FieldMatch $match = null): string
|
string $conjunction = 'AND'): string
|
||||||
{
|
{
|
||||||
return self::update($tableName, $parameters, Query::whereByFields($fields, $match));
|
return self::update($tableName, $parameters, Query::whereByFields($fields, $conjunction));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,15 +28,15 @@ class RemoveFields
|
|||||||
* @param string $tableName The table in which documents should have fields removed
|
* @param string $tableName The table in which documents should have fields removed
|
||||||
* @param array|Field[] $fields The field comparison to match
|
* @param array|Field[] $fields The field comparison to match
|
||||||
* @param array|string[] $fieldNames The names of the fields to be removed
|
* @param array|string[] $fieldNames The names of the fields to be removed
|
||||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||||
* @throws DocumentException If any is encountered
|
* @throws DocumentException If any is encountered
|
||||||
*/
|
*/
|
||||||
public static function byFields(string $tableName, array $fields, array $fieldNames,
|
public static function byFields(string $tableName, array $fields, array $fieldNames,
|
||||||
?FieldMatch $match = null): void
|
string $conjunction = 'AND'): void
|
||||||
{
|
{
|
||||||
$nameParams = Parameters::fieldNames(':name', $fieldNames);
|
$nameParams = Parameters::fieldNames(':name', $fieldNames);
|
||||||
$namedFields = Parameters::nameFields($fields);
|
$namedFields = Parameters::nameFields($fields);
|
||||||
Custom::nonQuery(Query\RemoveFields::byFields($tableName, $namedFields, $nameParams, $match),
|
Custom::nonQuery(Query\RemoveFields::byFields($tableName, $namedFields, $nameParams, $conjunction),
|
||||||
Parameters::addFields($namedFields, $nameParams));
|
Parameters::addFields($namedFields, $nameParams));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Test\Integration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A test document with a numeric ID
|
|
||||||
*/
|
|
||||||
class NumDocument
|
|
||||||
{
|
|
||||||
public function __construct(public int $id = 0, public string $value = '') { }
|
|
||||||
}
|
|
||||||
@@ -2,11 +2,10 @@
|
|||||||
|
|
||||||
namespace Test\Integration\SQLite;
|
namespace Test\Integration\SQLite;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{AutoId, Configuration, Custom, Document, DocumentException, Field, Find};
|
use BitBadger\PDODocument\{Document, DocumentException, Find};
|
||||||
use BitBadger\PDODocument\Mapper\ArrayMapper;
|
|
||||||
use PHPUnit\Framework\Attributes\TestDox;
|
use PHPUnit\Framework\Attributes\TestDox;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Test\Integration\{NumDocument, SubDocument, TestDocument};
|
use Test\Integration\{SubDocument, TestDocument};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SQLite integration tests for the Document class
|
* SQLite integration tests for the Document class
|
||||||
@@ -29,125 +28,7 @@ class DocumentTest extends TestCase
|
|||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for array no auto ID')]
|
public function testInsertSucceeds(): void
|
||||||
public function testInsertSucceedsForArrayNoAutoId(): void
|
|
||||||
{
|
|
||||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'turkey', 'sub' => ['foo' => 'gobble', 'bar' => 'gobble']]);
|
|
||||||
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document inserted');
|
|
||||||
$this->assertEquals('turkey', $doc->id, 'The ID was incorrect');
|
|
||||||
$this->assertEquals('', $doc->value, 'The value was incorrect');
|
|
||||||
$this->assertEquals(0, $doc->num_value, 'The numeric value was incorrect');
|
|
||||||
$this->assertNotNull($doc->sub, 'The sub-document should not have been null');
|
|
||||||
$this->assertEquals('gobble', $doc->sub->foo, 'The sub-document foo property was incorrect');
|
|
||||||
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for array with auto number ID not provided')]
|
|
||||||
public function testInsertSucceedsForArrayWithAutoNumberIdNotProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::Number;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
|
|
||||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'new', 'num_value' => 8]);
|
|
||||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$obj = json_decode($doc['data']);
|
|
||||||
$this->assertEquals(1, $obj->id, 'The ID 1 should have been auto-generated');
|
|
||||||
|
|
||||||
Document::insert(ThrowawayDb::TABLE, ['id' => 0, 'value' => 'again', 'num_value' => 7]);
|
|
||||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE . " WHERE data->>'id' = 2", [],
|
|
||||||
new ArrayMapper());
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$obj = json_decode($doc['data']);
|
|
||||||
$this->assertEquals(2, $obj->id, 'The ID 2 should have been auto-generated');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for array with auto number ID with ID provided')]
|
|
||||||
public function testInsertSucceedsForArrayWithAutoNumberIdWithIdProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::Number;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
Document::insert(ThrowawayDb::TABLE, ['id' => 7, 'value' => 'new', 'num_value' => 8]);
|
|
||||||
$doc = Custom::single('SELECT data FROM ' . ThrowawayDb::TABLE, [], new ArrayMapper());
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$obj = json_decode($doc['data']);
|
|
||||||
$this->assertEquals(7, $obj->id, 'The ID 7 should have been stored');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for array with auto UUID ID not provided')]
|
|
||||||
public function testInsertSucceedsForArrayWithAutoUuidIdNotProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::UUID;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'num_value' => 5]);
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 5)], TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertNotEmpty($doc->id, 'The ID should have been auto-generated');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for array with auto UUID ID with ID provided')]
|
|
||||||
public function testInsertSucceedsForArrayWithAutoUuidIdWithIdProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::UUID;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
$uuid = AutoId::generateUUID();
|
|
||||||
Document::insert(ThrowawayDb::TABLE, ['id' => $uuid, 'value' => 'uuid', 'num_value' => 12]);
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 12)], TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals($uuid, $doc->id, 'The ID should not have been changed');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for array with auto string ID not provided')]
|
|
||||||
public function testInsertSucceedsForArrayWithAutoStringIdNotProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::RandomString;
|
|
||||||
Configuration::$idStringLength = 6;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
Document::insert(ThrowawayDb::TABLE, ['id' => '', 'value' => 'new', 'num_value' => 8]);
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 8)], TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals(6, strlen($doc->id), 'The ID should have been auto-generated and had 6 characters');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
Configuration::$idStringLength = 16;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for array with auto string ID with ID provided')]
|
|
||||||
public function testInsertSucceedsForArrayWithAutoStringIdWithIdProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::RandomString;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
Document::insert(ThrowawayDb::TABLE, ['id' => 'my-key', 'value' => 'old', 'num_value' => 3]);
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 3)], TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals('my-key', $doc->id, 'The ID should not have been changed');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for object no auto ID')]
|
|
||||||
public function testInsertSucceedsForObjectNoAutoId(): void
|
|
||||||
{
|
{
|
||||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
|
Document::insert(ThrowawayDb::TABLE, new TestDocument('turkey', sub: new SubDocument('gobble', 'gobble')));
|
||||||
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
$doc = Find::byId(ThrowawayDb::TABLE, 'turkey', TestDocument::class);
|
||||||
@@ -160,105 +41,6 @@ class DocumentTest extends TestCase
|
|||||||
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
|
$this->assertEquals('gobble', $doc->sub->bar, 'The sub-document bar property was incorrect');
|
||||||
}
|
}
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for object with auto number ID not provided')]
|
|
||||||
public function testInsertSucceedsForObjectWithAutoNumberIdNotProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::Number;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
|
|
||||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'taco'));
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'taco')], NumDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals(1, $doc->id, 'The ID 1 should have been auto-generated');
|
|
||||||
|
|
||||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(value: 'burrito'));
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'burrito')], NumDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals(2, $doc->id, 'The ID 2 should have been auto-generated');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for object with auto number ID with ID provided')]
|
|
||||||
public function testInsertSucceedsForObjectWithAutoNumberIdWithIdProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::Number;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
Document::insert(ThrowawayDb::TABLE, new NumDocument(64, 'large'));
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('value', 'large')], NumDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals(64, $doc->id, 'The ID 64 should have been stored');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for object with auto UUID ID not provided')]
|
|
||||||
public function testInsertSucceedsForObjectWithAutoUuidIdNotProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::UUID;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(value: 'something', num_value: 9));
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EX('value')], TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertNotEmpty($doc->id, 'The ID should have been auto-generated');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for object with auto UUID ID with ID provided')]
|
|
||||||
public function testInsertSucceedsForObjectWithAutoUuidIdWithIdProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::UUID;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
$uuid = AutoId::generateUUID();
|
|
||||||
Document::insert(ThrowawayDb::TABLE, new TestDocument($uuid, num_value: 14));
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 14)], TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals($uuid, $doc->id, 'The ID should not have been changed');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for object with auto string ID not provided')]
|
|
||||||
public function testInsertSucceedsForObjectWithAutoStringIdNotProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::RandomString;
|
|
||||||
Configuration::$idStringLength = 40;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
Document::insert(ThrowawayDb::TABLE, new TestDocument(num_value: 55));
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 55)], TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals(40, strlen($doc->id), 'The ID should have been auto-generated and had 40 characters');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
Configuration::$idStringLength = 16;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds for object with auto string ID with ID provided')]
|
|
||||||
public function testInsertSucceedsForObjectWithAutoStringIdWithIdProvided(): void
|
|
||||||
{
|
|
||||||
Configuration::$autoId = AutoId::RandomString;
|
|
||||||
try {
|
|
||||||
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
|
|
||||||
Document::insert(ThrowawayDb::TABLE, new TestDocument('my-key', num_value: 3));
|
|
||||||
$doc = Find::firstByFields(ThrowawayDb::TABLE, [Field::EQ('num_value', 3)], TestDocument::class);
|
|
||||||
$this->assertNotFalse($doc, 'There should have been a document returned');
|
|
||||||
$this->assertEquals('my-key', $doc->id, 'The ID should not have been changed');
|
|
||||||
} finally {
|
|
||||||
Configuration::$autoId = AutoId::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testInsertFailsForDuplicateKey(): void
|
public function testInsertFailsForDuplicateKey(): void
|
||||||
{
|
{
|
||||||
$this->expectException(DocumentException::class);
|
$this->expectException(DocumentException::class);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Test\Unit;
|
namespace Test\Unit;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{AutoId, Configuration, DocumentException};
|
use BitBadger\PDODocument\{Configuration, DocumentException};
|
||||||
use PHPUnit\Framework\Attributes\TestDox;
|
use PHPUnit\Framework\Attributes\TestDox;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
@@ -29,18 +29,6 @@ class ConfigurationTest extends TestCase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[TestDox('Auto ID default succeeds')]
|
|
||||||
public function testAutoIdDefaultSucceeds(): void
|
|
||||||
{
|
|
||||||
$this->assertEquals(AutoId::None, Configuration::$autoId, 'Auto ID should default to None');
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('ID string length default succeeds')]
|
|
||||||
public function testIdStringLengthDefaultSucceeds(): void
|
|
||||||
{
|
|
||||||
$this->assertEquals(16, Configuration::$idStringLength, 'ID string length should default to 16');
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox("Db conn fails when no DSN specified")]
|
#[TestDox("Db conn fails when no DSN specified")]
|
||||||
public function testDbConnFailsWhenNoDSNSpecified(): void
|
public function testDbConnFailsWhenNoDSNSpecified(): void
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
<?php declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Test\Unit;
|
|
||||||
|
|
||||||
use BitBadger\PDODocument\FieldMatch;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unit tests for the FieldMatch enum
|
|
||||||
*/
|
|
||||||
class FieldMatchTest extends TestCase
|
|
||||||
{
|
|
||||||
public function testToStringSucceedsForAll(): void
|
|
||||||
{
|
|
||||||
$this->assertEquals('AND', FieldMatch::All->toString(), 'All should have returned AND');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testToStringSucceedsForAny(): void
|
|
||||||
{
|
|
||||||
$this->assertEquals('OR', FieldMatch::Any->toString(), 'Any should have returned OR');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Test\Unit\Query;
|
namespace Test\Unit\Query;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{Configuration, Field, FieldMatch, Mode};
|
use BitBadger\PDODocument\{Configuration, Field, Mode};
|
||||||
use BitBadger\PDODocument\Query\Find;
|
use BitBadger\PDODocument\Query\Find;
|
||||||
use PHPUnit\Framework\Attributes\TestDox;
|
use PHPUnit\Framework\Attributes\TestDox;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
@@ -32,8 +32,7 @@ class FindTest extends TestCase
|
|||||||
public function testByFieldsSucceeds(): void
|
public function testByFieldsSucceeds(): void
|
||||||
{
|
{
|
||||||
$this->assertEquals("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock",
|
$this->assertEquals("SELECT data FROM there WHERE data->>'active' = :act OR data->>'locked' = :lock",
|
||||||
Find::byFields('there', [Field::EQ('active', true, ':act'), Field::EQ('locked', true, ':lock')],
|
Find::byFields('there', [Field::EQ('active', true, ':act'), Field::EQ('locked', true, ':lock')], 'OR'),
|
||||||
FieldMatch::Any),
|
|
||||||
'SELECT query not generated correctly');
|
'SELECT query not generated correctly');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Test\Unit;
|
namespace Test\Unit;
|
||||||
|
|
||||||
use BitBadger\PDODocument\{AutoId, Configuration, DocumentException, Field, FieldMatch, Mode, Query};
|
use BitBadger\PDODocument\{Configuration, Field, Mode, Query};
|
||||||
use PHPUnit\Framework\Attributes\TestDox;
|
use PHPUnit\Framework\Attributes\TestDox;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
@@ -33,18 +33,17 @@ class QueryTest extends TestCase
|
|||||||
Query::whereByFields([Field::LE('test_field', '', ':it')]), 'WHERE fragment not constructed correctly');
|
Query::whereByFields([Field::LE('test_field', '', ':it')]), 'WHERE fragment not constructed correctly');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWhereByFieldsSucceedsForMultipleFieldsAll(): void
|
public function testWhereByFieldsSucceedsForMultipleFields(): void
|
||||||
{
|
{
|
||||||
$this->assertEquals("data->>'test_field' <= :it AND data->>'other_field' = :other",
|
$this->assertEquals("data->>'test_field' <= :it AND data->>'other_field' = :other",
|
||||||
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')]),
|
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')]),
|
||||||
'WHERE fragment not constructed correctly');
|
'WHERE fragment not constructed correctly');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWhereByFieldsSucceedsForMultipleFieldsAny(): void
|
public function testWhereByFieldsSucceedsForMultipleFieldsWithOr(): void
|
||||||
{
|
{
|
||||||
$this->assertEquals("data->>'test_field' <= :it OR data->>'other_field' = :other",
|
$this->assertEquals("data->>'test_field' <= :it OR data->>'other_field' = :other",
|
||||||
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')],
|
Query::whereByFields([Field::LE('test_field', '', ':it'), Field::EQ('other_field', '', ':other')], 'OR'),
|
||||||
FieldMatch::Any),
|
|
||||||
'WHERE fragment not constructed correctly');
|
'WHERE fragment not constructed correctly');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,125 +59,10 @@ class QueryTest extends TestCase
|
|||||||
$this->assertEquals("data->>'id' = :di", Query::whereById(':di'), 'WHERE fragment not constructed correctly');
|
$this->assertEquals("data->>'id' = :di", Query::whereById(':di'), 'WHERE fragment not constructed correctly');
|
||||||
}
|
}
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with no auto-ID for PostgreSQL')]
|
public function testInsertSucceeds(): void
|
||||||
public function testInsertSucceedsWithNoAutoIdForPostgreSQL(): void
|
|
||||||
{
|
{
|
||||||
Configuration::$mode = Mode::PgSQL;
|
$this->assertEquals('INSERT INTO my_table VALUES (:data)', Query::insert('my_table'),
|
||||||
try {
|
'INSERT statement not constructed correctly');
|
||||||
$this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'),
|
|
||||||
'INSERT statement not constructed correctly');
|
|
||||||
} finally {
|
|
||||||
Configuration::$mode = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with no auto-ID for SQLite')]
|
|
||||||
public function testInsertSucceedsWithNoAutoIdForSQLite(): void
|
|
||||||
{
|
|
||||||
Configuration::$mode = Mode::SQLite;
|
|
||||||
try {
|
|
||||||
$this->assertEquals('INSERT INTO test_tbl VALUES (:data)', Query::insert('test_tbl'),
|
|
||||||
'INSERT statement not constructed correctly');
|
|
||||||
} finally {
|
|
||||||
Configuration::$mode = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto numeric ID for PostgreSQL')]
|
|
||||||
public function testInsertSucceedsWithAutoNumericIdForPostgreSQL(): void
|
|
||||||
{
|
|
||||||
Configuration::$mode = Mode::PgSQL;
|
|
||||||
try {
|
|
||||||
$this->assertEquals(
|
|
||||||
"INSERT INTO test_tbl VALUES (:data || ('{\"id\":' "
|
|
||||||
. "|| (SELECT COALESCE(MAX(data->>'id'), 0) + 1 FROM test_tbl) || '}'))",
|
|
||||||
Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly');
|
|
||||||
} finally {
|
|
||||||
Configuration::$mode = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto numeric ID for SQLite')]
|
|
||||||
public function testInsertSucceedsWithAutoNumericIdForSQLite(): void
|
|
||||||
{
|
|
||||||
Configuration::$mode = Mode::SQLite;
|
|
||||||
try {
|
|
||||||
$this->assertEquals(
|
|
||||||
"INSERT INTO test_tbl VALUES (json_set(:data, '$.id', "
|
|
||||||
. "(SELECT coalesce(max(data->>'id'), 0) + 1 FROM test_tbl)))",
|
|
||||||
Query::insert('test_tbl', AutoId::Number), 'INSERT statement not constructed correctly');
|
|
||||||
} finally {
|
|
||||||
Configuration::$mode = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto UUID for PostgreSQL')]
|
|
||||||
public function testInsertSucceedsWithAutoUuidForPostgreSQL(): void
|
|
||||||
{
|
|
||||||
Configuration::$mode = Mode::PgSQL;
|
|
||||||
try {
|
|
||||||
$query = Query::insert('test_tbl', AutoId::UUID);
|
|
||||||
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", $query,
|
|
||||||
'INSERT statement not constructed correctly');
|
|
||||||
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
|
||||||
} finally {
|
|
||||||
Configuration::$mode = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto UUID for SQLite')]
|
|
||||||
public function testInsertSucceedsWithAutoUuidForSQLite(): void
|
|
||||||
{
|
|
||||||
Configuration::$mode = Mode::SQLite;
|
|
||||||
try {
|
|
||||||
$query = Query::insert('test_tbl', AutoId::UUID);
|
|
||||||
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query,
|
|
||||||
'INSERT statement not constructed correctly');
|
|
||||||
$this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly');
|
|
||||||
} finally {
|
|
||||||
Configuration::$mode = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto random string for PostgreSQL')]
|
|
||||||
public function testInsertSucceedsWithAutoRandomStringForPostgreSQL(): void
|
|
||||||
{
|
|
||||||
Configuration::$mode = Mode::PgSQL;
|
|
||||||
Configuration::$idStringLength = 8;
|
|
||||||
try {
|
|
||||||
$query = Query::insert('test_tbl', AutoId::RandomString);
|
|
||||||
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", $query,
|
|
||||||
'INSERT statement not constructed correctly');
|
|
||||||
$this->assertStringEndsWith("\"}')", $query, 'INSERT statement not constructed correctly');
|
|
||||||
$id = str_replace(["INSERT INTO test_tbl VALUES (:data || '{\"id\":\"", "\"}')"], '', $query);
|
|
||||||
$this->assertEquals(8, strlen($id), "Generated ID [$id] should have been 8 characters long");
|
|
||||||
} finally {
|
|
||||||
Configuration::$mode = null;
|
|
||||||
Configuration::$idStringLength = 16;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[TestDox('Insert succeeds with auto random string for SQLite')]
|
|
||||||
public function testInsertSucceedsWithAutoRandomStringForSQLite(): void
|
|
||||||
{
|
|
||||||
Configuration::$mode = Mode::SQLite;
|
|
||||||
try {
|
|
||||||
$query = Query::insert('test_tbl', AutoId::RandomString);
|
|
||||||
$this->assertStringStartsWith("INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", $query,
|
|
||||||
'INSERT statement not constructed correctly');
|
|
||||||
$this->assertStringEndsWith("'))", $query, 'INSERT statement not constructed correctly');
|
|
||||||
$id = str_replace(["INSERT INTO test_tbl VALUES (json_set(:data, '$.id', '", "'))"], '', $query);
|
|
||||||
$this->assertEquals(16, strlen($id), "Generated ID [$id] should have been 16 characters long");
|
|
||||||
} finally {
|
|
||||||
Configuration::$mode = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testInsertFailsWhenModeNotSet(): void
|
|
||||||
{
|
|
||||||
$this->expectException(DocumentException::class);
|
|
||||||
Configuration::$mode = null;
|
|
||||||
Query::insert('kaboom');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSaveSucceeds(): void
|
public function testSaveSucceeds(): void
|
||||||
|
|||||||
Reference in New Issue
Block a user