]>
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
110 * @return bool Result of connect
112 public function connect($server, $user, $password, $database)
114 if ($this->dblink
== false)
116 $this->dblink
= $this->_connect($server, $user, $password, $database);
118 if ($this->dblink
== false)
120 $this->_error('DB-Link == false, cannot connect');
129 * Abstract function that returns a database link after establishing a connection. This just
130 * calls the function and does not do any checking
132 * @param string Server name
133 * @param string User name
134 * @param string Password
135 * @param string Database name
137 * @return integer Database link
139 protected abstract function _connect($server, $user, $password, $database);
141 // ###################################################################
143 * Send a query to the open database link
145 * @param string Query string
147 * @return integer Result
149 public function query($string)
153 $this->querystr
= $string;
154 $this->result
= $this->_query($string);
158 $this->_error('Invalid SQL query');
161 $this->history
[] = $history = array('query' => $string, 'time' => BSFunctions
::FetchMicrotimeDiff($time), 'trace' => BSFunctions
::FormatBacktrace(debug_backtrace()));
163 if (defined('ISSO_SHOW_QUERIES_LIVE'))
165 if (constant('ISSO_SHOW_QUERIES_LIVE'))
167 print($this->_constructDebugQuery($history));
171 return $this->result
;
175 * Abstract function that executes the query command on the database
177 * @param string Query string
179 * @return integer Result ID
181 protected abstract function _query($query);
183 // ###################################################################
185 * Escape a string (depending on character set, if supported)
187 * @param string String to be escaped
189 * @return string Escaped string
191 public function escapeString($string)
193 return $this->_escapeString($string);
197 * Abstract function that calls the escape_string() method
199 * @param string String to escape
201 * @return string Escaped string
203 protected abstract function _escapeString($string);
205 // ###################################################################
207 * Escapes a binary string for insertion into the database
209 * @param string Unescaped data
211 * @return string Escaped binary data
213 public function escapeBinary($binary)
215 return $this->_escapeBinary($binary);
219 * Abstract function that calls escape_binary()
221 * @param string Binary to escape
223 * @return string Escaped binary
225 protected abstract function _escapeBinary($string);
227 // ###################################################################
229 * Unescapes a binary string that was fetched from the database
231 * @param string Escaped data
233 * @return string Unescaped binary data
235 public function unescapeBinary($binary)
237 return $this->_unescapeBinary($binary);
241 * Abstract function that calls unescape_binary()
243 * @param string Escaped data
245 * @return string Data that has been unescaped
247 protected abstract function _unescapeBinary($string);
249 // ###################################################################
251 * Fetch the query result as an array
253 * @param integer Result
254 * @param bool Return an associative array?
256 * @return array A row of the query result
258 public function fetchArray($result, $assoc = true)
260 return $this->{($assoc
? '_fetchAssocArray' : '_fetchRowArray')}($result);
264 * Abstract function that returns an associative array of given result
266 * @param integer Result
268 * @return array Result array
270 protected abstract function _fetchAssocArray($result);
273 * Abstract function that returns a row array of given result
275 * @param integer Result
277 * @return array Result array
279 protected abstract function _fetchRowArray($result);
281 // ###################################################################
283 * Fetch the query result as an object
285 * @param integer Result
287 * @return object An object with the query result
289 public function fetchObject($result)
291 return $this->_fetchObject($result);
295 * Abstract function that returns an object for a given result
297 * @param integer Result
299 * @return object Row object
301 public abstract function _fetchObject($result);
303 // ###################################################################
305 * Send a query and return the first row of the results
307 * @param string Query string
308 * @param string Result return function (in the database layer)
310 * @return mixed Results in variable formats
312 public function queryFirst($string, $callback = 'fetchArray')
314 $resource = $this->query($string);
317 $return = $this->$callback($resource);
318 $this->_freeResult($resource);
327 // ###################################################################
329 * Free the current query result
331 * @param integer Result
333 public function freeResult($result)
335 $this->_freeResult($result);
336 $this->result
= null;
337 $this->querystr
= '';
341 * Abstract function that frees a given result
343 * @param integer Result ID
345 protected abstract function _freeResult($result);
347 // ###################################################################
349 * Fetch the unique ID of the record just inserted
351 * @return integer Insert-ID
353 public function insertId()
355 return $this->_insertID();
359 * Abstract function that returns the ID of the most recently-inserted
362 * @return integer Insertion ID
364 protected abstract function _insertId();
366 // ###################################################################
368 * Fetch the number of rows in the result
370 * @param integer Result
372 * @return integer Number of rows
374 public function numRows($result)
376 return $this->_numRows($result);
380 * Abstract function that returns the number of rows in the result
382 * @param integer Result ID
384 * @return integer Number of rows
386 protected abstract function _numRows($result);
388 // ###################################################################
390 * Fetch the number of rows affected by the query
392 * @param integer Result
394 * @return integer Number of affected rows
396 public function affectedRows($result)
398 return $this->_affectedRows($result);
402 * Abstract function that returns the number of affected rows in the result
404 * @param integer Result ID
406 * @return integer Number of rows
408 protected abstract function _affectedRows($result);
410 // ###################################################################
412 * Returns the errror number
414 public abstract function _errorNumber();
417 * Returns the error string
419 public abstract function _errorString();
421 // ###################################################################
423 * Sends the command to start a transaction. This command should never
424 * be reached as it's always overridden
426 public abstract function transactionStart();
428 // ###################################################################
430 * Sends the command to set this as a savepoint. This command should never
431 * be reached as it's always overridden
433 * @param string Named savepoint
435 public abstract function transactionSavepoint($name);
437 // ###################################################################
439 * Sends the command to rollback to a given savepoint. This command
440 * should never be reached as it's always overridden
442 * @param string Named savepoint
444 public abstract function transactionRollback($name = null);
446 // ###################################################################
448 * Sends the command to commit the entire transaction. This command
449 * should never be reached as it's always overridden
451 public abstract function transactionCommit();
453 // ###################################################################
455 * Error wrapper for ISSO->message()
457 * @param string User defined error message
459 protected function _error($message)
461 if ($this->showerrors
)
465 $this->errnum
= $this->_errorNumber();
466 $this->errstr
= $this->_errorString();
469 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
471 $message_prepped = "<blockquote>\n<p>";
472 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
473 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
474 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
475 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
476 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
477 $message_prepped .= "\n</p>\n</blockquote>";
479 BSRegister
::Message('Database Error in `<em>' . BSRegister
::GetApplication() . '</em>`', $message_prepped, 3);
485 /*=====================================================================*\
486 || ###################################################################
489 || ###################################################################
490 \*=====================================================================*/