Index: tags/2.0.2/lib/DB.inc.php
===================================================================
--- branches/2.0/lib/DB.inc.php	(revision 156)
+++ tags/2.0.2/lib/DB.inc.php	(revision 157)
@@ -85,11 +85,9 @@
     function setParam($params)
     {
-        if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
-            $this =& DB::getInstance();
-        }
+        $_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);
+            $_this->_params = array_merge($_this->_params, $params);
         } else {
             App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
@@ -108,10 +106,8 @@
     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];
+        $_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__);
@@ -129,9 +125,7 @@
     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')) {
+        $_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__);
             return false;
@@ -139,16 +133,16 @@
 
         // 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.';
+        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 {
@@ -157,5 +151,5 @@
 
             // 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 +160,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 +177,11 @@
     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);
+        $_this =& DB::getInstance();
+
+        if (!$_this->_connected) {
+            return false;
+        }
+
+        mysql_close($_this->dbh);
     }
 
@@ -198,5 +190,5 @@
      *
      * @access  public
-     * @return  resource Current value of $this->dbh.
+     * @return  resource Current value of $_this->dbh.
      * @author  Quinn Comendant <quinn@strangecode.com>
      * @since   20 Aug 2005 13:50:36
@@ -204,13 +196,11 @@
     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;
+        $_this =& DB::getInstance();
+
+        if (!$_this->_connected) {
+            return false;
+        }
+
+        return $_this->dbh;
     }
 
@@ -224,5 +214,7 @@
     function isConnected()
     {
-        return $this->_connected;
+        $_this =& DB::getInstance();
+
+        return $_this->_connected;
     }
     
@@ -238,8 +230,7 @@
     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);
+        $_this =& DB::getInstance();
+
+        return mysql_real_escape_string($string, $_this->dbh);
     }
 
@@ -256,9 +247,7 @@
         static $_query_count = 0;
 
-        if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
-            $this =& DB::getInstance();
-        }
-
-        if (!$this->_connected) {
+        $_this =& DB::getInstance();
+
+        if (!$_this->_connected) {
            return false;
         }
@@ -266,20 +255,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 +291,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");
+        $_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");
             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 +323,12 @@
     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) {
+        $_this =& DB::getInstance();
+
+        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 +339,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 +351,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);
@@ -386,6 +371,6 @@
     function resetCache()
     {
-        $this->existing_tables = null;
-        $this->table_columns = null;
+        $_this->existing_tables = null;
+        $_this->table_columns = null;
     }
 
