Index: drupal-6.x-dev/includes/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.inc,v
retrieving revision 1.80
diff -u -p -r1.80 database.inc
--- drupal-6.x-dev/includes/database.inc	14 Sep 2007 10:43:26 -0000	1.80
+++ drupal-6.x-dev/includes/database.inc	26 Sep 2007 05:33:19 -0000
@@ -176,21 +176,21 @@ function _db_query_callback($match, $ini
   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 '%f':
+      return (float) array_shift($args);
     case '%s':
       return db_escape_string(array_shift($args));
+    case '%b': // Binary Large OBject.
+      return db_encode_blob(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)/');
+define('DB_QUERY_REGEXP', '/(%d|%f|%s|%b|%%)/');
 
 /**
  * Helper function for db_rewrite_sql.
Index: drupal-6.x-dev/includes/database.mysql-common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysql-common.inc,v
retrieving revision 1.12
diff -u -p -r1.12 database.mysql-common.inc
--- drupal-6.x-dev/includes/database.mysql-common.inc	14 Sep 2007 17:46:32 -0000	1.12
+++ drupal-6.x-dev/includes/database.mysql-common.inc	26 Sep 2007 05:33:22 -0000
@@ -7,6 +7,27 @@
  */
 
 /**
+ * Report database status.
+ */
+function db_status_report($phase) {
+  $t = get_t();
+
+  $version = db_version();
+
+  $form['mysql'] = array(
+    'title' => $t('MySQL database'),
+    'value' => ($phase == 'runtime') ? l($version, 'admin/logs/status/sql') : $version,
+  );
+
+  if (version_compare($version, DRUPAL_MINIMUM_MYSQL) < 0) {
+    $form['mysql']['severity'] = REQUIREMENT_ERROR;
+    $form['mysql']['description'] = $t('Your MySQL Server is too old. Drupal requires at least MySQL %version.', array('%version' => DRUPAL_MINIMUM_MYSQL));
+  }
+
+  return $form;
+}
+
+/**
  * Runs a basic query in the active database.
  *
  * User-supplied arguments to the query should be passed in as separate
@@ -43,11 +64,216 @@ function db_query($query) {
 }
 
 /**
+ * Returns the last insert id.
+ *
+ * @param $table
+ *   The name of the table you inserted into.
+ * @param $field
+ *   The name of the autoincrement field.
+ */
+function db_last_insert_id($table, $field) {
+  return db_result(db_query('SELECT LAST_INSERT_ID()'));
+}
+
+/**
+ * Runs a limited-range query in the active database.
+ *
+ * Use this as a substitute for db_query() when a subset of the query is to be
+ * returned.
+ * User-supplied arguments to the query should be passed in as separate parameters
+ * so that they can be properly escaped to avoid SQL injection attacks.
+ *
+ * @param $query
+ *   A string containing an SQL query.
+ * @param ...
+ *   A variable number of arguments which are substituted into the query
+ *   using printf() syntax. The query arguments can be enclosed in one
+ *   array instead.
+ *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
+ *   in '') and %%.
+ *
+ *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
+ *   and TRUE values to decimal 1.
+ *
+ * @param $from
+ *   The first result row to return.
+ * @param $count
+ *   The maximum number of result rows to return.
+ * @return
+ *   A database query result resource, or FALSE if the query was not executed
+ *   correctly.
+ */
+function db_query_range($query) {
+  $args = func_get_args();
+  $count = array_pop($args);
+  $from = array_pop($args);
+  array_shift($args);
+
+  $query .= ' LIMIT '. (int)$from .', '. (int)$count;
+  $query = db_prefix_tables($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);
+  return _db_query($query);
+}
+
+/**
+ * Runs a SELECT query and stores its results in a temporary table.
+ *
+ * Use this as a substitute for db_query() when the results need to stored
+ * in a temporary table. Temporary tables exist for the duration of the page
+ * request.
+ * User-supplied arguments to the query should be passed in as separate parameters
+ * so that they can be properly escaped to avoid SQL injection attacks.
+ *
+ * Note that if you need to know how many results were returned, you should do
+ * a SELECT COUNT(*) on the temporary table afterwards. db_affected_rows() does
+ * not give consistent result across different database types in this case.
+ *
+ * @param $query
+ *   A string containing a normal SELECT SQL query.
+ * @param ...
+ *   A variable number of arguments which are substituted into the query
+ *   using printf() syntax. The query arguments can be enclosed in one
+ *   array instead.
+ *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
+ *   in '') and %%.
+ *
+ *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
+ *   and TRUE values to decimal 1.
+ *
+ * @param $table
+ *   The name of the temporary table to select into. This name will not be
+ *   prefixed as there is no risk of collision.
+ * @return
+ *   A database query result resource, or FALSE if the query was not executed
+ *   correctly.
+ */
+function db_query_temporary($query) {
+  $args = func_get_args();
+  $tablename = array_pop($args);
+  array_shift($args);
+
+  $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' Engine=HEAP SELECT', $query);
+  $query = db_prefix_tables($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);
+  return _db_query($query);
+}
+
+/**
+ * Returns text from a Binary Large Object value.
+ *
+ * @param $data
+ *   Data to decode.
+ * @return
+ *  Decoded data.
+ */
+function db_decode_blob($data) {
+  return $data;
+}
+
+/**
+ * Lock a table.
+ */
+function db_lock_table($table) {
+  db_query('LOCK TABLES {'. db_escape_table($table) .'} WRITE');
+}
+
+/**
+ * Unlock all locked tables.
+ */
+function db_unlock_tables() {
+  db_query('UNLOCK TABLES');
+}
+
+/**
+ * Check if a table exists.
+ */
+function db_table_exists($table) {
+  return db_fetch_object(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'")) ? TRUE : FALSE;
+}
+
+/**
+ * Check if a column exists in the given table.
+ */
+function db_column_exists($table, $column) {
+  return db_fetch_object(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", $table, $column)) ? TRUE : FALSE;
+}
+
+/**
+ * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to
+ * the SELECT list entry of the given query and the resulting query is returned.
+ * This function only applies the wrapper if a DISTINCT doesn't already exist in
+ * the query.
+ *
+ * @param $table Table containing the field to set as DISTINCT
+ * @param $field Field to set as DISTINCT
+ * @param $query Query to apply the wrapper to
+ * @return SQL query with the DISTINCT wrapper surrounding the given table.field.
+ */
+function db_distinct_field($table, $field, $query) {
+  $field_to_select = 'DISTINCT('. $table .'.'. $field .')';
+  // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
+  return preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query);
+}
+
+/**
  * @ingroup schemaapi
  * @{
  */
 
 /**
+ * This maps a generic data type in combination with its data size
+ * to the engine-specific data type.
+ */
+function db_type_map() {
+  // Put :normal last so it gets preserved by array_flip.  This makes
+  // it much easier for modules (such as schema.module) to map
+  // database types back into schema types.
+  $map = array(
+    'varchar:normal'  => 'VARCHAR',
+
+    'text:tiny'       => 'SMALLTEXT',
+    'text:small'      => 'SMALLTEXT',
+    'text:medium'     => 'MEDIUMTEXT',
+    'text:big'        => 'LONGTEXT',
+    'text:normal'     => 'TEXT',
+
+    'serial:tiny'     => 'TINYINT',
+    'serial:small'    => 'SMALLINT',
+    'serial:medium'   => 'MEDIUMINT',
+    'serial:big'      => 'BIGINT',
+    'serial:normal'   => 'INT',
+
+    'int:tiny'        => 'TINYINT',
+    'int:small'       => 'SMALLINT',
+    'int:medium'      => 'MEDIUMINT',
+    'int:big'         => 'BIGINT',
+    'int:normal'      => 'INT',
+
+    'float:tiny'      => 'FLOAT',
+    'float:small'     => 'FLOAT',
+    'float:medium'    => 'FLOAT',
+    'float:big'       => 'DOUBLE',
+    'float:normal'    => 'FLOAT',
+
+    'numeric:normal'  => 'NUMERIC',
+
+    'blob:big'        => 'LONGBLOB',
+    'blob:normal'     => 'BLOB',
+
+    'datetime:normal' => 'DATETIME',
+  );
+  return $map;
+}
+
+/**
  * Generate SQL to create a new table from a Drupal schema definition.
  *
  * @param $name
@@ -71,7 +297,7 @@ function db_create_table_sql($name, $tab
   }
 
   // Process keys & indexes.
-  $keys = _db_create_keys_sql($table);
+  $keys = _db_create_keys($table);
   if (count($keys)) {
     $sql .= implode(", \n", $keys) .", \n";
   }
@@ -84,7 +310,20 @@ function db_create_table_sql($name, $tab
   return array($sql);
 }
 
-function _db_create_keys_sql($spec) {
+function _db_create_key_sql($fields) {
+  $ret = array();
+  foreach ($fields as $field) {
+    if (is_array($field)) {
+      $ret[] = $field[0] .'('. $field[1] .')';
+    }
+    else {
+      $ret[] = $field;
+    }
+  }
+  return implode(', ', $ret);
+}
+
+function _db_create_keys($spec) {
   $keys = array();
 
   if (!empty($spec['primary key'])) {
@@ -104,19 +343,6 @@ function _db_create_keys_sql($spec) {
   return $keys;
 }
 
-function _db_create_key_sql($fields) {
-  $ret = array();
-  foreach ($fields as $field) {
-    if (is_array($field)) {
-      $ret[] = $field[0] .'('. $field[1] .')';
-    }
-    else {
-      $ret[] = $field;
-    }
-  }
-  return implode(', ', $ret);
-}
-
 /**
  * Set database-engine specific properties for a field.
  *
@@ -164,7 +390,7 @@ function _db_create_field_sql($name, $sp
   }
 
   if (!empty($spec['unsigned'])) {
-    $sql .= ' unsigned';
+    $sql .= ' UNSIGNED';
   }
 
   if (!empty($spec['not null'])) {
@@ -172,7 +398,7 @@ function _db_create_field_sql($name, $sp
   }
 
   if (!empty($spec['auto_increment'])) {
-    $sql .= ' auto_increment';
+    $sql .= ' AUTO_INCREMENT';
   }
 
   if (isset($spec['default'])) {
@@ -190,51 +416,6 @@ function _db_create_field_sql($name, $sp
 }
 
 /**
- * This maps a generic data type in combination with its data size
- * to the engine-specific data type.
- */
-function db_type_map() {
-  // Put :normal last so it gets preserved by array_flip.  This makes
-  // it much easier for modules (such as schema.module) to map
-  // database types back into schema types.
-  $map = array(
-    'varchar:normal'  => 'VARCHAR',
-
-    'text:tiny'       => 'SMALLTEXT',
-    'text:small'      => 'SMALLTEXT',
-    'text:medium'     => 'MEDIUMTEXT',
-    'text:big'        => 'LONGTEXT',
-    'text:normal'     => 'TEXT',
-
-    'serial:tiny'     => 'TINYINT',
-    'serial:small'    => 'SMALLINT',
-    'serial:medium'   => 'MEDIUMINT',
-    'serial:big'      => 'BIGINT',
-    'serial:normal'   => 'INT',
-
-    'int:tiny'        => 'TINYINT',
-    'int:small'       => 'SMALLINT',
-    'int:medium'      => 'MEDIUMINT',
-    'int:big'         => 'BIGINT',
-    'int:normal'      => 'INT',
-
-    'float:tiny'      => 'FLOAT',
-    'float:small'     => 'FLOAT',
-    'float:medium'    => 'FLOAT',
-    'float:big'       => 'DOUBLE',
-    'float:normal'    => 'FLOAT',
-
-    'numeric:normal'  => 'NUMERIC',
-
-    'blob:big'        => 'LONGBLOB',
-    'blob:normal'     => 'BLOB',
-
-    'datetime:normal' => 'DATETIME',
-  );
-  return $map;
-}
-
-/**
  * Rename a table.
  *
  * @param $ret
@@ -292,7 +473,7 @@ function db_add_field(&$ret, $table, $fi
   $query = 'ALTER TABLE {'. $table .'} ADD ';
   $query .= _db_create_field_sql($field, _db_process_field($spec));
   if (count($keys_new)) {
-    $query .= ', ADD '. implode(', ADD ', _db_create_keys_sql($keys_new));
+    $query .= ', ADD '. implode(', ADD ', _db_create_keys($keys_new));
   }
   $ret[] = update_sql($query);
   if (isset($spec['initial'])) {
@@ -514,19 +695,11 @@ function db_change_field(&$ret, $table, 
   $sql = 'ALTER TABLE {'. $table .'} CHANGE '. $field .' '.
     _db_create_field_sql($field_new, _db_process_field($spec));
   if (count($keys_new)) {
-    $sql .= ', ADD '.implode(', ADD ', _db_create_keys_sql($keys_new));
+    $sql .= ', ADD '.implode(', ADD ', _db_create_keys($keys_new));
   }
   $ret[] = update_sql($sql);
 }
 
 /**
- * Returns the last insert id.
- *
- * @param $table
- *   The name of the table you inserted into.
- * @param $field
- *   The name of the autoincrement field.
+ * @} End of "ingroup schemaapi".
  */
-function db_last_insert_id($table, $field) {
-  return db_result(db_query('SELECT LAST_INSERT_ID()'));
-}
Index: drupal-6.x-dev/includes/database.mysql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysql.inc,v
retrieving revision 1.79
diff -u -p -r1.79 database.mysql.inc
--- drupal-6.x-dev/includes/database.mysql.inc	29 Aug 2007 18:38:55 -0000	1.79
+++ drupal-6.x-dev/includes/database.mysql.inc	26 Sep 2007 05:33:26 -0000
@@ -15,27 +15,6 @@
 require_once './includes/database.mysql-common.inc';
 
 /**
- * Report database status.
- */
-function db_status_report($phase) {
-  $t = get_t();
-
-  $version = db_version();
-
-  $form['mysql'] = array(
-    'title' => $t('MySQL database'),
-    'value' => ($phase == 'runtime') ? l($version, 'admin/logs/status/sql') : $version,
-  );
-
-  if (version_compare($version, DRUPAL_MINIMUM_MYSQL) < 0) {
-    $form['mysql']['severity'] = REQUIREMENT_ERROR;
-    $form['mysql']['description'] = $t('Your MySQL Server is too old. Drupal requires at least MySQL %version.', array('%version' => DRUPAL_MINIMUM_MYSQL));
-  }
-
-  return $form;
-}
-
-/**
  * Returns the version of the database server currently in use.
  *
  * @return Database server version
@@ -230,93 +209,11 @@ function db_affected_rows() {
 }
 
 /**
- * Runs a limited-range query in the active database.
- *
- * Use this as a substitute for db_query() when a subset of the query is to be
- * returned.
- * User-supplied arguments to the query should be passed in as separate parameters
- * so that they can be properly escaped to avoid SQL injection attacks.
- *
- * @param $query
- *   A string containing an SQL query.
- * @param ...
- *   A variable number of arguments which are substituted into the query
- *   using printf() syntax. The query arguments can be enclosed in one
- *   array instead.
- *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
- *   in '') and %%.
- *
- *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
- *   and TRUE values to decimal 1.
- *
- * @param $from
- *   The first result row to return.
- * @param $count
- *   The maximum number of result rows to return.
- * @return
- *   A database query result resource, or FALSE if the query was not executed
- *   correctly.
- */
-function db_query_range($query) {
-  $args = func_get_args();
-  $count = array_pop($args);
-  $from = array_pop($args);
-  array_shift($args);
-
-  $query = db_prefix_tables($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 .= ' LIMIT '. (int)$from .', '. (int)$count;
-  return _db_query($query);
-}
-
-/**
- * Runs a SELECT query and stores its results in a temporary table.
- *
- * Use this as a substitute for db_query() when the results need to stored
- * in a temporary table. Temporary tables exist for the duration of the page
- * request.
- * User-supplied arguments to the query should be passed in as separate parameters
- * so that they can be properly escaped to avoid SQL injection attacks.
- *
- * Note that if you need to know how many results were returned, you should do
- * a SELECT COUNT(*) on the temporary table afterwards. db_affected_rows() does
- * not give consistent result across different database types in this case.
- *
- * @param $query
- *   A string containing a normal SELECT SQL query.
- * @param ...
- *   A variable number of arguments which are substituted into the query
- *   using printf() syntax. The query arguments can be enclosed in one
- *   array instead.
- *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
- *   in '') and %%.
- *
- *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
- *   and TRUE values to decimal 1.
- *
- * @param $table
- *   The name of the temporary table to select into. This name will not be
- *   prefixed as there is no risk of collision.
- * @return
- *   A database query result resource, or FALSE if the query was not executed
- *   correctly.
+ * Prepare user input for use in a database query, preventing SQL injection attacks.
  */
-function db_query_temporary($query) {
-  $args = func_get_args();
-  $tablename = array_pop($args);
-  array_shift($args);
-
-  $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' Engine=HEAP SELECT', db_prefix_tables($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);
-  return _db_query($query);
+function db_escape_string($data) {
+  global $active_db;
+  return mysql_real_escape_string($data, $active_db);
 }
 
 /**
@@ -333,70 +230,5 @@ function db_encode_blob($data) {
 }
 
 /**
- * Returns text from a Binary Large Object value.
- *
- * @param $data
- *   Data to decode.
- * @return
- *  Decoded data.
- */
-function db_decode_blob($data) {
-  return $data;
-}
-
-/**
- * Prepare user input for use in a database query, preventing SQL injection attacks.
- */
-function db_escape_string($text) {
-  global $active_db;
-  return mysql_real_escape_string($text, $active_db);
-}
-
-/**
- * Lock a table.
- */
-function db_lock_table($table) {
-  db_query('LOCK TABLES {'. db_escape_table($table) .'} WRITE');
-}
-
-/**
- * Unlock all locked tables.
- */
-function db_unlock_tables() {
-  db_query('UNLOCK TABLES');
-}
-
-/**
- * Check if a table exists.
- */
-function db_table_exists($table) {
-  return db_fetch_object(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'")) ? TRUE : FALSE;
-}
-
-/**
- * Check if a column exists in the given table.
- */
-function db_column_exists($table, $column) {
-  return db_fetch_object(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", $table, $column)) ? TRUE : FALSE;
-}
-
-/**
- * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to
- * the SELECT list entry of the given query and the resulting query is returned.
- * This function only applies the wrapper if a DISTINCT doesn't already exist in
- * the query.
- *
- * @param $table Table containing the field to set as DISTINCT
- * @param $field Field to set as DISTINCT
- * @param $query Query to apply the wrapper to
- * @return SQL query with the DISTINCT wrapper surrounding the given table.field.
- */
-function db_distinct_field($table, $field, $query) {
-  $field_to_select = 'DISTINCT('. $table .'.'. $field .')';
-  // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
-  return preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query);
-}
-
-/**
  * @} End of "ingroup database".
  */
Index: drupal-6.x-dev/includes/database.mysqli.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysqli.inc,v
retrieving revision 1.44
diff -u -p -r1.44 database.mysqli.inc
--- drupal-6.x-dev/includes/database.mysqli.inc	3 Sep 2007 17:02:16 -0000	1.44
+++ drupal-6.x-dev/includes/database.mysqli.inc	26 Sep 2007 05:33:28 -0000
@@ -19,27 +19,6 @@
 require_once './includes/database.mysql-common.inc';
 
 /**
- * Report database status.
- */
-function db_status_report($phase) {
-  $t = get_t();
-
-  $version = db_version();
-
-  $form['mysql'] = array(
-    'title' => $t('MySQL database'),
-    'value' => ($phase == 'runtime') ? l($version, 'admin/logs/status/sql') : $version,
-  );
-
-  if (version_compare($version, DRUPAL_MINIMUM_MYSQL) < 0) {
-    $form['mysql']['severity'] = REQUIREMENT_ERROR;
-    $form['mysql']['description'] = $t('Your MySQL Server is too old. Drupal requires at least MySQL %version.', array('%version' => DRUPAL_MINIMUM_MYSQL));
-  }
-
-  return $form;
-}
-
-/**
  * Returns the version of the database server currently in use.
  *
  * @return Database server version
@@ -229,93 +208,11 @@ function db_affected_rows() {
 }
 
 /**
- * Runs a limited-range query in the active database.
- *
- * Use this as a substitute for db_query() when a subset of the query is to be
- * returned.
- * User-supplied arguments to the query should be passed in as separate parameters
- * so that they can be properly escaped to avoid SQL injection attacks.
- *
- * @param $query
- *   A string containing an SQL query.
- * @param ...
- *   A variable number of arguments which are substituted into the query
- *   using printf() syntax. The query arguments can be enclosed in one
- *   array instead.
- *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
- *   in '') and %%.
- *
- *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
- *   and TRUE values to decimal 1.
- *
- * @param $from
- *   The first result row to return.
- * @param $count
- *   The maximum number of result rows to return.
- * @return
- *   A database query result resource, or FALSE if the query was not executed
- *   correctly.
- */
-function db_query_range($query) {
-  $args = func_get_args();
-  $count = array_pop($args);
-  $from = array_pop($args);
-  array_shift($args);
-
-  $query = db_prefix_tables($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 .= ' LIMIT '. (int)$from .', '. (int)$count;
-  return _db_query($query);
-}
-
-/**
- * Runs a SELECT query and stores its results in a temporary table.
- *
- * Use this as a substitute for db_query() when the results need to stored
- * in a temporary table. Temporary tables exist for the duration of the page
- * request.
- * User-supplied arguments to the query should be passed in as separate parameters
- * so that they can be properly escaped to avoid SQL injection attacks.
- *
- * Note that if you need to know how many results were returned, you should do
- * a SELECT COUNT(*) on the temporary table afterwards. db_affected_rows() does
- * not give consistent result across different database types in this case.
- *
- * @param $query
- *   A string containing a normal SELECT SQL query.
- * @param ...
- *   A variable number of arguments which are substituted into the query
- *   using printf() syntax. The query arguments can be enclosed in one
- *   array instead.
- *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
- *   in '') and %%.
- *
- *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
- *   and TRUE values to decimal 1.
- *
- * @param $table
- *   The name of the temporary table to select into. This name will not be
- *   prefixed as there is no risk of collision.
- * @return
- *   A database query result resource, or FALSE if the query was not executed
- *   correctly.
+ * Prepare user input for use in a database query, preventing SQL injection attacks.
  */
-function db_query_temporary($query) {
-  $args = func_get_args();
-  $tablename = array_pop($args);
-  array_shift($args);
-
-  $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' Engine=HEAP SELECT', db_prefix_tables($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);
-  return _db_query($query);
+function db_escape_string($data) {
+  global $active_db;
+  return mysqli_real_escape_string($active_db, $data);
 }
 
 /**
@@ -332,71 +229,5 @@ function db_encode_blob($data) {
 }
 
 /**
- * Returns text from a Binary Large OBject value.
- *
- * @param $data
- *   Data to decode.
- * @return
- *  Decoded data.
- */
-function db_decode_blob($data) {
-  return $data;
-}
-
-/**
- * Prepare user input for use in a database query, preventing SQL injection attacks.
- */
-function db_escape_string($text) {
-  global $active_db;
-  return mysqli_real_escape_string($active_db, $text);
-}
-
-/**
- * Lock a table.
- */
-function db_lock_table($table) {
-  db_query('LOCK TABLES {'. db_escape_table($table) .'} WRITE');
-}
-
-/**
- * Unlock all locked tables.
- */
-function db_unlock_tables() {
-  db_query('UNLOCK TABLES');
-}
-
-/**
- * Check if a table exists.
- */
-function db_table_exists($table) {
-  return db_fetch_object(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'")) ? TRUE : FALSE;
-}
-
-/**
- * Check if a column exists in the given table.
- */
-function db_column_exists($table, $column) {
-  return db_fetch_object(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", $table, $column)) ? TRUE : FALSE;
-}
-
-/**
- * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to
- * the SELECT list entry of the given query and the resulting query is returned.
- * This function only applies the wrapper if a DISTINCT doesn't already exist in
- * the query.
- *
- * @param $table Table containing the field to set as DISTINCT
- * @param $field Field to set as DISTINCT
- * @param $query Query to apply the wrapper to
- * @return SQL query with the DISTINCT wrapper surrounding the given table.field.
- */
-function db_distinct_field($table, $field, $query) {
-  $field_to_select = 'DISTINCT('. $table .'.'. $field .')';
-  // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
-  return preg_replace('/(SELECT.*)(?:'. $table .'\.|\s)(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1 '. $field_to_select .'\2', $query);
-}
-
-/**
  * @} End of "ingroup database".
  */
-
Index: drupal-6.x-dev/includes/database.pgsql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.pgsql.inc,v
retrieving revision 1.61
diff -u -p -r1.61 database.pgsql.inc
--- drupal-6.x-dev/includes/database.pgsql.inc	25 Sep 2007 11:44:35 -0000	1.61
+++ drupal-6.x-dev/includes/database.pgsql.inc	26 Sep 2007 05:33:33 -0000
@@ -291,13 +291,13 @@ function db_query_range($query) {
   $from = array_pop($args);
   array_shift($args);
 
+  $query .= ' LIMIT '. (int)$count .' OFFSET '. (int)$from;
   $query = db_prefix_tables($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 .= ' LIMIT '. (int)$count .' OFFSET '. (int)$from;
   return _db_query($query);
 }
 
@@ -338,7 +338,8 @@ function db_query_temporary($query) {
   $tablename = array_pop($args);
   array_shift($args);
 
-  $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' AS SELECT', db_prefix_tables($query));
+  $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' AS SELECT', $query);
+  $query = db_prefix_tables($query);
   if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
     $args = $args[0];
   }
@@ -348,6 +349,14 @@ function db_query_temporary($query) {
 }
 
 /**
+ * Prepare user input for use in a database query, preventing SQL injection attacks.
+ * Note: This function requires PostgreSQL 7.2 or later.
+ */
+function db_escape_string($data) {
+  return pg_escape_string($data);
+}
+
+/**
  * Returns a properly formatted Binary Large OBject value.
  * In case of PostgreSQL encodes data for insert into bytea field.
  *
@@ -374,14 +383,6 @@ function db_decode_blob($data) {
 }
 
 /**
- * Prepare user input for use in a database query, preventing SQL injection attacks.
- * Note: This function requires PostgreSQL 7.2 or later.
- */
-function db_escape_string($text) {
-  return pg_escape_string($text);
-}
-
-/**
  * Lock a table.
  * This function automatically starts a transaction.
  */
@@ -401,14 +402,14 @@ function db_unlock_tables() {
  * Check if a table exists.
  */
 function db_table_exists($table) {
-  return db_result(db_query("SELECT COUNT(*) FROM pg_class WHERE relname = '{". db_escape_table($table) ."}'"));
+  return db_result(db_query("SELECT COUNT(*) FROM pg_class WHERE relname = '{". db_escape_table($table) ."}'")) ? TRUE : FALSE;
 }
 
 /**
  * Check if a column exists in the given table.
  */
 function db_column_exists($table, $column) {
-  return db_result(db_query("SELECT COUNT(pg_attribute.attname) FROM pg_class, pg_attribute WHERE pg_attribute.attrelid = pg_class.oid AND pg_class.relname = '{". db_escape_table($table) ."}' AND attname='%s'", $column));
+  return db_result(db_query("SELECT COUNT(pg_attribute.attname) FROM pg_class, pg_attribute WHERE pg_attribute.attrelid = pg_class.oid AND pg_class.relname = '{". db_escape_table($table) ."}' AND attname='%s'", $column)) ? TRUE : FALSE;
 }
 
 /**
@@ -460,38 +461,41 @@ function db_type_map() {
   // it much easier for modules (such as schema.module) to map
   // database types back into schema types.
   $map = array(
-    'varchar:normal' => 'varchar',
+    'varchar:normal'  => 'VARCHAR',
 
-    'text:tiny' => 'text',
-    'text:small' => 'text',
-    'text:medium' => 'text',
-    'text:big' => 'text',
-    'text:normal' => 'text',
-
-    'int:tiny' => 'smallint',
-    'int:small' => 'smallint',
-    'int:medium' => 'int',
-    'int:big' => 'bigint',
-    'int:normal' => 'int',
-
-    'float:tiny' => 'real',
-    'float:small' => 'real',
-    'float:medium' => 'real',
-    'float:big' => 'double precision',
-    'float:normal' => 'real',
-
-    'numeric:normal'  => 'numeric',
-
-    'blob:big' => 'bytea',
-    'blob:normal' => 'bytea',
-
-    'datetime:normal' => 'timestamp',
-
-    'serial:tiny' => 'serial',
-    'serial:small' => 'serial',
-    'serial:medium' => 'serial',
-    'serial:big' => 'bigserial',
-    'serial:normal' => 'serial',
+    'text:tiny'       => 'TEXT',
+    'text:small'      => 'TEXT',
+    'text:medium'     => 'TEXT',
+    'text:big'        => 'TEXT',
+    'text:normal'     => 'TEXT',
+
+    'serial:tiny'     => 'SERIAL',
+    'serial:small'    => 'SERIAL',
+    'serial:medium'   => 'SERIAL',
+    'serial:big'      => 'BIGSERIAL',
+    'serial:normal'   => 'SERIAL',
+
+    'int:tiny'        => 'SMALLINT',
+    'int:small'       => 'SMALLINT',
+    'int:medium'      => 'INT',
+    'int:big'         => 'BIGINT',
+    'int:normal'      => 'INT',
+
+    'float:tiny'      => 'REAL',
+    'float:small'     => 'REAL',
+    'float:medium'    => 'REAL',
+    'float:big'       => 'DOUBLE PRECISION',
+    'float:normal'    => 'REAL',
+
+    'numeric:normal'  => 'NUMERIC',
+
+    'blob:big'        => 'BYTEA',
+    'blob:normal'     => 'BYTEA',
+
+    'clob:big'        => 'TEXT',
+    'clob:normal'     => 'TEXT',
+
+    'datetime:normal' => 'TIMESTAMP',
   );
   return $map;
 }
@@ -634,7 +638,7 @@ function _db_create_field_sql($name, $sp
   }
   if (isset($spec['default'])) {
     $default = is_string($spec['default']) ? "'". $spec['default'] ."'" : $spec['default'];
-    $sql .= " default $default";
+    $sql .= " DEFAULT $default";
   }
 
   return $sql;
@@ -939,4 +943,3 @@ function db_change_field(&$ret, $table, 
 /**
  * @} End of "ingroup schemaapi".
  */
-
