diff --git a/docs/db.md b/docs/db.md index c7a78fd..2853098 100644 --- a/docs/db.md +++ b/docs/db.md @@ -485,7 +485,7 @@ dump($cnt); // (integer) 7 ```php $departmentsQuery = Forrest79\PhPgSql\Db\Sql\DefaultQuery::createArgs('SELECT id FROM departments WHERE id = ?', [1]); -$query = $departmentsQuery->createDbQuery(); +$query = $departmentsQuery->toDbQuery(); dump($query->sql); // (string) 'SELECT id FROM departments WHERE id = $1' dump($query->params); // (array) [1] diff --git a/docs/fluent.md b/docs/fluent.md index 0a112db..101be7a 100644 --- a/docs/fluent.md +++ b/docs/fluent.md @@ -23,7 +23,7 @@ $query = $fluent ->select(['*']) ->from('users') ->where('id', 1) - ->createDbQuery(); + ->toDbQuery(); dump($query->sql); // (string) 'SELECT * FROM users WHERE id = $1' dump($query->params); // (array) [1] diff --git a/phpcs-ignores.neon b/phpcs-ignores.neon index e890988..c9e79ba 100644 --- a/phpcs-ignores.neon +++ b/phpcs-ignores.neon @@ -301,7 +301,7 @@ ignoreErrors: - sniff: Squiz.Scope.MethodScope.Missing - message: Visibility must be declared on method "createDbQuery" + message: Visibility must be declared on method "toDbQuery" count: 1 path: src/Db/Sql/Query.php diff --git a/src/Db/Sql/DefaultQuery.php b/src/Db/Sql/DefaultQuery.php index 0808c6a..640f756 100644 --- a/src/Db/Sql/DefaultQuery.php +++ b/src/Db/Sql/DefaultQuery.php @@ -9,7 +9,7 @@ class DefaultQuery extends Expression implements Query private Db\Query|NULL $dbQuery = NULL; - public function createDbQuery(): Db\Query + public function toDbQuery(): Db\Query { if ($this->dbQuery === NULL) { $this->dbQuery = Db\SqlDefinition::createQuery($this->getSqlDefinition()); diff --git a/src/Db/Sql/Query.php b/src/Db/Sql/Query.php index 86c344a..bc9116d 100644 --- a/src/Db/Sql/Query.php +++ b/src/Db/Sql/Query.php @@ -7,6 +7,6 @@ interface Query extends Db\Sql { - function createDbQuery(): Db\Query; + function toDbQuery(): Db\Query; } diff --git a/src/Fluent/Query.php b/src/Fluent/Query.php index 2413267..e966fa3 100644 --- a/src/Fluent/Query.php +++ b/src/Fluent/Query.php @@ -962,7 +962,7 @@ public function getSqlDefinition(): Db\SqlDefinition } - public function createDbQuery(): Db\Query + public function toDbQuery(): Db\Query { if ($this->dbQuery === NULL) { $this->dbQuery = Db\SqlDefinition::createQuery($this->getSqlDefinition()); diff --git a/tests/Integration/DocsTest.php b/tests/Integration/DocsTest.php index 5e895ed..fefb2e2 100644 --- a/tests/Integration/DocsTest.php +++ b/tests/Integration/DocsTest.php @@ -148,7 +148,7 @@ public static function dump(mixed $var): string return \sprintf('(%s) [%s]', $type, \implode(', ', $isList ? $list : $array)); } else if ($var instanceof Fluent\Query) { - $query = $var->createDbQuery(); + $query = $var->toDbQuery(); return '(Query) ' . $query->sql . ($query->params === [] ? '' : \sprintf(' [Params: %s]', self::dump($query->params))); } diff --git a/tests/Unit/FluentConnectionTest.php b/tests/Unit/FluentConnectionTest.php index 2fe0506..93c202a 100644 --- a/tests/Unit/FluentConnectionTest.php +++ b/tests/Unit/FluentConnectionTest.php @@ -30,7 +30,7 @@ public function testCreateQuery(): void ->createQuery() ->select(['column']) ->from('table') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table', $query->sql); Tester\Assert::same([], $query->params); @@ -58,7 +58,7 @@ protected function prepareSql(string $sql, array $params): Db\SqlDefinition ->createQuery() ->select(['column']) ->from('table') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT custom_column FROM $1', $query->sql); Tester\Assert::same(['custom_table'], $query->params); diff --git a/tests/Unit/FluentQueryTest.php b/tests/Unit/FluentQueryTest.php index a19023a..b0190c8 100644 --- a/tests/Unit/FluentQueryTest.php +++ b/tests/Unit/FluentQueryTest.php @@ -27,7 +27,7 @@ public function testSelect(): void ->orderBy('column') ->limit(10) ->offset(20) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT DISTINCT column FROM table AS t WHERE (column = $1) AND (text IS NULL) GROUP BY column ORDER BY column LIMIT $2 OFFSET $3', $query->sql); Tester\Assert::same([100, 10, 20], $query->params); @@ -41,7 +41,7 @@ public function testSelectDistinctOn(): void ->distinctOn('t.column') ->from('table', 't') ->where('t.column', 100) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT DISTINCT ON (t.column) t.column FROM table AS t WHERE t.column = $1', $query->sql); Tester\Assert::same([100], $query->params); @@ -55,7 +55,7 @@ public function testSelectCombineDistinctAndDistinctOn(): void ->select(['t.column']) ->distinct() ->distinctOn('t.column') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::CANT_COMBINE_DISTINCT_AND_DISTINCT_ON); } @@ -64,7 +64,7 @@ public function testSelectWithFluentQuery(): void { $query = $this->query() ->select(['column' => $this->query()->select([1])]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT (SELECT 1) AS "column"', $query->sql); Tester\Assert::same([], $query->params); @@ -75,7 +75,7 @@ public function testSelectWithQuery(): void { $query = $this->query() ->select(['column' => Db\Sql\DefaultQuery::create('SELECT 1')]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT (SELECT 1) AS "column"', $query->sql); Tester\Assert::same([], $query->params); @@ -86,7 +86,7 @@ public function testSelectColumnAliases(): void { $query = $this->query() ->select(['column' => 'another', 'next', 10 => 'column_with_integer_key', '1' => 'column_with_integer_in_string_key', 'a' => 'b']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT another AS "column", next, column_with_integer_key, column_with_integer_in_string_key, b AS "a"', $query->sql); Tester\Assert::same([], $query->params); @@ -97,7 +97,7 @@ public function testSelectBoolNull(): void { $query = $this->query() ->select(['is_true' => TRUE, 'is_false' => FALSE, 'is_null' => NULL]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT TRUE AS "is_true", FALSE AS "is_false", NULL AS "is_null"', $query->sql); Tester\Assert::same([], $query->params); @@ -108,7 +108,7 @@ public function testSelectEnum(): void { $query = $this->query() ->select([Tests\TestEnum::One, 'column' => Tests\TestEnum::One]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT 1, 1 AS "column"', $query->sql); Tester\Assert::same([], $query->params); @@ -120,7 +120,7 @@ public function testFromWithFluentQuery(): void $query = $this->query() ->select(['x.column']) ->from($this->query()->select(['column' => 1]), 'x') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM (SELECT 1 AS "column") AS x', $query->sql); Tester\Assert::same([], $query->params); @@ -132,7 +132,7 @@ public function testFromWithQuery(): void $query = $this->query() ->select(['x.column']) ->from(Db\Sql\DefaultQuery::create('SELECT 1 AS column'), 'x') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM (SELECT 1 AS column) AS x', $query->sql); Tester\Assert::same([], $query->params); @@ -144,7 +144,7 @@ public function testFromWithParameter(): void $query = $this->query() ->select(['gs']) ->from(Db\Sql\Expression::create('generate_series(?::integer, ?::integer, ?::integer)', 2, 1, -1), 'gs') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT gs FROM generate_series($1::integer, $2::integer, $3::integer) AS gs', $query->sql); Tester\Assert::same([2, 1, -1], $query->params); @@ -157,7 +157,7 @@ public function testMoreFrom(): void ->select(['column']) ->from('table1', 't1') ->from('table2', 't2') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table1 AS t1, table2 AS t2', $query->sql); Tester\Assert::same([], $query->params); @@ -170,7 +170,7 @@ public function testWhereSimple(): void ->select(['x.column']) ->from('table', 't') ->where('x.column = t.id') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE x.column = t.id', $query->sql); Tester\Assert::same([], $query->params); @@ -183,7 +183,7 @@ public function testWhereParameters(): void ->select(['x.column']) ->from('table', 't') ->where('x.column', 1) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE x.column = $1', $query->sql); Tester\Assert::same([1], $query->params); @@ -200,7 +200,7 @@ public function testWhereWithCondition(): void ->select(['x.column']) ->from('table', 't') ->where($condition) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE (x.column = t.id) AND (x.id IN ($1, $2))', $query->sql); Tester\Assert::same([1, 2], $query->params); @@ -213,7 +213,7 @@ public function testWhereWithSql(): void ->select(['x.column']) ->from('table', 't') ->where(Db\Sql\Expression::create('x.id', [1, 2])) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE x.id IN ($1, $2)', $query->sql); Tester\Assert::same([1, 2], $query->params); @@ -234,7 +234,7 @@ public function testWhereIf(): void ->select(['x.column']) ->from('table', 't') ->whereIf(TRUE, 'x.column = t.id') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE x.column = t.id', $queryWithTrueIfCondition->sql); Tester\Assert::same([], $queryWithTrueIfCondition->params); @@ -243,7 +243,7 @@ public function testWhereIf(): void ->select(['x.column']) ->from('table', 't') ->whereIf(FALSE, 'x.column = t.id') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t', $queryWithFalseIfCondition->sql); Tester\Assert::same([], $queryWithFalseIfCondition->params); @@ -266,7 +266,7 @@ public function testWhereAnd(): void Db\Sql\Expression::create('x.id', 7), ]) ->query() - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE (x.column = t.id) AND (x.column = $1) AND ((x.type = t.id) AND (x.test IN ($2, $3))) AND (x.id = $4)', $query->sql); Tester\Assert::same([1, 3, 5, 7], $query->params); @@ -288,7 +288,7 @@ public function testWhereAndContinue(): void ->add(Fluent\Condition::createAnd()->add('x.type = t.id')->add('x.test', [3, 5])) ->add('x.column', 1); - $query = $sourceQuery->createDbQuery(); + $query = $sourceQuery->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE (x.column = t.id) AND (x.id = $1) AND ((x.type = t.id) AND (x.test IN ($2, $3))) AND (x.column = $4)', $query->sql); Tester\Assert::same([7, 3, 5, 1], $query->params); @@ -311,7 +311,7 @@ public function testWhereOr(): void Db\Sql\Expression::create('x.id', 7), ]) ->query() - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE (x.column = t.id) OR (x.column = $1) OR ((x.type = t.id) AND (x.test IN ($2, $3))) OR (x.id = $4)', $query->sql); Tester\Assert::same([1, 3, 5, 7], $query->params); @@ -324,7 +324,7 @@ public function testHavingSimple(): void ->select(['x.column']) ->from('table', 't') ->having('x.column = t.id') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING x.column = t.id', $query->sql); Tester\Assert::same([], $query->params); @@ -337,7 +337,7 @@ public function testHavingParameters(): void ->select(['x.column']) ->from('table', 't') ->having('x.column', 1) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING x.column = $1', $query->sql); Tester\Assert::same([1], $query->params); @@ -354,7 +354,7 @@ public function testHavingWithCondition(): void ->select(['x.column']) ->from('table', 't') ->having($condition) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING (x.column = t.id) AND (x.id IN ($1, $2))', $query->sql); Tester\Assert::same([1, 2], $query->params); @@ -367,7 +367,7 @@ public function testHavingWithSql(): void ->select(['x.column']) ->from('table', 't') ->having(Db\Sql\Expression::create('x.id', [1, 2])) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING x.id IN ($1, $2)', $query->sql); Tester\Assert::same([1, 2], $query->params); @@ -398,7 +398,7 @@ public function testHavingAnd(): void Db\Sql\Expression::create('x.id', 7), ]) ->query() - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING (x.column = t.id) AND (x.column = $1) AND ((x.type = t.id) AND (x.test IN ($2, $3))) AND (x.id = $4)', $query->sql); Tester\Assert::same([1, 3, 5, 7], $query->params); @@ -421,7 +421,7 @@ public function testHavingOr(): void Db\Sql\Expression::create('x.id', 7), ]) ->query() - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING (x.column = t.id) OR (x.column = $1) OR ((x.type = t.id) AND (x.test IN ($2, $3))) OR (x.id = $4)', $query->sql); Tester\Assert::same([1, 3, 5, 7], $query->params); @@ -444,7 +444,7 @@ public function testHavingOrContinue(): void ->add('x.column', 1); $query = $sourceQuery - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING (x.column = t.id) OR (x.id = $1) OR ((x.type = t.id) AND (x.test IN ($2, $3))) OR (x.column = $4)', $query->sql); Tester\Assert::same([7, 3, 5, 1], $query->params); @@ -457,7 +457,7 @@ public function testJoinWithFluentQuery(): void ->select(['x.column']) ->from('table', 't') ->join($this->query()->select(['column' => 1]), 'x', 'x.column = t.id') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN (SELECT 1 AS "column") AS x ON x.column = t.id', $query->sql); Tester\Assert::same([], $query->params); @@ -470,7 +470,7 @@ public function testJoinWithQuery(): void ->select(['x.column']) ->from('table', 't') ->join(Db\Sql\DefaultQuery::create('SELECT 1 AS column'), 'x', 'x.column = t.id') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN (SELECT 1 AS column) AS x ON x.column = t.id', $query->sql); Tester\Assert::same([], $query->params); @@ -483,7 +483,7 @@ public function testJoinWithStringOn(): void ->select(['x.column']) ->from('table', 't') ->join('another', 'x', 'x.column = t.id') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN another AS x ON x.column = t.id', $query->sql); Tester\Assert::same([], $query->params); @@ -500,7 +500,7 @@ public function testJoinWithConditionOn(): void ->select(['x.column']) ->from('table', 't') ->join('another', 'x', $conditionOn) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN another AS x ON (x.column = t.id) AND (x.id IN ($1, $2))', $query->sql); Tester\Assert::same([1, 2], $query->params); @@ -513,7 +513,7 @@ public function testJoinWithSqlOn(): void ->select(['x.column']) ->from('table', 't') ->join('another', 'x', Db\Sql\Expression::create('(x.column = t.id) AND (x.id = ?)', 2)) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN another AS x ON (x.column = t.id) AND (x.id = $1)', $query->sql); Tester\Assert::same([2], $query->params); @@ -534,7 +534,7 @@ public function testJoinWithAddOn(): void ->on('x', 'x.id = ?', 2) ->on('x', Db\Sql\Expression::create('x.type_id = ?', 'test')) ->on('x', $conditionOn) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN another AS x ON (x.column = t.id) AND (x.id = 1) AND (x.id = $1) AND (x.type_id = $2) AND ((x.condition_id = t.id) OR (x.condition_id IN ($3, $4)))', $query->sql); Tester\Assert::same([2, 'test', 3, 4], $query->params); @@ -556,7 +556,7 @@ public function testJoinNoOn(): void ->select(['x.column']) ->from('table', 't') ->join('another', 'x') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_ON_CONDITION); } @@ -568,7 +568,7 @@ public function testLateralFrom(): void ->from('table1', 't1') ->from($this->query()->select(['column2'])->from('table2'), 't2') ->lateral('t2') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT t1.column1, t2.column2 FROM table1 AS t1, LATERAL (SELECT column2 FROM table2) AS t2', $query->sql); Tester\Assert::same([], $query->params); @@ -582,7 +582,7 @@ public function testLateralJoinWithSqlQuery(): void ->from('table1', 't1') ->join(Db\Sql\DefaultQuery::create('SELECT column FROM table2'), 't2', 't2.column = t1.column') ->lateral('t2') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT t1.column AS "column1", t2.column AS "column2" FROM table1 AS t1 INNER JOIN LATERAL (SELECT column FROM table2) AS t2 ON t2.column = t1.column', $query->sql); Tester\Assert::same([], $query->params); @@ -596,7 +596,7 @@ public function testLateralJoinWithFluentQuery(): void ->from('table1', 't1') ->join($this->query()->select(['column'])->from('table2'), 't2', 't2.column = t1.column') ->lateral('t2') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT t1.column AS "column1", t2.column AS "column2" FROM table1 AS t1 INNER JOIN LATERAL (SELECT column FROM table2) AS t2 ON t2.column = t1.column', $query->sql); Tester\Assert::same([], $query->params); @@ -609,7 +609,7 @@ public function testSelectCombine(): void ->from('table', 't') ->select(['column']) ->union('SELECT column FROM table2 AS t2') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('(SELECT column FROM table AS t) UNION (SELECT column FROM table2 AS t2)', $query->sql); Tester\Assert::same([], $query->params); @@ -626,7 +626,7 @@ public function testSelectCombineFluent(): void ->select(['column']) ->from('table2', 't2'), ) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('(SELECT column FROM table AS t) UNION (SELECT column FROM table2 AS t2)', $query->sql); Tester\Assert::same([], $query->params); @@ -639,7 +639,7 @@ public function testSelectCombineQuery(): void ->from('table', 't') ->select(['column']) ->union(Db\Sql\DefaultQuery::create('SELECT column FROM table2 AS t2')) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('(SELECT column FROM table AS t) UNION (SELECT column FROM table2 AS t2)', $query->sql); Tester\Assert::same([], $query->params); @@ -651,7 +651,7 @@ public function testSelectNoColumns(): void Tester\Assert::exception(function (): void { $this->query() ->from('table') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_COLUMNS_TO_SELECT); } @@ -662,7 +662,7 @@ public function testOrderByFluent(): void ->select(['column']) ->from('table', 't') ->orderBy($this->query()->select(['sort_by_value(column)'])) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table AS t ORDER BY (SELECT sort_by_value(column))', $query->sql); Tester\Assert::same([], $query->params); @@ -675,7 +675,7 @@ public function testOrderByQuery(): void ->select(['column']) ->from('table', 't') ->orderBy(Db\Sql\Expression::create('sort_by_value(column)')) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table AS t ORDER BY sort_by_value(column)', $query->sql); Tester\Assert::same([], $query->params); @@ -693,7 +693,7 @@ public function testInsertRow(): void ]) ->insert('table') ->returning(['column']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (column, column_from, column_fluent_query, column_query) VALUES($1, 3, (SELECT \'test_fluent\' WHERE 4 = $2), (SELECT \'test\' WHERE 5 = $3)) RETURNING column', $query->sql); Tester\Assert::same([1, 4, 5], $query->params); @@ -712,7 +712,7 @@ public function testInsertMergeData(): void 'column2' => 2, 'column3' => 1, ]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (column2, column3, column1) VALUES($1, $2, $3)', $query->sql); Tester\Assert::same([2, 1, 3], $query->params); @@ -732,7 +732,7 @@ public function testInsertRows(): void ]) ->insert('table') ->returning(['column']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (column) VALUES($1), ($2), ($3), (4), ((SELECT \'test_fluent\' WHERE 6 = $4)), ((SELECT \'test\' WHERE 7 = $5)) RETURNING column', $query->sql); Tester\Assert::same([1, 2, 3, 6, 7], $query->params); @@ -751,7 +751,7 @@ public function testInsertRowsMergeData(): void ['column' => 4], ]) ->insert('table') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (column) VALUES($1), ($2), ($3), ($4)', $query->sql); Tester\Assert::same([1, 2, 3, 4], $query->params); @@ -765,7 +765,7 @@ public function testInsertSelect(): void ->select(['column']) ->from('table2', 't2') ->returning(['name']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name) SELECT column FROM table2 AS t2 RETURNING name', $query->sql); Tester\Assert::same([], $query->params); @@ -779,7 +779,7 @@ public function testInsertSelectDetectColumnsFromSelect(): void ->select(['column', 'name' => 'column2']) ->from('table2', 't2') ->returning(['column']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (column, name) SELECT column, column2 AS "name" FROM table2 AS t2 RETURNING column', $query->sql); Tester\Assert::same([], $query->params); @@ -792,7 +792,7 @@ public function testInsertSelectAllColumns(): void ->insert('table1') ->select(['*']) ->from('table2') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table1 SELECT * FROM table2', $query->sql); Tester\Assert::same([], $query->params); @@ -806,7 +806,7 @@ public function testInsertSelectNoColumn(): void ->insert('table1') ->select([]) ->from('table2') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_DATA_TO_INSERT); } @@ -818,7 +818,7 @@ public function testInsertSelectMissingColumnAlias(): void ->insert('table1') ->select([1]) ->from('table2') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::MISSING_COLUMN_ALIAS); Tester\Assert::exception(function (): void { @@ -826,7 +826,7 @@ public function testInsertSelectMissingColumnAlias(): void ->insert('table1') ->select([Tests\TestEnum::One]) ->from('table2') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::MISSING_COLUMN_ALIAS); } @@ -841,7 +841,7 @@ public function testInsertOnConflictDoUpdate(): void ]) ->onConflict(['name']) ->doUpdate(['info']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name, info) VALUES($1, $2) ON CONFLICT (name) DO UPDATE SET info = EXCLUDED.info', $query->sql); Tester\Assert::same(['Bob', 'Text'], $query->params); @@ -859,7 +859,7 @@ public function testInsertOnConflictWithWhereDoUpdate(): void ]) ->onConflict(['name', 'age'], Fluent\Condition::createAnd()->add('age < ?', 30)) ->doUpdate(['info']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name, age, info) VALUES($1, $2, $3) ON CONFLICT (name, age) WHERE age < $4 DO UPDATE SET info = EXCLUDED.info', $query->sql); Tester\Assert::same(['Bob', 20, 'Text', 30], $query->params); @@ -877,7 +877,7 @@ public function testInsertOnConflictConstraintDoUpdate(): void ]) ->onConflict('name_ukey') ->doUpdate(['info']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name, age, info) VALUES($1, $2, $3) ON CONFLICT ON CONSTRAINT name_ukey DO UPDATE SET info = EXCLUDED.info', $query->sql); Tester\Assert::same(['Bob', 20, 'Text'], $query->params); @@ -896,7 +896,7 @@ public function testInsertOnConflictConstraintWithWhereDoUpdate(): void ]) ->onConflict('name_ukey', Fluent\Condition::createAnd()->add('age < ?', 30)) ->doUpdate(['info']) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryException::class, NULL, Fluent\Exceptions\QueryException::ON_CONFLICT_WHERE_NOT_FOR_CONSTRAINT); } @@ -911,7 +911,7 @@ public function testInsertOnConflictDoUpdateWithConditionSet(): void ]) ->onConflict(['name']) ->doUpdate(['info', 'name' => 'EXCLUDED.name || t.age']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table AS t (name, info) VALUES($1, $2) ON CONFLICT (name) DO UPDATE SET info = EXCLUDED.info, name = EXCLUDED.name || t.age', $query->sql); Tester\Assert::same(['Bob', 'Text'], $query->params); @@ -928,7 +928,7 @@ public function testInsertOnConflictDoUpdateWithExpressionSet(): void ]) ->onConflict(['name']) ->doUpdate(['info', 'name' => Db\Sql\Expression::create('EXCLUDED.name || ?', 'Jimmy')]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name, info) VALUES($1, $2) ON CONFLICT (name) DO UPDATE SET info = EXCLUDED.info, name = EXCLUDED.name || $3', $query->sql); Tester\Assert::same(['Bob', 'Text', 'Jimmy'], $query->params); @@ -946,7 +946,7 @@ public function testInsertOnConflictDoUpdateWithWhere(): void ]) ->onConflict(['name', 'age']) ->doUpdate(['info'], Fluent\Condition::createAnd()->add('age < ?', 30)) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name, age, info) VALUES($1, $2, $3) ON CONFLICT (name, age) DO UPDATE SET info = EXCLUDED.info WHERE age < $4', $query->sql); Tester\Assert::same(['Bob', 20, 'Text', 30], $query->params); @@ -963,7 +963,7 @@ public function testInsertOnConflictDoNothing(): void ]) ->onConflict() ->doNothing() - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name, info) VALUES($1, $2) ON CONFLICT DO NOTHING', $query->sql); Tester\Assert::same(['Bob', 'Text'], $query->params); @@ -981,7 +981,7 @@ public function testInsertOnConflictWithReturning(): void ->onConflict(['name']) ->doUpdate(['info']) ->returning(['id']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name, info) VALUES($1, $2) ON CONFLICT (name) DO UPDATE SET info = EXCLUDED.info RETURNING id', $query->sql); Tester\Assert::same(['Bob', 'Text'], $query->params); @@ -999,7 +999,7 @@ public function testInsertOnConflictDoUpdateWithoutOnConflictDefinition(): void 'info' => 'Text', ]) ->doUpdate(['info']) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::ON_CONFLICT_DO_WITHOUT_DEFINITION); } @@ -1015,7 +1015,7 @@ public function testInsertOnConflictWithoutDoStatement(): void 'info' => 'Text', ]) ->onConflict(['name']) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::ON_CONFLICT_NO_DO); } @@ -1032,7 +1032,7 @@ public function testInsertOnConflictDoUpdateWithExpressionAsSimpleColumn(): void ]) ->onConflict(['name']) ->doUpdate([Db\Sql\Expression::create('EXCLUDED.name || ?', 'Jimmy')]) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::ON_CONFLICT_DO_UPDATE_SET_SINGLE_COLUMN_CAN_BE_ONLY_STRING); } @@ -1044,7 +1044,7 @@ public function testInsertSelectAllColumnsWithConcrete(): void ->insert('table1') ->select(['*', 'id']) ->from('table2') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::SELECT_ALL_COLUMNS_CANT_BE_COMBINED_WITH_CONCRETE_COLUMN_FOR_INSERT_SELECT_WITH_COLUMN_DETECTION); } @@ -1057,7 +1057,7 @@ public function testInsertRowWithArray(): void ->values([ 'column' => [1, 2], ]) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::DATA_CANT_CONTAIN_ARRAY); } @@ -1070,7 +1070,7 @@ public function testInsertRowsWithArray(): void ->rows([ ['column' => [1, 2]], ]) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::DATA_CANT_CONTAIN_ARRAY); } @@ -1080,7 +1080,7 @@ public function testInsertNoData(): void Tester\Assert::exception(function (): void { $this->query() ->insert('table') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_DATA_TO_INSERT); } @@ -1098,7 +1098,7 @@ public function testUpdate(): void ->from('table2', 't2') ->where('t2.column', 100) ->returning(['t.column']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('UPDATE table AS t SET column = $1, column_from = t2.id, column_fluent_query = (SELECT \'test_fluent\' WHERE 2 = $2), column_query = (SELECT \'test\' WHERE 3 = $3) FROM table2 AS t2 WHERE t2.column = $4 RETURNING t.column', $query->sql); Tester\Assert::same([1, 2, 3, 100], $query->params); @@ -1117,7 +1117,7 @@ public function testUpdateMergeData(): void 'column2' => 2, 'column3' => 1, ]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('UPDATE table AS t SET column2 = $1, column3 = $2, column1 = $3', $query->sql); Tester\Assert::same([2, 1, 3], $query->params); @@ -1132,7 +1132,7 @@ public function testUpdateWithArray(): void ->set([ 'column1' => [1, 2], ]) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::DATA_CANT_CONTAIN_ARRAY); } @@ -1142,7 +1142,7 @@ public function testUpdateNoData(): void Tester\Assert::exception(function (): void { $this->query() ->update('table') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_DATA_TO_UPDATE); } @@ -1153,7 +1153,7 @@ public function testNoMainTable(): void $this->query() ->update() ->set(['column' => 1]) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_MAIN_TABLE); } @@ -1164,7 +1164,7 @@ public function testDelete(): void ->delete('table', 't') ->where('column', 100) ->returning(['c' => 'column']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('DELETE FROM table AS t WHERE column = $1 RETURNING column AS "c"', $query->sql); Tester\Assert::same([100], $query->params); @@ -1177,7 +1177,7 @@ public function testReturningFluentQuery(): void ->delete('table', 't') ->where('column', 100) ->returning(['c' => $this->query()->select(['to_value(column)'])]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('DELETE FROM table AS t WHERE column = $1 RETURNING (SELECT to_value(column)) AS "c"', $query->sql); Tester\Assert::same([100], $query->params); @@ -1190,7 +1190,7 @@ public function testReturningQuery(): void ->delete('table', 't') ->where('column', 100) ->returning(['c' => Db\Sql\Expression::create('to_value(column)')]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('DELETE FROM table AS t WHERE column = $1 RETURNING (to_value(column)) AS "c"', $query->sql); Tester\Assert::same([100], $query->params); @@ -1204,7 +1204,7 @@ public function testMerge(): void ->using('recent_transactions', 't', 't.customer_id = ca.customer_id') ->whenMatched('UPDATE SET balance = balance + transaction_value') ->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('MERGE INTO customer_account AS ca USING recent_transactions AS t ON t.customer_id = ca.customer_id WHEN MATCHED THEN UPDATE SET balance = balance + transaction_value WHEN NOT MATCHED THEN INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)', $query->sql); Tester\Assert::same([], $query->params); @@ -1218,7 +1218,7 @@ public function testMergeUsingFluentQuery(): void ->using($this->query()->select(['customer_id', 'transaction_value'])->from('recent_transactions')->where('customer_id > ?', 10), 't', 't.customer_id = ca.customer_id') ->whenMatched('UPDATE SET balance = balance + transaction_value') ->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('MERGE INTO customer_account AS ca USING (SELECT customer_id, transaction_value FROM recent_transactions WHERE customer_id > $1) AS t ON t.customer_id = ca.customer_id WHEN MATCHED THEN UPDATE SET balance = balance + transaction_value WHEN NOT MATCHED THEN INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)', $query->sql); Tester\Assert::same([10], $query->params); @@ -1232,7 +1232,7 @@ public function testMergeUsingSql(): void ->using(Db\Sql\DefaultQuery::create('SELECT customer_id, transaction_value FROM recent_transactions WHERE customer_id > ?', 10), 't', 't.customer_id = ca.customer_id') ->whenMatched('UPDATE SET balance = balance + transaction_value') ->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('MERGE INTO customer_account AS ca USING (SELECT customer_id, transaction_value FROM recent_transactions WHERE customer_id > $1) AS t ON t.customer_id = ca.customer_id WHEN MATCHED THEN UPDATE SET balance = balance + transaction_value WHEN NOT MATCHED THEN INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)', $query->sql); Tester\Assert::same([10], $query->params); @@ -1246,7 +1246,7 @@ public function testMergeOnCondition(): void ->using('(SELECT customer_id, transaction_value FROM recent_transactions)', 't', Fluent\Condition::createAnd()->add('t.customer_id = ca.customer_id')->add('t.customer_id > ?', 10)) ->whenMatched('UPDATE SET balance = balance + transaction_value') ->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('MERGE INTO customer_account AS ca USING (SELECT customer_id, transaction_value FROM recent_transactions) AS t ON (t.customer_id = ca.customer_id) AND (t.customer_id > $1) WHEN MATCHED THEN UPDATE SET balance = balance + transaction_value WHEN NOT MATCHED THEN INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)', $query->sql); Tester\Assert::same([10], $query->params); @@ -1262,7 +1262,7 @@ public function testMergeWhenOn(): void ->whenMatched('UPDATE SET stock = w.stock + s.stock_delta', Fluent\Condition::createAnd()->add('w.stock + s.stock_delta > ?', 0)) ->whenMatched('UPDATE SET stock = w.stock - s.stock_delta', Db\Sql\Expression::create('w.stock + s.stock_delta < ?', 0)) ->whenMatched('DELETE') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('MERGE INTO wines AS w USING wine_stock_changes AS s ON s.winename = w.winename WHEN NOT MATCHED AND s.stock_delta > 0 THEN INSERT VALUES(s.winename, s.stock_delta) WHEN MATCHED AND w.stock + s.stock_delta > $1 THEN UPDATE SET stock = w.stock + s.stock_delta WHEN MATCHED AND w.stock + s.stock_delta < $2 THEN UPDATE SET stock = w.stock - s.stock_delta WHEN MATCHED THEN DELETE', $query->sql); Tester\Assert::same([0, 0], $query->params); @@ -1276,7 +1276,7 @@ public function testMergeDoNothing(): void ->using('wine_stock_changes', 's', 's.winename = w.winename') ->whenNotMatched('INSERT VALUES(s.winename, s.stock_delta)') ->whenMatched('DO NOTHING') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('MERGE INTO wines AS w USING wine_stock_changes AS s ON s.winename = w.winename WHEN NOT MATCHED THEN INSERT VALUES(s.winename, s.stock_delta) WHEN MATCHED THEN DO NOTHING', $query->sql); Tester\Assert::same([], $query->params); @@ -1290,7 +1290,7 @@ public function testMergeCommonUpsert(): void ->using('(SELECT 1)', 's', 'w.winename = $1') ->whenNotMatched(Db\Sql\DefaultQuery::create('INSERT (winename, balance) VALUES($1, $2)', 'Red wine', 10)) ->whenMatched('UPDATE SET balance = $2') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('MERGE INTO wines AS w USING (SELECT 1) AS s ON w.winename = $1 WHEN NOT MATCHED THEN INSERT (winename, balance) VALUES($1, $2) WHEN MATCHED THEN UPDATE SET balance = $2', $query->sql); Tester\Assert::same(['Red wine', 10], $query->params); @@ -1304,7 +1304,7 @@ public function testMergeNoUsing(): void ->merge('customer_account', 'ca') ->whenMatched('UPDATE SET balance = balance + transaction_value') ->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::MERGE_NO_USING); } @@ -1318,7 +1318,7 @@ public function testMergeMoreUsings(): void ->using('recent_transactions2', 't2') ->whenMatched('UPDATE SET balance = balance + transaction_value') ->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryException::class, NULL, Fluent\Exceptions\QueryException::MERGE_ONLY_ONE_USING); } @@ -1331,7 +1331,7 @@ public function testMergeNoOn(): void ->using('recent_transactions', 't') ->whenMatched('UPDATE SET balance = balance + transaction_value') ->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_ON_CONDITION); } @@ -1342,7 +1342,7 @@ public function testMergeNoWhen(): void $this->query() ->merge('customer_account', 'ca') ->using('recent_transactions', 't', 't.customer_id = ca.customer_id') - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::MERGE_NO_WHEN); } @@ -1355,7 +1355,7 @@ public function testMergeReturning(): void ->whenMatched('UPDATE SET balance = balance + transaction_value') ->whenNotMatched('INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value)') ->returning(['merge_action()', 'ca.*']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('MERGE INTO customer_account AS ca USING recent_transactions AS t ON t.customer_id = ca.customer_id WHEN MATCHED THEN UPDATE SET balance = balance + transaction_value WHEN NOT MATCHED THEN INSERT (customer_id, balance) VALUES (t.customer_id, t.transaction_value) RETURNING merge_action(), ca.*', $query->sql); Tester\Assert::same([], $query->params); @@ -1364,7 +1364,7 @@ public function testMergeReturning(): void public function testTruncate(): void { - $query = $this->query()->truncate('table')->createDbQuery(); + $query = $this->query()->truncate('table')->toDbQuery(); Tester\Assert::same('TRUNCATE table', $query->sql); Tester\Assert::same([], $query->params); } @@ -1380,7 +1380,7 @@ public function testWith(): void ->where('region', Db\Sql\DefaultQuery::create('SELECT region FROM top_regions')) ->where('region != ?', 'Prague') ->groupBy('region', 'product') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('WITH regional_sales AS (SELECT region, SUM(amount) AS total_sales FROM orders GROUP BY region), top_regions AS (SELECT region FROM regional_sales WHERE total_sales > (SELECT SUM(total_sales) / 10 FROM regional_sales) AND total_sales < $1) SELECT region, product, SUM(quantity) AS "product_units", SUM(amount) AS "product_sales" FROM orders WHERE (region IN (SELECT region FROM top_regions)) AND (region != $2) GROUP BY region, product', $query->sql); Tester\Assert::same([10000, 'Prague'], $query->params); @@ -1394,7 +1394,7 @@ public function testWithRecursive(): void ->recursive() ->select(['sum(n)']) ->from('t') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('WITH RECURSIVE t(n) AS (VALUES (1) UNION ALL SELECT n + 1 FROM t WHERE n < 100) SELECT sum(n) FROM t', $query->sql); Tester\Assert::same([], $query->params); @@ -1421,7 +1421,7 @@ public function testWithSuffix(): void ->select(['*']) ->from('search_tree') ->orderBy('ordercol') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('WITH search_tree(id, link, data) AS ((SELECT t.id, t.link, t.data FROM tree AS t) UNION ALL (SELECT t.id, t.link, t.data FROM tree AS t, search_tree AS st WHERE t.id = st.link)) SEARCH BREADTH FIRST BY id SET ordercol SELECT * FROM search_tree ORDER BY ordercol', $query->sql); Tester\Assert::same([], $query->params); @@ -1436,7 +1436,7 @@ public function testWithNotMaterialized(): void ->from('w', 'w1') ->join('w', 'w2', 'w1.key = w2.ref') ->where('w2.key', 123) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('WITH w AS NOT MATERIALIZED (SELECT * FROM big_table) SELECT * FROM w AS w1 INNER JOIN w AS w2 ON w1.key = w2.ref WHERE w2.key = $1', $query->sql); Tester\Assert::same([123], $query->params); @@ -1457,7 +1457,7 @@ public function testWithInsert(): void ->insert('products_log') ->select(['*']) ->from('moved_rows') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('WITH moved_rows AS (DELETE FROM products WHERE (date >= $1) AND (date < $2) RETURNING *) INSERT INTO products_log SELECT * FROM moved_rows', $query->sql); Tester\Assert::same(['2010-10-01', '2010-11-01'], $query->params); @@ -1483,7 +1483,7 @@ public function testWithDelete(): void ) ->delete('parts') ->where('part', $this->query()->select(['part'])->from('included_parts')) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('WITH included_parts(sub_part, part) AS ((SELECT sub_part, part FROM parts WHERE part = $1) UNION ALL (SELECT p.sub_part, p.part FROM included_parts AS pr, parts AS p WHERE p.part = pr.sub_part)) DELETE FROM parts WHERE part IN (SELECT part FROM included_parts)', $query->sql); Tester\Assert::same(['our_product'], $query->params); @@ -1502,7 +1502,7 @@ public function testWithUpdate(): void ) ->select(['*']) ->from('t') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('WITH t AS (UPDATE products SET price = price * 1.05 RETURNING *) SELECT * FROM t', $query->sql); Tester\Assert::same([], $query->params); @@ -1521,7 +1521,7 @@ public function testParamsPrefix(): void ->from('table') ->where('column', 100) ->prefix('WITH cte AS (?)', $withQuery) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('WITH cte AS (SELECT columnWith FROM tableWith WHERE columnWith > $1) SELECT column FROM table WHERE column = $2', $query->sql); Tester\Assert::same([5, 100], $query->params); @@ -1535,7 +1535,7 @@ public function testSimpleSuffix(): void ->from('table') ->where('column', 100) ->suffix('FOR UPDATE') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table WHERE column = $1 FOR UPDATE', $query->sql); Tester\Assert::same([100], $query->params); @@ -1543,7 +1543,7 @@ public function testSimpleSuffix(): void $query = $this->query() ->truncate('table') ->suffix('CASCADE') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('TRUNCATE table CASCADE', $query->sql); Tester\Assert::same([], $query->params); @@ -1557,7 +1557,7 @@ public function testSuffixWithReturning(): void ->values(['column' => 'value']) ->suffix('ON CONFLICT (column) DO NOTHING') ->returning(['column']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (column) VALUES($1) ON CONFLICT (column) DO NOTHING RETURNING column', $query->sql); Tester\Assert::same(['value'], $query->params); @@ -1567,7 +1567,7 @@ public function testSuffixWithReturning(): void ->set(['column' => 'value']) ->suffix('WHERE CURRENT OF cursor_name') ->returning(['column']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('UPDATE table SET column = $1 WHERE CURRENT OF cursor_name RETURNING column', $query->sql); Tester\Assert::same(['value'], $query->params); @@ -1576,7 +1576,7 @@ public function testSuffixWithReturning(): void ->delete('table') ->suffix('WHERE CURRENT OF cursor_name') ->returning(['column']) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('DELETE FROM table WHERE CURRENT OF cursor_name RETURNING column', $query->sql); Tester\Assert::same([], $query->params); @@ -1596,7 +1596,7 @@ public function testConditionWhere(): void $query = $conditionOr->query() ->select(['*']) ->from('table') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE (column = $1) OR (column2 IN ($2, $3)) OR ((column IN (SELECT 1)) AND (column2 = ANY(SELECT 2))) OR (column3 IS NOT NULL)', $query->sql); Tester\Assert::same([1, 2, 3], $query->params); @@ -1617,7 +1617,7 @@ public function testConditionHaving(): void ->select(['*']) ->from('table') ->groupBy('column', 'column2') - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT * FROM table GROUP BY column, column2 HAVING (column = $1) OR (column2 IN ($2, $3)) OR ((column IN (SELECT 1)) AND (column2 = ANY(SELECT 2))) OR (column3 IS NOT NULL)', $query->sql); Tester\Assert::same([1, 2, 3], $query->params); @@ -1632,7 +1632,7 @@ public function testConditionBadParams(): void ->add('columns = ? AND column2 = ?', 1, 2, 3) ->query() ->select(['*']) - ->createDbQuery(); + ->toDbQuery(); }, Fluent\Exceptions\ConditionException::class, NULL, Fluent\Exceptions\ConditionException::BAD_PARAMS_COUNT); } @@ -1774,7 +1774,7 @@ public function testReset(): void $query = $this->query() ->select([1]); - $sql = $query->createDbQuery(); + $sql = $query->toDbQuery(); Tester\Assert::same('SELECT 1', $sql->sql); Tester\Assert::same([], $sql->params); @@ -1782,7 +1782,7 @@ public function testReset(): void $query2 = $query ->reset(Fluent\Query::PARAM_SELECT) ->select([2]) - ->createDbQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT 2', $query2->sql); Tester\Assert::same([], $query2->params); @@ -1849,12 +1849,12 @@ public function testCloneQuery(): void ->where('x.another_column', 4) ->having('avg(column)', 1); - $testBaseQuery = $baseQuery->createDbQuery(); + $testBaseQuery = $baseQuery->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN another_table AS at ON (at.id = t.another_table_id) AND (at.type_id = $1) WHERE x.column = $2 HAVING count(*) > $3', $testBaseQuery->sql); Tester\Assert::same([2, 1, 10], $testBaseQuery->params); - $testClonedQuery = $clonedQuery->createDbQuery(); + $testClonedQuery = $clonedQuery->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t INNER JOIN another_table AS at ON (at.id = t.another_table_id) AND (at.type_id = $1) AND (at.another_type = $2) WHERE (x.column = $3) AND (x.another_column = $4) HAVING (count(*) > $5) AND (avg(column) = $6)', $testClonedQuery->sql); Tester\Assert::same([2, 3, 1, 4, 10, 1], $testClonedQuery->params); diff --git a/tests/Unit/QueryTest.php b/tests/Unit/QueryTest.php index 4c442df..3ca2913 100644 --- a/tests/Unit/QueryTest.php +++ b/tests/Unit/QueryTest.php @@ -16,7 +16,7 @@ final class QueryTest extends Tests\TestCase public function testPrepareQuery(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table')->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table')->toDbQuery(); Tester\Assert::same('SELECT * FROM table', $query->sql); Tester\Assert::same([], $query->params); } @@ -24,19 +24,19 @@ public function testPrepareQuery(): void public function testPrepareQueryWithParams(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = $1', 1)->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = $1', 1)->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = $1', $query->sql); Tester\Assert::same([1], $query->params); - $query = Db\Sql\DefaultQuery::createArgs('SELECT * FROM table WHERE column = $1', [1])->createDbQuery(); + $query = Db\Sql\DefaultQuery::createArgs('SELECT * FROM table WHERE column = $1', [1])->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = $1', $query->sql); Tester\Assert::same([1], $query->params); - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ?', 1)->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ?', 1)->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = $1', $query->sql); Tester\Assert::same([1], $query->params); - $query = Db\Sql\DefaultQuery::createArgs('SELECT * FROM table WHERE column = ?', [1])->createDbQuery(); + $query = Db\Sql\DefaultQuery::createArgs('SELECT * FROM table WHERE column = ?', [1])->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = $1', $query->sql); Tester\Assert::same([1], $query->params); } @@ -45,7 +45,7 @@ public function testPrepareQueryWithParams(): void public function testPrepareQueryWithMissingParam(): void { Tester\Assert::exception(static function (): void { - Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ? AND column2 = ?', 1)->createDbQuery(); + Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ? AND column2 = ?', 1)->toDbQuery(); }, Db\Exceptions\QueryException::class, NULL, Db\Exceptions\QueryException::MISSING_PARAM); } @@ -53,14 +53,14 @@ public function testPrepareQueryWithMissingParam(): void public function testPrepareQueryWithExtraParam(): void { Tester\Assert::exception(static function (): void { - Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ?', 1, 2)->createDbQuery(); + Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ?', 1, 2)->toDbQuery(); }, Db\Exceptions\QueryException::class, NULL, Db\Exceptions\QueryException::EXTRA_PARAM); } public function testPrepareQueryWithLiteral(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM ? WHERE column = ?', Db\Sql\Literal::create('table'), 1)->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM ? WHERE column = ?', Db\Sql\Literal::create('table'), 1)->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = $1', $query->sql); Tester\Assert::same([1], $query->params); } @@ -68,7 +68,7 @@ public function testPrepareQueryWithLiteral(): void public function testPrepareQueryWithLiteralWithParams(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM ? WHERE column = ?', Db\Sql\Expression::create('function(?, ?)', 'param1', 2), 1)->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM ? WHERE column = ?', Db\Sql\Expression::create('function(?, ?)', 'param1', 2), 1)->toDbQuery(); Tester\Assert::same('SELECT * FROM function($1, $2) WHERE column = $3', $query->sql); Tester\Assert::same(['param1', 2, 1], $query->params); } @@ -76,7 +76,7 @@ public function testPrepareQueryWithLiteralWithParams(): void public function testPrepareQueryWithArray(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column IN (?)', [1, 2])->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column IN (?)', [1, 2])->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column IN ($1, $2)', $query->sql); Tester\Assert::same([1, 2], $query->params); } @@ -84,7 +84,7 @@ public function testPrepareQueryWithArray(): void public function testPrepareQueryWithArrayNotList(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column1 = ? AND column2 IN (?)', 3, [10 => 2, 20 => 1])->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column1 = ? AND column2 IN (?)', 3, [10 => 2, 20 => 1])->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column1 = $1 AND column2 IN ($2, $3)', $query->sql); Tester\Assert::same([3, 2, 1], $query->params); } @@ -92,7 +92,7 @@ public function testPrepareQueryWithArrayNotList(): void public function testPrepareQueryWithBlankArray(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column IN (?)', [])->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column IN (?)', [])->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column IN ()', $query->sql); Tester\Assert::same([], $query->params); } @@ -100,7 +100,7 @@ public function testPrepareQueryWithBlankArray(): void public function testPrepareQueryWithArrayAsOneAnyParameter(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ANY(?)', Db\Helper::createPgArray([1, 2]))->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ANY(?)', Db\Helper::createPgArray([1, 2]))->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = ANY($1)', $query->sql); Tester\Assert::same(['{1,2}'], $query->params); } @@ -109,7 +109,7 @@ public function testPrepareQueryWithArrayAsOneAnyParameter(): void public function testPrepareQueryWithQuery(): void { $subquery = Db\Sql\DefaultQuery::create('SELECT id FROM subtable WHERE column = ?', 1); - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE id IN (?)', $subquery)->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE id IN (?)', $subquery)->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE id IN (SELECT id FROM subtable WHERE column = $1)', $query->sql); Tester\Assert::same([1], $query->params); } @@ -117,7 +117,7 @@ public function testPrepareQueryWithQuery(): void public function testPrepareQueryWithEnum(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ?', Tests\TestEnum::One)->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ?', Tests\TestEnum::One)->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = $1', $query->sql); Tester\Assert::same([Tests\TestEnum::One->value], $query->params); } @@ -125,7 +125,7 @@ public function testPrepareQueryWithEnum(): void public function testPrepareQueryWithArrayOfEnums(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column IN (?)', [Tests\TestEnum::Two, Tests\TestEnum::One])->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column IN (?)', [Tests\TestEnum::Two, Tests\TestEnum::One])->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column IN ($1, $2)', $query->sql); Tester\Assert::same([Tests\TestEnum::Two->value, Tests\TestEnum::One->value], $query->params); } @@ -133,7 +133,7 @@ public function testPrepareQueryWithArrayOfEnums(): void public function testPrepareQueryEscapeQuestionmark(): void { - $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ? AND text ILIKE \'What\?\'', 1)->createDbQuery(); + $query = Db\Sql\DefaultQuery::create('SELECT * FROM table WHERE column = ? AND text ILIKE \'What\?\'', 1)->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = $1 AND text ILIKE \'What?\'', $query->sql); Tester\Assert::same([1], $query->params); } @@ -152,7 +152,7 @@ public function testPrepareQueryCondition(): void 'yes', $subquery, [3, 2, 1], - )->createDbQuery(); + )->toDbQuery(); Tester\Assert::same('SELECT * FROM table WHERE column = $1 OR id IN (SELECT id FROM subtable WHERE when = now() AND text ILIKE \'When?\' AND year > $2) OR type IN ($3, $4, $5)', $query->sql); Tester\Assert::same(['yes', 2005, 3, 2, 1], $query->params);