Skip to content

Commit

Permalink
Adding test for named params in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 14, 2012
1 parent 4c4a764 commit ea74b74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/Cake/Model/Datasource/Database/Connection.php
Expand Up @@ -246,15 +246,24 @@ public function charset($collation) {
* @return void
**/
protected function _bindValues($statement, $params, $types) {
$offset = 1;
if (!empty($types) && is_int(key($types))) {
if (empty($params)) {
return;
}

if (!empty($types) && ctype_digit(key($types))) {
$params = array_values($params);
}

$annonymousParams = is_int(key($params)) ? true : false;
$offset = 1;
foreach ($params as $index => $value) {
if ($annonymousParams) {
$index += $offset;
}
if (isset($types[$index])) {
$statement->bindValue($index + $offset, $value, $type);
$statement->bindValue($index, $value, $type);
} else {
$statement->bindValue($index + $offset, $value);
$statement->bindValue($index, $value);
}
}
}
Expand Down
Expand Up @@ -116,6 +116,12 @@ public function testExecuteWithArguments() {
$this->assertCount(1, $statement);
$result = $statement->fetch('assoc');
$this->assertEquals(array('total' => 6), $result);

$sql = 'SELECT 1 + :one + :two AS total';
$statement = $this->connection->execute($sql, array('one' => 2, 'two' => 3));
$this->assertCount(1, $statement);
$result = $statement->fetch('assoc');
$this->assertEquals(array('total' => 6), $result);
}

}

0 comments on commit ea74b74

Please sign in to comment.