Skip to content

Determine if binds are simple or named by looking at the $binds array #5138

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

Merged
merged 6 commits into from
Nov 23, 2021
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
33 changes: 11 additions & 22 deletions system/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,35 +275,24 @@ protected function compileBinds()
{
$sql = $this->finalQueryString;

$hasBinds = strpos($sql, $this->bindMarker) !== false;
$hasNamedBinds = ! $hasBinds
&& preg_match('/:(?!=).+:/', $sql) === 1;

if (empty($this->binds)
|| empty($this->bindMarker)
|| (! $hasNamedBinds && ! $hasBinds)
) {
if (empty($this->binds)) {
return;
}

if (! is_array($this->binds)) {
$binds = [$this->binds];
$bindCount = 1;
} else {
$binds = $this->binds;
$binds = (array) $this->binds;

if (is_int(array_key_first($binds))) {
$bindCount = count($binds);
}
$ml = strlen($this->bindMarker);

// Reverse the binds so that duplicate named binds
// will be processed prior to the original binds.
if (! is_numeric(key(array_slice($binds, 0, 1)))) {
$this->finalQueryString = $this->matchSimpleBinds($sql, $binds, $bindCount, $ml);
} else {
// Reverse the binds so that duplicate named binds
// will be processed prior to the original binds.
$binds = array_reverse($binds);
}

$ml = strlen($this->bindMarker);
$sql = $hasNamedBinds ? $this->matchNamedBinds($sql, $binds) : $this->matchSimpleBinds($sql, $binds, $bindCount, $ml);

$this->finalQueryString = $sql;
$this->finalQueryString = $this->matchNamedBinds($sql, $binds);
}
}

/**
Expand Down
26 changes: 25 additions & 1 deletion tests/system/Database/BaseQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function testNamedBinds()
/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/3566
*/
public function testNamedBindsWithColonElseWhere()
public function testNamedBindsWithColonElsewhere()
{
$query = new Query($this->db);
$query->setQuery('SELECT `email`, @total:=(total+1) FROM `users` WHERE `id` = :id:', ['id' => 10]);
Expand All @@ -278,6 +278,30 @@ public function testNamedBindsWithColonElseWhere()
$this->assertSame($sql, $query->getQuery());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/pull/5138
*/
public function testNamedBindsWithBindMarkerElsewhere()
{
$query = new Query($this->db);
$query->setQuery('SELECT * FROM posts WHERE id = :id: AND title = \'The default bind marker is "?"\'', ['id' => 10]);

$sql = 'SELECT * FROM posts WHERE id = 10 AND title = \'The default bind marker is "?"\'';
$this->assertSame($sql, $query->getQuery());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/pull/5138
*/
public function testSimpleBindsWithNamedBindPlaceholderElsewhere()
{
$query = new Query($this->db);
$query->setQuery('SELECT * FROM posts WHERE id = ? AND title = \'A named bind placeholder looks like ":foobar:"\'', 10);

$sql = 'SELECT * FROM posts WHERE id = 10 AND title = \'A named bind placeholder looks like ":foobar:"\'';
$this->assertSame($sql, $query->getQuery());
}

/**
* @group single
*
Expand Down