Skip to content

Commit

Permalink
Merge pull request #3581 from michalsn/fix/queryBinding
Browse files Browse the repository at this point in the history
Fix for setBinds() method to sets the proper format of array
  • Loading branch information
michalsn authored Sep 2, 2020
2 parents 03767da + 560e048 commit 2a4f256
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
15 changes: 13 additions & 2 deletions system/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,23 @@ public function setQuery(string $sql, $binds = null, bool $setEscape = true)
/**
* Will store the variables to bind into the query later.
*
* @param array $binds
* @param array $binds
* @param boolean $setEscape
*
* @return $this
*/
public function setBinds(array $binds)
public function setBinds(array $binds, bool $setEscape = true)
{
if ($setEscape)
{
array_walk($binds, function (&$item) {
$item = [
$item,
true,
];
});
}

$this->binds = $binds;

return $this;
Expand Down
43 changes: 43 additions & 0 deletions tests/system/Database/Live/PreparedQueryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace CodeIgniter\Database\Live;

use CodeIgniter\Database\BasePreparedQuery;
use CodeIgniter\Database\Query;
use CodeIgniter\Test\CIDatabaseTestCase;

/**
Expand Down Expand Up @@ -41,6 +42,31 @@ public function testPrepareReturnsPreparedQuery()
$query->close();
}

public function testPrepareReturnsManualPreparedQuery()
{
$query = $this->db->prepare(function ($db) {
$sql = "INSERT INTO {$db->DBPrefix}user (name, email, country) VALUES (?, ?, ?)";

return (new Query($db))->setQuery($sql);
});

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

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

$placeholders = '?, ?, ?';

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

$expected = "INSERT INTO {$pre}user (name, email, country) VALUES ({$placeholders})";
$this->assertEquals($expected, $query->getQueryString());

$query->close();
}

//--------------------------------------------------------------------

public function testExecuteRunsQueryAndReturnsResultObject()
Expand All @@ -62,6 +88,23 @@ public function testExecuteRunsQueryAndReturnsResultObject()
$query->close();
}

public function testExecuteRunsQueryAndReturnsManualResultObject()
{
$query = $this->db->prepare(function ($db) {
$sql = "INSERT INTO {$db->DBPrefix}user (name, email, country) VALUES (?, ?, ?)";

return (new Query($db))->setQuery($sql);
});

$query->execute('foo', '[email protected]', '');
$query->execute('bar', '[email protected]', '');

$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'foo', 'email' => '[email protected]']);
$this->seeInDatabase($this->db->DBPrefix . 'user', ['name' => 'bar', 'email' => '[email protected]']);

$query->close();
}

//--------------------------------------------------------------------

}

0 comments on commit 2a4f256

Please sign in to comment.