diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-12-26 11:48:18 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-12-26 11:48:18 +0000 |
commit | fc063806de3ded56d1c3d78a49e8a60de7a7fe1c (patch) | |
tree | 10003f6e58dcc741b5f0759eb510bc7787409632 | |
parent | 9120540d19b83f8686b4f4c34d9cd011f93a14a6 (diff) | |
download | brdo-fc063806de3ded56d1c3d78a49e8a60de7a7fe1c.tar.gz brdo-fc063806de3ded56d1c3d78a49e8a60de7a7fe1c.tar.bz2 |
- Patch #314464 by Crell: fixing up the db_placeholder() patch that I committed incompletely.
-rw-r--r-- | includes/database/database.inc | 20 | ||||
-rw-r--r-- | includes/database/pgsql/database.inc | 3 | ||||
-rw-r--r-- | modules/simpletest/tests/database_test.test | 4 |
3 files changed, 14 insertions, 13 deletions
diff --git a/includes/database/database.inc b/includes/database/database.inc index 900a518c7..eecb8f88c 100644 --- a/includes/database/database.inc +++ b/includes/database/database.inc @@ -349,7 +349,7 @@ abstract class DatabaseConnection extends PDO { * @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 @@ abstract class DatabaseConnection extends PDO { $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]); diff --git a/includes/database/pgsql/database.inc b/includes/database/pgsql/database.inc index 822686ba1..9231517fc 100644 --- a/includes/database/pgsql/database.inc +++ b/includes/database/pgsql/database.inc @@ -41,7 +41,8 @@ class DatabaseConnection_pgsql extends DatabaseConnection { $stmt->execute(NULL, $options); } else { - $stmt = $this->prepareQuery($query); + $modified = $this->expandArguments($query, $args); + $stmt = $this->prepareQuery($query, !$modified); $stmt->execute($args, $options); } diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index c98c5be69..a725b2eee 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -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')); } |