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 BSDbResult Result object, or NULL
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 if (strtoupper(substr(trim($string), 0, 6)) == 'SELECT')
172 $class = get_class($this) . 'Result';
173 return new $class($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 * Send a query and return the first row of the results
256 * @param string Query string
257 * @param string Result return function (in the database layer)
259 * @return mixed Results in variable formats
261 public function queryFirst($string, $callback = 'fetchArray')
263 $resource = $this->query($string);
266 $return = $resource->$callback();
276 // ###################################################################
278 * Returns the errror number
280 public abstract function _errorNumber();
283 * Returns the error string
285 public abstract function _errorString();
287 // ###################################################################
289 * Sends the command to start a transaction. This command should never
290 * be reached as it's always overridden
292 public abstract function begin();
294 // ###################################################################
296 * Sends the command to rollback to a given savepoint. This command
297 * should never be reached as it's always overridden
299 * @param string Named savepoint
301 public abstract function rollback();
303 // ###################################################################
305 * Sends the command to commit the entire transaction. This command
306 * should never be reached as it's always overridden
308 public abstract function commit();
314 * This class holds result information for a database result
317 * @copyright Copyright (c)2002 - 2007, Blue Static
321 abstract class BSDbResult
324 * The result resource
329 // ###################################################################
331 * Sets the resource and returns a result object
333 * @param resource The result of the query
335 public function __construct($result)
337 $this->result
= $result;
340 // ###################################################################
342 * Fetch the query result as an array
344 * @param integer Result
345 * @param bool Return an associative array?
347 * @return array A row of the query result
349 public function fetchArray($assoc = true
)
351 return $this->{($assoc ?
'_fetchAssocArray' : '_fetchRowArray')}($this->result
);
355 * Abstract function that returns an associative array of given result
357 * @param integer Result
359 * @return array Result array
361 protected abstract function _fetchAssocArray($result);
364 * Abstract function that returns a row array of given result
366 * @param integer Result
368 * @return array Result array
370 protected abstract function _fetchRowArray($result);
372 // ###################################################################
374 * Fetch the query result as an object
376 * @param integer Result
378 * @return object An object with the query result
380 public function fetchObject()
382 return $this->_fetchObject($this->result
);
386 * Abstract function that returns an object for a given result
388 * @param integer Result
390 * @return object Row object
392 public abstract function _fetchObject($result);
394 // ###################################################################
396 * Free the current query result
398 * @param integer Result
400 public function free()
402 $this->_freeResult($result);
406 * Abstract function that frees a given result
408 * @param integer Result ID
410 protected abstract function _freeResult($result);
412 // ###################################################################
414 * Fetch the unique ID of the record just inserted
416 * @return integer Insert-ID
418 public function insertId()
420 return $this->_insertID();
424 * Abstract function that returns the ID of the most recently-inserted
427 * @return integer Insertion ID
429 protected abstract function _insertId();
431 // ###################################################################
433 * Fetch the number of rows in the result
435 * @param integer Result
437 * @return integer Number of rows
439 public function size()
441 return $this->_numRows($this->result
);
445 * Abstract function that returns the number of rows in the result
447 * @param integer Result ID
449 * @return integer Number of rows
451 protected abstract function _numRows($result);
453 // ###################################################################
455 * Fetch the number of rows affected by the query
457 * @param integer Result
459 * @return integer Number of affected rows
461 public function affectedRows()
463 return $this->_affectedRows($this->result
);
467 * Abstract function that returns the number of affected rows in the result
469 * @param integer Result ID
471 * @return integer Number of rows
473 protected abstract function _affectedRows($result);
479 * Exception handler class for the database classes
481 * @author Blue Static
482 * @copyright Copyright (c)2002 - 2007, Blue Static
486 class BSDbException
extends Exception
489 * The query string that caused the error
494 // ###################################################################
496 * Initializes a new database exception
498 * @param string The error message
499 * @param ineger MySQL error code
500 * @param sring Query string that caused the error
502 public function __construct($error, $errorNum, $query)
504 $this->query
= $query;
505 parent
::__construct($error, $errorNum);
508 // ###################################################################
510 * Returns the query that failed
514 public function getQuery()