diff --git a/docs/db.md b/docs/db.md index 44dad38..0330d6f 100644 --- a/docs/db.md +++ b/docs/db.md @@ -485,7 +485,7 @@ dump($cnt); // (integer) 7 ```php $departmentsQuery = Forrest79\PhPgSql\Db\Sql\Query::createArgs('SELECT id FROM departments WHERE id = ?', [1]); -$query = $departmentsQuery->createQuery(); +$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 38cf745..a846d51 100644 --- a/docs/fluent.md +++ b/docs/fluent.md @@ -23,8 +23,7 @@ $query = $fluent ->select(['*']) ->from('users') ->where('id', 1) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); dump($query->sql); // (string) 'SELECT * FROM users WHERE id = $1' dump($query->params); // (array) [1] diff --git a/phpstan.neon b/phpstan.neon index b25ef1a..abef9a0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -28,7 +28,7 @@ parameters: count: 47 - - message: '#^Parameter \#2 \$queryParams of method Forrest79\\PhPgSql\\Fluent\\QueryBuilder::createSqlQuery\(\) expects array\{.+\}, array\{.*\} given\.$#' + message: '#^Parameter \#2 \$queryParams of method Forrest79\\PhPgSql\\Fluent\\QueryBuilder::createSql\(\) expects array\{.+\}, array\{.*\} given\.$#' path: %rootDir%/../../../tests/Unit/FluentQueryTest.php count: 1 diff --git a/src/Db/Connection.php b/src/Db/Connection.php index d1b15f0..6c82ef5 100644 --- a/src/Db/Connection.php +++ b/src/Db/Connection.php @@ -296,7 +296,7 @@ private function getDataTypesCache(): array|NULL * @throws Exceptions\ConnectionException * @throws Exceptions\QueryException */ - public function query(string|Query|Sql\Query $sql, mixed ...$params): Result + public function query(string|Query|Sql $sql, mixed ...$params): Result { \assert(\array_is_list($params)); return $this->queryArgs($sql, $params); @@ -308,9 +308,9 @@ public function query(string|Query|Sql\Query $sql, mixed ...$params): Result * @throws Exceptions\ConnectionException * @throws Exceptions\QueryException */ - public function queryArgs(string|Query|Sql\Query $sql, array $params): Result + public function queryArgs(string|Query|Sql $sql, array $params): Result { - $query = $this->prepareQuery($this->normalizeQuery($sql, $params)); + $query = $this->prepareQuery(Query::from($sql, $params)); $startTime = $this->events->hasOnQuery() ? \hrtime(TRUE) : NULL; @@ -379,7 +379,7 @@ public function execute(string $sql): static * @throws Exceptions\ConnectionException * @throws Exceptions\QueryException */ - public function asyncQuery(string|Query|Sql\Query $sql, mixed ...$params): AsyncQuery + public function asyncQuery(string|Query|Sql $sql, mixed ...$params): AsyncQuery { \assert(\array_is_list($params)); return $this->asyncQueryArgs($sql, $params); @@ -391,9 +391,9 @@ public function asyncQuery(string|Query|Sql\Query $sql, mixed ...$params): Async * @throws Exceptions\ConnectionException * @throws Exceptions\QueryException */ - public function asyncQueryArgs(string|Query|Sql\Query $sql, array $params): AsyncQuery + public function asyncQueryArgs(string|Query|Sql $sql, array $params): AsyncQuery { - $query = $this->prepareQuery($this->normalizeQuery($sql, $params)); + $query = $this->prepareQuery(Query::from($sql, $params)); $queryParams = $query->params; if ($queryParams === []) { @@ -546,22 +546,6 @@ public function getResource(): PgSql\Connection } - /** - * @param list $params - * @throws Exceptions\QueryException - */ - private function normalizeQuery(string|Query|Sql\Query $sql, array $params): Query - { - if (\is_string($sql)) { - $sql = new Sql\Query($sql, $params); - } else if ($params !== []) { - throw Exceptions\QueryException::cantPassParams(); - } - - return $sql instanceof Query ? $sql : $sql->createQuery(); - } - - /** * Extend this method to update query before execution. * diff --git a/src/Db/Query.php b/src/Db/Query.php index 73d7ddf..1da4691 100644 --- a/src/Db/Query.php +++ b/src/Db/Query.php @@ -16,7 +16,7 @@ public function __construct(public readonly string $sql, public readonly array $ /** * @param list $params */ - public static function from(string|self|Sql $query, array $params): self + public static function from(string|self|Sql $query, array $params = []): self { if (is_string($query)) { $query = new Sql\Expression($query, $params); @@ -24,11 +24,7 @@ public static function from(string|self|Sql $query, array $params): self throw Exceptions\QueryException::cantPassParams(); } - if ($query instanceof Sql) { - $query = self::prepareQuery($query->getSql(), $query->getParams(), 0); - } - - return $query; + return $query instanceof Sql ? self::prepareQuery($query->getSql(), $query->getParams(), 0) : $query; } diff --git a/src/Db/Sql/Query.php b/src/Db/Sql/Query.php index 49ec1d3..5d10173 100644 --- a/src/Db/Sql/Query.php +++ b/src/Db/Sql/Query.php @@ -11,7 +11,7 @@ class Query implements Db\Sql /** @var list */ private array $params; - private Db\Query|NULL $query = NULL; + private Db\Query|NULL $dbQuery = NULL; /** @@ -42,73 +42,13 @@ public function getParams(): array /** * Create SQL query for pg_query_params function. */ - public function createQuery(): Db\Query + public function toDbQuery(): Db\Query { - if ($this->query === NULL) { - $this->query = self::prepareQuery($this->sql, $this->params, 0); + if ($this->dbQuery === NULL) { + $this->dbQuery = Db\Query::from($this->sql, $this->params); } - return $this->query; - } - - - /** - * @param list $params - */ - private static function prepareQuery(string $sql, array $params, int $paramIndex): Db\Query - { - $origParamIndex = 0; - $parsedParams = []; - - $sql = \preg_replace_callback( - '/([\\\\]?)\?/', - static function ($matches) use (&$params, &$parsedParams, &$origParamIndex, &$paramIndex): string { - if ($matches[1] === '\\') { - return '?'; - } - - if (!\array_key_exists($origParamIndex, $params)) { - throw Db\Exceptions\QueryException::missingParam($origParamIndex); - } - - $param = $params[$origParamIndex]; - unset($params[$origParamIndex]); - $origParamIndex++; - - if (\is_array($param)) { - $keys = []; - foreach ($param as $value) { - $keys[] = '$' . ++$paramIndex; - $parsedParams[] = ($value instanceof \BackedEnum) ? $value->value : $value; - } - return \implode(', ', $keys); - } else if (\is_bool($param)) { - return $param === TRUE ? 'TRUE' : 'FALSE'; - } else if ($param instanceof Db\Sql) { - $subquerySql = self::prepareQuery($param->getSql(), $param->getParams(), $paramIndex); - $paramIndex += \count($subquerySql->params); - $parsedParams = \array_merge($parsedParams, $subquerySql->params); - return $subquerySql->sql; - } - - $parsedParams[] = ($param instanceof \BackedEnum) ? $param->value : $param; - - return '$' . ++$paramIndex; - }, - $sql, - ); - - \assert(\is_string($sql)); - - if (($origParamIndex > 0) && ($params !== [])) { - throw Db\Exceptions\QueryException::extraParam(\array_values($params)); - } - - if ($parsedParams === []) { - $parsedParams = \array_values($params); - } - - return new Db\Query($sql, $parsedParams); + return $this->dbQuery; } diff --git a/src/Fluent/Query.php b/src/Fluent/Query.php index bfae9f5..6620475 100644 --- a/src/Fluent/Query.php +++ b/src/Fluent/Query.php @@ -117,7 +117,7 @@ class Query implements Db\Sql /** @phpstan-var QueryParams */ private array $params = self::DEFAULT_PARAMS; - private Db\Sql\Query|NULL $query = NULL; + private Db\Sql\Expression|NULL $sqlExpression = NULL; public function __construct(QueryBuilder $queryBuilder) @@ -931,7 +931,7 @@ public function reset(string $param): static protected function resetQuery(): void { - $this->query = NULL; + $this->sqlExpression = NULL; } @@ -946,31 +946,37 @@ private function checkAlias(mixed $data, string|NULL $alias): void } - /** - * @throws Exceptions\QueryBuilderException - */ - public function createSqlQuery(): Db\Sql\Query + public function getSql(): string { - if ($this->query === NULL) { - $this->query = $this->queryBuilder->createSqlQuery($this->queryType, $this->params); - } - - return $this->query; + return $this->buildSql()->getSql(); } - public function getSql(): string + /** + * @return list + */ + public function getParams(): array { - return $this->createSqlQuery()->getSql(); + return $this->buildSql()->getParams(); } /** - * @return list + * @throws Exceptions\QueryBuilderException */ - public function getParams(): array + protected function buildSql(): Db\Sql\Expression + { + if ($this->sqlExpression === NULL) { + $this->sqlExpression = $this->queryBuilder->createSql($this->queryType, $this->params); + } + + return $this->sqlExpression; + } + + + public function toDbQuery(): Db\Query { - return $this->createSqlQuery()->getParams(); + return Db\Query::from($this->buildSql()); } diff --git a/src/Fluent/QueryBuilder.php b/src/Fluent/QueryBuilder.php index 884c2de..2154b83 100644 --- a/src/Fluent/QueryBuilder.php +++ b/src/Fluent/QueryBuilder.php @@ -42,7 +42,7 @@ class QueryBuilder * @throws Exceptions\QueryBuilderException * @phpstan-param QueryParams $queryParams */ - public function createSqlQuery(string $queryType, array $queryParams): Db\Sql\Query + public function createSql(string $queryType, array $queryParams): Db\Sql\Expression { $params = []; @@ -68,16 +68,16 @@ public function createSqlQuery(string $queryType, array $queryParams): Db\Sql\Qu throw Exceptions\QueryBuilderException::badQueryType($queryType); } - return $this->prepareSqlQuery($sql, $params); + return $this->prepareSql($sql, $params); } /** * @param list $params */ - protected function prepareSqlQuery(string $sql, array $params): Db\Sql\Query + protected function prepareSql(string $sql, array $params): Db\Sql\Expression { - return new Db\Sql\Query($sql, $params); + return new Db\Sql\Expression($sql, $params); } @@ -815,7 +815,7 @@ private function processCondition(Condition $condition, array &$params): string private static function areParenthesisNeeded(Db\Sql $sql): bool { - return $sql instanceof Db\Sql\Query || $sql instanceof Query; + return $sql instanceof Query; } } diff --git a/src/Fluent/QueryExecute.php b/src/Fluent/QueryExecute.php index 942d920..bd46c9b 100644 --- a/src/Fluent/QueryExecute.php +++ b/src/Fluent/QueryExecute.php @@ -80,7 +80,7 @@ protected function resetQuery(): void public function execute(): Db\Result { if ($this->result === NULL) { - $this->result = $this->connection->query($this->createSqlQuery()); + $this->result = $this->connection->query($this); if ($this->rowFetchMutator !== NULL) { $this->result->setRowFetchMutator($this->rowFetchMutator); @@ -256,7 +256,7 @@ public function fetchIterator(): Db\ResultIterator */ public function asyncExecute(): Db\AsyncQuery { - return $this->connection->asyncQuery($this->createSqlQuery()); + return $this->connection->asyncQuery($this); } diff --git a/tests/Integration/BasicTest.php b/tests/Integration/BasicTest.php index 3473778..d7323ca 100644 --- a/tests/Integration/BasicTest.php +++ b/tests/Integration/BasicTest.php @@ -214,7 +214,7 @@ public function testFailedQuery(): void public function testPassParamToQuery(): void { Tester\Assert::exception(function (): void { - $query = Db\Sql\Query::create('SELECT 1'); + $query = Db\Sql\Expression::create('SELECT 1'); $this->connection->query($query, 1); }, Db\Exceptions\QueryException::class, NULL, Db\Exceptions\QueryException::CANT_PASS_PARAMS); } diff --git a/tests/Integration/CustomPrepareQueryTest.php b/tests/Integration/CustomPrepareQueryTest.php index b64802d..509a19e 100644 --- a/tests/Integration/CustomPrepareQueryTest.php +++ b/tests/Integration/CustomPrepareQueryTest.php @@ -21,8 +21,8 @@ public function testQuery(): void { $this->connection->query('SELECT 1'); - Tester\Assert::same('SELECT 1 + 1', $this->lastQuery->getSql()); - Tester\Assert::same([], $this->lastQuery->getParams()); + Tester\Assert::same('SELECT 1 + 1', $this->lastQuery->sql); + Tester\Assert::same([], $this->lastQuery->params); } @@ -30,8 +30,8 @@ public function testExecute(): void { $this->connection->execute('SELECT 2'); - Tester\Assert::same('SELECT 2 + 1', $this->lastQuery->getSql()); - Tester\Assert::same([], $this->lastQuery->getParams()); + Tester\Assert::same('SELECT 2 + 1', $this->lastQuery->sql); + Tester\Assert::same([], $this->lastQuery->params); } @@ -39,8 +39,8 @@ public function testPreparedStatement(): void { $this->connection->prepareStatement('SELECT 3')->execute(); - Tester\Assert::same('SELECT 3 + 1', $this->lastQuery->getSql()); - Tester\Assert::same([], $this->lastQuery->getParams()); + Tester\Assert::same('SELECT 3 + 1', $this->lastQuery->sql); + Tester\Assert::same([], $this->lastQuery->params); } @@ -48,8 +48,8 @@ public function testAsyncQuery(): void { $this->connection->asyncQuery('SELECT 4'); - Tester\Assert::same('SELECT 4 + 1', $this->lastQuery->getSql()); - Tester\Assert::same([], $this->lastQuery->getParams()); + Tester\Assert::same('SELECT 4 + 1', $this->lastQuery->sql); + Tester\Assert::same([], $this->lastQuery->params); } @@ -57,8 +57,8 @@ public function testAsyncExecute(): void { $this->connection->asyncExecute('SELECT 5'); - Tester\Assert::same('SELECT 5 + 1', $this->lastQuery->getSql()); - Tester\Assert::same([], $this->lastQuery->getParams()); + Tester\Assert::same('SELECT 5 + 1', $this->lastQuery->sql); + Tester\Assert::same([], $this->lastQuery->params); } @@ -66,8 +66,8 @@ public function testAsyncPreparedStatement(): void { $this->connection->asyncPrepareStatement('SELECT 6')->execute(); - Tester\Assert::same('SELECT 6 + 1', $this->lastQuery->getSql()); - Tester\Assert::same([], $this->lastQuery->getParams()); + Tester\Assert::same('SELECT 6 + 1', $this->lastQuery->sql); + Tester\Assert::same([], $this->lastQuery->params); } @@ -78,14 +78,14 @@ protected function createConnection(): Db\Connection protected function prepareQuery(string|Db\Query $query): string|Db\Query { if ($query instanceof Db\Query) { - $sql = $query->getSql(); + $sql = $query->sql; } else { $sql = $query; } $sql .= ' + 1'; - return ($query instanceof Db\Query) ? new Db\Query($sql, $query->getParams()) : $sql; + return ($query instanceof Db\Query) ? new Db\Query($sql, $query->params) : $sql; } }; diff --git a/tests/Integration/DocsTest.php b/tests/Integration/DocsTest.php index f8d1ae9..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->createSqlQuery()->createQuery(); + $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 e966a01..dfc6aef 100644 --- a/tests/Unit/FluentConnectionTest.php +++ b/tests/Unit/FluentConnectionTest.php @@ -30,8 +30,7 @@ public function testCreateQuery(): void ->createQuery() ->select(['column']) ->from('table') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table', $query->sql); Tester\Assert::same([], $query->params); @@ -46,9 +45,9 @@ public function testCustomQueryBuilder(): void /** * @param list $params */ - protected function prepareSqlQuery(string $sql, array $params): Db\Sql\Query + protected function prepareSql(string $sql, array $params): Db\Sql\Expression { - return parent::prepareSqlQuery('SELECT custom_column FROM ?', ['custom_table']); + return parent::prepareSql('SELECT custom_column FROM ?', ['custom_table']); } }; @@ -59,8 +58,7 @@ protected function prepareSqlQuery(string $sql, array $params): Db\Sql\Query ->createQuery() ->select(['column']) ->from('table') - ->createSqlQuery() - ->createQuery(); + ->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 b87c283..235642f 100644 --- a/tests/Unit/FluentQueryTest.php +++ b/tests/Unit/FluentQueryTest.php @@ -27,8 +27,7 @@ public function testSelect(): void ->orderBy('column') ->limit(10) ->offset(20) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -66,8 +65,7 @@ public function testSelectWithFluentQuery(): void { $query = $this->query() ->select(['column' => $this->query()->select([1])]) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT (SELECT 1) AS "column"', $query->sql); Tester\Assert::same([], $query->params); @@ -77,9 +75,8 @@ public function testSelectWithFluentQuery(): void public function testSelectWithQuery(): void { $query = $this->query() - ->select(['column' => new Db\Sql\Query('SELECT 1')]) - ->createSqlQuery() - ->createQuery(); + ->select(['column' => Db\Sql\Expression::create('SELECT 1')]) + ->toDbQuery(); Tester\Assert::same('SELECT (SELECT 1) AS "column"', $query->sql); Tester\Assert::same([], $query->params); @@ -90,8 +87,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']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -102,8 +98,7 @@ public function testSelectBoolNull(): void { $query = $this->query() ->select(['is_true' => TRUE, 'is_false' => FALSE, 'is_null' => NULL]) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT TRUE AS "is_true", FALSE AS "is_false", NULL AS "is_null"', $query->sql); Tester\Assert::same([], $query->params); @@ -114,8 +109,7 @@ public function testSelectEnum(): void { $query = $this->query() ->select([Tests\TestEnum::One, 'column' => Tests\TestEnum::One]) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT 1, 1 AS "column"', $query->sql); Tester\Assert::same([], $query->params); @@ -127,8 +121,7 @@ public function testFromWithFluentQuery(): void $query = $this->query() ->select(['x.column']) ->from($this->query()->select(['column' => 1]), 'x') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM (SELECT 1 AS "column") AS x', $query->sql); Tester\Assert::same([], $query->params); @@ -139,9 +132,8 @@ public function testFromWithQuery(): void { $query = $this->query() ->select(['x.column']) - ->from(new Db\Sql\Query('SELECT 1 AS column'), 'x') - ->createSqlQuery() - ->createQuery(); + ->from(Db\Sql\Expression::create('SELECT 1 AS column'), 'x') + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM (SELECT 1 AS column) AS x', $query->sql); Tester\Assert::same([], $query->params); @@ -153,8 +145,7 @@ public function testFromWithParameter(): void $query = $this->query() ->select(['gs']) ->from(Db\Sql\Expression::create('generate_series(?::integer, ?::integer, ?::integer)', 2, 1, -1), 'gs') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -167,8 +158,7 @@ public function testMoreFrom(): void ->select(['column']) ->from('table1', 't1') ->from('table2', 't2') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table1 AS t1, table2 AS t2', $query->sql); Tester\Assert::same([], $query->params); @@ -181,8 +171,7 @@ public function testWhereSimple(): void ->select(['x.column']) ->from('table', 't') ->where('x.column = t.id') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE x.column = t.id', $query->sql); Tester\Assert::same([], $query->params); @@ -195,8 +184,7 @@ public function testWhereParameters(): void ->select(['x.column']) ->from('table', 't') ->where('x.column', 1) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE x.column = $1', $query->sql); Tester\Assert::same([1], $query->params); @@ -213,8 +201,7 @@ public function testWhereWithCondition(): void ->select(['x.column']) ->from('table', 't') ->where($condition) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -227,8 +214,7 @@ public function testWhereWithSql(): void ->select(['x.column']) ->from('table', 't') ->where(Db\Sql\Expression::create('x.id', [1, 2])) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -249,8 +235,7 @@ public function testWhereIf(): void ->select(['x.column']) ->from('table', 't') ->whereIf(TRUE, 'x.column = t.id') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t WHERE x.column = t.id', $queryWithTrueIfCondition->sql); Tester\Assert::same([], $queryWithTrueIfCondition->params); @@ -259,8 +244,7 @@ public function testWhereIf(): void ->select(['x.column']) ->from('table', 't') ->whereIf(FALSE, 'x.column = t.id') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t', $queryWithFalseIfCondition->sql); Tester\Assert::same([], $queryWithFalseIfCondition->params); @@ -283,8 +267,7 @@ public function testWhereAnd(): void Db\Sql\Expression::create('x.id', 7), ]) ->query() - ->createSqlQuery() - ->createQuery(); + ->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); @@ -306,9 +289,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 - ->createSqlQuery() - ->createQuery(); + $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); @@ -331,8 +312,7 @@ public function testWhereOr(): void Db\Sql\Expression::create('x.id', 7), ]) ->query() - ->createSqlQuery() - ->createQuery(); + ->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); @@ -345,8 +325,7 @@ public function testHavingSimple(): void ->select(['x.column']) ->from('table', 't') ->having('x.column = t.id') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING x.column = t.id', $query->sql); Tester\Assert::same([], $query->params); @@ -359,8 +338,7 @@ public function testHavingParameters(): void ->select(['x.column']) ->from('table', 't') ->having('x.column', 1) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT x.column FROM table AS t HAVING x.column = $1', $query->sql); Tester\Assert::same([1], $query->params); @@ -377,8 +355,7 @@ public function testHavingWithCondition(): void ->select(['x.column']) ->from('table', 't') ->having($condition) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -391,8 +368,7 @@ public function testHavingWithSql(): void ->select(['x.column']) ->from('table', 't') ->having(Db\Sql\Expression::create('x.id', [1, 2])) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -423,8 +399,7 @@ public function testHavingAnd(): void Db\Sql\Expression::create('x.id', 7), ]) ->query() - ->createSqlQuery() - ->createQuery(); + ->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); @@ -447,8 +422,7 @@ public function testHavingOr(): void Db\Sql\Expression::create('x.id', 7), ]) ->query() - ->createSqlQuery() - ->createQuery(); + ->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); @@ -471,8 +445,7 @@ public function testHavingOrContinue(): void ->add('x.column', 1); $query = $sourceQuery - ->createSqlQuery() - ->createQuery(); + ->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); @@ -485,8 +458,7 @@ public function testJoinWithFluentQuery(): void ->select(['x.column']) ->from('table', 't') ->join($this->query()->select(['column' => 1]), 'x', 'x.column = t.id') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -498,9 +470,8 @@ public function testJoinWithQuery(): void $query = $this->query() ->select(['x.column']) ->from('table', 't') - ->join(new Db\Sql\Query('SELECT 1 AS column'), 'x', 'x.column = t.id') - ->createSqlQuery() - ->createQuery(); + ->join(Db\Sql\Expression::create('SELECT 1 AS column'), 'x', 'x.column = t.id') + ->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); @@ -513,8 +484,7 @@ public function testJoinWithStringOn(): void ->select(['x.column']) ->from('table', 't') ->join('another', 'x', 'x.column = t.id') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -531,8 +501,7 @@ public function testJoinWithConditionOn(): void ->select(['x.column']) ->from('table', 't') ->join('another', 'x', $conditionOn) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -545,8 +514,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)) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -567,8 +535,7 @@ public function testJoinWithAddOn(): void ->on('x', 'x.id = ?', 2) ->on('x', Db\Sql\Expression::create('x.type_id = ?', 'test')) ->on('x', $conditionOn) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -590,7 +557,7 @@ public function testJoinNoOn(): void ->select(['x.column']) ->from('table', 't') ->join('another', 'x') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_ON_CONDITION); } @@ -602,8 +569,7 @@ public function testLateralFrom(): void ->from('table1', 't1') ->from($this->query()->select(['column2'])->from('table2'), 't2') ->lateral('t2') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -615,10 +581,9 @@ public function testLateralJoinWithSqlQuery(): void $query = $this->query() ->select(['column1' => 't1.column', 'column2' => 't2.column']) ->from('table1', 't1') - ->join(new Db\Sql\Query('SELECT column FROM table2'), 't2', 't2.column = t1.column') + ->join(Db\Sql\Expression::create('SELECT column FROM table2'), 't2', 't2.column = t1.column') ->lateral('t2') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -632,8 +597,7 @@ public function testLateralJoinWithFluentQuery(): void ->from('table1', 't1') ->join($this->query()->select(['column'])->from('table2'), 't2', 't2.column = t1.column') ->lateral('t2') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -646,8 +610,7 @@ public function testSelectCombine(): void ->from('table', 't') ->select(['column']) ->union('SELECT column FROM table2 AS t2') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('(SELECT column FROM table AS t) UNION (SELECT column FROM table2 AS t2)', $query->sql); Tester\Assert::same([], $query->params); @@ -664,8 +627,7 @@ public function testSelectCombineFluent(): void ->select(['column']) ->from('table2', 't2'), ) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('(SELECT column FROM table AS t) UNION (SELECT column FROM table2 AS t2)', $query->sql); Tester\Assert::same([], $query->params); @@ -677,9 +639,8 @@ public function testSelectCombineQuery(): void $query = $this->query() ->from('table', 't') ->select(['column']) - ->union(new Db\Sql\Query('SELECT column FROM table2 AS t2')) - ->createSqlQuery() - ->createQuery(); + ->union(Db\Sql\Expression::create('SELECT column FROM table2 AS t2')) + ->toDbQuery(); Tester\Assert::same('(SELECT column FROM table AS t) UNION (SELECT column FROM table2 AS t2)', $query->sql); Tester\Assert::same([], $query->params); @@ -691,7 +652,7 @@ public function testSelectNoColumns(): void Tester\Assert::exception(function (): void { $this->query() ->from('table') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_COLUMNS_TO_SELECT); } @@ -702,8 +663,7 @@ public function testOrderByFluent(): void ->select(['column']) ->from('table', 't') ->orderBy($this->query()->select(['sort_by_value(column)'])) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table AS t ORDER BY (SELECT sort_by_value(column))', $query->sql); Tester\Assert::same([], $query->params); @@ -715,9 +675,8 @@ public function testOrderByQuery(): void $query = $this->query() ->select(['column']) ->from('table', 't') - ->orderBy(Db\Sql\Query::create('sort_by_value(column)')) - ->createSqlQuery() - ->createQuery(); + ->orderBy(Db\Sql\Expression::create('sort_by_value(column)')) + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table AS t ORDER BY (sort_by_value(column))', $query->sql); Tester\Assert::same([], $query->params); @@ -731,12 +690,11 @@ public function testInsertRow(): void 'column' => 1, 'column_from' => Db\Sql\Literal::create('3'), 'column_fluent_query' => $this->query()->select(['\'test_fluent\''])->where('4', 4), - 'column_query' => new Db\Sql\Query('SELECT \'test\' WHERE 5 = ?', [5]), + 'column_query' => new Db\Sql\Expression('SELECT \'test\' WHERE 5 = ?', [5]), ]) ->insert('table') ->returning(['column']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -755,8 +713,7 @@ public function testInsertMergeData(): void 'column2' => 2, 'column3' => 1, ]) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (column2, column3, column1) VALUES($1, $2, $3)', $query->sql); Tester\Assert::same([2, 1, 3], $query->params); @@ -772,12 +729,11 @@ public function testInsertRows(): void ['column' => 3], ['column' => Db\Sql\Literal::create('4')], ['column' => $this->query()->select(['\'test_fluent\''])->where('6', 6)], - ['column' => new Db\Sql\Query('SELECT \'test\' WHERE 7 = ?', [7])], + ['column' => new Db\Sql\Expression('SELECT \'test\' WHERE 7 = ?', [7])], ]) ->insert('table') ->returning(['column']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -796,8 +752,7 @@ public function testInsertRowsMergeData(): void ['column' => 4], ]) ->insert('table') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (column) VALUES($1), ($2), ($3), ($4)', $query->sql); Tester\Assert::same([1, 2, 3, 4], $query->params); @@ -811,8 +766,7 @@ public function testInsertSelect(): void ->select(['column']) ->from('table2', 't2') ->returning(['name']) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table (name) SELECT column FROM table2 AS t2 RETURNING name', $query->sql); Tester\Assert::same([], $query->params); @@ -826,8 +780,7 @@ public function testInsertSelectDetectColumnsFromSelect(): void ->select(['column', 'name' => 'column2']) ->from('table2', 't2') ->returning(['column']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -840,8 +793,7 @@ public function testInsertSelectAllColumns(): void ->insert('table1') ->select(['*']) ->from('table2') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('INSERT INTO table1 SELECT * FROM table2', $query->sql); Tester\Assert::same([], $query->params); @@ -855,7 +807,7 @@ public function testInsertSelectNoColumn(): void ->insert('table1') ->select([]) ->from('table2') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_DATA_TO_INSERT); } @@ -867,7 +819,7 @@ public function testInsertSelectMissingColumnAlias(): void ->insert('table1') ->select([1]) ->from('table2') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::MISSING_COLUMN_ALIAS); Tester\Assert::exception(function (): void { @@ -875,7 +827,7 @@ public function testInsertSelectMissingColumnAlias(): void ->insert('table1') ->select([Tests\TestEnum::One]) ->from('table2') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::MISSING_COLUMN_ALIAS); } @@ -890,8 +842,7 @@ public function testInsertOnConflictDoUpdate(): void ]) ->onConflict(['name']) ->doUpdate(['info']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -909,8 +860,7 @@ public function testInsertOnConflictWithWhereDoUpdate(): void ]) ->onConflict(['name', 'age'], Fluent\Condition::createAnd()->add('age < ?', 30)) ->doUpdate(['info']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -928,8 +878,7 @@ public function testInsertOnConflictConstraintDoUpdate(): void ]) ->onConflict('name_ukey') ->doUpdate(['info']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -948,7 +897,7 @@ public function testInsertOnConflictConstraintWithWhereDoUpdate(): void ]) ->onConflict('name_ukey', Fluent\Condition::createAnd()->add('age < ?', 30)) ->doUpdate(['info']) - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryException::class, NULL, Fluent\Exceptions\QueryException::ON_CONFLICT_WHERE_NOT_FOR_CONSTRAINT); } @@ -963,8 +912,7 @@ public function testInsertOnConflictDoUpdateWithConditionSet(): void ]) ->onConflict(['name']) ->doUpdate(['info', 'name' => 'EXCLUDED.name || t.age']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -981,8 +929,7 @@ public function testInsertOnConflictDoUpdateWithExpressionSet(): void ]) ->onConflict(['name']) ->doUpdate(['info', 'name' => Db\Sql\Expression::create('EXCLUDED.name || ?', 'Jimmy')]) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1000,8 +947,7 @@ public function testInsertOnConflictDoUpdateWithWhere(): void ]) ->onConflict(['name', 'age']) ->doUpdate(['info'], Fluent\Condition::createAnd()->add('age < ?', 30)) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1018,8 +964,7 @@ public function testInsertOnConflictDoNothing(): void ]) ->onConflict() ->doNothing() - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1037,8 +982,7 @@ public function testInsertOnConflictWithReturning(): void ->onConflict(['name']) ->doUpdate(['info']) ->returning(['id']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1056,7 +1000,7 @@ public function testInsertOnConflictDoUpdateWithoutOnConflictDefinition(): void 'info' => 'Text', ]) ->doUpdate(['info']) - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::ON_CONFLICT_DO_WITHOUT_DEFINITION); } @@ -1072,7 +1016,7 @@ public function testInsertOnConflictWithoutDoStatement(): void 'info' => 'Text', ]) ->onConflict(['name']) - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::ON_CONFLICT_NO_DO); } @@ -1089,7 +1033,7 @@ public function testInsertOnConflictDoUpdateWithExpressionAsSimpleColumn(): void ]) ->onConflict(['name']) ->doUpdate([Db\Sql\Expression::create('EXCLUDED.name || ?', 'Jimmy')]) - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::ON_CONFLICT_DO_UPDATE_SET_SINGLE_COLUMN_CAN_BE_ONLY_STRING); } @@ -1101,7 +1045,7 @@ public function testInsertSelectAllColumnsWithConcrete(): void ->insert('table1') ->select(['*', 'id']) ->from('table2') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::SELECT_ALL_COLUMNS_CANT_BE_COMBINED_WITH_CONCRETE_COLUMN_FOR_INSERT_SELECT_WITH_COLUMN_DETECTION); } @@ -1114,7 +1058,7 @@ public function testInsertRowWithArray(): void ->values([ 'column' => [1, 2], ]) - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::DATA_CANT_CONTAIN_ARRAY); } @@ -1127,7 +1071,7 @@ public function testInsertRowsWithArray(): void ->rows([ ['column' => [1, 2]], ]) - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::DATA_CANT_CONTAIN_ARRAY); } @@ -1137,7 +1081,7 @@ public function testInsertNoData(): void Tester\Assert::exception(function (): void { $this->query() ->insert('table') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_DATA_TO_INSERT); } @@ -1150,13 +1094,12 @@ public function testUpdate(): void 'column' => 1, 'column_from' => Db\Sql\Literal::create('t2.id'), 'column_fluent_query' => $this->query()->select(['\'test_fluent\''])->where('2', 2), - 'column_query' => new Db\Sql\Query('SELECT \'test\' WHERE 3 = ?', [3]), + 'column_query' => new Db\Sql\Expression('SELECT \'test\' WHERE 3 = ?', [3]), ]) ->from('table2', 't2') ->where('t2.column', 100) ->returning(['t.column']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1175,8 +1118,7 @@ public function testUpdateMergeData(): void 'column2' => 2, 'column3' => 1, ]) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1191,7 +1133,7 @@ public function testUpdateWithArray(): void ->set([ 'column1' => [1, 2], ]) - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::DATA_CANT_CONTAIN_ARRAY); } @@ -1201,7 +1143,7 @@ public function testUpdateNoData(): void Tester\Assert::exception(function (): void { $this->query() ->update('table') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_DATA_TO_UPDATE); } @@ -1212,7 +1154,7 @@ public function testNoMainTable(): void $this->query() ->update() ->set(['column' => 1]) - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_MAIN_TABLE); } @@ -1223,8 +1165,7 @@ public function testDelete(): void ->delete('table', 't') ->where('column', 100) ->returning(['c' => 'column']) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('DELETE FROM table AS t WHERE column = $1 RETURNING column AS "c"', $query->sql); Tester\Assert::same([100], $query->params); @@ -1237,8 +1178,7 @@ public function testReturningFluentQuery(): void ->delete('table', 't') ->where('column', 100) ->returning(['c' => $this->query()->select(['to_value(column)'])]) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1250,9 +1190,8 @@ public function testReturningQuery(): void $query = $this->query() ->delete('table', 't') ->where('column', 100) - ->returning(['c' => new Db\Sql\Query('to_value(column)')]) - ->createSqlQuery() - ->createQuery(); + ->returning(['c' => Db\Sql\Expression::create('to_value(column)')]) + ->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); @@ -1266,8 +1205,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)') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1281,8 +1219,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)') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1293,11 +1230,10 @@ public function testMergeUsingSql(): void { $query = $this->query() ->merge('customer_account', 'ca') - ->using(new Db\Sql\Query('SELECT customer_id, transaction_value FROM recent_transactions WHERE customer_id > ?', [10]), 't', 't.customer_id = ca.customer_id') + ->using(new Db\Sql\Expression('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)') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1311,8 +1247,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)') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1328,8 +1263,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') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1343,8 +1277,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') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1358,8 +1291,7 @@ public function testMergeCommonUpsert(): void ->using('(SELECT 1)', 's', 'w.winename = $1') ->whenNotMatched(Db\Sql\Expression::create('INSERT (winename, balance) VALUES($1, $2)', 'Red wine', 10)) ->whenMatched('UPDATE SET balance = $2') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1373,7 +1305,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)') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::MERGE_NO_USING); } @@ -1387,7 +1319,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)') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryException::class, NULL, Fluent\Exceptions\QueryException::MERGE_ONLY_ONE_USING); } @@ -1400,7 +1332,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)') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::NO_ON_CONDITION); } @@ -1411,7 +1343,7 @@ public function testMergeNoWhen(): void $this->query() ->merge('customer_account', 'ca') ->using('recent_transactions', 't', 't.customer_id = ca.customer_id') - ->createSqlQuery(); + ->toDbQuery(); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::MERGE_NO_WHEN); } @@ -1434,7 +1366,7 @@ public function testMergeReturning(): void public function testTruncate(): void { - $query = $this->query()->truncate('table')->createSqlQuery()->createQuery(); + $query = $this->query()->truncate('table')->toDbQuery(); Tester\Assert::same('TRUNCATE table', $query->sql); Tester\Assert::same([], $query->params); } @@ -1444,14 +1376,13 @@ public function testWith(): void { $query = $this->query() ->with('regional_sales', 'SELECT region, SUM(amount) AS total_sales FROM orders GROUP BY region') - ->with('top_regions', new Db\Sql\Query('SELECT region FROM regional_sales WHERE total_sales > (?) AND total_sales < ?', [new Db\Sql\Query('SELECT SUM(total_sales) / 10 FROM regional_sales'), 10000])) + ->with('top_regions', Db\Sql\Expression::create('SELECT region FROM regional_sales WHERE total_sales > (?) AND total_sales < ?', [Db\Sql\Expression::create('SELECT SUM(total_sales) / 10 FROM regional_sales'), 10000])) ->select(['region', 'product', 'product_units' => 'SUM(quantity)', 'product_sales' => 'SUM(amount)']) ->from('orders') - ->where('region', new Db\Sql\Query('SELECT region FROM top_regions')) + ->where('region', Db\Sql\Expression::create('SELECT region FROM top_regions')) ->where('region != ?', 'Prague') ->groupBy('region', 'product') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1465,8 +1396,7 @@ public function testWithRecursive(): void ->recursive() ->select(['sum(n)']) ->from('t') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1493,8 +1423,7 @@ public function testWithSuffix(): void ->select(['*']) ->from('search_tree') ->orderBy('ordercol') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1509,8 +1438,7 @@ public function testWithNotMaterialized(): void ->from('w', 'w1') ->join('w', 'w2', 'w1.key = w2.ref') ->where('w2.key', 123) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1531,8 +1459,7 @@ public function testWithInsert(): void ->insert('products_log') ->select(['*']) ->from('moved_rows') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1558,8 +1485,7 @@ public function testWithDelete(): void ) ->delete('parts') ->where('part', $this->query()->select(['part'])->from('included_parts')) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1578,8 +1504,7 @@ public function testWithUpdate(): void ) ->select(['*']) ->from('t') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1598,8 +1523,7 @@ public function testParamsPrefix(): void ->from('table') ->where('column', 100) ->prefix('WITH cte AS (?)', $withQuery) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1613,8 +1537,7 @@ public function testSimpleSuffix(): void ->from('table') ->where('column', 100) ->suffix('FOR UPDATE') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT column FROM table WHERE column = $1 FOR UPDATE', $query->sql); Tester\Assert::same([100], $query->params); @@ -1622,8 +1545,7 @@ public function testSimpleSuffix(): void $query = $this->query() ->truncate('table') ->suffix('CASCADE') - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('TRUNCATE table CASCADE', $query->sql); Tester\Assert::same([], $query->params); @@ -1637,8 +1559,7 @@ public function testSuffixWithReturning(): void ->values(['column' => 'value']) ->suffix('ON CONFLICT (column) DO NOTHING') ->returning(['column']) - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1648,8 +1569,7 @@ public function testSuffixWithReturning(): void ->set(['column' => 'value']) ->suffix('WHERE CURRENT OF cursor_name') ->returning(['column']) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('UPDATE table SET column = $1 WHERE CURRENT OF cursor_name RETURNING column', $query->sql); Tester\Assert::same(['value'], $query->params); @@ -1658,8 +1578,7 @@ public function testSuffixWithReturning(): void ->delete('table') ->suffix('WHERE CURRENT OF cursor_name') ->returning(['column']) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('DELETE FROM table WHERE CURRENT OF cursor_name RETURNING column', $query->sql); Tester\Assert::same([], $query->params); @@ -1673,14 +1592,13 @@ public function testConditionWhere(): void $conditionOr->add('column2', [2, 3]); $conditionAnd = $conditionOr->addAndBranch(); $conditionAnd->add('column', $this->query()->select([1])); - $conditionAnd->add('column2 = ANY(?)', new Db\Sql\Query('SELECT 2')); + $conditionAnd->add('column2 = ANY(?)', Db\Sql\Expression::create('SELECT 2')); $conditionOr->add('column3 IS NOT NULL'); $query = $conditionOr->query() ->select(['*']) ->from('table') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1694,15 +1612,14 @@ public function testConditionHaving(): void $conditionOr->add('column2', [2, 3]); $conditionAnd = $conditionOr->addAndBranch(); $conditionAnd->add('column', $this->query()->select([1])); - $conditionAnd->add('column2 = ANY(?)', new Db\Sql\Query('SELECT 2')); + $conditionAnd->add('column2 = ANY(?)', Db\Sql\Expression::create('SELECT 2')); $conditionOr->add('column3 IS NOT NULL'); $query = $conditionOr->query() ->select(['*']) ->from('table') ->groupBy('column', 'column2') - ->createSqlQuery() - ->createQuery(); + ->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); @@ -1717,8 +1634,7 @@ public function testConditionBadParams(): void ->add('columns = ? AND column2 = ?', 1, 2, 3) ->query() ->select(['*']) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); }, Fluent\Exceptions\ConditionException::class, NULL, Fluent\Exceptions\ConditionException::BAD_PARAMS_COUNT); } @@ -1860,7 +1776,7 @@ public function testReset(): void $query = $this->query() ->select([1]); - $sql = $query->createSqlQuery()->createQuery(); + $sql = $query->toDbQuery(); Tester\Assert::same('SELECT 1', $sql->sql); Tester\Assert::same([], $sql->params); @@ -1868,8 +1784,7 @@ public function testReset(): void $query2 = $query ->reset(Fluent\Query::PARAM_SELECT) ->select([2]) - ->createSqlQuery() - ->createQuery(); + ->toDbQuery(); Tester\Assert::same('SELECT 2', $query2->sql); Tester\Assert::same([], $query2->params); @@ -1915,7 +1830,7 @@ public function testColumnMustBeScalarOrEnumOrExpression(): void public function testBadQueryBuilderType(): void { Tester\Assert::exception(static function (): void { - (new Fluent\QueryBuilder())->createSqlQuery('table', [Fluent\Query::PARAM_WITH => [Fluent\Query::WITH_QUERIES => []]]); + (new Fluent\QueryBuilder())->createSql('table', [Fluent\Query::PARAM_WITH => [Fluent\Query::WITH_QUERIES => []]]); }, Fluent\Exceptions\QueryBuilderException::class, NULL, Fluent\Exceptions\QueryBuilderException::BAD_QUERY_TYPE); } @@ -1936,16 +1851,12 @@ public function testCloneQuery(): void ->where('x.another_column', 4) ->having('avg(column)', 1); - $testBaseQuery = $baseQuery - ->createSqlQuery() - ->createQuery(); + $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 - ->createSqlQuery() - ->createQuery(); + $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 20f4be1..dc7cab6 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\Query::create('SELECT * FROM table')->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column = $1', 1)->createQuery(); + $query = Db\Sql\Query::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\Query::createArgs('SELECT * FROM table WHERE column = $1', [1])->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column = ?', 1)->createQuery(); + $query = Db\Sql\Query::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\Query::createArgs('SELECT * FROM table WHERE column = ?', [1])->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column = ? AND column2 = ?', 1)->createQuery(); + Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column = ?', 1, 2)->createQuery(); + Db\Sql\Query::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\Query::create('SELECT * FROM ? WHERE column = ?', Db\Sql\Literal::create('table'), 1)->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM ? WHERE column = ?', Db\Sql\Expression::create('function(?, ?)', 'param1', 2), 1)->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column IN (?)', [1, 2])->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column1 = ? AND column2 IN (?)', 3, [10 => 2, 20 => 1])->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column IN (?)', [])->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column = ANY(?)', Db\Helper::createPgArray([1, 2]))->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT id FROM subtable WHERE column = ?', 1); - $query = Db\Sql\Query::create('SELECT * FROM table WHERE id IN (?)', $subquery)->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column = ?', Tests\TestEnum::One)->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column IN (?)', [Tests\TestEnum::Two, Tests\TestEnum::One])->createQuery(); + $query = Db\Sql\Query::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\Query::create('SELECT * FROM table WHERE column = ? AND text ILIKE \'What\?\'', 1)->createQuery(); + $query = Db\Sql\Query::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); } @@ -141,17 +141,19 @@ public function testPrepareQueryEscapeQuestionmark(): void public function testPrepareQueryCondition(): void { - $subquery = Db\Sql\Query::create( + $subquery = Db\Sql\Expression::create( 'SELECT id FROM subtable WHERE when = ? AND text ILIKE \'When\?\' AND year > ?', Db\Sql\Literal::create('now()'), 2005, ); - $query = Db\Sql\Query::create( + + $query = Db\Query::from(Db\Sql\Expression::create( 'SELECT * FROM table WHERE column = ? OR id IN (?) OR type IN (?)', 'yes', $subquery, [3, 2, 1], - )->createQuery(); + )); + 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); }