/*=====================================================================*\
|| ###################################################################
|| # Blue Static ISSO Framework
-|| # Copyright (c)2005-2008 Blue Static
+|| # Copyright (c)2005-2009 Blue Static
|| #
|| # This program is free software; you can redistribute it and/or modify
|| # it under the terms of the GNU General Public License as published by
* ISSO_SHOW_QUERIES_LIVE - Show queries in page output as they are sent
*
* @author Blue Static
- * @copyright Copyright (c)2005 - 2008, Blue Static
+ * @copyright Copyright (c)2005 - 2009, Blue Static
* @package ISSO
*
*/
}
}
- if (strtoupper(substr(trim($string), 0, 6)) == 'SELECT')
+ if (preg_match('/^\s*(select|describe|show|explain)/i', $string))
{
$class = get_class($this) . 'Result';
return new $class($this->result);
* This class holds result information for a database result
*
* @author rsesek
- * @copyright Copyright (c)2005 - 2008, Blue Static
+ * @copyright Copyright (c)2005 - 2009, Blue Static
* @package ISSO
*
*/
-abstract class BSDbResult
+abstract class BSDbResult implements Iterator
{
/**
* The result resource
* @var resource
*/
- private $result;
+ protected $result;
+
+ /**
+ * All the results (as used by the iterator implementation)
+ * @var string
+ */
+ protected $iterator;
/**
* Sets the resource and returns a result object
$this->result = $result;
}
+ /**
+ * Method to call on the result
+ *
+ * @param string Method name
+ *
+ * @return array Array of results
+ */
+ public function fetchAll($method = 'fetchArray')
+ {
+ $results = array();
+ while ($result = $this->$method())
+ {
+ $results[] = $result;
+ }
+ return $results;
+ }
+
/**
* Fetch the query result as an array
*
* @return integer Number of rows
*/
protected abstract function _numRows($result);
+
+ // ###################################################################
+ // # Iterator implementation
+ // ###################################################################
+ /**
+ * Rewinds the iterator by resetting the result pointer
+ *
+ * @return void
+ */
+ public function rewind()
+ {
+ if ($this->iterator == null)
+ {
+ $this->iterator = $this->fetchAll();
+ }
+ reset($this->iterator);
+ }
+
+ /**
+ * Returns the current row
+ *
+ * @return array
+ */
+ public function current()
+ {
+ return current($this->iterator);
+ }
+
+ /**
+ * Returns the key (result pointer index)
+ *
+ * @return integer
+ */
+ public function key()
+ {
+ return key($this->iterator);
+ }
+
+ /**
+ * Advances the iterator and returns the row
+ *
+ * @return array
+ */
+ public function next()
+ {
+ return next($this->iterator);
+ }
+
+ /**
+ * Checks whether the iterator is valid
+ *
+ * @return boolean
+ */
+ public function valid()
+ {
+ return ($this->current() !== false);
+ }
}
/**
* Exception handler class for the database classes
*
* @author Blue Static
- * @copyright Copyright (c)2005 - 2008, Blue Static
+ * @copyright Copyright (c)2005 - 2009, Blue Static
* @package ISSO
*
*/
* The query string that caused the error
* @var string
*/
- private $query;
+ protected $query;
/**
* Initializes a new database exception