]>
src.bluestatic.org Git - isso.git/blob - Db.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
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 * Returns the history information array
94 * @return array History record
96 public function getHistory()
98 return $this->history
;
101 // ###################################################################
103 * Connect to a the specified database
105 * @param string Server name
106 * @param string User name
107 * @param string Password
108 * @param string Database name
109 * @param bool Use p-connect?
111 * @return bool Result of connect
113 public function connect($server, $user, $password, $database, $pconnect)
115 if ($this->dblink
== false)
117 $this->dblink
= $this->{($pconnect
? '_connect' : '_pConnect')}($server, $user, $password, $database);
119 if ($this->dblink
== false)
121 $this->_error('DB-Link == false, cannot connect');
130 * Abstract function that returns a database link after establishing a connection. This just
131 * calls the function and does not do any checking
133 * @param string Server name
134 * @param string User name
135 * @param string Password
136 * @param string Database name
138 * @return integer Database link
140 protected abstract function _connect($server, $user, $password, $database);
143 * Abstract function that returns a database link after establishing a connection. This just
144 * calls the function and does not do any checking
146 * @param string Server name
147 * @param string User name
148 * @param string Password
149 * @param string Database name
151 * @return resource Database link
153 protected abstract function _pConnect($server, $user, $password, $database);
155 // ###################################################################
157 * Send a query to the open database link
159 * @param string Query string
161 * @return integer Result
163 public function query($string)
167 $this->querystr
= $string;
168 $this->result
= $this->_query($string);
172 $this->_error('Invalid SQL query');
175 $this->history
[] = $history = array('query' => $string, 'time' => BSFunctions
::FetchMicrotimeDiff($time), 'trace' => BSFunctions
::FormatBacktrace(debug_backtrace()));
177 if (defined('ISSO_SHOW_QUERIES_LIVE'))
179 if (constant('ISSO_SHOW_QUERIES_LIVE'))
181 print($this->_constructDebugQuery($history));
185 return $this->result
;
189 * Abstract function that executes the query command on the database
191 * @param string Query string
193 * @return integer Result ID
195 protected abstract function _query($query);
197 // ###################################################################
199 * Escape a string (depending on character set, if supported)
201 * @param string String to be escaped
203 * @return string Escaped string
205 public function escapeString($string)
207 return $this->_escapeString($string);
211 * Abstract function that calls the escape_string() method
213 * @param string String to escape
215 * @return string Escaped string
217 protected abstract function _escapeString($string);
219 // ###################################################################
221 * Escapes a binary string for insertion into the database
223 * @param string Unescaped data
225 * @return string Escaped binary data
227 public function escapeBinary($binary)
229 return $this->_escapeBinary($binary);
233 * Abstract function that calls escape_binary()
235 * @param string Binary to escape
237 * @return string Escaped binary
239 protected abstract function _escapeBinary($string);
241 // ###################################################################
243 * Unescapes a binary string that was fetched from the database
245 * @param string Escaped data
247 * @return string Unescaped binary data
249 public function unescapeBinary($binary)
251 return $this->_unescapeBinary($binary);
255 * Abstract function that calls unescape_binary()
257 * @param string Escaped data
259 * @return string Data that has been unescaped
261 protected abstract function _unescapeBinary($string);
263 // ###################################################################
265 * Fetch the query result as an array
267 * @param integer Result
268 * @param bool Return an associative array?
270 * @return array A row of the query result
272 public function fetchArray($result, $assoc = true)
274 return $this->{($assoc
? '_fetchAssocArray' : '_fetchRowArray')}($result);
278 * Abstract function that returns an associative array of given result
280 * @param integer Result
282 * @return array Result array
284 protected abstract function _fetchAssocArray($result);
287 * Abstract function that returns a row array of given result
289 * @param integer Result
291 * @return array Result array
293 protected abstract function _fetchRowArray($result);
295 // ###################################################################
297 * Fetch the query result as an object
299 * @param integer Result
301 * @return object An object with the query result
303 public function fetchObject($result)
305 return $this->_fetchObject($result);
309 * Abstract function that returns an object for a given result
311 * @param integer Result
313 * @return object Row object
315 public abstract function _fetchObject($result);
317 // ###################################################################
319 * Send a query and return the first row of the results
321 * @param string Query string
322 * @param string Result return function (in the database layer)
324 * @return mixed Results in variable formats
326 public function queryFirst($string, $callback = 'fetchArray')
328 $resource = $this->query($string);
331 $return = $this->$callback($resource);
332 $this->_freeResult($resource);
341 // ###################################################################
343 * Free the current query result
345 * @param integer Result
347 public function freeResult($result)
349 $this->_freeResult($result);
350 $this->result
= null;
351 $this->querystr
= '';
355 * Abstract function that frees a given result
357 * @param integer Result ID
359 protected abstract function _freeResult($result);
361 // ###################################################################
363 * Fetch the unique ID of the record just inserted
365 * @return integer Insert-ID
367 public function insertId()
369 return $this->_insertID();
373 * Abstract function that returns the ID of the most recently-inserted
376 * @return integer Insertion ID
378 protected abstract function _insertId();
380 // ###################################################################
382 * Fetch the number of rows in the result
384 * @param integer Result
386 * @return integer Number of rows
388 public function numRows($result)
390 return $this->_numRows($result);
394 * Abstract function that returns the number of rows in the result
396 * @param integer Result ID
398 * @return integer Number of rows
400 protected abstract function _numRows($result);
402 // ###################################################################
404 * Fetch the number of rows affected by the query
406 * @param integer Result
408 * @return integer Number of affected rows
410 public function affectedRows($result)
412 return $this->_affectedRows($result);
416 * Abstract function that returns the number of affected rows in the result
418 * @param integer Result ID
420 * @return integer Number of rows
422 protected abstract function _affectedRows($result);
424 // ###################################################################
426 * Returns the errror number
428 public abstract function _errorNumber();
431 * Returns the error string
433 public abstract function _errorString();
435 // ###################################################################
437 * Sends the command to start a transaction. This command should never
438 * be reached as it's always overridden
440 public abstract function transactionStart();
442 // ###################################################################
444 * Sends the command to set this as a savepoint. This command should never
445 * be reached as it's always overridden
447 * @param string Named savepoint
449 public abstract function transactionSavepoint($name);
451 // ###################################################################
453 * Sends the command to rollback to a given savepoint. This command
454 * should never be reached as it's always overridden
456 * @param string Named savepoint
458 public abstract function transactionRollback($name = null);
460 // ###################################################################
462 * Sends the command to commit the entire transaction. This command
463 * should never be reached as it's always overridden
465 public abstract function transactionCommit();
467 // ###################################################################
469 * Error wrapper for ISSO->message()
471 * @param string User defined error message
473 protected function _error($message)
475 if ($this->showerrors
)
479 $this->errnum
= $this->_errorNumber();
480 $this->errstr
= $this->_errorString();
483 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
485 $message_prepped = "<blockquote>\n<p>";
486 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
487 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
488 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
489 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
490 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
491 $message_prepped .= "\n</p>\n</blockquote>";
493 BSRegister
::Message('Database Error in `<em>' . BSRegister
::GetApplication() . '</em>`', $message_prepped, 3);
499 /*=====================================================================*\
500 || ###################################################################
503 || ###################################################################
504 \*=====================================================================*/