Skip to content

Commit

Permalink
Execute Query without ->from() (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jan 14, 2024
1 parent 805e639 commit 8e05361
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Enh #248: Change property `Schema::$typeMap` to constant `Schema::TYPE_MAP` (@Tigrov)
- Bug #250: Fix `Command::insertWithReturningPks()` method for table without primary keys (@Tigrov)
- Enh #251: Allow to use `DMLQueryBuilderInterface::batchInsert()` method with empty columns (@Tigrov)
- Bug #238: Fix execution `Query` without table(s) to select from (@Tigrov)

## 1.2.0 November 12, 2023

Expand Down
9 changes: 9 additions & 0 deletions src/DQLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public function selectExists(string $rawSql): string
return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END FROM DUAL';
}

public function buildFrom(array|null $tables, array &$params): string
{
if (empty($tables)) {
return 'FROM DUAL';
}

return parent::buildFrom($tables, $params);
}

public function buildWithQueries(array $withs, array &$params): string
{
/** @psalm-var array{query:string|Query, alias:ExpressionInterface|string, recursive:bool}[] $withs */
Expand Down
4 changes: 2 additions & 2 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function testBuildWithLimit(): void

$this->assertSame(
<<<SQL
WITH USER_SQL AS (SELECT *), PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
WITH USER_SQL AS (SELECT * FROM DUAL), PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
SELECT * FROM PAGINATION WHERE rownum <= 10
SQL,
$sql,
Expand All @@ -257,7 +257,7 @@ public function testBuildWithOffset(): void

$this->assertSame(
<<<SQL
WITH USER_SQL AS (SELECT *), PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
WITH USER_SQL AS (SELECT * FROM DUAL), PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
SELECT * FROM PAGINATION WHERE rowNumId > 10
SQL,
$sql,
Expand Down

0 comments on commit 8e05361

Please sign in to comment.