Skip to content

Commit 445cb65

Browse files
authored
Merge pull request #5138 from vlakoff/db-query-2
Determine if binds are simple or named by looking at the $binds array
2 parents d002547 + 9d92d56 commit 445cb65

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

system/Database/Query.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -275,35 +275,24 @@ protected function compileBinds()
275275
{
276276
$sql = $this->finalQueryString;
277277

278-
$hasBinds = strpos($sql, $this->bindMarker) !== false;
279-
$hasNamedBinds = ! $hasBinds
280-
&& preg_match('/:(?!=).+:/', $sql) === 1;
281-
282-
if (empty($this->binds)
283-
|| empty($this->bindMarker)
284-
|| (! $hasNamedBinds && ! $hasBinds)
285-
) {
278+
if (empty($this->binds)) {
286279
return;
287280
}
288281

289-
if (! is_array($this->binds)) {
290-
$binds = [$this->binds];
291-
$bindCount = 1;
292-
} else {
293-
$binds = $this->binds;
282+
$binds = (array) $this->binds;
283+
284+
if (is_int(array_key_first($binds))) {
294285
$bindCount = count($binds);
295-
}
286+
$ml = strlen($this->bindMarker);
296287

297-
// Reverse the binds so that duplicate named binds
298-
// will be processed prior to the original binds.
299-
if (! is_numeric(key(array_slice($binds, 0, 1)))) {
288+
$this->finalQueryString = $this->matchSimpleBinds($sql, $binds, $bindCount, $ml);
289+
} else {
290+
// Reverse the binds so that duplicate named binds
291+
// will be processed prior to the original binds.
300292
$binds = array_reverse($binds);
301-
}
302-
303-
$ml = strlen($this->bindMarker);
304-
$sql = $hasNamedBinds ? $this->matchNamedBinds($sql, $binds) : $this->matchSimpleBinds($sql, $binds, $bindCount, $ml);
305293

306-
$this->finalQueryString = $sql;
294+
$this->finalQueryString = $this->matchNamedBinds($sql, $binds);
295+
}
307296
}
308297

309298
/**

tests/system/Database/BaseQueryTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public function testNamedBinds()
269269
/**
270270
* @see https://github.com/codeigniter4/CodeIgniter4/issues/3566
271271
*/
272-
public function testNamedBindsWithColonElseWhere()
272+
public function testNamedBindsWithColonElsewhere()
273273
{
274274
$query = new Query($this->db);
275275
$query->setQuery('SELECT `email`, @total:=(total+1) FROM `users` WHERE `id` = :id:', ['id' => 10]);
@@ -278,6 +278,30 @@ public function testNamedBindsWithColonElseWhere()
278278
$this->assertSame($sql, $query->getQuery());
279279
}
280280

281+
/**
282+
* @see https://github.com/codeigniter4/CodeIgniter4/pull/5138
283+
*/
284+
public function testNamedBindsWithBindMarkerElsewhere()
285+
{
286+
$query = new Query($this->db);
287+
$query->setQuery('SELECT * FROM posts WHERE id = :id: AND title = \'The default bind marker is "?"\'', ['id' => 10]);
288+
289+
$sql = 'SELECT * FROM posts WHERE id = 10 AND title = \'The default bind marker is "?"\'';
290+
$this->assertSame($sql, $query->getQuery());
291+
}
292+
293+
/**
294+
* @see https://github.com/codeigniter4/CodeIgniter4/pull/5138
295+
*/
296+
public function testSimpleBindsWithNamedBindPlaceholderElsewhere()
297+
{
298+
$query = new Query($this->db);
299+
$query->setQuery('SELECT * FROM posts WHERE id = ? AND title = \'A named bind placeholder looks like ":foobar:"\'', 10);
300+
301+
$sql = 'SELECT * FROM posts WHERE id = 10 AND title = \'A named bind placeholder looks like ":foobar:"\'';
302+
$this->assertSame($sql, $query->getQuery());
303+
}
304+
281305
/**
282306
* @group single
283307
*

0 commit comments

Comments
 (0)