Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute Query without ->from() #760

Merged
merged 5 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}
}
Loading