'%server %user %password %database', 'connect' => '%server %user %password %database', 'query' => '%link %query', 'error_num' => '%link', 'error_str' => '%link', 'escape_string' => '%link %string', 'escape_binary' => '%string', 'unescape_binary' => '%string', 'fetch_assoc' => '%result', 'fetch_row' => '%result', 'fetch_object' => '%result', 'free_result' => '%result', 'insert_id' => '%link', 'num_rows' => '%result', 'affected_rows' => '%result' ); // ################################################################### /** * Constructor */ function __construct(&$registry) { $this->registry =& $registry; // because ivars and call_user_func() are conspiring against us... foreach ($this->commands AS $key => $string) { if (strpos($string, '$this->') !== false) { $this->commands["$key"] = array($this, str_replace('$this->', '', $string)); } } } // ################################################################### /** * (PHP 4) Constructor */ function DB_Abstract(&$registry) { $this->__construct($registry); } // ################################################################### /** * Initializes the class and all subclasses under a common package name * * @access protected * * @return string The package name */ function init_as_package() { if (!defined('ISSO_DB_LAYER')) { define('ISSO_DB_LAYER', get_class($this)); trigger_error('ISSO_DB_LAYER was defined automatically by DB::init_as_package(). Define the constant yourself to remove this warning', E_USER_WARNING); } return 'db'; } // ################################################################### /** * Connect to a the specified database * * @access public * * @param string Server name * @param string User name * @param string Password * @param string Database name * @param bool Use p-connect? * * @return bool Result of connect */ function connect($server, $user, $password, $database, $pconnect) { if ($this->dblink == false) { $this->dblink = call_user_func(($pconnect ? $this->commands['pconnect'] : $this->commands['connect']), $server, $user, $password, $database); if ($this->dblink == false) { $this->error('DB-Link == false, cannot connect'); return false; } return true; } } // ################################################################### /** * Send a query to the open database link * * @access public * * @param string Query string * * @return integer Result */ function query($string) { $time = microtime(); $this->querystr = $string; $this->result = @call_user_func($this->commands['query'], $this->dblink, $string); if (!$this->result) { $this->error('Invalid SQL query'); } $this->history[] = $history = array('query' => $string, 'time' => ($this->registry->is_loaded('functions') ? $this->registry->modules['functions']->fetch_microtime_diff($time) : 0), 'trace' => $this->registry->format_debug_trace(debug_backtrace())); if (defined('ISSO_SHOW_QUERIES_LIVE')) { if (constant('ISSO_SHOW_QUERIES_LIVE')) { print($this->construct_query_debug($history)); } } return $this->result; } // ################################################################### /** * Escape a string (depending on character set, if supported) * * @access public * * @param string String to be escaped * * @return string Escaped string */ function escape_string($string) { return call_user_func($this->commands['escape_string'], $this->dblink, $string); } // ################################################################### /** * Escapes a binary string for insertion into the database * * @access public * * @param string Unescaped data * * @return string Escaped binary data */ function escape_binary($binary) { return call_user_func($this->commands['escape_binary'], $binary); } // ################################################################### /** * Unescapes a binary string that was fetched from the database * * @access public * * @param string Escaped data * * @return string Unescaped binary data */ function unescape_binary($binary) { return call_user_func($this->commands['unescape_binary'], $binary); } // ################################################################### /** * Fetch the query result as an array * * @access public * * @param integer Result * @param bool Return an associative array? * * @return array A row of the query result */ function fetch_array($result, $assoc = true) { return call_user_func($this->commands[ ($assoc ? 'fetch_assoc' : 'fetch_row') ], $result); } // ################################################################### /** * Fetch the query result as an object * * @access public * * @param integer Result * * @return object An object with the query result */ function fetch_object($result) { return call_user_func($this->commands['fetch_object'], $result); } // ################################################################### /** * Send a query and return the first row of the results * * @access public * * @param string Query string * @param string Result return function (in the database layer) * * @return mixed Results in variable formats */ function query_first($string, $callback = 'fetch_array') { $resource = $this->query($string); if ($resource) { $return = $this->$callback($resource); $this->free_result($resource); return $return; } else { return false; } } // ################################################################### /** * Free the current query result * * @access public * * @param integer Result */ function free_result($result) { call_user_func($this->commands['free_result'], $result); $this->result = null; $this->querystr = ''; } // ################################################################### /** * Fetch the unique ID of the record just inserted * * @access public * * @return integer Insert-ID */ function insert_id() { return call_user_func($this->commands['insert_id'], $this->dblink); } // ################################################################### /** * Fetch the number of rows in the result * * @access public * * @param integer Result * * @return integer Number of rows */ function num_rows($result) { return call_user_func($this->commands['num_rows'], $result); } // ################################################################### /** * Fetch the number of rows affected by the query * * @access public * * @param integer Result * * @return integer Number of affected rows */ function affected_rows($result) { return call_user_func($this->commands['affected_rows'], $result); } // ################################################################### /** * Sends the command to start a transaction. This command should never * be reached as it's always overridden * * @access public */ function transaction_start() { trigger_error('DB_Abstract::transaction_start() needs to be overridden when subclassed', E_USER_ERROR); } // ################################################################### /** * Sends the command to set this as a savepoint. This command should never * be reached as it's always overridden * * @access public * * @param string Named savepoint */ function transaction_savepoint($name) { trigger_error('DB_Abstract::transaction_savepoint() needs to be overridden when subclassed', E_USER_ERROR); } // ################################################################### /** * Sends the command to rollback to a given savepoint. This command * should never be reached as it's always overridden * * @access public * * @param string Named savepoint */ function transaction_rollback($name) { trigger_error('DB_Abstract::transaction_rollback() needs to be overridden when subclassed', E_USER_ERROR); } // ################################################################### /** * Sends the command to commit the entire transaction. This command * should never be reached as it's always overridden * * @access public */ function transaction_commit($name) { trigger_error('DB_Abstract::transaction_commit() needs to be overridden when subclassed', E_USER_ERROR); } // ################################################################### /** * Constructs a table of query information output that is used in some * other modules to display a list of queries. This merely formats * a DB->history array entry nicely * * @access public * * @param array An entry from DB->history * * @return string A formatted table block */ function construct_query_debug($query) { $block = "Query:\n\n
" . $this->registry->entity_encode($query['query']) . "
\n"; $block .= "\n\t\n\t\t"; $block .= "Time: $query[time]
\n\t\t
\n\t\t"; $block .= "Backtrace:\n\t\t
" . implode("
\n", $query['trace']) . "
\n\t\n"; return $this->registry->message('Query Debug', $block, 1, true, false, 0); } // ################################################################### /** * Error wrapper for ISSO->message() * * @access protected * * @param string User defined error message */ function error($message) { if ($this->showerrors) { if ($this->dblink) { $this->errnum = call_user_func($this->commands['error_num'], $this->dblink); $this->errstr = call_user_func($this->commands['error_str'], $this->dblink); } $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;'; $message_prepped = "
\n

"; $message_prepped .= "\n\t» Query:\n

" . htmlspecialchars($this->querystr) ."
\n
"; $message_prepped .= "\n\t» Error Number: " . $this->errnum . "\n
"; $message_prepped .= "\n\t» Error Message: " . $this->errstr . "\n
"; $message_prepped .= "\n\t» Additional Notes: " . $message . "\n
"; $message_prepped .= "\n\t» File: " . $_SERVER['PHP_SELF'] . "\n"; $message_prepped .= "\n

\n
"; $this->registry->message('Database Error in `' . $this->registry->application . '`', $message_prepped, 3); exit; } } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>