Index: trunk/lib/DB.inc.php
===================================================================
--- trunk/lib/DB.inc.php	(revision 119)
+++ trunk/lib/DB.inc.php	(revision 136)
@@ -4,16 +4,16 @@
  * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
  *
- * DB abstraction layer.
+ * Very lightweight DB semi-abstraction layer. Mainly to catch errors with mysql_query, with some goodies.
  *
  * @author  Quinn Comendant <quinn@strangecode.com>
- * @version 1.0.1
+ * @version 2.1
  */
 
 class DB {
 
-    // If DB::connect has successfully opened a db connection.
+    // If $db->connect has successfully opened a db connection.
     var $_connected = false;
 
-    // Database handler.
+    // Database handle.
     var $dbh;
 
@@ -30,8 +30,12 @@
         'db_pass' => null,
 
-        // Debugging.
-        'db_always_debug' => false, // TRUE = display all SQL queries.
-        'db_debug' => false, // TRUE = display db errors.
-        'db_die_on_failure' => false, // TRUE = script stops on db error.
+        // Display all SQL queries.
+        'db_always_debug' => false,
+
+        // Display db errors.
+        'db_debug' => false,
+        
+        // Script stops on db error.
+        'db_die_on_failure' => false,
     );
 
@@ -49,5 +53,5 @@
      * This method enforces the singleton pattern for this class.
      *
-     * @return  object  Reference to the global SessionCache object.
+     * @return  object  Reference to the global DB object.
      * @access  public
      * @static
@@ -65,55 +69,37 @@
 
     /**
-     * Constructor.
-     */
-    function DB()
-    {
-        // Initialize default params.
+     * Set (or overwrite existing) parameters by passing an array of new parameters.
+     *
+     * @access public
+     *
+     * @param  array    $params     Array of parameters (key => val pairs).
+     */
+    function setParam($params)
+    {
+		$app =& App::getInstance();
+    
         if (isset($params) && is_array($params)) {
             // Merge new parameters with old overriding only those passed.
             $this->_params = array_merge($this->_params, $params);
-        }
-    }
-
-    /**
-     * Set (or overwrite existing) parameters by passing an array of new parameters.
+        } else {
+            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
+        }
+    }
+
+    /**
+     * Return the value of a parameter, if it exists.
      *
      * @access public
-     *
-     * @param  array    $params     Array of parameters (key => val pairs).
-     */
-    function setParam($params)
-    {
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-
-        if (isset($params) && is_array($params)) {
-            // Merge new parameters with old overriding only those passed.
-            $_this->_params = array_merge($_this->_params, $params);
+     * @param string $param        Which parameter to return.
+     * @return mixed               Configured parameter value.
+     */
+    function getParam($param)
+    {
+		$app =& App::getInstance();
+    
+        if (isset($this->_params[$param])) {
+            return $this->_params[$param];
         } else {
-            App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
-        }
-    }
-
-    /**
-     * Return the value of a parameter.
-     *
-     * @access  public
-     *
-     * @param   string  $param      The key of the parameter to return.
-     *
-     * @return  mixed               Parameter value.
-     */
-    function getParam($param)
-    {
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-
-        if (isset($_this->_params[$param])) {
-            return $_this->_params[$param];
-        } else {
-            App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
+            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
             return null;
         }
@@ -129,33 +115,29 @@
     function connect()
     {
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-
-        if (!$_this->getParam('db_name') || !$_this->getParam('db_user') || !$_this->getParam('db_pass')) {
-            App::logMsg('Database credentials missing.', LOG_EMERG, __FILE__, __LINE__);
+		$app =& App::getInstance();
+    
+        if (!$this->getParam('db_name') || !$this->getParam('db_user') || !$this->getParam('db_pass')) {
+            $app->logMsg('Database credentials missing.', LOG_EMERG, __FILE__, __LINE__);
             return false;
         }
 
         // Connect to database. Always create a new link to the server.
-        if ($_this->dbh = mysql_connect($_this->getParam('db_server'), $_this->getParam('db_user'), $_this->getParam('db_pass'), true)) {
+        if ($this->dbh = mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) {
             // Select database
-            mysql_select_db($_this->getParam('db_name'), $_this->dbh);
+            mysql_select_db($this->getParam('db_name'), $this->dbh);
         }
 
         // Test for connection errors.
-        if (!$_this->dbh || mysql_error($_this->dbh)) {
-            $mysql_error_msg = $_this->dbh ? 'Codebase MySQL error: (' . mysql_errno($_this->dbh) . ') ' . mysql_error($_this->dbh) : 'Codebase MySQL error: Could not connect to server.';
-            App::logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__);
+        if (!$this->dbh || mysql_error($this->dbh)) {
+            $mysql_error_msg = $this->dbh ? 'Codebase MySQL error: (' . mysql_errno($this->dbh) . ') ' . mysql_error($this->dbh) : 'Codebase MySQL error: Could not connect to server.';
+            $app->logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__);
 
             // Print helpful or pretty error?
-            if ($_this->getParam('db_debug')) {
+            if ($this->getParam('db_debug')) {
                 echo $mysql_error_msg . "\n";
-            } else {
-                echo _("This page is temporarily unavailable. It should be back up in a few minutes.");
             }
 
             // Die or continue without connection?
-            if ($_this->getParam('db_die_on_failure')) {
+            if ($this->getParam('db_die_on_failure')) {
                 echo "\n\n<!-- Script execution stopped out of embarrassment. -->";
                 die;
@@ -166,8 +148,8 @@
 
         // DB connection success!
-        $_this->_connected = true;
+        $this->_connected = true;
 
         // Tell MySQL what character set we're useing. Available only on MySQL verions > 4.01.01.
-        $_this->query("/*!40101 SET NAMES '" . $_this->mysql_character_sets[strtolower(App::getParam('character_set'))] . "' */");
+        $this->query("/*!40101 SET NAMES '" . $this->mysql_character_sets[strtolower($app->getParam('character_set'))] . "' */");
 
         return true;
@@ -183,13 +165,9 @@
     function close()
     {
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-
-        if (!$_this->_connected) {
-            return false;
-        }
-
-        mysql_close($_this->dbh);
+        if (!$this->_connected) {
+            return false;
+        }
+
+        return mysql_close($this->dbh);
     }
 
@@ -204,13 +182,9 @@
     function getDBH()
     {
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-
-        if (!$_this->_connected) {
-            return false;
-        }
-
-        return $_this->dbh;
+        if (!$this->_connected) {
+            return false;
+        }
+
+        return $this->dbh;
     }
 
@@ -224,5 +198,5 @@
     function isConnected()
     {
-        return $this->_connected;
+        return (true === $this->_connected);
     }
     
@@ -238,8 +212,9 @@
     function escapeString($string)
     {
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-        return mysql_real_escape_string($string, $_this->dbh);
+        if (!$this->_connected) {
+            return false;
+        }
+
+        return mysql_real_escape_string($string, $this->dbh);
     }
 
@@ -253,12 +228,9 @@
      */
     function query($query, $debug=false)
-    {
+    {    
         static $_query_count = 0;
-
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-
-        if (!$_this->_connected) {
+        $app =& App::getInstance();
+
+        if (!$this->_connected) {
            return false;
         }
@@ -266,20 +238,20 @@
         $_query_count++;
         $debugqry = preg_replace("/\n[\t ]+/", "\n", $query);
-        if ($_this->getParam('db_always_debug') || $debug) {
+        if ($this->getParam('db_always_debug') || $debug) {
             echo "<!-- ----------------- Query $_query_count ---------------------\n$debugqry\n-->\n";
         }
 
         // Execute!
-        $qid = mysql_query($query, $_this->dbh);
+        $qid = mysql_query($query, $this->dbh);
 
         // Error checking.
-        if (!$qid || mysql_error($_this->dbh)) {
-            if ($_this->getParam('db_debug')) {
-                echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($_this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>';
+        if (!$qid || mysql_error($this->dbh)) {
+            if ($this->getParam('db_debug')) {
+                echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>';
             } else {
                 echo _("This page is temporarily unavailable. It should be back up in a few minutes.");
             }
-            App::logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($_this->dbh), mysql_error($_this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__);
-            if ($_this->getParam('db_die_on_failure')) {
+            $app->logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($this->dbh), mysql_error($this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__);
+            if ($this->getParam('db_die_on_failure')) {
                 echo "\n\n<!-- Script execution stopped out of embarrassment. -->";
                 die;
@@ -302,23 +274,21 @@
     function tableExists($table, $use_cached_results=true)
     {
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-
-        if (!$_this->_connected) {
-            return false;
-        }
-
-        if (!isset($_this->existing_tables) || !$use_cached_results) {
-            $_this->existing_tables = array();
-            $qid = $_this->query("SHOW TABLES");
+		$app =& App::getInstance();
+    
+        if (!$this->_connected) {
+            return false;
+        }
+
+        if (!isset($this->existing_tables) || !$use_cached_results) {
+            $this->existing_tables = array();
+            $qid = $this->query("SHOW TABLES");
             while (list($row) = mysql_fetch_row($qid)) {
-                $_this->existing_tables[] = $row;
-            }
-        }
-        if (in_array($table, $_this->existing_tables)) {
+                $this->existing_tables[] = $row;
+            }
+        }
+        if (in_array($table, $this->existing_tables)) {
             return true;
         } else {
-            App::logMsg(sprintf('nonexistent DB table: %s.%s', $_this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);
+            $app->logMsg(sprintf('nonexistent DB table: %s.%s', $this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);
             return false;
         }
@@ -336,14 +306,10 @@
     function columnExists($table, $columns, $strict=true, $use_cached_results=true)
     {
-        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
-            $_this =& DB::getInstance();
-        }
-
-        if (!$_this->_connected) {
+        if (!$this->_connected) {
             return false;
         }
 
         // Ensure the table exists.
-        if (!$_this->tableExists($table, $use_cached_results)) {
+        if (!$this->tableExists($table, $use_cached_results)) {
             return false;
         }
@@ -354,10 +320,10 @@
         }
 
-        if (!isset($_this->table_columns[$table]) || !$use_cached_results) {
+        if (!isset($this->table_columns[$table]) || !$use_cached_results) {
             // Populate and cache array of current columns for this table.
-            $_this->table_columns[$table] = array();
-            $qid = $_this->query("DESCRIBE $table");
+            $this->table_columns[$table] = array();
+            $qid = $this->query("DESCRIBE $table");
             while ($row = mysql_fetch_row($qid)) {
-                $_this->table_columns[$table][] = $row[0];
+                $this->table_columns[$table][] = $row[0];
             }
         }
@@ -366,9 +332,9 @@
             // Do an exact comparison of table schemas.
             sort($columns);
-            sort($_this->table_columns[$table]);
-            return $_this->table_columns[$table] == $columns;
+            sort($this->table_columns[$table]);
+            return $this->table_columns[$table] == $columns;
         } else {
             // Only check that the specified columns are available in the table.
-            $match_columns = array_intersect($_this->table_columns[$table], $columns);
+            $match_columns = array_intersect($this->table_columns[$table], $columns);
             sort($columns);
             sort($match_columns);
