From ca4401d4fb57906610157380d53716ea1d4bad6f Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 16 Dec 2006 23:46:25 +0000 Subject: [PATCH] Finished updating the DB stuff for MySQL --- db.php | 281 ++++++++++++++++++++++++++++++--------------------- db_mysql.php | 166 +++++++++++++++--------------- 2 files changed, 246 insertions(+), 201 deletions(-) diff --git a/db.php b/db.php index 6ed9564..06ac9da 100644 --- a/db.php +++ b/db.php @@ -20,12 +20,13 @@ \*=====================================================================*/ /** -* Abstract Database Layer -* db.php +* Abstract Database Layer (Db.php) * * @package ISSO */ +require_once('ISSO/Functions.php'); + /** * Abstract Database Layer * @@ -34,7 +35,6 @@ * reporting, SQL analysis, and general connection functionality. * * Constants: -* [required] ISSO_DB_LAYER - The name of the DB layer module used in the application * ISSO_SHOW_QUERIES_LIVE - Show queries in page output as they are sent * * @author Blue Static @@ -43,14 +43,8 @@ * @package ISSO * */ -class DB_Abstract +abstract class BSDb { - /** - * Framework registry object - * @var object - */ - protected $registry = null; - /** * Determines whether or not errors should be shown * @var bool @@ -70,8 +64,8 @@ class DB_Abstract protected $errstr = ''; /** - * Currend open MySQL connexion - * @var resource + * Currend open database connection + * @var integer */ protected $dblink = null; @@ -93,63 +87,6 @@ class DB_Abstract */ protected $history = array(); - /** - * Command mapping list - * @var array - */ - protected $commands = array( - 'pconnect' => '%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 @@ -164,15 +101,13 @@ class DB_Abstract */ 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); + $this->dblink = $this->{($pconnect ? '_connect' : '_pConnect')}($server, $user, $password, $database); if ($this->dblink == false) { - $this->error('DB-Link == false, cannot connect'); + $this->_error('DB-Link == false, cannot connect'); return false; } @@ -180,6 +115,32 @@ class DB_Abstract } } + /** + * Abstract function that returns a database link after establishing a connection. This just + * calls the function and does not do any checking + * + * @param string Server name + * @param string User name + * @param string Password + * @param string Database name + * + * @return integer Database link + */ + protected abstract function _connect($server, $user, $password, $database); + + /** + * Abstract function that returns a database link after establishing a connection. This just + * calls the function and does not do any checking + * + * @param string Server name + * @param string User name + * @param string Password + * @param string Database name + * + * @return resource Database link + */ + protected abstract function _pConnect($server, $user, $password, $database); + // ################################################################### /** * Send a query to the open database link @@ -193,26 +154,35 @@ class DB_Abstract $time = microtime(); $this->querystr = $string; - $this->result = @call_user_func($this->commands['query'], $this->dblink, $string); + $this->result = $this->_query($this->dblink, $string); if (!$this->result) { - $this->error('Invalid SQL query'); + $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())); + $this->history[] = $history = array('query' => $string, 'time' => BSFunctions::FetchMicrotimeDiff($time), '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)); + print($this->_constructDebugQuery($history)); } } return $this->result; } + /** + * Abstract function that executes the query command on the database + * + * @param string Query string + * + * @return integer Result ID + */ + protected abstract function _query($query); + // ################################################################### /** * Escape a string (depending on character set, if supported) @@ -221,11 +191,20 @@ class DB_Abstract * * @return string Escaped string */ - public function escape_string($string) + public function escapeString($string) { - return call_user_func($this->commands['escape_string'], $this->dblink, $string); + return $this->_escapeString($string); } + /** + * Abstract function that calls the escape_string() method + * + * @param string String to escape + * + * @return string Escaped string + */ + protected abstract function _escapeString($string); + // ################################################################### /** * Escapes a binary string for insertion into the database @@ -234,11 +213,20 @@ class DB_Abstract * * @return string Escaped binary data */ - public function escape_binary($binary) + public function escapeBinary($binary) { - return call_user_func($this->commands['escape_binary'], $binary); + return $this->_escapeBinary($binary); } + /** + * Abstract function that calls escape_binary() + * + * @param string Binary to escape + * + * @return string Escaped binary + */ + protected abstract function _escapeBinary($string); + // ################################################################### /** * Unescapes a binary string that was fetched from the database @@ -247,11 +235,20 @@ class DB_Abstract * * @return string Unescaped binary data */ - public function unescape_binary($binary) + public function unescapeBinary($binary) { - return call_user_func($this->commands['unescape_binary'], $binary); + return $this->_unescapeBinary($binary); } + /** + * Abstract function that calls unescape_binary() + * + * @param string Escaped data + * + * @return string Data that has been unescaped + */ + protected abstract function _unescapeBinary($string); + // ################################################################### /** * Fetch the query result as an array @@ -261,11 +258,29 @@ class DB_Abstract * * @return array A row of the query result */ - public function fetch_array($result, $assoc = true) + public function fetchArray($result, $assoc = true) { - return call_user_func($this->commands[ ($assoc ? 'fetch_assoc' : 'fetch_row') ], $result); + return $this->{($assoc ? '_fetchAssocArray' : '_fetchRowArray')}($result); } + /** + * Abstract function that returns an associative array of given result + * + * @param integer Result + * + * @return array Result array + */ + protected abstract function _fetchAssocArray($result); + + /** + * Abstract function that returns a row array of given result + * + * @param integer Result + * + * @return array Result array + */ + protected abstract function _fetchRowArray($result); + // ################################################################### /** * Fetch the query result as an object @@ -274,11 +289,20 @@ class DB_Abstract * * @return object An object with the query result */ - public function fetch_object($result) + public function fetchObject($result) { - return call_user_func($this->commands['fetch_object'], $result); + return $this->_fetchObject($result); } + /** + * Abstract function that returns an object for a given result + * + * @param integer Result + * + * @return object Row object + */ + public abstract function _fetchObject($result); + // ################################################################### /** * Send a query and return the first row of the results @@ -288,13 +312,13 @@ class DB_Abstract * * @return mixed Results in variable formats */ - public function query_first($string, $callback = 'fetch_array') + public function queryFirst($string, $callback = 'fetchArray') { $resource = $this->query($string); if ($resource) { $return = $this->$callback($resource); - $this->free_result($resource); + $this->_freeResult($resource); return $return; } else @@ -309,24 +333,39 @@ class DB_Abstract * * @param integer Result */ - public function free_result($result) + public function freeResult($result) { - call_user_func($this->commands['free_result'], $result); + $this->_freeResult($result); $this->result = null; $this->querystr = ''; } + /** + * Abstract function that frees a given result + * + * @param integer Result ID + */ + protected abstract function _freeResult($result); + // ################################################################### /** * Fetch the unique ID of the record just inserted * * @return integer Insert-ID */ - public function insert_id() + public function insertId() { - return call_user_func($this->commands['insert_id'], $this->dblink); + return $this->_insertID(); } + /** + * Abstract function that returns the ID of the most recently-inserted + * record + * + * @return integer Insertion ID + */ + protected abstract function _insertId(); + // ################################################################### /** * Fetch the number of rows in the result @@ -335,11 +374,20 @@ class DB_Abstract * * @return integer Number of rows */ - public function num_rows($result) + public function numRows($result) { - return call_user_func($this->commands['num_rows'], $result); + return $this->_numRows($result); } + /** + * Abstract function that returns the number of rows in the result + * + * @param integer Result ID + * + * @return integer Number of rows + */ + protected abstract function _numRows($result); + // ################################################################### /** * Fetch the number of rows affected by the query @@ -348,20 +396,26 @@ class DB_Abstract * * @return integer Number of affected rows */ - public function affected_rows($result) + public function affectedRows($result) { - return call_user_func($this->commands['affected_rows'], $result); + return $this->_affectedRows($result); } + /** + * Abstract function that returns the number of affected rows in the result + * + * @param integer Result ID + * + * @return integer Number of rows + */ + protected abstract function _affectedRows($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'); - } + public abstract function transaction_start(); // ################################################################### /** @@ -370,10 +424,7 @@ class DB_Abstract * * @param string Named savepoint */ - public function transaction_savepoint($name) - { - trigger_error('DB_Abstract::transaction_savepoint() needs to be overridden when subclassed'); - } + public abstract function transaction_savepoint($name); // ################################################################### /** @@ -382,20 +433,14 @@ class DB_Abstract * * @param string Named savepoint */ - public function transaction_rollback($name) - { - trigger_error('DB_Abstract::transaction_rollback() needs to be overridden when subclassed'); - } + public abstract function transaction_rollback($name = null); // ################################################################### /** * 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'); - } + public abstract function transaction_commit(); // ################################################################### /** @@ -407,14 +452,14 @@ class DB_Abstract * * @return string A formatted table block */ - public function construct_query_debug($query) + protected function _constructDebugQuery($query) { - $block = "Query:\n\n
" . $this->registry->entity_encode($query['query']) . "
\n"; + $block = "Query:\n\n
" . htmlspecialchars($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); + return BSRegister::Message('Query Debug', $block, 1, true, false, 0); } // ################################################################### @@ -423,7 +468,7 @@ class DB_Abstract * * @param string User defined error message */ - protected function error($message) + protected function _error($message) { if ($this->showerrors) { @@ -443,7 +488,7 @@ class DB_Abstract $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); + BSRegister::Message('Database Error in `' . BSRegister::GetApplication() . '`', $message_prepped, 3); exit; } } diff --git a/db_mysql.php b/db_mysql.php index 5f7850a..cb24bef 100644 --- a/db_mysql.php +++ b/db_mysql.php @@ -20,13 +20,12 @@ \*=====================================================================*/ /** -* MySQL Database Abstraction Layer -* db_mysql.php +* MySQL Database Layer (DbMySql.php) * * @package ISSO */ -$this->load('db', null); +require_once('ISSO/Db.php'); /** * MySQL Database Abstraction Layer @@ -40,71 +39,27 @@ $this->load('db', null); * @package ISSO * */ -class DB_MySQL extends DB_Abstract +class BSDbMySql extends BSDb { - /** - * Command mapping list - * @var array - */ - private $commands = array( - 'pconnect' => '$this->command_mysql_pconnect', - 'connect' => '$this->command_mysql_connect', - 'query' => '$this->command_mysql_query', - 'error_num' => 'mysql_errno', - 'error_str' => 'mysql_error', - 'escape_string' => '$this->command_mysql_escape_string', - 'escape_binary' => '$this->escape_binary', - 'fetch_assoc' => 'mysql_fetch_assoc', - 'fetch_row' => 'mysql_fetch_row', - 'fetch_object' => 'mysql_fetch_object', - 'free_result' => 'mysql_free_result', - 'insert_id' => 'mysql_insert_id', - 'num_rows' => 'mysql_num_rows', - 'affected_rows' => 'mysql_affected_rows' - ); - - // ################################################################### - /** - * Constructor - */ - public function __construct(&$registry) - { - parent::__construct($registry); - } - // ################################################################### /** - * Wrapper: mysql_pconnect - * - * @param string Server name - * @param string User name - * @param string Password - * @param string Database - * - * @return integer DB-Link + * Wrapper: mysql_connect */ - protected function command_mysql_pconnect($server, $user, $password, $database) + protected function _connect($server, $user, $password, $database) { - $link = mysql_pconnect($server, $user, $password); - $this->select_db($database, $link); + $link = mysql_connect($server, $user, $password); + $this->_selectDb($database, $link); return $link; } // ################################################################### /** - * Wrapper: mysql_connect - * - * @param string Server name - * @param string User name - * @param string Password - * @param string Database - * - * @return integer DB-Link + * Wrapper: mysql_pconnect */ - protected function command_mysql_connect($server, $user, $password, $database) + protected function _pConnect($server, $user, $password, $database) { - $link = mysql_connect($server, $user, $password); + $link = mysql_pconnect($server, $user, $password); $this->select_db($database, $link); return $link; @@ -112,42 +67,33 @@ class DB_MySQL extends DB_Abstract // ################################################################### /** - * Wrapper: mysql_select_db + * Opens a connection to the specified database name * * @param string Database name * @param integer DB-Link */ - protected function select_db($database, $link) + private function _selectDb($database, $link) { if (!mysql_select_db($database, $link)) { - $this->error('Cannot use the database \'' . $this->database . '\''); + $this->_error('Cannot use the database "' . $database . '"'); } } // ################################################################### /** * Wrapper: mysql_query - * - * @param integer DB-Link - * @param string Query string - * - * @return integer Result */ - protected function command_mysql_query($link, $string) + protected function _query($string) { - return mysql_query($string, $link); + return mysql_query($string, $this->dblink); } // ################################################################### /** - * Escapes a binary string for insertion into the database - * - * @param string Unescaped data - * - * @return string Escaped binary data + * Wrapper: mysql_escape_string */ - public function escape_binary($binary) + protected function _escapeBinary($binary) { return mysql_escape_string($binary); } @@ -155,17 +101,12 @@ class DB_MySQL extends DB_Abstract // ################################################################### /** * Wrapper: mysql(_real)_escape_string - * - * @param integer DB-Link - * @param string Unescaped text - * - * @return string Escaped text */ - protected function command_mysql_escape_string($link, $string) + protected function _escapeString($string) { if (function_exists('mysql_real_escape_string')) { - return @mysql_real_escape_string($string, $link); + return @mysql_real_escape_string($string, $this->dblink); } else { @@ -176,16 +117,75 @@ class DB_MySQL extends DB_Abstract // ################################################################### /** * Not supported: unescape binary string - * - * @param string Escaped data - * - * @return string Same data */ - protected function command_unescape_binary($string) + protected function _unescapeBinary($string) { return $string; } + // ################################################################### + /** + * Wrapper: mysql_fetch_assoc + */ + protected function _fetchAssocArray($result) + { + return mysql_fetch_assoc($result); + } + + // ################################################################### + /** + * Wrapper: mysql_fetch_row + */ + protected function _fetchRowArray($result) + { + return mysql_fetch_row($result); + } + + // ################################################################### + /** + * Wrapper: mysql_fetch_object + */ + public function _fetchObject($result) + { + return mysql_fetch_object($result); + } + + // ################################################################### + /** + * Wrapper: mysql_free_result + */ + protected function _freeResult($result) + { + mysql_free_result($result); + } + + // ################################################################### + /** + * Wrapper: mysql_insert_id + */ + protected function _insertId() + { + return mysql_insert_id($this->dblink); + } + + // ################################################################### + /** + * Wrapper: mysql_num_rows + */ + protected function _numRows($result) + { + return mysql_num_rows($result); + } + + // ################################################################### + /** + * Wrapper: mysql_affected_rows + */ + protected function _affectedRows($result) + { + return mysql_affected_rows($result); + } + // ################################################################### /** * Starts a database transaction -- 2.43.5