From ec8ca475f7a70332f9ced330500baf1bd9668e1d Mon Sep 17 00:00:00 2001
From: Robert Sesek
Date: Fri, 23 Dec 2005 08:21:53 +0000
Subject: [PATCH] Abstracting database layer
---
db.php | 362 +++++++++++++++++++++++++++++++++++++++++++++++++++
db_mysql.php | 298 ++++++++++--------------------------------
2 files changed, 428 insertions(+), 232 deletions(-)
create mode 100644 db.php
diff --git a/db.php b/db.php
new file mode 100644
index 0000000..9ff16ad
--- /dev/null
+++ b/db.php
@@ -0,0 +1,362 @@
+ '%server %user %password %database',
+ 'connect' => '%server %user %password %database',
+ 'query' => '%link %query',
+ 'error_num' => '%link',
+ 'error_str' => '%link',
+ 'escape_string' => '%link %string',
+ 'fetch_assoc' => '%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);
+ }
+
+ // ###################################################################
+ /**
+ * 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 Query-ID
+ */
+ function query($string)
+ {
+ $this->querystr = $string;
+ $this->result = call_user_func($this->commands['query'], $this->dblink, $string);
+ $this->history[] = $string;
+
+ if (defined('ISSO_SHOW_QUERIES_LIVE'))
+ {
+ if (constant('ISSO_SHOW_QUERIES_LIVE'))
+ {
+ print($this->querystr . '
');
+ }
+ }
+
+ if (!$this->result)
+ {
+ $this->error('Invalid SQL query');
+ }
+
+ 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);
+ }
+
+ // ###################################################################
+ /**
+ * Fetch the query result as an array
+ *
+ * @access public
+ *
+ * @param integer Query-ID
+ *
+ * @return array A row of the query result
+ */
+ function fetch_array($result)
+ {
+ return call_user_func($this->commands['fetch_assoc'], $result);
+ }
+
+ // ###################################################################
+ /**
+ * Fetch the query result as an object
+ *
+ * @access public
+ *
+ * @param integer Query-ID
+ *
+ * @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);
+ $return = $this->$callback($resource);
+ $this->free_result($resource);
+ return $return;
+ }
+
+ // ###################################################################
+ /**
+ * Free the current query result
+ *
+ * @access public
+ *
+ * @param integer Query-ID
+ */
+ 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 Query-ID
+ *
+ * @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 Query-ID
+ *
+ * @return integer Number of affected rows
+ */
+ function affected_rows($result)
+ {
+ return call_uesr_func($this->commands['affected_rows'], $result);
+ }
+
+ // ###################################################################
+ /**
+ * Error wrapper for ISSO->message()
+ *
+ * @access protected
+ *
+ * @param string User defined error message
+ */
+ function error($message)
+ {
+ if ($this->showerrors)
+ {
+ if ($this->dblink)
+ {
+ $this->errnum = $this->commands['error_num']($this->dblink);
+ $this->errstr = $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$
+|| ###################################################################
+\*=====================================================================*/
+?>
\ No newline at end of file
diff --git a/db_mysql.php b/db_mysql.php
index 2bc37d9..726a905 100644
--- a/db_mysql.php
+++ b/db_mysql.php
@@ -26,6 +26,8 @@
* @package ISSO
*/
+$this->load('db', null);
+
/**
* MySQL Database Abstraction Layer
*
@@ -38,61 +40,26 @@
* @package ISSO
*
*/
-class DB_MySQL
+class DB_MySQL extends DB_Abstract
{
/**
- * Framework registry object
- * @var object
- */
- var $registry = null;
-
- /**
- * Database that is used in the application
- * @var string
- */
- var $database = '';
-
- /**
- * Determines whether or not errors should be shown
- * @var bool
- */
- var $errshow = true;
-
- /**
- * Current error number
- * @var integer
- */
- var $error_no = 0;
-
- /**
- * Description of current error
- * @var string
- */
- var $error_str = '';
-
- /**
- * Currend open MySQL connexion
- * @var resource
- */
- var $link_id = null;
-
- /**
- * Current query ID
- * @var integer
- */
- var $query_id = null;
-
- /**
- * Current query string
- * @var string
- */
- var $query_str = '';
-
- /**
- * History of all executed queryies
+ * Command mapping list
* @var array
*/
- var $history = array();
+ var $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',
+ 'fetch_assoc' => 'mysql_fetch_assoc',
+ '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'
+ );
// ###################################################################
/**
@@ -100,7 +67,7 @@ class DB_MySQL
*/
function __construct(&$registry)
{
- $this->registry =& $registry;
+ parent::__construct($registry);
}
// ###################################################################
@@ -114,232 +81,99 @@ class DB_MySQL
// ###################################################################
/**
- * Connect to a the specified database
+ * Wrapper: mysql_pconnect
*
- * @access public
+ * @access protected
*
* @param string Server name
* @param string User name
* @param string Password
- * @param bool Use p-connect?
- *
- * @return bool Result of connect
- */
- function connect($server, $user, $password, $pconnect)
- {
- if ($this->link_id == 0)
- {
- if ($pconnect)
- {
- $this->link_id = @mysql_pconnect($server, $user, $password);
- }
- else
- {
- $this->link_id = @mysql_connect($server, $user, $password);
- }
-
- if ($this->link_id == false)
- {
- $this->error('Link-Id == false, cannot connect');
- return false;
- }
-
- if (@mysql_select_db($this->database, $this->link_id))
- {
- return true;
- }
- else
- {
- $this->error('Cannot use the database \'' . $this->database . '\'');
- return false;
- }
- }
- }
-
- // ###################################################################
- /**
- * Send a query to MySQL
- *
- * @access public
- *
- * @param string Query string
+ * @param string Database
*
- * @return integer Resource ID for query
+ * @return integer DB-Link
*/
- function query($query_str)
+ function command_mysql_pconnect($server, $user, $password, $database)
{
- $this->query_id = @mysql_query($query_str, $this->link_id);
- $this->query_str = $query_str;
- $this->history[] = $this->query_str;
+ $link = mysql_pconnect($server, $user, $password);
+ $this->select_db($database, $link);
- if (defined('ISSO_SHOW_QUERIES_LIVE'))
- {
- if (constant('ISSO_SHOW_QUERIES_LIVE'))
- {
- print($this->query_str . '
');
- }
- }
-
- if (!$this->query_id)
- {
- $this->error('Invalid SQL query');
- }
-
- return $this->query_id;
- }
-
- // ###################################################################
- /**
- * Escape a string depending on character set
- *
- * @access public
- *
- * @param string String to be escaped
- *
- * @return string Escaped string
- */
- function escape_string($string)
- {
- return @mysql_real_escape_string($string, $this->link_id);
+ return $link;
}
// ###################################################################
/**
- * Fetch the query result as an array
- *
- * @access public
+ * Wrapper: mysql_connect
*
- * @param integer Query ID
- *
- * @return array A row of the query result
- */
- function fetch_array($query_id)
- {
- return @mysql_fetch_array($query_id, MYSQL_ASSOC);
- }
-
- // ###################################################################
- /**
- * Fetch the query result as an object
- *
- * @access public
+ * @access protected
*
- * @param integer Query ID
+ * @param string Server name
+ * @param string User name
+ * @param string Password
+ * @param string Database
*
- * @return object An object with the query result
+ * @return integer DB-Link
*/
- function fetch_object($query_id)
+ function command_mysql_connect($server, $user, $password, $database)
{
- return @mysql_fetch_object($query_id);
+ $link = mysql_connect($server, $user, $password);
+ $this->select_db($database, $link);
+
+ return $link;
}
// ###################################################################
/**
- * Free the current MySQL query result
+ * Wrapper: mysql_select_db
*
- * @access public
+ * @access protected
*
- * @param integer Query ID
+ * @param string Database name
+ * @param integer DB-Link
*/
- function free_result($query_id)
+ function select_db($database, $link)
{
- @mysql_free_result($query_id);
- $this->query_id = 0;
- $this->query_str = '';
+ if (!mysql_select_db($database, $link))
+ {
+ $this->error('Cannot use the database \'' . $this->database . '\'');
+ }
}
// ###################################################################
/**
- * Send a MySQL query and return the first row of the results
+ * Wrapper: mysql_query
*
- * @access public
+ * @access protected
*
+ * @param integer DB-Link
* @param string Query string
- * @param string Result return function (in the database layer)
- *
- * @return mixed Results in variable formats
- */
- function query_first($query_str, $callback = 'fetch_array')
- {
- $resource = $this->query($query_str);
- $return = $this->$callback($resource);
- $this->free_result($resource);
- return $return;
- }
-
- // ###################################################################
- /**
- * Fetch the unique ID of the record just inserted
- *
- * @access public
- *
- * @return integer MySQL insert ID
- */
- function insert_id()
- {
- return @mysql_insert_id($this->link_id);
- }
-
- // ###################################################################
- /**
- * Fetch the number of rows in the result
- *
- * @access public
- *
- * @param integer Query ID
*
- * @return integer Number of MySQL rows
+ * @return integer Query-ID
*/
- function num_rows($query_id)
+ function command_mysql_query($link, $string)
{
- return @mysql_num_rows($query_id);
+ return mysql_query($string, $link);
}
// ###################################################################
/**
- * Fetch the number of rows affected by the query
- *
- * @access public
- *
- * @param integer Query ID
- *
- * @return integer Number of affected rows
- */
- function affected_rows($query_id)
- {
- return @mysql_affected_rows($this->link_id);
- }
-
- // ###################################################################
- /**
- * MySQL error wrapper for ISSO->message()
+ * Wrapper: mysql(_real)_escape_string
*
* @access protected
*
- * @param string User defined error message
+ * @param integer DB-Link
+ * @param string Unescaped text
+ *
+ * @return string Escaped text
*/
- function error($message)
+ function command_mysql_escape_string($link, $string)
{
- if ($this->errshow)
+ if (function_exists('mysql_real_escape_string'))
+ {
+ return mysql_real_escape_string($string, $link);
+ }
+ else
{
- if ($this->link_id)
- {
- $this->error_no = mysql_errno($this->link_id);
- $this->error_str = mysql_error($this->link_id);
- }
-
- $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
-
- $message_prepped = "\n";
- $message_prepped .= "\n\t» Query:\n
" . htmlspecialchars($this->query_str) ."
\n
";
- $message_prepped .= "\n\t» Error Number: " . $this->error_no . "\n
";
- $message_prepped .= "\n\t» Error Message: " . $this->error_str . "\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;
+ return mysql_escape_string($string);
}
}
}
--
2.43.5