=== modified file 'includes/database.inc'
--- includes/database.inc	2007-05-08 16:36:55 +0000
+++ includes/database.inc	2007-05-14 16:31:33 +0000
@@ -141,35 +141,6 @@ function db_set_active($name = 'default'
 }
 
 /**
- * Helper function for db_query().
- */
-function _db_query_callback($match, $init = FALSE) {
-  static $args = NULL;
-  if ($init) {
-    $args = $match;
-    return;
-  }
-
-  switch ($match[1]) {
-    case '%d': // We must use type casting to int to convert FALSE/NULL/(TRUE?)
-      return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe
-    case '%s':
-      return db_escape_string(array_shift($args));
-    case '%%':
-      return '%';
-    case '%f':
-      return (float) array_shift($args);
-    case '%b': // binary data
-      return db_encode_blob(array_shift($args));
-  }
-}
-
-/**
- * Indicates the place holders that should be replaced in _db_query_callback().
- */
-define('DB_QUERY_REGEXP', '/(%d|%s|%%|%f|%b)/');
-
-/**
  * Runs a basic query in the active database.
  *
  * User-supplied arguments to the query should be passed in as separate
@@ -200,8 +171,7 @@ function db_query($query) {
   if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
     $args = $args[0];
   }
-  _db_query_callback($args, TRUE);
-  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
+  $query = db_query_load_values($query, $args);
   return _db_query($query);
 }
 

=== modified file 'includes/database.mysql.inc'
--- includes/database.mysql.inc	2007-05-08 16:36:55 +0000
+++ includes/database.mysql.inc	2007-05-14 16:35:28 +0000
@@ -303,8 +303,7 @@ function db_query_range($query) {
   if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
     $args = $args[0];
   }
-  _db_query_callback($args, TRUE);
-  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
+  $query = db_query_load_values($query, $args);
   $query .= ' LIMIT '. (int)$from .', '. (int)$count;
   return _db_query($query);
 }
@@ -434,6 +433,11 @@ function db_distinct_field($table, $fiel
   return preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query);
 }
 
+function db_query_load_values($query, $args) {
+  array_unshift($args, str_replace('%b', "'%s'", $query));
+  return call_user_func_array('sprintf', $args);
+}
+
 /**
  * @} End of "ingroup database".
  */

=== modified file 'includes/database.mysqli.inc'
--- includes/database.mysqli.inc	2007-04-21 18:08:41 +0000
+++ includes/database.mysqli.inc	2007-05-14 16:35:19 +0000
@@ -294,8 +294,7 @@ function db_query_range($query) {
   if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
     $args = $args[0];
   }
-  _db_query_callback($args, TRUE);
-  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
+  $query = db_query_load_values($query, $args);
   $query .= ' LIMIT '. (int)$from .', '. (int)$count;
   return _db_query($query);
 }
@@ -425,6 +424,11 @@ function db_distinct_field($table, $fiel
   return preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query);
 }
 
+function db_query_load_values($query, $args) {
+  array_unshift($args, str_replace('%b', "'%s'", $query));
+  return call_user_func_array('sprintf', $args);
+}
+
 /**
  * @} End of "ingroup database".
  */

=== modified file 'includes/database.pgsql.inc'
--- includes/database.pgsql.inc	2007-04-21 18:08:41 +0000
+++ includes/database.pgsql.inc	2007-05-14 16:39:11 +0000
@@ -433,7 +433,42 @@ function db_distinct_field($table, $fiel
 }
 
 /**
- * @} End of "ingroup database".
+ * Helper function for db_query().
+ */
+function _db_query_callback($match, $init = FALSE) {
+  static $args = NULL;
+  if ($init) {
+    $args = $match;
+    return;
+  }
+
+  switch ($match[1]) {
+    case '%d': // We must use type casting to int to convert FALSE/NULL/(TRUE?)
+      return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe
+    case '%s':
+      return db_escape_string(array_shift($args));
+    case '%%':
+      return '%';
+    case '%f':
+      return (float) array_shift($args);
+    case '%b': // binary data
+      return db_encode_blob(array_shift($args));
+    case '%u':
+      return sprintf('%u', array_shift($args));
+  }
+}
+
+/**
+ * Indicates the place holders that should be replaced in _db_query_callback().
  */
+define('DB_QUERY_REGEXP', '/(%d|%s|%%|%f|%b)/');
 
+function db_query_load_values($query, $args) {
+  _db_query_callback($args, TRUE);
+  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
+}
+
+/**
+ * @} End of "ingroup database".
+ */
 

