]>
src.bluestatic.org Git - isso.git/blob - Db.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 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)2005 - 2008, 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();
90 * Returns the history information array
92 * @return array History record
94 public function getHistory()
96 return $this->history
;
100 * Connect to a the specified database
102 * @param string Server name
103 * @param string User name
104 * @param string Password
105 * @param string Database name
107 * @return bool Result of connect
109 public function connect($server, $user, $password, $database)
111 if ($this->dblink
== false)
113 $this->dblink
= $this->_connect($server, $user, $password, $database);
115 if ($this->dblink
== false)
117 throw new BSDbException('Connect Failed', -1, 'DB-Link == false; connect failed');
126 * Abstract function that returns a database link after establishing a connection. This just
127 * calls the function and does not do any checking
129 * @param string Server name
130 * @param string User name
131 * @param string Password
132 * @param string Database name
134 * @return integer Database link
136 protected abstract function _connect($server, $user, $password, $database);
139 * Send a query to the open database link
141 * @param string Query string
143 * @return BSDbResult Result object, or NULL
145 public function query($string)
149 $this->querystr
= $string;
150 $this->result
= $this->_query($string);
154 throw new BSDbException($this->_errorString(), $this->_errorNumber(), $string);
157 $this->history
[] = $history = array('query' => $string, 'time' => BSFunctions
::fetch_microtime_diff($time), 'trace' => BSFunctions
::format_backtrace(debug_backtrace()));
159 if (defined('ISSO_SHOW_QUERIES_LIVE'))
161 if (constant('ISSO_SHOW_QUERIES_LIVE'))
163 print($this->_constructDebugQuery($history));
167 if (strtoupper(substr(trim($string), 0, 6)) == 'SELECT')
169 $class = get_class($this) . 'Result';
170 return new $class($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);
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);
205 * Escapes a binary string for insertion into the database
207 * @param string Unescaped data
209 * @return string Escaped binary data
211 public function escapeBinary($binary)
213 return $this->_escapeBinary($binary);
217 * Abstract function that calls escape_binary()
219 * @param string Binary to escape
221 * @return string Escaped binary
223 protected abstract function _escapeBinary($string);
226 * Unescapes a binary string that was fetched from the database
228 * @param string Escaped data
230 * @return string Unescaped binary data
232 public function unescapeBinary($binary)
234 return $this->_unescapeBinary($binary);
238 * Abstract function that calls unescape_binary()
240 * @param string Escaped data
242 * @return string Data that has been unescaped
244 protected abstract function _unescapeBinary($string);
247 * Send a query and return the first row of the results
249 * @param string Query string
250 * @param string Result return function (in the database layer)
252 * @return mixed Results in variable formats
254 public function queryFirst($string, $callback = 'fetchArray')
256 $resource = $this->query($string);
259 $return = $resource->$callback();
270 * Returns the errror number
272 public abstract function _errorNumber();
275 * Returns the error string
277 public abstract function _errorString();
280 * Fetch the unique ID of the record just inserted
282 * @return integer Insert-ID
284 public function insertId()
286 return $this->_insertID();
290 * Abstract function that returns the ID of the most recently-inserted
293 * @return integer Insertion ID
295 protected abstract function _insertId();
298 * Fetch the number of rows affected by the query
300 * @param integer Result
302 * @return integer Number of affected rows
304 public function affectedRows()
306 return $this->_affectedRows($this->result
);
310 * Abstract function that returns the number of affected rows in the result
312 * @param integer Result ID
314 * @return integer Number of rows
316 protected abstract function _affectedRows($result);
319 * Sends the command to start a transaction. This command should never
320 * be reached as it's always overridden
322 public abstract function begin();
325 * Sends the command to rollback to a given savepoint. This command
326 * should never be reached as it's always overridden
328 * @param string Named savepoint
330 public abstract function rollback();
333 * Sends the command to commit the entire transaction. This command
334 * should never be reached as it's always overridden
336 public abstract function commit();
342 * This class holds result information for a database result
345 * @copyright Copyright (c)2005 - 2008, Blue Static
349 abstract class BSDbResult
352 * The result resource
358 * Sets the resource and returns a result object
360 * @param resource The result of the query
362 public function __construct($result)
364 $this->result
= $result;
368 * Fetch the query result as an array
370 * @param integer Result
371 * @param bool Return an associative array?
373 * @return array A row of the query result
375 public function fetchArray($assoc = true)
377 return $this->{($assoc
? '_fetchAssocArray' : '_fetchRowArray')}($this->result
);
381 * Abstract function that returns an associative array of given result
383 * @param integer Result
385 * @return array Result array
387 protected abstract function _fetchAssocArray($result);
390 * Abstract function that returns a row array of given result
392 * @param integer Result
394 * @return array Result array
396 protected abstract function _fetchRowArray($result);
399 * Fetch the query result as an object
401 * @param integer Result
403 * @return object An object with the query result
405 public function fetchObject()
407 return $this->_fetchObject($this->result
);
411 * Abstract function that returns an object for a given result
413 * @param integer Result
415 * @return object Row object
417 public abstract function _fetchObject($result);
420 * Free the current query result
422 * @param integer Result
424 public function free()
426 $this->_freeResult($this->result
);
430 * Abstract function that frees a given result
432 * @param integer Result ID
434 protected abstract function _freeResult($result);
437 * Fetch the number of rows in the result
439 * @param integer Result
441 * @return integer Number of rows
443 public function size()
445 return $this->_numRows($this->result
);
449 * Abstract function that returns the number of rows in the result
451 * @param integer Result ID
453 * @return integer Number of rows
455 protected abstract function _numRows($result);
461 * Exception handler class for the database classes
463 * @author Blue Static
464 * @copyright Copyright (c)2005 - 2008, Blue Static
468 class BSDbException
extends Exception
471 * The query string that caused the error
477 * Initializes a new database exception
479 * @param string The error message
480 * @param ineger MySQL error code
481 * @param sring Query string that caused the error
483 public function __construct($error, $errorNum, $query)
485 $this->query
= $query;
486 parent
::__construct($error, $errorNum);
490 * Returns the query that failed
494 public function getQuery()