Skip to content

Commit

Permalink
Execute Query without ->from() (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jan 14, 2024
1 parent 29ae078 commit 38c9090
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
53 changes: 29 additions & 24 deletions tests/AbstractQueryBuilderTest.php
Expand Up @@ -246,10 +246,12 @@ public function testBuildCondition(
[$sql, $params] = $db->getQueryBuilder()->build($query);
$replacedQuotes = DbHelper::replaceQuotes((string) $expected, $db->getDriverName());
$this->assertIsString($replacedQuotes);
$this->assertEquals('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $replacedQuotes), $sql);
$this->assertEquals(
'SELECT *'
. ($db->getDriverName() === 'oci' ? ' FROM DUAL' : '')
. (empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getDriverName())),
$sql
);
$this->assertEquals($expectedParams, $params);
}
Expand Down Expand Up @@ -349,9 +351,9 @@ public function testBuildFilterCondition(array $condition, string $expected, arr
[$sql, $params] = $qb->build($query);

$this->assertSame(
'SELECT *' . (
empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getDriverName())
),
'SELECT *'
. ($db->getDriverName() === 'oci' ? ' FROM DUAL' : '')
. (empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getDriverName())),
$sql,
);
$this->assertSame($expectedParams, $params);
Expand Down Expand Up @@ -472,10 +474,12 @@ public function testBuildLikeCondition(

[$sql, $params] = $db->getQueryBuilder()->build($query);

$replacedQuotes = DbHelper::replaceQuotes($expected, $db->getDriverName());

$this->assertIsString($replacedQuotes);
$this->assertSame('SELECT *' . (empty($expected) ? '' : ' WHERE ' . $replacedQuotes), $sql);
$this->assertSame(
'SELECT *'
. ($db->getDriverName() === 'oci' ? ' FROM DUAL' : '')
. (empty($expected) ? '' : ' WHERE ' . DbHelper::replaceQuotes($expected, $db->getDriverName())),
$sql
);
$this->assertSame($expectedParams, $params);
}

Expand Down Expand Up @@ -1306,12 +1310,13 @@ public function testBuildWithSelectOption(): void

[$sql, $params] = $qb->build($query);

$this->assertSame(
<<<SQL
SELECT DISTINCT *
SQL,
$sql,
);
$expected = 'SELECT DISTINCT *';

if ($this->getDriverName() === 'oci') {
$expected .= ' FROM DUAL';
}

$this->assertSame($expected, $sql);
$this->assertSame([], $params);
}

Expand Down Expand Up @@ -1558,16 +1563,16 @@ public function testCreateIndex(string $sql, Closure $builder): void
public function testCreateView(): void
{
$db = $this->getConnection();

$qb = $db->getQueryBuilder();

$expected = 'CREATE VIEW [[animal_view]] AS SELECT [[1]]';

if ($this->getDriverName() === 'oci') {
$expected .= ' FROM DUAL';
}

$this->assertSame(
DbHelper::replaceQuotes(
<<<SQL
CREATE VIEW [[animal_view]] AS SELECT [[1]]
SQL,
$db->getDriverName(),
),
DbHelper::replaceQuotes($expected, $db->getDriverName()),
$qb->createView('animal_view', (new query($db))->select('1')),
);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Common/CommonQueryTest.php
Expand Up @@ -81,4 +81,15 @@ public function testWithQueryRecursive()

$db->close();
}

public function testSelectWithoutFrom()
{
$db = $this->getConnection();

$query = (new Query($db))->select(new Expression('1'));

$this->assertEquals(1, $query->scalar());

$db->close();
}
}

0 comments on commit 38c9090

Please sign in to comment.