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

fix: prepared query is executed when using QueryBuilder #6164

Merged
merged 4 commits into from
Jun 21, 2022
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
5 changes: 0 additions & 5 deletions phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ parameters:
count: 1
path: system/Database/BaseConnection.php

-
message: "#^Negated boolean expression is always true\\.$#"
count: 2
path: system/Database/BaseConnection.php

-
message: "#^Negated boolean expression is always false\\.$#"
count: 3
Expand Down
38 changes: 17 additions & 21 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ abstract class BaseConnection implements ConnectionInterface

/**
* If true, no queries will actually be
* ran against the database.
* run against the database.
*
* @var bool
*/
Expand Down Expand Up @@ -603,6 +603,15 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
// the getLastQuery() method.
$this->lastQuery = $query;

// If $pretend is true, then we just want to return
// the actual query object here. There won't be
// any results to return.
if ($this->pretend) {
$query->setDuration($startTime);

return $query;
}

// Run the query for real
try {
$exception = null;
Expand All @@ -611,7 +620,7 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
$this->resultID = false;
}

if (! $this->pretend && $this->resultID === false) {
if ($this->resultID === false) {
$query->setDuration($startTime, $startTime);

// This will trigger a rollback if transactions are being used
Expand All @@ -634,10 +643,8 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
}
}

if (! $this->pretend) {
// Let others do something with this query.
Events::trigger('DBQuery', $query);
}
// Let others do something with this query.
Events::trigger('DBQuery', $query);

if ($exception !== null) {
throw $exception;
Expand All @@ -646,27 +653,16 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
return false;
}

if (! $this->pretend) {
// Let others do something with this query.
Events::trigger('DBQuery', $query);
}
// Let others do something with this query.
Events::trigger('DBQuery', $query);

return false;
}

$query->setDuration($startTime);

if (! $this->pretend) {
// Let others do something with this query
Events::trigger('DBQuery', $query);
}

// If $pretend is true, then we just want to return
// the actual query object here. There won't be
// any results to return.
if ($this->pretend) {
return $query;
}
// Let others do something with this query
Events::trigger('DBQuery', $query);

// resultID is not false, so it must be successful
if ($this->isWriteType($sql)) {
Expand Down
15 changes: 9 additions & 6 deletions tests/system/Database/Live/PreparedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class PreparedQueryTest extends CIUnitTestCase
protected function setUp(): void
{
parent::setUp();

$this->query = null;
}

Expand All @@ -47,26 +48,27 @@ protected function tearDown(): void
public function testPrepareReturnsPreparedQuery()
{
$this->query = $this->db->prepare(static fn ($db) => $db->table('user')->insert([
'name' => 'a',
'email' => 'b@example.com',
'name' => 'a',
'email' => 'b@example.com',
'country' => 'JP',
]));

$this->assertInstanceOf(BasePreparedQuery::class, $this->query);

$ec = $this->db->escapeChar;
$pre = $this->db->DBPrefix;

$placeholders = '?, ?';
$placeholders = '?, ?, ?';

if ($this->db->DBDriver === 'Postgre') {
$placeholders = '$1, $2';
$placeholders = '$1, $2, $3';
}

if ($this->db->DBDriver === 'SQLSRV') {
$database = $this->db->getDatabase();
$expected = "INSERT INTO {$ec}{$database}{$ec}.{$ec}{$this->db->schema}{$ec}.{$ec}{$pre}user{$ec} ({$ec}name{$ec},{$ec}email{$ec}) VALUES ({$placeholders})";
$expected = "INSERT INTO {$ec}{$database}{$ec}.{$ec}{$this->db->schema}{$ec}.{$ec}{$pre}user{$ec} ({$ec}name{$ec},{$ec}email{$ec},{$ec}country{$ec}) VALUES ({$placeholders})";
} else {
$expected = "INSERT INTO {$ec}{$pre}user{$ec} ({$ec}name{$ec}, {$ec}email{$ec}) VALUES ({$placeholders})";
$expected = "INSERT INTO {$ec}{$pre}user{$ec} ({$ec}name{$ec}, {$ec}email{$ec}, {$ec}country{$ec}) VALUES ({$placeholders})";
}

$this->assertSame($expected, $this->query->getQueryString());
Expand Down Expand Up @@ -105,6 +107,7 @@ public function testExecuteRunsQueryAndReturnsResultObject()
$this->query->execute('foo', 'foo@example.com', 'US');
$this->query->execute('bar', 'bar@example.com', 'GB');

$this->dontSeeInDatabase($this->db->DBPrefix . 'user', ['name' => 'a', 'email' => 'b@example.com']);
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'foo', 'email' => 'foo@example.com']);
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'bar', 'email' => 'bar@example.com']);
}
Expand Down