'%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 */ public 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)); } } } // ################################################################### /** * Initializes the class and all subclasses under a common package name * * @return string The package name */ protected 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'); } return 'db'; } // ################################################################### /** * Connect to a the specified database * * @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 */ public function connect($server, $user, $password, $database, $pconnect) { $this->registry->check_isso_fields(get_class($this)); 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 * * @param string Query string * * @return integer Result */ public 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) * * @param string String to be escaped * * @return string Escaped string */ public function escape_string($string) { return call_user_func($this->commands['escape_string'], $this->dblink, $string); } // ################################################################### /** * Escapes a binary string for insertion into the database * * @param string Unescaped data * * @return string Escaped binary data */ public function escape_binary($binary) { return call_user_func($this->commands['escape_binary'], $binary); } // ################################################################### /** * Unescapes a binary string that was fetched from the database * * @param string Escaped data * * @return string Unescaped binary data */ public function unescape_binary($binary) { return call_user_func($this->commands['unescape_binary'], $binary); } // ################################################################### /** * Fetch the query result as an array * * @param integer Result * @param bool Return an associative array? * * @return array A row of the query result */ public 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 * * @param integer Result * * @return object An object with the query result */ public function fetch_object($result) { return call_user_func($this->commands['fetch_object'], $result); } // ################################################################### /** * Send a query and return the first row of the results * * @param string Query string * @param string Result return function (in the database layer) * * @return mixed Results in variable formats */ public 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 * * @param integer Result */ public 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 * * @return integer Insert-ID */ public function insert_id() { return call_user_func($this->commands['insert_id'], $this->dblink); } // ################################################################### /** * Fetch the number of rows in the result * * @param integer Result * * @return integer Number of rows */ public function num_rows($result) { return call_user_func($this->commands['num_rows'], $result); } // ################################################################### /** * Fetch the number of rows affected by the query * * @param integer Result * * @return integer Number of affected rows */ public 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 */ public function transaction_start() { trigger_error('DB_Abstract::transaction_start() needs to be overridden when subclassed'); } // ################################################################### /** * Sends the command to set this as a savepoint. This command should never * be reached as it's always overridden * * @param string Named savepoint */ public function transaction_savepoint($name) { trigger_error('DB_Abstract::transaction_savepoint() needs to be overridden when subclassed'); } // ################################################################### /** * Sends the command to rollback to a given savepoint. This command * should never be reached as it's always overridden * * @param string Named savepoint */ public function transaction_rollback($name) { trigger_error('DB_Abstract::transaction_rollback() needs to be overridden when subclassed'); } // ################################################################### /** * Sends the command to commit the entire transaction. This command * should never be reached as it's always overridden */ public function transaction_commit($name) { trigger_error('DB_Abstract::transaction_commit() needs to be overridden when subclassed'); } // ################################################################### /** * 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 * * @param array An entry from DB->history * * @return string A formatted table block */ public function construct_query_debug($query) { $block = "Query:\n\n
\n"; $this->registry->message('Database Error in `' . $this->registry->application . '`', $message_prepped, 3); exit; } } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>"; $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