Changeset:
        927e879b2e34
        
https://sourceforge.net/p/mrbs/hg-code/ci/927e879b2e34df519739c7f6a6a4ab040e5ae3d4
Author:
        Campbell Morrison <[email protected]>
Date:
        Tue Nov 24 17:11:59 2015 +0000
Log message:

Moved SQL error handling into sql_query1() to avoid having to check for errors 
after every call.

diffstat:

 web/Themes/default/header.inc |   1 -
 web/auth/auth_db.inc          |  27 +-------------------
 web/edit_entry_handler.php    |   1 -
 web/functions_mail.inc        |  14 +++-------
 web/functions_table.inc       |   8 +-----
 web/import.php                |  56 +++++++++++++-----------------------------
 web/mrbs_sql.inc              |   7 +----
 web/mysqli.inc                |   9 +-----
 web/pgsql.inc                 |  11 +++-----
 web/search.php                |   1 -
 10 files changed, 32 insertions(+), 103 deletions(-)

diffs (truncated from 317 to 300 lines):

diff -r 92167a5bec4a -r 927e879b2e34 web/Themes/default/header.inc
--- a/web/Themes/default/header.inc     Tue Nov 17 15:55:59 2015 +0000
+++ b/web/Themes/default/header.inc     Tue Nov 24 17:11:59 2015 +0000
@@ -169,7 +169,6 @@
           $n_outstanding = sql_query1($sql);
           if ($n_outstanding < 0)
           {
-            trigger_error(sql_error(), E_USER_WARNING);
             fatal_error(FALSE, get_vocab("fatal_db_error"));
           }
           echo "<div id=\"n_outstanding\"" .
diff -r 92167a5bec4a -r 927e879b2e34 web/auth/auth_db.inc
--- a/web/auth/auth_db.inc      Tue Nov 17 15:55:59 2015 +0000
+++ b/web/auth/auth_db.inc      Tue Nov 24 17:11:59 2015 +0000
@@ -128,19 +128,8 @@
   }
   
   $result = sql_query1("SELECT level FROM $tbl_users WHERE name='" . 
sql_escape(utf8_strtolower($user)) . "' LIMIT 1");
-  if ($result == -1)
-  {
-    $sql_error = sql_error();
-    if (!empty($sql_error))
-    {
-      // It's possible that sql_query1 returned -1 because there were no 
matching rows,
-      // so we only trigger an error if there was a genuine SQL error.
-      trigger_error($sql_error, E_USER_WARNING);
-    }
-    return 0;
-  }
 
-  return $result;
+  return ($result == -1) ? 0 : $result;
 }
 
 
@@ -162,18 +151,6 @@
            
   $email = sql_query1($sql);
   
-  if ($email == -1)
-  {
-    $sql_error = sql_error();
-    if (!empty($sql_error))
-    {
-      // It's possible that sql_query1 returned -1 because there were no 
matching rows,
-      // so we only trigger an error if there was a genuine SQL error.
-      trigger_error($sql_error, E_USER_WARNING);
-    }
-    $email = '';
-  }
-  
-  return $email;
+  return ($email == -1) ? '' : $email;
 }
 
diff -r 92167a5bec4a -r 927e879b2e34 web/edit_entry_handler.php
--- a/web/edit_entry_handler.php        Tue Nov 17 15:55:59 2015 +0000
+++ b/web/edit_entry_handler.php        Tue Nov 24 17:11:59 2015 +0000
@@ -400,7 +400,6 @@
   $target_room = sql_query1("SELECT room_id FROM $tbl_entry WHERE id=$id LIMIT 
1");
   if ($target_room < 0)
   {
-    trigger_error(sql_error(), E_USER_NOTICE);
     fatal_error(FALSE, get_vocab("fatal_db_error"));
   }
 }
diff -r 92167a5bec4a -r 927e879b2e34 web/functions_mail.inc
--- a/web/functions_mail.inc    Tue Nov 17 15:55:59 2015 +0000
+++ b/web/functions_mail.inc    Tue Nov 24 17:11:59 2015 +0000
@@ -240,11 +240,8 @@
              AND a.id=r.area_id
            LIMIT 1";
   $email = sql_query1($sql);
-  if ($email == -1)
-  {
-    $email = "";
-  }
-  return $email;
+
+  return ($email == -1) ? '' : $email;
 }
 
 
@@ -269,11 +266,8 @@
              AND r.id=${id_table}.room_id
            LIMIT 1";
   $email = sql_query1($sql);
-  if ($email == -1)
-  {
-    $email = "";
-  }
-  return $email;
+
+  return ($email == -1) ? '' : $email;
 }
 
 
diff -r 92167a5bec4a -r 927e879b2e34 web/functions_table.inc
--- a/web/functions_table.inc   Tue Nov 17 15:55:59 2015 +0000
+++ b/web/functions_table.inc   Tue Nov 24 17:11:59 2015 +0000
@@ -730,7 +730,6 @@
 
   if ($n_rooms < 0)
   {
-    trigger_error(sql_error(), E_USER_WARNING);
     fatal_error(FALSE, get_vocab("fatal_db_error"));
   }
   elseif ($n_rooms == 0)
@@ -1048,12 +1047,7 @@
   $n_rooms = sql_query1($sql);
   if (($n_rooms < 0) || ($n_rooms > 1))
   {
-    if ($n_rooms < 0)
-    {
-      // SQL error, probably because the tables haven't been created
-      trigger_error(sql_error(), E_USER_WARNING);
-    }
-    else
+    if ($n_rooms > 1)
     {
       // Should never happen
       trigger_error("Internal error: multiple rooms with same id", 
E_USER_WARNING);
diff -r 92167a5bec4a -r 927e879b2e34 web/import.php
--- a/web/import.php    Tue Nov 17 15:55:59 2015 +0000
+++ b/web/import.php    Tue Nov 24 17:11:59 2015 +0000
@@ -80,7 +80,6 @@
     $count = sql_query1($sql);
     if ($count < 0)
     {
-      trigger_error(sql_error(), E_USER_WARNING);
       fatal_error(FALSE, get_vocab("fatal_db_error"));
     }
     elseif ($count == 0)
@@ -99,7 +98,6 @@
       $id = sql_query1($sql);
       if ($id < 0)
       {
-        trigger_error(sql_error(), E_USER_WARNING);
         fatal_error(FALSE, get_vocab("fatal_db_error"));
       }
       return $id;
@@ -117,31 +115,22 @@
     $area_id = sql_query1($sql);
     if ($area_id < 0)
     {
-      $sql_error = sql_error();
-      if (!empty($sql_error))
+      // The area does not exist - create it if we are allowed to
+      if (!$area_room_create)
       {
-        trigger_error(sql_error(), E_USER_WARNING);
-        fatal_error(FALSE, get_vocab("fatal_db_error"));
+        $error = get_vocab("area_does_not_exist") . " '$location_area'";
+        return FALSE;
       }
       else
       {
-        // The area does not exist - create it if we are allowed to
-        if (!$area_room_create)
+        echo get_vocab("creating_new_area") . " '$location_area'<br>\n";
+        $error_add_area = '';
+        $area_id = mrbsAddArea($location_area, $error_add_area);
+        if ($area_id === FALSE)
         {
-          $error = get_vocab("area_does_not_exist") . " '$location_area'";
+          $error = get_vocab("could_not_create_area") . " '$location_area'";
           return FALSE;
         }
-        else
-        {
-          echo get_vocab("creating_new_area") . " '$location_area'<br>\n";
-          $error_add_area = '';
-          $area_id = mrbsAddArea($location_area, $error_add_area);
-          if ($area_id === FALSE)
-          {
-            $error = get_vocab("could_not_create_area") . " '$location_area'";
-            return FALSE;
-          }
-        }
       }
     }
   }
@@ -154,31 +143,22 @@
   $room_id = sql_query1($sql);
   if ($room_id < 0)
   {
-    $sql_error = sql_error();
-    if (!empty($sql_error))
+    // The room does not exist - create it if we are allowed to
+    if (!$area_room_create)
     {
-      trigger_error(sql_error(), E_USER_WARNING);
-      fatal_error(FALSE, get_vocab("fatal_db_error"));
+      $error = get_vocab("room_does_not_exist") . " '$location_room'";
+      return FALSE;
     }
     else
     {
-      // The room does not exist - create it if we are allowed to
-      if (!$area_room_create)
+      echo get_vocab("creating_new_room") . " '$location_room'<br>\n";
+      $error_add_room = '';
+      $room_id = mrbsAddRoom($location_room, $area_id, $error_add_room);
+      if ($room_id === FALSE)
       {
-        $error = get_vocab("room_does_not_exist") . " '$location_room'";
+        $error = get_vocab("could_not_create_room") . " '$location_room'";
         return FALSE;
       }
-      else
-      {
-        echo get_vocab("creating_new_room") . " '$location_room'<br>\n";
-        $error_add_room = '';
-        $room_id = mrbsAddRoom($location_room, $area_id, $error_add_room);
-        if ($room_id === FALSE)
-        {
-          $error = get_vocab("could_not_create_room") . " '$location_room'";
-          return FALSE;
-        }
-      }
     }
   }
   return $room_id;
diff -r 92167a5bec4a -r 927e879b2e34 web/mrbs_sql.inc
--- a/web/mrbs_sql.inc  Tue Nov 17 15:55:59 2015 +0000
+++ b/web/mrbs_sql.inc  Tue Nov 24 17:11:59 2015 +0000
@@ -194,7 +194,6 @@
       $existing[$location][$interval_type][$interval_start] = sql_query1($sql);
       if ($existing[$location][$interval_type][$interval_start] < 0)
       {
-        trigger_error(sql_error(), E_USER_WARNING);
         fatal_error(FALSE, get_vocab("fatal_db_error"));
       }
       $proposed[$location][$interval_type][$interval_start] = 1;
@@ -1449,12 +1448,8 @@
   global $tbl_room;
 
   $id = sql_query1("SELECT area_id FROM $tbl_room WHERE id=$id LIMIT 1");
-  if ($id <= 0)
-  {
-    $id = 0;
-  }
 
-  return $id;
+  return ($id <= 0) ? 0 : $id;
 }
 
 
diff -r 92167a5bec4a -r 927e879b2e34 web/mysqli.inc
--- a/web/mysqli.inc    Tue Nov 17 15:55:59 2015 +0000
+++ b/web/mysqli.inc    Tue Nov 24 17:11:59 2015 +0000
@@ -85,8 +85,9 @@
   sql_mysqli_ensure_handle($db_conn);
 
   $r = $db_conn->query($sql);
-  if (! $r)
+  if (!$r)
   {
+    trigger_error($db_conn->error, E_USER_WARNING);
     return -1;
   }
 
@@ -440,12 +441,6 @@
   sql_mysqli_ensure_handle($db_conn);
 
   $res = sql_mysqli_query1("SHOW TABLES LIKE '" . sql_mysqli_escape($table) . 
"'", $db_conn);
-
-  if ($res < 0)
-  {
-    trigger_error($db_conn->error, E_USER_WARNING);
-    fatal_error(FALSE, get_vocab("fatal_db_error"));
-  }
   
   return ($res == -1) ? FALSE : TRUE;
 }
diff -r 92167a5bec4a -r 927e879b2e34 web/pgsql.inc
--- a/web/pgsql.inc     Tue Nov 17 15:55:59 2015 +0000
+++ b/web/pgsql.inc     Tue Nov 24 17:11:59 2015 +0000
@@ -119,10 +119,13 @@
   $e = error_reporting(E_ALL & ~(E_WARNING|E_NOTICE));
   $r = pg_query($db_conn, $sql);
   error_reporting($e);
-  if (! $r)
+  
+  if (!$r)
   {
+    trigger_error (sql_pgsql_error($db_conn), E_USER_WARNING);
     return -1;
   }
+  
   if (pg_num_rows($r) != 1 || pg_num_fields($r) != 1
       || ($result = pg_fetch_result($r, 0, 0)) == "")
   {
@@ -457,12 +460,6 @@
   }
 
   $res = sql_pgsql_query1($sql, $db_conn);
-  
-  if ($res < 0)
-  {

------------------------------------------------------------------------------
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to