Commit fc063806 authored by Dries's avatar Dries

- Patch #314464 by Crell: fixing up the db_placeholder() patch that I committed incompletely.

parent 9120540d
......@@ -349,7 +349,7 @@ protected function prefixTables($sql) {
* @param $query
* The query string as SQL, with curly-braces surrounding the
* table names.
* @param $query
* @param $cache
* Whether or not to cache the prepared statement for later reuse in this
* same request. Usually we want to, but queries that require preprocessing
* cannot be safely cached.
......@@ -529,21 +529,21 @@ protected function expandArguments(&$query, &$args) {
$modified = FALSE;
foreach ($args as $key => $data) {
// is_array() is slower than checking a string value, so do that first.
// If the placeholder value to insert is an array, assume that we need
// to expand it out into a comma-delimited set of placeholders.
if (is_array($data)) {
$new_keys = array();
$base = $key;
$base[0] = ':';
foreach ($data as $i => $value) {
$candidate_placeholder = $base . '_' . $i;
while (isset($args[$candidate_placeholder])) {
$candidate_placeholder .= mt_rand();
}
$new_keys[$candidate_placeholder] = $value;
// This assumes that there are no other placeholders that use the same
// name. For example, if the array placeholder is defined as :example
// and there is already an :example_2 placeholder, this will generate
// a duplicate key. We do not account for that as the calling code
// is already broken if that happens.
$new_keys[$key . '_' . $i] = $value;
}
// Update the query with the new placeholders.
$query = str_replace($key, implode(', ', $new_keys), $query);
$query = str_replace($key, implode(', ', array_keys($new_keys)), $query);
// Update the args array with the new placeholders.
unset($args[$key]);
......
......@@ -41,7 +41,8 @@ public function query($query, array $args = array(), $options = array()) {
$stmt->execute(NULL, $options);
}
else {
$stmt = $this->prepareQuery($query);
$modified = $this->expandArguments($query, $args);
$stmt = $this->prepareQuery($query, !$modified);
$stmt->execute($args, $options);
}
......
......@@ -2085,10 +2085,10 @@ class DatabaseQueryTestCase extends DatabaseTestCase {
}
/**
* Confirm that temporary tables work and are limited to one request.
* Test that we can specify an array of values in the query by simply passing in an array.
*/
function testArraySubstitution() {
$names = db_query("SELECT name FROM {test} WHERE age IN (@ages) ORDER BY age", array('@ages' => array(25, 26, 27)))->fetchAll();
$names = db_query("SELECT name FROM {test} WHERE age IN (:ages) ORDER BY age", array(':ages' => array(25, 26, 27)))->fetchAll();
$this->assertEqual(count($names), 3, t('Correct number of names returned'));
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment