2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Abstract Database Layer
30 * Abstract Database Layer
32 * This class provides an abstract template for all RDBMS layers. All
33 * ISSO abstraction layers should inherit this class. It provides error
34 * reporting, SQL analysis, and general connection functionality.
37 * [required] ISSO_DB_LAYER - The name of the DB layer module used in the application
38 * ISSO_SHOW_QUERIES_LIVE - Show queries in page output as they are sent
41 * @copyright Copyright ©2002 - [#]year[#], Blue Static
49 * Framework registry object
52 protected $registry = null
;
55 * Determines whether or not errors should be shown
58 public $showerrors = true
;
61 * Current error number
64 protected $errnum = 0;
67 * Description of current error
70 protected $errstr = '';
73 * Currend open MySQL connexion
76 protected $dblink = null
;
82 protected $result = null
;
85 * Current query string
88 protected $querystr = '';
91 * History of all executed queryies
94 protected $history = array();
97 * Command mapping list
100 protected $commands = array(
101 'pconnect' => '%server %user %password %database',
102 'connect' => '%server %user %password %database',
103 'query' => '%link %query',
104 'error_num' => '%link',
105 'error_str' => '%link',
106 'escape_string' => '%link %string',
107 'escape_binary' => '%string',
108 'unescape_binary' => '%string',
109 'fetch_assoc' => '%result',
110 'fetch_row' => '%result',
111 'fetch_object' => '%result',
112 'free_result' => '%result',
113 'insert_id' => '%link',
114 'num_rows' => '%result',
115 'affected_rows' => '%result'
118 // ###################################################################
122 function __construct(&$registry)
124 $this->registry
=& $registry;
126 // because ivars and call_user_func() are conspiring against us...
127 foreach ($this->commands
AS $key => $string)
129 if (strpos($string, '$this->') !== false
)
131 $this->commands
["$key"] = array($this, str_replace('$this->', '', $string));
136 // ###################################################################
138 * Initializes the class and all subclasses under a common package name
142 * @return string The package name
144 function init_as_package()
146 if (!defined('ISSO_DB_LAYER'))
148 define('ISSO_DB_LAYER', get_class($this));
149 trigger_error('ISSO_DB_LAYER was defined automatically by DB::init_as_package(). Define the constant yourself to remove this warning', E_USER_WARNING
);
155 // ###################################################################
157 * Connect to a the specified database
161 * @param string Server name
162 * @param string User name
163 * @param string Password
164 * @param string Database name
165 * @param bool Use p-connect?
167 * @return bool Result of connect
169 function connect($server, $user, $password, $database, $pconnect)
171 $this->registry
->check_isso_fields(get_class($this));
173 if ($this->dblink
== false
)
175 $this->dblink
= call_user_func(($pconnect ?
$this->commands
['pconnect'] : $this->commands
['connect']), $server, $user, $password, $database);
177 if ($this->dblink
== false
)
179 $this->error('DB-Link == false, cannot connect');
187 // ###################################################################
189 * Send a query to the open database link
193 * @param string Query string
195 * @return integer Result
197 function query($string)
201 $this->querystr
= $string;
202 $this->result
= @call_user_func($this->commands
['query'], $this->dblink
, $string);
206 $this->error('Invalid SQL query');
209 $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()));
211 if (defined('ISSO_SHOW_QUERIES_LIVE'))
213 if (constant('ISSO_SHOW_QUERIES_LIVE'))
215 print($this->construct_query_debug($history));
219 return $this->result
;
222 // ###################################################################
224 * Escape a string (depending on character set, if supported)
228 * @param string String to be escaped
230 * @return string Escaped string
232 function escape_string($string)
234 return call_user_func($this->commands
['escape_string'], $this->dblink
, $string);
237 // ###################################################################
239 * Escapes a binary string for insertion into the database
243 * @param string Unescaped data
245 * @return string Escaped binary data
247 function escape_binary($binary)
249 return call_user_func($this->commands
['escape_binary'], $binary);
252 // ###################################################################
254 * Unescapes a binary string that was fetched from the database
258 * @param string Escaped data
260 * @return string Unescaped binary data
262 function unescape_binary($binary)
264 return call_user_func($this->commands
['unescape_binary'], $binary);
267 // ###################################################################
269 * Fetch the query result as an array
273 * @param integer Result
274 * @param bool Return an associative array?
276 * @return array A row of the query result
278 function fetch_array($result, $assoc = true
)
280 return call_user_func($this->commands
[ ($assoc ?
'fetch_assoc' : 'fetch_row') ], $result);
283 // ###################################################################
285 * Fetch the query result as an object
289 * @param integer Result
291 * @return object An object with the query result
293 function fetch_object($result)
295 return call_user_func($this->commands
['fetch_object'], $result);
298 // ###################################################################
300 * Send a query and return the first row of the results
304 * @param string Query string
305 * @param string Result return function (in the database layer)
307 * @return mixed Results in variable formats
309 function query_first($string, $callback = 'fetch_array')
311 $resource = $this->query($string);
314 $return = $this->$callback($resource);
315 $this->free_result($resource);
324 // ###################################################################
326 * Free the current query result
330 * @param integer Result
332 function free_result($result)
334 call_user_func($this->commands
['free_result'], $result);
335 $this->result
= null
;
336 $this->querystr
= '';
339 // ###################################################################
341 * Fetch the unique ID of the record just inserted
345 * @return integer Insert-ID
349 return call_user_func($this->commands
['insert_id'], $this->dblink
);
352 // ###################################################################
354 * Fetch the number of rows in the result
358 * @param integer Result
360 * @return integer Number of rows
362 function num_rows($result)
364 return call_user_func($this->commands
['num_rows'], $result);
367 // ###################################################################
369 * Fetch the number of rows affected by the query
373 * @param integer Result
375 * @return integer Number of affected rows
377 function affected_rows($result)
379 return call_user_func($this->commands
['affected_rows'], $result);
382 // ###################################################################
384 * Sends the command to start a transaction. This command should never
385 * be reached as it's always overridden
389 function transaction_start()
391 trigger_error('DB_Abstract::transaction_start() needs to be overridden when subclassed', E_USER_ERROR
);
394 // ###################################################################
396 * Sends the command to set this as a savepoint. This command should never
397 * be reached as it's always overridden
401 * @param string Named savepoint
403 function transaction_savepoint($name)
405 trigger_error('DB_Abstract::transaction_savepoint() needs to be overridden when subclassed', E_USER_ERROR
);
408 // ###################################################################
410 * Sends the command to rollback to a given savepoint. This command
411 * should never be reached as it's always overridden
415 * @param string Named savepoint
417 function transaction_rollback($name)
419 trigger_error('DB_Abstract::transaction_rollback() needs to be overridden when subclassed', E_USER_ERROR
);
422 // ###################################################################
424 * Sends the command to commit the entire transaction. This command
425 * should never be reached as it's always overridden
429 function transaction_commit($name)
431 trigger_error('DB_Abstract::transaction_commit() needs to be overridden when subclassed', E_USER_ERROR
);
434 // ###################################################################
436 * Constructs a table of query information output that is used in some
437 * other modules to display a list of queries. This merely formats
438 * a DB->history array entry nicely
442 * @param array An entry from DB->history
444 * @return string A formatted table block
446 function construct_query_debug($query)
448 $block = "<strong>Query:</strong>\n\n<div>" . $this->registry
->entity_encode($query['query']) . "</div>\n";
449 $block .= "<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
450 $block .= "<strong>Time:</strong> $query[time]<br />\n\t\t<br />\n\t\t";
451 $block .= "<strong>Backtrace:</strong>\n\t\t<div>" . implode("<br />\n", $query['trace']) . "</div>\n\t</td>\n</tr>";
453 return $this->registry
->message('Query Debug', $block, 1, true
, false
, 0);
456 // ###################################################################
458 * Error wrapper for ISSO->message()
462 * @param string User defined error message
464 function error($message)
466 if ($this->showerrors
)
470 $this->errnum
= call_user_func($this->commands
['error_num'], $this->dblink
);
471 $this->errstr
= call_user_func($this->commands
['error_str'], $this->dblink
);
474 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
476 $message_prepped = "<blockquote>\n<p>";
477 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
478 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
479 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
480 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
481 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
482 $message_prepped .= "\n</p>\n</blockquote>";
484 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);
490 /*=====================================================================*\
491 || ###################################################################
494 || ###################################################################
495 \*=====================================================================*/