Index: drupal-6.x-dev/includes/database.mysql-common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysql-common.inc,v
retrieving revision 1.13
diff -u -p -r1.13 database.mysql-common.inc
--- drupal-6.x-dev/includes/database.mysql-common.inc	2 Oct 2007 16:15:56 -0000	1.13
+++ drupal-6.x-dev/includes/database.mysql-common.inc	19 Oct 2007 11:35:23 -0000
@@ -7,6 +7,55 @@
  */
 
 /**
+ * Indicates the concatenation operator.
+ */
+define('DB_CONCAT_OPERATOR', '+');
+
+/**
+ * Indicates the name of the SQL strtoupper function.
+ */
+define('DB_UPPER', 'UPPER');
+
+/**
+ * Indicates the SQL to generate a random number between 0.00 and 1.00.
+ */
+define('DB_RAND', 'RAND()');
+
+/**
+ * Indicates the name of the SQL strlen function.
+ */
+define('DB_STRLEN', 'LENGTH');
+
+/**
+ * Indicates the name of the SQL substring function.
+ */
+define('DB_SUBSTR', 'SUBSTRING');
+
+/**
+ * Portable SQL generating function.
+ *
+ * @param ...
+ *   Variable number of string parameters.
+ * @return
+ *   Portably concatenate strings.
+ */
+function db_concat() {
+  $args = func_get_args();
+  $return = implode(', ', $args);
+  return (strlen($return) > 0) ? "CONCAT($return)" : '';
+}
+
+/**
+ * Returns a string that is the equivalent of MySQL IFNULL or Oracle NVL.
+ *
+ * @return
+ *   If $expr1 is not NULL, returns $expr1; otherwise it returns $expr2.
+ */
+function db_if_null($expr1, $expr2) {
+  return " IFNULL($expr1, $expr2) ";
+}
+
+/**
  * Runs a basic query in the active database.
  *
  * User-supplied arguments to the query should be passed in as separate
Index: drupal-6.x-dev/includes/database.pgsql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.pgsql.inc,v
retrieving revision 1.63
diff -u -p -r1.63 database.pgsql.inc
--- drupal-6.x-dev/includes/database.pgsql.inc	17 Oct 2007 12:47:28 -0000	1.63
+++ drupal-6.x-dev/includes/database.pgsql.inc	19 Oct 2007 11:35:24 -0000
@@ -12,6 +12,54 @@
  */
 
 /**
+ * Indicates the concatenation operator.
+ */
+define('DB_CONCAT_OPERATOR', '||');
+
+/**
+ * Indicates the name of the SQL strtoupper function.
+ */
+define('DB_UPPER', 'UPPER');
+
+/**
+ * Indicates the SQL to generate a random number between 0.00 and 1.00.
+ */
+define('DB_RAND', 'RANDOM()');
+
+/**
+ * Indicates the name of the SQL strlen function.
+ */
+define('DB_STRLEN', 'LENGTH');
+
+/**
+ * Indicates the name of the SQL substr function.
+ */
+define('DB_SUBSTR', 'SUBSTRING');
+
+/**
+ * Return a portably concatenate strings.
+ *
+ * @param ...
+ *   Variable number of string parameters.
+ * @return
+ *   Portably concatenate strings.
+ */
+function db_concat() {
+  $args = func_get_args();
+  return implode(DB_CONCAT_OPERATOR, $args);
+}
+
+/**
+ * Returns a string that is the equivalent of MySQL IFNULL or Oracle NVL.
+ *
+ * @return
+ *   If $expr1 is not NULL, returns $expr1; otherwise it returns $expr2.
+ */
+function db_if_null($expr1, $expr2) {
+  return " COALESCE($expr1, $expr2) ";
+}
+
+/**
  * Report database status.
  */
 function db_status_report() {
Index: drupal-6.x-dev/modules/comment/comment.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/comment/comment.module,v
retrieving revision 1.593
diff -u -p -r1.593 comment.module
--- drupal-6.x-dev/modules/comment/comment.module	19 Oct 2007 10:19:02 -0000	1.593
+++ drupal-6.x-dev/modules/comment/comment.module	19 Oct 2007 11:35:24 -0000
@@ -353,9 +353,9 @@ function comment_new_page_count($num_com
       }
       else {
         // Oldest first: find the first thread with new comment
-        $result = db_query('(SELECT thread FROM {comments} WHERE nid = %d  AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1)) LIMIT 1', $node->nid, $new_replies);
+        $result = db_query('(SELECT thread FROM {comments} WHERE nid = %d  AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY '. DB_SUBSTR .'(thread, 1, ('. DB_STRLEN .'(thread) - 1)) LIMIT 1', $node->nid, $new_replies);
         $thread = substr(db_result($result), 0, -1);
-        $result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < '". $thread ."'", $node->nid);
+        $result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND ". DB_SUBSTR ."(thread, 1, (". DB_STRLEN ."(thread) - 1)) < '". $thread ."'", $node->nid);
       }
       $count = db_result($result_count);
     }
@@ -1044,7 +1044,7 @@ function comment_render($node, $cid = 0)
           ** comment structure.
           */
 
-          $query .= ' ORDER BY SUBSTRING(c.thread, 1, (LENGTH(c.thread) - 1))';
+          $query .= ' ORDER BY '. DB_SUBSTR .'(c.thread, 1, ('. DB_STRLEN .'(c.thread) - 1))';
         }
       }
       $query = db_rewrite_sql($query, 'c', 'cid');
Index: drupal-6.x-dev/modules/locale/locale.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/locale/locale.module,v
retrieving revision 1.195
diff -u -p -r1.195 locale.module
--- drupal-6.x-dev/modules/locale/locale.module	15 Oct 2007 19:51:06 -0000	1.195
+++ drupal-6.x-dev/modules/locale/locale.module	19 Oct 2007 11:35:25 -0000
@@ -328,7 +328,7 @@ function locale($string = NULL, $langcod
         // Refresh database stored cache of translations for given language.
         // We only store short strings used in current version, to improve
         // performance and consume less memory.
-        $result = db_query("SELECT s.source, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = 'default' AND s.version = '%s' AND LENGTH(s.source) < 75", $langcode, VERSION);
+        $result = db_query("SELECT s.source, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = 'default' AND s.version = '%s' AND ". DB_STRLEN ."(s.source) < 75", $langcode, VERSION);
         while ($data = db_fetch_object($result)) {
           $locale_t[$langcode][$data->source] = (empty($data->translation) ? TRUE : $data->translation);
         }
Index: drupal-6.x-dev/modules/statistics/statistics.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.admin.inc,v
retrieving revision 1.1
diff -u -p -r1.1 statistics.admin.inc
--- drupal-6.x-dev/modules/statistics/statistics.admin.inc	23 Aug 2007 16:34:44 -0000	1.1
+++ drupal-6.x-dev/modules/statistics/statistics.admin.inc	19 Oct 2007 11:35:25 -0000
@@ -82,7 +82,7 @@ function statistics_top_visitors() {
   );
 
   $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, ac.aid FROM {accesslog} a LEFT JOIN {access} ac ON ac.type = 'host' AND LOWER(a.hostname) LIKE (ac.mask) LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.hostname, a.uid, u.name, ac.aid". tablesort_sql($header);
-  $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(uid, hostname))) FROM {accesslog}";
+  $sql_cnt = "SELECT COUNT(DISTINCT(". db_concat('uid', 'hostname') . ")) FROM {accesslog}";
   $result = pager_query($sql, 30, 0, $sql_cnt);
 
   $rows = array();
Index: drupal-6.x-dev/modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.164
diff -u -p -r1.164 system.install
--- drupal-6.x-dev/modules/system/system.install	19 Oct 2007 10:19:03 -0000	1.164
+++ drupal-6.x-dev/modules/system/system.install	19 Oct 2007 11:35:27 -0000
@@ -210,19 +210,6 @@ function system_install() {
       \'SELECT greatest($1, greatest($2, $3));\'
       LANGUAGE \'sql\''
     );
-    if (!db_result(db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'rand'"))) {
-      db_query('CREATE OR REPLACE FUNCTION "rand"() RETURNS float AS
-        \'SELECT random();\'
-        LANGUAGE \'sql\''
-      );
-    }
-
-    if (!db_result(db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'concat'"))) {
-      db_query('CREATE OR REPLACE FUNCTION "concat"(text, text) RETURNS text AS
-        \'SELECT $1 || $2;\'
-        LANGUAGE \'sql\''
-      );
-    }
     db_query('CREATE OR REPLACE FUNCTION "if"(boolean, text, text) RETURNS text AS
       \'SELECT CASE WHEN $1 THEN $2 ELSE $3 END;\'
       LANGUAGE \'sql\''
