]>
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 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 * Fetch the unique ID of the record just inserted
291 * @return integer Insert-ID
293 public function insertId()
295 return $this->_insertID();
299 * Abstract function that returns the ID of the most recently-inserted
302 * @return integer Insertion ID
304 protected abstract function _insertId();
306 // ###################################################################
308 * Fetch the number of rows affected by the query
310 * @param integer Result
312 * @return integer Number of affected rows
314 public function affectedRows()
316 return $this->_affectedRows($this->result
);
320 * Abstract function that returns the number of affected rows in the result
322 * @param integer Result ID
324 * @return integer Number of rows
326 protected abstract function _affectedRows($result);
328 // ###################################################################
330 * Sends the command to start a transaction. This command should never
331 * be reached as it's always overridden
333 public abstract function begin();
335 // ###################################################################
337 * Sends the command to rollback to a given savepoint. This command
338 * should never be reached as it's always overridden
340 * @param string Named savepoint
342 public abstract function rollback();
344 // ###################################################################
346 * Sends the command to commit the entire transaction. This command
347 * should never be reached as it's always overridden
349 public abstract function commit();
355 * This class holds result information for a database result
358 * @copyright Copyright (c)2002 - 2007, Blue Static
362 abstract class BSDbResult
365 * The result resource
370 // ###################################################################
372 * Sets the resource and returns a result object
374 * @param resource The result of the query
376 public function __construct($result)
378 $this->result
= $result;
381 // ###################################################################
383 * Fetch the query result as an array
385 * @param integer Result
386 * @param bool Return an associative array?
388 * @return array A row of the query result
390 public function fetchArray($assoc = true)
392 return $this->{($assoc
? '_fetchAssocArray' : '_fetchRowArray')}($this->result
);
396 * Abstract function that returns an associative array of given result
398 * @param integer Result
400 * @return array Result array
402 protected abstract function _fetchAssocArray($result);
405 * Abstract function that returns a row array of given result
407 * @param integer Result
409 * @return array Result array
411 protected abstract function _fetchRowArray($result);
413 // ###################################################################
415 * Fetch the query result as an object
417 * @param integer Result
419 * @return object An object with the query result
421 public function fetchObject()
423 return $this->_fetchObject($this->result
);
427 * Abstract function that returns an object for a given result
429 * @param integer Result
431 * @return object Row object
433 public abstract function _fetchObject($result);
435 // ###################################################################
437 * Free the current query result
439 * @param integer Result
441 public function free()
443 $this->_freeResult($this->result
);
447 * Abstract function that frees a given result
449 * @param integer Result ID
451 protected abstract function _freeResult($result);
453 // ###################################################################
455 * Fetch the number of rows in the result
457 * @param integer Result
459 * @return integer Number of rows
461 public function size()
463 return $this->_numRows($this->result
);
467 * Abstract function that returns the number of rows in the result
469 * @param integer Result ID
471 * @return integer Number of rows
473 protected abstract function _numRows($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()