]>
src.bluestatic.org Git - isso.git/blob - Db.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 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 2 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 (c)2002 - 2007, Blue Static
48 * Determines whether or not errors should be shown
51 public $showerrors = true;
54 * Current error number
57 protected $errnum = 0;
60 * Description of current error
63 protected $errstr = '';
66 * Currend open database connection
69 protected $dblink = null;
75 protected $result = null;
78 * Current query string
81 protected $querystr = '';
84 * History of all executed queryies
87 protected $history = array();
89 // ###################################################################
91 * Returns the history information array
93 * @return array History record
95 public function getHistory()
97 return $this->history
;
100 // ###################################################################
102 * Connect to a the specified database
104 * @param string Server name
105 * @param string User name
106 * @param string Password
107 * @param string Database name
109 * @return bool Result of connect
111 public function connect($server, $user, $password, $database)
113 if ($this->dblink
== false)
115 $this->dblink
= $this->_connect($server, $user, $password, $database);
117 if ($this->dblink
== false)
119 throw new BSDbException('Connect Failed', -1, 'DB-Link == false; connect failed');
128 * Abstract function that returns a database link after establishing a connection. This just
129 * calls the function and does not do any checking
131 * @param string Server name
132 * @param string User name
133 * @param string Password
134 * @param string Database name
136 * @return integer Database link
138 protected abstract function _connect($server, $user, $password, $database);
140 // ###################################################################
142 * Send a query to the open database link
144 * @param string Query string
146 * @return integer Result
148 public function query($string)
152 $this->querystr
= $string;
153 $this->result
= $this->_query($string);
157 throw new BSDbException($this->_errorString(), $this->_errorNumber(), $string);
160 $this->history
[] = $history = array('query' => $string, 'time' => BSFunctions
::FetchMicrotimeDiff($time), 'trace' => BSFunctions
::FormatBacktrace(debug_backtrace()));
162 if (defined('ISSO_SHOW_QUERIES_LIVE'))
164 if (constant('ISSO_SHOW_QUERIES_LIVE'))
166 print($this->_constructDebugQuery($history));
170 return $this->result
;
174 * Abstract function that executes the query command on the database
176 * @param string Query string
178 * @return integer Result ID
180 protected abstract function _query($query);
182 // ###################################################################
184 * Escape a string (depending on character set, if supported)
186 * @param string String to be escaped
188 * @return string Escaped string
190 public function escapeString($string)
192 return $this->_escapeString($string);
196 * Abstract function that calls the escape_string() method
198 * @param string String to escape
200 * @return string Escaped string
202 protected abstract function _escapeString($string);
204 // ###################################################################
206 * Escapes a binary string for insertion into the database
208 * @param string Unescaped data
210 * @return string Escaped binary data
212 public function escapeBinary($binary)
214 return $this->_escapeBinary($binary);
218 * Abstract function that calls escape_binary()
220 * @param string Binary to escape
222 * @return string Escaped binary
224 protected abstract function _escapeBinary($string);
226 // ###################################################################
228 * Unescapes a binary string that was fetched from the database
230 * @param string Escaped data
232 * @return string Unescaped binary data
234 public function unescapeBinary($binary)
236 return $this->_unescapeBinary($binary);
240 * Abstract function that calls unescape_binary()
242 * @param string Escaped data
244 * @return string Data that has been unescaped
246 protected abstract function _unescapeBinary($string);
248 // ###################################################################
250 * Fetch the query result as an array
252 * @param integer Result
253 * @param bool Return an associative array?
255 * @return array A row of the query result
257 public function fetchArray($result, $assoc = true)
259 return $this->{($assoc
? '_fetchAssocArray' : '_fetchRowArray')}($result);
263 * Abstract function that returns an associative array of given result
265 * @param integer Result
267 * @return array Result array
269 protected abstract function _fetchAssocArray($result);
272 * Abstract function that returns a row array of given result
274 * @param integer Result
276 * @return array Result array
278 protected abstract function _fetchRowArray($result);
280 // ###################################################################
282 * Fetch the query result as an object
284 * @param integer Result
286 * @return object An object with the query result
288 public function fetchObject($result)
290 return $this->_fetchObject($result);
294 * Abstract function that returns an object for a given result
296 * @param integer Result
298 * @return object Row object
300 public abstract function _fetchObject($result);
302 // ###################################################################
304 * Send a query and return the first row of the results
306 * @param string Query string
307 * @param string Result return function (in the database layer)
309 * @return mixed Results in variable formats
311 public function queryFirst($string, $callback = 'fetchArray')
313 $resource = $this->query($string);
316 $return = $this->$callback($resource);
317 $this->_freeResult($resource);
326 // ###################################################################
328 * Free the current query result
330 * @param integer Result
332 public function freeResult($result)
334 $this->_freeResult($result);
335 $this->result
= null;
336 $this->querystr
= '';
340 * Abstract function that frees a given result
342 * @param integer Result ID
344 protected abstract function _freeResult($result);
346 // ###################################################################
348 * Fetch the unique ID of the record just inserted
350 * @return integer Insert-ID
352 public function insertId()
354 return $this->_insertID();
358 * Abstract function that returns the ID of the most recently-inserted
361 * @return integer Insertion ID
363 protected abstract function _insertId();
365 // ###################################################################
367 * Fetch the number of rows in the result
369 * @param integer Result
371 * @return integer Number of rows
373 public function numRows($result)
375 return $this->_numRows($result);
379 * Abstract function that returns the number of rows in the result
381 * @param integer Result ID
383 * @return integer Number of rows
385 protected abstract function _numRows($result);
387 // ###################################################################
389 * Fetch the number of rows affected by the query
391 * @param integer Result
393 * @return integer Number of affected rows
395 public function affectedRows($result)
397 return $this->_affectedRows($result);
401 * Abstract function that returns the number of affected rows in the result
403 * @param integer Result ID
405 * @return integer Number of rows
407 protected abstract function _affectedRows($result);
409 // ###################################################################
411 * Returns the errror number
413 public abstract function _errorNumber();
416 * Returns the error string
418 public abstract function _errorString();
420 // ###################################################################
422 * Sends the command to start a transaction. This command should never
423 * be reached as it's always overridden
425 public abstract function begin();
427 // ###################################################################
429 * Sends the command to rollback to a given savepoint. This command
430 * should never be reached as it's always overridden
432 * @param string Named savepoint
434 public abstract function rollback();
436 // ###################################################################
438 * Sends the command to commit the entire transaction. This command
439 * should never be reached as it's always overridden
441 public abstract function commit();
447 * Exception handler class for the database classes
449 * @author Blue Static
450 * @copyright Copyright (c)2002 - 2007, Blue Static
455 class BSDbException
extends Exception
458 * The query string that caused the error
463 // ###################################################################
465 * Initializes a new database exception
467 * @param string The error message
468 * @param ineger MySQL error code
469 * @param sring Query string that caused the error
471 public function __construct($error, $errorNum, $query)
473 $this->query
= $query;
474 parent
::__construct($error, $errorNum);
477 // ###################################################################
479 * Returns the query that failed
483 public function getQuery()