]>
src.bluestatic.org Git - isso.git/blob - db.php
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 (Db.php)
28 require_once('ISSO/Functions.php');
31 * Abstract Database Layer
33 * This class provides an abstract template for all RDBMS layers. All
34 * ISSO abstraction layers should inherit this class. It provides error
35 * reporting, SQL analysis, and general connection functionality.
38 * ISSO_SHOW_QUERIES_LIVE - Show queries in page output as they are sent
41 * @copyright Copyright ©2002 - [#]year[#], Blue Static
49 * Determines whether or not errors should be shown
52 public $showerrors = true;
55 * Current error number
58 protected $errnum = 0;
61 * Description of current error
64 protected $errstr = '';
67 * Currend open database connection
70 protected $dblink = null;
76 protected $result = null;
79 * Current query string
82 protected $querystr = '';
85 * History of all executed queryies
88 protected $history = array();
90 // ###################################################################
92 * Connect to a the specified database
94 * @param string Server name
95 * @param string User name
96 * @param string Password
97 * @param string Database name
98 * @param bool Use p-connect?
100 * @return bool Result of connect
102 public function connect($server, $user, $password, $database, $pconnect)
104 if ($this->dblink
== false)
106 $this->dblink
= $this->{($pconnect
? '_connect' : '_pConnect')}($server, $user, $password, $database);
108 if ($this->dblink
== false)
110 $this->_error('DB-Link == false, cannot connect');
119 * Abstract function that returns a database link after establishing a connection. This just
120 * calls the function and does not do any checking
122 * @param string Server name
123 * @param string User name
124 * @param string Password
125 * @param string Database name
127 * @return integer Database link
129 protected abstract function _connect($server, $user, $password, $database);
132 * Abstract function that returns a database link after establishing a connection. This just
133 * calls the function and does not do any checking
135 * @param string Server name
136 * @param string User name
137 * @param string Password
138 * @param string Database name
140 * @return resource Database link
142 protected abstract function _pConnect($server, $user, $password, $database);
144 // ###################################################################
146 * Send a query to the open database link
148 * @param string Query string
150 * @return integer Result
152 public function query($string)
156 $this->querystr
= $string;
157 $this->result
= $this->_query($this->dblink
, $string);
161 $this->_error('Invalid SQL query');
164 $this->history
[] = $history = array('query' => $string, 'time' => BSFunctions
::FetchMicrotimeDiff($time), 'trace' => $this->registry
->format_debug_trace(debug_backtrace()));
166 if (defined('ISSO_SHOW_QUERIES_LIVE'))
168 if (constant('ISSO_SHOW_QUERIES_LIVE'))
170 print($this->_constructDebugQuery($history));
174 return $this->result
;
178 * Abstract function that executes the query command on the database
180 * @param string Query string
182 * @return integer Result ID
184 protected abstract function _query($query);
186 // ###################################################################
188 * Escape a string (depending on character set, if supported)
190 * @param string String to be escaped
192 * @return string Escaped string
194 public function escapeString($string)
196 return $this->_escapeString($string);
200 * Abstract function that calls the escape_string() method
202 * @param string String to escape
204 * @return string Escaped string
206 protected abstract function _escapeString($string);
208 // ###################################################################
210 * Escapes a binary string for insertion into the database
212 * @param string Unescaped data
214 * @return string Escaped binary data
216 public function escapeBinary($binary)
218 return $this->_escapeBinary($binary);
222 * Abstract function that calls escape_binary()
224 * @param string Binary to escape
226 * @return string Escaped binary
228 protected abstract function _escapeBinary($string);
230 // ###################################################################
232 * Unescapes a binary string that was fetched from the database
234 * @param string Escaped data
236 * @return string Unescaped binary data
238 public function unescapeBinary($binary)
240 return $this->_unescapeBinary($binary);
244 * Abstract function that calls unescape_binary()
246 * @param string Escaped data
248 * @return string Data that has been unescaped
250 protected abstract function _unescapeBinary($string);
252 // ###################################################################
254 * Fetch the query result as an array
256 * @param integer Result
257 * @param bool Return an associative array?
259 * @return array A row of the query result
261 public function fetchArray($result, $assoc = true)
263 return $this->{($assoc
? '_fetchAssocArray' : '_fetchRowArray')}($result);
267 * Abstract function that returns an associative array of given result
269 * @param integer Result
271 * @return array Result array
273 protected abstract function _fetchAssocArray($result);
276 * Abstract function that returns a row array of given result
278 * @param integer Result
280 * @return array Result array
282 protected abstract function _fetchRowArray($result);
284 // ###################################################################
286 * Fetch the query result as an object
288 * @param integer Result
290 * @return object An object with the query result
292 public function fetchObject($result)
294 return $this->_fetchObject($result);
298 * Abstract function that returns an object for a given result
300 * @param integer Result
302 * @return object Row object
304 public abstract function _fetchObject($result);
306 // ###################################################################
308 * Send a query and return the first row of the results
310 * @param string Query string
311 * @param string Result return function (in the database layer)
313 * @return mixed Results in variable formats
315 public function queryFirst($string, $callback = 'fetchArray')
317 $resource = $this->query($string);
320 $return = $this->$callback($resource);
321 $this->_freeResult($resource);
330 // ###################################################################
332 * Free the current query result
334 * @param integer Result
336 public function freeResult($result)
338 $this->_freeResult($result);
339 $this->result
= null;
340 $this->querystr
= '';
344 * Abstract function that frees a given result
346 * @param integer Result ID
348 protected abstract function _freeResult($result);
350 // ###################################################################
352 * Fetch the unique ID of the record just inserted
354 * @return integer Insert-ID
356 public function insertId()
358 return $this->_insertID();
362 * Abstract function that returns the ID of the most recently-inserted
365 * @return integer Insertion ID
367 protected abstract function _insertId();
369 // ###################################################################
371 * Fetch the number of rows in the result
373 * @param integer Result
375 * @return integer Number of rows
377 public function numRows($result)
379 return $this->_numRows($result);
383 * Abstract function that returns the number of rows in the result
385 * @param integer Result ID
387 * @return integer Number of rows
389 protected abstract function _numRows($result);
391 // ###################################################################
393 * Fetch the number of rows affected by the query
395 * @param integer Result
397 * @return integer Number of affected rows
399 public function affectedRows($result)
401 return $this->_affectedRows($result);
405 * Abstract function that returns the number of affected rows in the result
407 * @param integer Result ID
409 * @return integer Number of rows
411 protected abstract function _affectedRows($result);
413 // ###################################################################
415 * Sends the command to start a transaction. This command should never
416 * be reached as it's always overridden
418 public abstract function transaction_start();
420 // ###################################################################
422 * Sends the command to set this as a savepoint. This command should never
423 * be reached as it's always overridden
425 * @param string Named savepoint
427 public abstract function transaction_savepoint($name);
429 // ###################################################################
431 * Sends the command to rollback to a given savepoint. This command
432 * should never be reached as it's always overridden
434 * @param string Named savepoint
436 public abstract function transaction_rollback($name = null);
438 // ###################################################################
440 * Sends the command to commit the entire transaction. This command
441 * should never be reached as it's always overridden
443 public abstract function transaction_commit();
445 // ###################################################################
447 * Constructs a table of query information output that is used in some
448 * other modules to display a list of queries. This merely formats
449 * a DB->history array entry nicely
451 * @param array An entry from DB->history
453 * @return string A formatted table block
455 protected function _constructDebugQuery($query)
457 $block = "<strong>Query:</strong>\n\n<div>" . htmlspecialchars($query['query']) . "</div>\n";
458 $block .= "<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
459 $block .= "<strong>Time:</strong> $query[time]<br />\n\t\t<br />\n\t\t";
460 $block .= "<strong>Backtrace:</strong>\n\t\t<div>" . implode("<br />\n", $query['trace']) . "</div>\n\t</td>\n</tr>";
462 return BSRegister
::Message('Query Debug', $block, 1, true, false, 0);
465 // ###################################################################
467 * Error wrapper for ISSO->message()
469 * @param string User defined error message
471 protected function _error($message)
473 if ($this->showerrors
)
477 $this->errnum
= call_user_func($this->commands
['error_num'], $this->dblink
);
478 $this->errstr
= call_user_func($this->commands
['error_str'], $this->dblink
);
481 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
483 $message_prepped = "<blockquote>\n<p>";
484 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
485 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
486 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
487 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
488 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
489 $message_prepped .= "\n</p>\n</blockquote>";
491 BSRegister
::Message('Database Error in `<em>' . BSRegister
::GetApplication() . '</em>`', $message_prepped, 3);
497 /*=====================================================================*\
498 || ###################################################################
501 || ###################################################################
502 \*=====================================================================*/