Skip to content
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

Support named placeholders logging and test #223

Merged
merged 1 commit into from
Jun 27, 2014
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
38 changes: 21 additions & 17 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,29 +456,33 @@ protected static function _log_query($query, $parameters, $connection_name, $que
self::$_query_log[$connection_name] = array();
}

// Strip out any non-integer indexes from the parameters
foreach($parameters as $key => $value) {
if (!is_int($key)) unset($parameters[$key]);
}

if (count($parameters) > 0) {
if (empty($parameters)) {
$bound_query = $query;
} else {
// Escape the parameters
$parameters = array_map(array(self::get_db($connection_name), 'quote'), $parameters);

// Avoid %format collision for vsprintf
$query = str_replace("%", "%%", $query);
if (array_values($parameters) === $parameters) {
// ? placeholders
// Avoid %format collision for vsprintf
$query = str_replace("%", "%%", $query);

// Replace placeholders in the query for vsprintf
if(false !== strpos($query, "'") || false !== strpos($query, '"')) {
$query = IdiormString::str_replace_outside_quotes("?", "%s", $query);
// Replace placeholders in the query for vsprintf
if(false !== strpos($query, "'") || false !== strpos($query, '"')) {
$query = IdiormString::str_replace_outside_quotes("?", "%s", $query);
} else {
$query = str_replace("?", "%s", $query);
}

// Replace the question marks in the query with the parameters
$bound_query = vsprintf($query, $parameters);
} else {
$query = str_replace("?", "%s", $query);
// named placeholders
foreach ($parameters as $key => $val) {
$query = str_replace($key, $val, $query);
}
$bound_query = $query;
}

// Replace the question marks in the query with the parameters
$bound_query = vsprintf($query, $parameters);
} else {
$bound_query = $query;
}

self::$_last_query = $bound_query;
Expand Down
6 changes: 6 additions & 0 deletions test/QueryBuilderPsr1Test53.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ public function testRawQueryWithParameters() {
$this->assertEquals($expected, ORM::getLastQuery());
}

public function testRawQueryWithNamedPlaceholders() {
ORM::forTable('widget')->rawQuery('SELECT `w`.* FROM `widget` w WHERE `name` = :name AND `age` = :age', array(':name' => 'Fred', ':age' => 5))->findMany();
$expected = "SELECT `w`.* FROM `widget` w WHERE `name` = 'Fred' AND `age` = '5'";
$this->assertEquals($expected, ORM::getLastQuery());
}

public function testSimpleResultColumn() {
ORM::forTable('widget')->select('name')->findMany();
$expected = "SELECT `name` FROM `widget`";
Expand Down
6 changes: 6 additions & 0 deletions test/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ public function testRawQueryWithParameters() {
$this->assertEquals($expected, ORM::get_last_query());
}

public function testRawQueryWithNamedPlaceholders() {
ORM::for_table('widget')->raw_query('SELECT `w`.* FROM `widget` w WHERE `name` = :name AND `age` = :age', array(':name' => 'Fred', ':age' => 5))->find_many();
$expected = "SELECT `w`.* FROM `widget` w WHERE `name` = 'Fred' AND `age` = '5'";
$this->assertEquals($expected, ORM::get_last_query());
}

public function testSimpleResultColumn() {
ORM::for_table('widget')->select('name')->find_many();
$expected = "SELECT `name` FROM `widget`";
Expand Down
7 changes: 3 additions & 4 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ public function execute($params = NULL) {
/**
* Add data to arrays
*/
public function bindParam($index, $value, $type)
public function bindParam($key, $value, $type)
{
// Do check on index, and type
if (!is_int($index)) throw new Exception('Incorrect parameter type. Expected $index to be an integer.');
// Do check on type
if (!is_int($type) || ($type != PDO::PARAM_STR && $type != PDO::PARAM_NULL && $type != PDO::PARAM_BOOL && $type != PDO::PARAM_INT))
throw new Exception('Incorrect parameter type. Expected $type to be an integer.');

// Add param to array
$this->bindParams[$index - 1] = $value;
$this->bindParams[is_int($key) ? --$key : $key] = $value;
}

/**
Expand Down