]>
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 (preg_match('/^\s*(select|describe|show|explain)/i', $string))
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
implements Iterator
352 * The result resource
358 * All the results (as used by the iterator implementation)
364 * Sets the resource and returns a result object
366 * @param resource The result of the query
368 public function __construct($result)
370 $this->result
= $result;
374 * Method to call on the result
376 * @param string Method name
378 * @return array Array of results
380 public function fetchAll($method = 'fetchArray')
383 while ($result = $this->$method())
385 $results[] = $result;
391 * Fetch the query result as an array
393 * @param integer Result
394 * @param bool Return an associative array?
396 * @return array A row of the query result
398 public function fetchArray($assoc = true)
400 return $this->{($assoc
? '_fetchAssocArray' : '_fetchRowArray')}($this->result
);
404 * Abstract function that returns an associative array of given result
406 * @param integer Result
408 * @return array Result array
410 protected abstract function _fetchAssocArray($result);
413 * Abstract function that returns a row array of given result
415 * @param integer Result
417 * @return array Result array
419 protected abstract function _fetchRowArray($result);
422 * Fetch the query result as an object
424 * @param integer Result
426 * @return object An object with the query result
428 public function fetchObject()
430 return $this->_fetchObject($this->result
);
434 * Abstract function that returns an object for a given result
436 * @param integer Result
438 * @return object Row object
440 public abstract function _fetchObject($result);
443 * Free the current query result
445 * @param integer Result
447 public function free()
449 $this->_freeResult($this->result
);
453 * Abstract function that frees a given result
455 * @param integer Result ID
457 protected abstract function _freeResult($result);
460 * Fetch the number of rows in the result
462 * @param integer Result
464 * @return integer Number of rows
466 public function size()
468 return $this->_numRows($this->result
);
472 * Abstract function that returns the number of rows in the result
474 * @param integer Result ID
476 * @return integer Number of rows
478 protected abstract function _numRows($result);
480 // ###################################################################
481 // # Iterator implementation
482 // ###################################################################
484 * Rewinds the iterator by resetting the result pointer
488 public function rewind()
490 if ($this->iterator
== null)
492 $this->iterator
= $this->fetchAll();
494 reset($this->iterator
);
498 * Returns the current row
502 public function current()
504 return current($this->iterator
);
508 * Returns the key (result pointer index)
512 public function key()
514 return key($this->iterator
);
518 * Advances the iterator and returns the row
522 public function next()
524 return next($this->iterator
);
528 * Checks whether the iterator is valid
532 public function valid()
534 return ($this->current() !== false);
541 * Exception handler class for the database classes
543 * @author Blue Static
544 * @copyright Copyright (c)2005 - 2008, Blue Static
548 class BSDbException
extends Exception
551 * The query string that caused the error
557 * Initializes a new database exception
559 * @param string The error message
560 * @param ineger MySQL error code
561 * @param sring Query string that caused the error
563 public function __construct($error, $errorNum, $query)
565 $this->query
= $query;
566 parent
::__construct($error, $errorNum);
570 * Returns the query that failed
574 public function getQuery()