Derive mode from DSN function

- Add headers in all files
- Minor field name changes
This commit is contained in:
2024-07-20 21:47:21 -04:00
parent 1a37b009ea
commit d8330d828a
81 changed files with 1053 additions and 551 deletions

View File

@@ -1,4 +1,10 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
@@ -15,7 +21,7 @@ class CountTest extends TestCase
{
public function tearDown(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
parent::tearDown();
}
@@ -27,7 +33,7 @@ class CountTest extends TestCase
public function testByFieldsSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("SELECT COUNT(*) FROM somewhere WHERE data->>'errors' > :errors",
Count::byFields('somewhere', [Field::GT('errors', 10, ':errors')]),
'SELECT statement not generated correctly');
@@ -36,7 +42,7 @@ class CountTest extends TestCase
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('SELECT COUNT(*) FROM the_table WHERE data @> :criteria', Count::byContains('the_table'),
'SELECT statement not generated correctly');
}
@@ -51,7 +57,7 @@ class CountTest extends TestCase
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('SELECT COUNT(*) FROM a_table WHERE jsonb_path_exists(data, :path::jsonpath)',
Count::byJsonPath('a_table'), 'SELECT statement not generated correctly');
}

View File

@@ -1,4 +1,10 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
@@ -15,14 +21,14 @@ class DefinitionTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
parent::tearDown();
}
#[TestDox('Ensure table succeeds for PosgtreSQL')]
public function testEnsureTableSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('CREATE TABLE IF NOT EXISTS documents (data JSONB NOT NULL)',
Definition::ensureTable('documents'), 'CREATE TABLE statement not generated correctly');
}
@@ -30,7 +36,7 @@ class DefinitionTest extends TestCase
#[TestDox('Ensure table succeeds for SQLite')]
public function testEnsureTableSucceedsForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals('CREATE TABLE IF NOT EXISTS dox (data TEXT NOT NULL)', Definition::ensureTable('dox'),
'CREATE TABLE statement not generated correctly');
}
@@ -63,14 +69,14 @@ class DefinitionTest extends TestCase
public function testEnsureDocumentIndexOnSucceedsForSchemaAndFull(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_tbl_document ON my.tbl USING GIN (data)",
Definition::ensureDocumentIndexOn('my.tbl', DocumentIndex::Full));
}
public function testEnsureDocumentIndexOnSucceedsForNoSchemaAndOptimized(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals("CREATE INDEX IF NOT EXISTS idx_it_document ON it USING GIN (data jsonb_path_ops)",
Definition::ensureDocumentIndexOn('it', DocumentIndex::Optimized));
}

View File

@@ -1,4 +1,10 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
@@ -15,20 +21,20 @@ class DeleteTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
}
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("DELETE FROM over_there WHERE data->>'id' = :id", Delete::byId('over_there'),
'DELETE statement not constructed correctly');
}
public function testByFieldsSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("DELETE FROM my_table WHERE data->>'value' < :max AND data->>'value' >= :min",
Delete::byFields('my_table', [Field::LT('value', 99, ':max'), Field::GE('value', 18, ':min')]),
'DELETE statement not constructed correctly');
@@ -37,7 +43,7 @@ class DeleteTest extends TestCase
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('DELETE FROM somewhere WHERE data @> :criteria', Delete::byContains('somewhere'),
'DELETE statement not constructed correctly');
}
@@ -52,7 +58,7 @@ class DeleteTest extends TestCase
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('DELETE FROM here WHERE jsonb_path_exists(data, :path::jsonpath)',
Delete::byJsonPath('here'), 'DELETE statement not constructed correctly');
}

View File

@@ -1,4 +1,10 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
@@ -15,12 +21,12 @@ class ExistsTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
}
public function testQuerySucceeds(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM abc WHERE def)', Exists::query('abc', 'def'),
'Existence query not generated correctly');
}
@@ -28,14 +34,14 @@ class ExistsTest extends TestCase
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM dox WHERE data->>'id' = :id)", Exists::byId('dox'),
'Existence query not generated correctly');
}
public function testByFieldsSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("SELECT EXISTS (SELECT 1 FROM box WHERE data->>'status' <> :status)",
Exists::byFields('box', [Field::NE('status', 'occupied', ':status')]),
'Existence query not generated correctly');
@@ -44,7 +50,7 @@ class ExistsTest extends TestCase
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM pocket WHERE data @> :criteria)',
Exists::byContains('pocket'), 'Existence query not generated correctly');
}
@@ -59,7 +65,7 @@ class ExistsTest extends TestCase
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('SELECT EXISTS (SELECT 1 FROM lint WHERE jsonb_path_exists(data, :path::jsonpath))',
Exists::byJsonPath('lint'), 'Existence query not generated correctly');
}

View File

@@ -1,4 +1,10 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
@@ -15,20 +21,20 @@ class FindTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
}
#[TestDox('By ID succeeds')]
public function testByIdSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("SELECT data FROM here WHERE data->>'id' = :id", Find::byId('here'),
'SELECT query not generated correctly');
}
public function testByFieldsSucceeds(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$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')],
FieldMatch::Any),
@@ -38,7 +44,7 @@ class FindTest extends TestCase
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('SELECT data FROM disc WHERE data @> :criteria', Find::byContains('disc'),
'SELECT query not generated correctly');
}
@@ -53,7 +59,7 @@ class FindTest extends TestCase
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('SELECT data FROM light WHERE jsonb_path_exists(data, :path::jsonpath)',
Find::byJsonPath('light'), 'SELECT query not generated correctly');
}

View File

@@ -1,4 +1,10 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
@@ -15,13 +21,13 @@ class PatchTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
parent::tearDown();
}
#[TestDox('By ID succeeds for PostgreSQL')]
public function testByIdSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals("UPDATE doc_table SET data = data || :data WHERE data->>'id' = :id",
Patch::byId('doc_table'), 'Patch UPDATE statement is not correct');
}
@@ -29,7 +35,7 @@ class PatchTest extends TestCase
#[TestDox('By ID succeeds for SQLite')]
public function testByIdSucceedsForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("UPDATE my_table SET data = json_patch(data, json(:data)) WHERE data->>'id' = :id",
Patch::byId('my_table'), 'Patch UPDATE statement is not correct');
}
@@ -44,7 +50,7 @@ class PatchTest extends TestCase
#[TestDox('By fields succeeds for PostgreSQL')]
public function testByFieldsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals("UPDATE that SET data = data || :data WHERE (data->>'something')::numeric < :some",
Patch::byFields('that', [Field::LT('something', 17, ':some')]), 'Patch UPDATE statement is not correct');
}
@@ -52,7 +58,7 @@ class PatchTest extends TestCase
#[TestDox('By fields succeeds for SQLite')]
public function testByFieldsSucceedsForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals(
"UPDATE a_table SET data = json_patch(data, json(:data)) WHERE data->>'something' > :it",
Patch::byFields('a_table', [Field::GT('something', 17, ':it')]), 'Patch UPDATE statement is not correct');
@@ -67,7 +73,7 @@ class PatchTest extends TestCase
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('UPDATE this SET data = data || :data WHERE data @> :criteria', Patch::byContains('this'),
'Patch UPDATE statement is not correct');
}
@@ -82,7 +88,7 @@ class PatchTest extends TestCase
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('UPDATE that SET data = data || :data WHERE jsonb_path_exists(data, :path::jsonpath)',
Patch::byJsonPath('that'), 'Patch UPDATE statement is not correct');
}

View File

@@ -1,4 +1,10 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace Test\Unit\Query;
@@ -15,13 +21,13 @@ class RemoveFieldsTest extends TestCase
{
protected function tearDown(): void
{
Configuration::$mode = null;
Configuration::overrideMode(null);
}
#[TestDox('Update succeeds for PostgreSQL')]
public function testUpdateSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('UPDATE taco SET data = data - :names::text[] WHERE it = true',
RemoveFields::update('taco', [':names' => "{one,two}"], 'it = true'), 'UPDATE statement not correct');
}
@@ -29,7 +35,7 @@ class RemoveFieldsTest extends TestCase
#[TestDox('Update succeeds for SQLite')]
public function testUpdateSucceedsForSQLite(): void
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals('UPDATE burrito SET data = json_remove(data, :name0, :name1, :name2) WHERE a = b',
RemoveFields::update('burrito', Parameters::fieldNames(':name', ['one', 'two', 'ten']), 'a = b'),
'UPDATE statement not correct');
@@ -44,7 +50,7 @@ class RemoveFieldsTest extends TestCase
#[TestDox('By ID succeeds for PostgreSQL')]
public function testByIdSucceedsForPostgreSQL()
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id",
RemoveFields::byId('churro', Parameters::fieldNames(':bite', ['byte'])), 'UPDATE statement not correct');
}
@@ -52,7 +58,7 @@ class RemoveFieldsTest extends TestCase
#[TestDox('By ID succeeds for SQLite')]
public function testByIdSucceedsForSQLite()
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("UPDATE quesadilla SET data = json_remove(data, :bite0) WHERE data->>'id' = :id",
RemoveFields::byId('quesadilla', Parameters::fieldNames(':bite', ['byte'])),
'UPDATE statement not correct');
@@ -68,7 +74,7 @@ class RemoveFieldsTest extends TestCase
#[TestDox('By fields succeeds for PostgreSQL')]
public function testByFieldsSucceedsForPostgreSQL()
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals("UPDATE enchilada SET data = data - :sauce::text[] WHERE data->>'cheese' = :queso",
RemoveFields::byFields('enchilada', [Field::EQ('cheese', 'jack', ':queso')],
Parameters::fieldNames(':sauce', ['white'])),
@@ -78,7 +84,7 @@ class RemoveFieldsTest extends TestCase
#[TestDox('By fields succeeds for SQLite')]
public function testByFieldsSucceedsForSQLite()
{
Configuration::$mode = Mode::SQLite;
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals(
"UPDATE chimichanga SET data = json_remove(data, :filling0) WHERE data->>'side' = :rice",
RemoveFields::byFields('chimichanga', [Field::EQ('side', 'beans', ':rice')],
@@ -95,7 +101,7 @@ class RemoveFieldsTest extends TestCase
#[TestDox('By contains succeeds for PostgreSQL')]
public function testByContainsSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals('UPDATE food SET data = data - :drink::text[] WHERE data @> :criteria',
RemoveFields::byContains('food', Parameters::fieldNames(':drink', ['a', 'b'])),
'UPDATE statement not correct');
@@ -111,7 +117,7 @@ class RemoveFieldsTest extends TestCase
#[TestDox('By JSON Path succeeds for PostgreSQL')]
public function testByJsonPathSucceedsForPostgreSQL(): void
{
Configuration::$mode = Mode::PgSQL;
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals(
'UPDATE dessert SET data = data - :cake::text[] WHERE jsonb_path_exists(data, :path::jsonpath)',
RemoveFields::byJsonPath('dessert', Parameters::fieldNames(':cake', ['b', 'c'])),