Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.677
diff -u -p -r1.677 common.inc
--- includes/common.inc	12 Aug 2007 15:55:35 -0000	1.677
+++ includes/common.inc	21 Aug 2007 03:49:32 -0000
@@ -531,7 +531,7 @@ function drupal_http_request($url, $head
  *  0 = Log errors to database.
  *  1 = Log errors to database and to screen.
  */
-function drupal_error_handler($errno, $message, $filename, $line) {
+function drupal_error_handler($errno, $message, $filename, $line, $context) {
   // If the @ error suppression operator was used, error_reporting is temporarily set to 0
   if (error_reporting() == 0) {
     return;
@@ -539,6 +539,34 @@ function drupal_error_handler($errno, $m
 
   if ($errno & (E_ALL)) {
     $types = array(1 => 'error', 2 => 'warning', 4 => 'parse error', 8 => 'notice', 16 => 'core error', 32 => 'core warning', 64 => 'compile error', 128 => 'compile warning', 256 => 'user error', 512 => 'user warning', 1024 => 'user notice', 2048 => 'strict warning');
+
+    // For database errors, we want the line number/file name of the place that
+    // the query was originally called, not _db_query().
+    if (isset($context[DB_ERROR])) {
+      $backtrace = debug_backtrace();
+ 
+      // List of functions where SQL queries can originate, mapped to their
+      // indicies in the backtrace. This is necessary so that errors point to
+      // the right place because, for example, update_sql() calls db_query().
+      $query_functions = array(
+        'update_sql' => 4,
+        'db_query' => 3,
+        'pager_query' => 4,
+        'db_query_range' => 3,
+        'db_query_temporary' => 3,
+      );
+
+      // Determine where query function was called, and adjust line/file
+      // accordingly.
+      foreach ($query_functions as $function => $index) {
+        if ($backtrace[$index]['function'] == $function) {
+          $line = $backtrace[$index]['line'];
+          $filename = $backtrace[$index]['file'];
+          break;
+        }
+      }
+    }
+
     $entry = $types[$errno] .': '. $message .' in '. $filename .' on line '. $line .'.';
 
     // Force display of error messages in update.php
Index: includes/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.inc,v
retrieving revision 1.75
diff -u -p -r1.75 database.inc
--- includes/database.inc	6 Aug 2007 10:25:56 -0000	1.75
+++ includes/database.inc	21 Aug 2007 03:49:33 -0000
@@ -7,6 +7,13 @@
  */
 
 /**
+ * A hash value to check when outputting database errors.
+ *
+ * @see drupal_error_handler
+ */
+define('DB_ERROR', md5('DB_ERROR'));
+
+/**
  * @defgroup database Database abstraction layer
  * @{
  * Allow the use of different database servers using the same code base.
Index: includes/database.mysql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysql.inc,v
retrieving revision 1.78
diff -u -p -r1.78 database.mysql.inc
--- includes/database.mysql.inc	12 Aug 2007 15:55:35 -0000	1.78
+++ includes/database.mysql.inc	21 Aug 2007 03:49:33 -0000
@@ -154,6 +154,8 @@ function _db_query($query, $debug = 0) {
     return $result;
   }
   else {
+    // Indicate to drupal_error_handler that this is a database error.
+    ${DB_ERROR} = TRUE;
     trigger_error(check_plain(mysql_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
     return FALSE;
   }
Index: includes/database.mysqli.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysqli.inc,v
retrieving revision 1.42
diff -u -p -r1.42 database.mysqli.inc
--- includes/database.mysqli.inc	12 Aug 2007 15:55:35 -0000	1.42
+++ includes/database.mysqli.inc	21 Aug 2007 03:49:34 -0000
@@ -151,6 +151,8 @@ function _db_query($query, $debug = 0) {
     return $result;
   }
   else {
+    // Indicate to drupal_error_handler that this is a database error.
+    ${DB_ERROR} = TRUE;
     trigger_error(check_plain(mysqli_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
     return FALSE;
   }
Index: includes/database.pgsql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.pgsql.inc,v
retrieving revision 1.55
diff -u -p -r1.55 database.pgsql.inc
--- includes/database.pgsql.inc	12 Aug 2007 15:55:35 -0000	1.55
+++ includes/database.pgsql.inc	21 Aug 2007 03:49:35 -0000
@@ -171,6 +171,8 @@ function _db_query($query, $debug = 0) {
     return $last_result;
   }
   else {
+    // Indicate to drupal_error_handler that this is a database error.
+    ${DB_ERROR} = TRUE;
     trigger_error(check_plain(pg_last_error($active_db) ."\nquery: ". $query), E_USER_WARNING);
     return FALSE;
   }
