Skip to content

Commit

Permalink
Fix for setBinds() method to sets the proper format of array
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Sep 1, 2020
1 parent fc113f2 commit 560e048
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 560e048

Please sign in to comment.