Ignore:
Timestamp:
Jun 9, 2006 9:25:54 PM (18 years ago)
Author:
scdev
Message:

Q - added tags/2.0.2 as the first php5 compatible version of the 2.0 branch.

Location:
tags/2.0.2
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • tags/2.0.2/lib/DB.inc.php

    r71 r157  
    8585    function setParam($params)
    8686    {
    87         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    88             $this =& DB::getInstance();
    89         }
     87        $_this =& DB::getInstance();
    9088
    9189        if (isset($params) && is_array($params)) {
    9290            // Merge new parameters with old overriding only those passed.
    93             $this->_params = array_merge($this->_params, $params);
     91            $_this->_params = array_merge($_this->_params, $params);
    9492        } else {
    9593            App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
     
    108106    function getParam($param)
    109107    {
    110         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    111             $this =& DB::getInstance();
    112         }
    113 
    114         if (isset($this->_params[$param])) {
    115             return $this->_params[$param];
     108        $_this =& DB::getInstance();
     109
     110        if (isset($_this->_params[$param])) {
     111            return $_this->_params[$param];
    116112        } else {
    117113            App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
     
    129125    function connect()
    130126    {
    131         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    132             $this =& DB::getInstance();
    133         }
    134 
    135         if (!$this->getParam('db_name') || !$this->getParam('db_user') || !$this->getParam('db_pass')) {
     127        $_this =& DB::getInstance();
     128
     129        if (!$_this->getParam('db_name') || !$_this->getParam('db_user') || !$_this->getParam('db_pass')) {
    136130            App::logMsg('Database credentials missing.', LOG_EMERG, __FILE__, __LINE__);
    137131            return false;
     
    139133
    140134        // Connect to database. Always create a new link to the server.
    141         if ($this->dbh = mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) {
     135        if ($_this->dbh = mysql_connect($_this->getParam('db_server'), $_this->getParam('db_user'), $_this->getParam('db_pass'), true)) {
    142136            // Select database
    143             mysql_select_db($this->getParam('db_name'), $this->dbh);
     137            mysql_select_db($_this->getParam('db_name'), $_this->dbh);
    144138        }
    145139
    146140        // Test for connection errors.
    147         if (!$this->dbh || mysql_error($this->dbh)) {
    148             $mysql_error_msg = $this->dbh ? 'Codebase MySQL error: (' . mysql_errno($this->dbh) . ') ' . mysql_error($this->dbh) : 'Codebase MySQL error: Could not connect to server.';
     141        if (!$_this->dbh || mysql_error($_this->dbh)) {
     142            $mysql_error_msg = $_this->dbh ? 'Codebase MySQL error: (' . mysql_errno($_this->dbh) . ') ' . mysql_error($_this->dbh) : 'Codebase MySQL error: Could not connect to server.';
    149143            App::logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__);
    150144
    151145            // Print helpful or pretty error?
    152             if ($this->getParam('db_debug')) {
     146            if ($_this->getParam('db_debug')) {
    153147                echo $mysql_error_msg . "\n";
    154148            } else {
     
    157151
    158152            // Die or continue without connection?
    159             if ($this->getParam('db_die_on_failure')) {
     153            if ($_this->getParam('db_die_on_failure')) {
    160154                echo "\n\n<!-- Script execution stopped out of embarrassment. -->";
    161155                die;
     
    166160
    167161        // DB connection success!
    168         $this->_connected = true;
     162        $_this->_connected = true;
    169163
    170164        // Tell MySQL what character set we're useing. Available only on MySQL verions > 4.01.01.
    171         $this->query("/*!40101 SET NAMES '" . $this->mysql_character_sets[strtolower(App::getParam('character_set'))] . "' */");
     165        $_this->query("/*!40101 SET NAMES '" . $_this->mysql_character_sets[strtolower(App::getParam('character_set'))] . "' */");
    172166
    173167        return true;
     
    183177    function close()
    184178    {
    185         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    186             $this =& DB::getInstance();
    187         }
    188 
    189         if (!$this->_connected) {
    190             return false;
    191         }
    192 
    193         mysql_close($this->dbh);
     179        $_this =& DB::getInstance();
     180
     181        if (!$_this->_connected) {
     182            return false;
     183        }
     184
     185        mysql_close($_this->dbh);
    194186    }
    195187
     
    198190     *
    199191     * @access  public
    200      * @return  resource Current value of $this->dbh.
     192     * @return  resource Current value of $_this->dbh.
    201193     * @author  Quinn Comendant <quinn@strangecode.com>
    202194     * @since   20 Aug 2005 13:50:36
     
    204196    function getDBH()
    205197    {
    206         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    207             $this =& DB::getInstance();
    208         }
    209 
    210         if (!$this->_connected) {
    211             return false;
    212         }
    213 
    214         return $this->dbh;
     198        $_this =& DB::getInstance();
     199
     200        if (!$_this->_connected) {
     201            return false;
     202        }
     203
     204        return $_this->dbh;
    215205    }
    216206
     
    224214    function isConnected()
    225215    {
    226         return $this->_connected;
     216        $_this =& DB::getInstance();
     217
     218        return $_this->_connected;
    227219    }
    228220   
     
    238230    function escapeString($string)
    239231    {
    240         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    241             $this =& DB::getInstance();
    242         }
    243         return mysql_real_escape_string($string, $this->dbh);
     232        $_this =& DB::getInstance();
     233
     234        return mysql_real_escape_string($string, $_this->dbh);
    244235    }
    245236
     
    256247        static $_query_count = 0;
    257248
    258         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    259             $this =& DB::getInstance();
    260         }
    261 
    262         if (!$this->_connected) {
     249        $_this =& DB::getInstance();
     250
     251        if (!$_this->_connected) {
    263252           return false;
    264253        }
     
    266255        $_query_count++;
    267256        $debugqry = preg_replace("/\n[\t ]+/", "\n", $query);
    268         if ($this->getParam('db_always_debug') || $debug) {
     257        if ($_this->getParam('db_always_debug') || $debug) {
    269258            echo "<!-- ----------------- Query $_query_count ---------------------\n$debugqry\n-->\n";
    270259        }
    271260
    272261        // Execute!
    273         $qid = mysql_query($query, $this->dbh);
     262        $qid = mysql_query($query, $_this->dbh);
    274263
    275264        // Error checking.
    276         if (!$qid || mysql_error($this->dbh)) {
    277             if ($this->getParam('db_debug')) {
    278                 echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>';
     265        if (!$qid || mysql_error($_this->dbh)) {
     266            if ($_this->getParam('db_debug')) {
     267                echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($_this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>';
    279268            } else {
    280269                echo _("This page is temporarily unavailable. It should be back up in a few minutes.");
    281270            }
    282             App::logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($this->dbh), mysql_error($this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__);
    283             if ($this->getParam('db_die_on_failure')) {
     271            App::logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($_this->dbh), mysql_error($_this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__);
     272            if ($_this->getParam('db_die_on_failure')) {
    284273                echo "\n\n<!-- Script execution stopped out of embarrassment. -->";
    285274                die;
     
    302291    function tableExists($table, $use_cached_results=true)
    303292    {
    304         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    305             $this =& DB::getInstance();
    306         }
    307 
    308         if (!$this->_connected) {
    309             return false;
    310         }
    311 
    312         if (!isset($this->existing_tables) || !$use_cached_results) {
    313             $this->existing_tables = array();
    314             $qid = $this->query("SHOW TABLES");
     293        $_this =& DB::getInstance();
     294
     295        if (!$_this->_connected) {
     296            return false;
     297        }
     298
     299        if (!isset($_this->existing_tables) || !$use_cached_results) {
     300            $_this->existing_tables = array();
     301            $qid = $_this->query("SHOW TABLES");
    315302            while (list($row) = mysql_fetch_row($qid)) {
    316                 $this->existing_tables[] = $row;
    317             }
    318         }
    319         if (in_array($table, $this->existing_tables)) {
     303                $_this->existing_tables[] = $row;
     304            }
     305        }
     306        if (in_array($table, $_this->existing_tables)) {
    320307            return true;
    321308        } else {
    322             App::logMsg(sprintf('nonexistent DB table: %s.%s', $this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);
     309            App::logMsg(sprintf('nonexistent DB table: %s.%s', $_this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);
    323310            return false;
    324311        }
     
    336323    function columnExists($table, $columns, $strict=true, $use_cached_results=true)
    337324    {
    338         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    339             $this =& DB::getInstance();
    340         }
    341 
    342         if (!$this->_connected) {
     325        $_this =& DB::getInstance();
     326
     327        if (!$_this->_connected) {
    343328            return false;
    344329        }
    345330
    346331        // Ensure the table exists.
    347         if (!$this->tableExists($table, $use_cached_results)) {
     332        if (!$_this->tableExists($table, $use_cached_results)) {
    348333            return false;
    349334        }
     
    354339        }
    355340
    356         if (!isset($this->table_columns[$table]) || !$use_cached_results) {
     341        if (!isset($_this->table_columns[$table]) || !$use_cached_results) {
    357342            // Populate and cache array of current columns for this table.
    358             $this->table_columns[$table] = array();
    359             $qid = $this->query("DESCRIBE $table");
     343            $_this->table_columns[$table] = array();
     344            $qid = $_this->query("DESCRIBE $table");
    360345            while ($row = mysql_fetch_row($qid)) {
    361                 $this->table_columns[$table][] = $row[0];
     346                $_this->table_columns[$table][] = $row[0];
    362347            }
    363348        }
     
    366351            // Do an exact comparison of table schemas.
    367352            sort($columns);
    368             sort($this->table_columns[$table]);
    369             return $this->table_columns[$table] == $columns;
     353            sort($_this->table_columns[$table]);
     354            return $_this->table_columns[$table] == $columns;
    370355        } else {
    371356            // Only check that the specified columns are available in the table.
    372             $match_columns = array_intersect($this->table_columns[$table], $columns);
     357            $match_columns = array_intersect($_this->table_columns[$table], $columns);
    373358            sort($columns);
    374359            sort($match_columns);
     
    386371    function resetCache()
    387372    {
    388         $this->existing_tables = null;
    389         $this->table_columns = null;
     373        $_this->existing_tables = null;
     374        $_this->table_columns = null;
    390375    }
    391376
Note: See TracChangeset for help on using the changeset viewer.