Update version.php to 3.3.0
[isso.git] / Db.php
diff --git a/Db.php b/Db.php
index 57dc5e60ac23901d866645bf40a4e9f625a0a512..169893d0d88f8a7b885d7635a638583c8d80ed4f 100644 (file)
--- a/Db.php
+++ b/Db.php
@@ -2,7 +2,7 @@
 /*=====================================================================*\
 || ###################################################################
 || # 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
@@ -38,7 +38,7 @@ require_once(ISSO . '/Functions.php');
  *             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
  * 
  */
@@ -164,7 +164,7 @@ abstract class BSDb
                        }
                }
                
-               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);
@@ -342,17 +342,23 @@ abstract class BSDb
  * 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
@@ -364,6 +370,23 @@ abstract class BSDbResult
                $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
         *
@@ -453,6 +476,63 @@ abstract class BSDbResult
         * @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);
+       }
 }
 
 /**
@@ -461,7 +541,7 @@ abstract class BSDbResult
  * 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
  *
  */
@@ -471,7 +551,7 @@ class BSDbException extends Exception
         * The query string that caused the error
         * @var string
         */
-       private $query;
+       protected $query;
        
        /**
         * Initializes a new database exception