Add option to set fetchmode.
authorJan Schneider <jan@horde.org>
Tue, 1 Feb 2011 14:53:18 +0000 (15:53 +0100)
committerJan Schneider <jan@horde.org>
Tue, 1 Feb 2011 14:53:18 +0000 (15:53 +0100)
framework/Db/lib/Horde/Db.php [new file with mode: 0644]
framework/Db/lib/Horde/Db/Adapter/Mysql/Result.php
framework/Db/lib/Horde/Db/Adapter/Mysqli/Result.php
framework/Db/package.xml

diff --git a/framework/Db/lib/Horde/Db.php b/framework/Db/lib/Horde/Db.php
new file mode 100644 (file)
index 0000000..aee966c
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Copyright 2011 The Horde Project (http://www.horde.org/)
+ *
+ * @author     Jan Schneider <jan@horde.org>
+ * @license    http://opensource.org/licenses/bsd-license.php
+ * @category   Horde
+ * @package    Db
+ */
+
+/**
+ * @author     Jan Schneider <jan@horde.org>
+ * @license    http://opensource.org/licenses/bsd-license.php
+ * @category   Horde
+ * @package    Db
+ */
+class Horde_Db
+{
+    /**
+     * Specifies that the fetch method shall return each row as an array
+     * indexed by column name as returned in the corresponding result set.
+     */
+    const FETCH_ASSOC = 2;
+
+    /**
+     * Specifies that the fetch method shall return each row as an array
+     * indexed by column number as returned in the corresponding result set,
+     * starting at column 0.
+     */
+    const FETCH_NUM = 3;
+
+    /**
+     * Specifies that the fetch method shall return each row as an array
+     * indexed by both column name and number as returned in the corresponding
+     * result set, starting at column 0.
+     */
+    const FETCH_BOTH = 4;
+}
index 176568a..8735891 100644 (file)
@@ -68,6 +68,11 @@ class Horde_Db_Adapter_Mysql_Result implements Iterator
     protected $_eof;
 
     /**
+     * Which kind of keys to use for results.
+     */
+    protected $_fetchMode = MYSQL_ASSOC;
+
+    /**
      * Constructor
      *
      * @param   Horde_Db_Adapter $adapter
@@ -148,7 +153,7 @@ class Horde_Db_Adapter_Mysql_Result implements Iterator
         }
 
         if ($this->_result) {
-            $row = mysql_fetch_array($this->_result, MYSQL_BOTH);
+            $row = mysql_fetch_array($this->_result, $this->_fetchMode);
             if (!$row) {
                 $this->_eof = true;
             } else {
@@ -168,13 +173,17 @@ class Horde_Db_Adapter_Mysql_Result implements Iterator
     }
 
     /**
-     * Return the current row and advance the recordset one row.
+     * Returns the current row and advances the recordset one row.
+     *
+     * @param integer $fetchmode  The default fetch mode for this result. One
+     *                            of the Horde_Db::FETCH_* constants.
      */
-    public function fetch()
+    public function fetch($fetchmode = Horde_Db::FETCH_ASSOC)
     {
         if (!$this->valid()) {
             return null;
         }
+        $this->setFetchMode($fetchmode);
         $row = $this->current();
         $this->next();
         return $row;
@@ -193,4 +202,16 @@ class Horde_Db_Adapter_Mysql_Result implements Iterator
         return !$this->_eof;
     }
 
+    /**
+     * Sets the default fetch mode for this result.
+     *
+     * @param integer $fetchmode  One of the Horde_Db::FETCH_* constants.
+     */
+    public function setFetchMode($fetchmode)
+    {
+        $map = array(Horde_Db::FETCH_ASSOC => MYSQL_ASSOC,
+                     Horde_Db::FETCH_NUM   => MYSQL_NUM,
+                     Horde_Db::FETCH_BOTH  => MYSQL_BOTH);
+        $this->_fetchMode = $map[$fetchmode];
+    }
 }
index 4465d35..9318bb2 100644 (file)
@@ -68,6 +68,11 @@ class Horde_Db_Adapter_Mysqli_Result implements Iterator
     protected $_eof;
 
     /**
+     * Which kind of keys to use for results.
+     */
+    protected $_fetchMode = MYSQLI_ASSOC;
+
+    /**
      * Constructor
      *
      * @param   Horde_Db_Adapter $adapter
@@ -148,7 +153,7 @@ class Horde_Db_Adapter_Mysqli_Result implements Iterator
         }
 
         if ($this->_result) {
-            $row = $this->_result->fetch_array(MYSQLI_ASSOC);
+            $row = $this->_result->fetch_array($this->_fetchMode);
             if (!$row) {
                 $this->_eof = true;
             } else {
@@ -168,13 +173,17 @@ class Horde_Db_Adapter_Mysqli_Result implements Iterator
     }
 
     /**
-     * Return the current row and advance the recordset one row.
+     * Returns the current row and advances the recordset one row.
+     *
+     * @param integer $fetchmode  The default fetch mode for this result. One
+     *                            of the Horde_Db::FETCH_* constants.
      */
-    public function fetch()
+    public function fetch($fetchmode = Horde_Db::FETCH_ASSOC)
     {
         if (!$this->valid()) {
             return null;
         }
+        $this->setFetchMode($fetchmode);
         $row = $this->current();
         $this->next();
         return $row;
@@ -193,4 +202,16 @@ class Horde_Db_Adapter_Mysqli_Result implements Iterator
         return !$this->_eof;
     }
 
+    /**
+     * Sets the default fetch mode for this result.
+     *
+     * @param integer $fetchmode  One of the Horde_Db::FETCH_* constants.
+     */
+    public function setFetchMode($fetchmode)
+    {
+        $map = array(Horde_Db::FETCH_ASSOC => MYSQL_ASSOC,
+                     Horde_Db::FETCH_NUM   => MYSQL_NUM,
+                     Horde_Db::FETCH_BOTH  => MYSQL_BOTH);
+        $this->_fetchMode = $map[$fetchmode];
+    }
 }
index 1c1dc5e..9e747fe 100644 (file)
@@ -16,8 +16,8 @@
   <email>chuck@horde.org</email>
   <active>yes</active>
  </lead>
- <date>2010-10-22</date>
- <time>09:45:53</time>
+ <date>2011-02-01</date>
+ <time>15:51:20</time>
  <version>
   <release>0.1.0</release>
   <api>0.1.0</api>
@@ -33,6 +33,9 @@
  </notes>
  <contents>
   <dir baseinstalldir="/" name="/">
+   <dir name="bin">
+    <file name="horde-db-migrate" role="script" />
+   </dir> <!-- /bin -->
    <dir name="doc">
     <file name="README.TESTING.txt" role="doc" />
     <file name="TODO.txt" role="doc" />
@@ -85,6 +88,7 @@
       <file name="Exception.php" role="php" />
       <file name="StatementParser.php" role="php" />
      </dir> <!-- /lib/Horde/Db -->
+     <file name="Db.php" role="php" />
     </dir> <!-- /lib/Horde -->
    </dir> <!-- /lib -->
    <dir name="test">
  </dependencies>
  <phprelease>
   <filelist>
+   <install as="horde-db-migrate" name="bin/horde-db-migrate" />
    <install as="README.TESTING.txt" name="doc/README.TESTING.txt" />
    <install as="TODO.txt" name="doc/TODO.txt" />
+   <install as="Horde/Db.php" name="lib/Horde/Db.php" />
    <install as="Horde/Db/Adapter.php" name="lib/Horde/Db/Adapter.php" />
    <install as="Horde/Db/Exception.php" name="lib/Horde/Db/Exception.php" />
    <install as="Horde/Db/StatementParser.php" name="lib/Horde/Db/StatementParser.php" />
     <release>beta</release>
     <api>beta</api>
    </stability>
-   <date>2010-10-22</date>
+   <date>2011-02-01</date>
    <license uri="http://opensource.org/licenses/bsd-license.php">BSD</license>
    <notes>
 * Add support for adding autoincrement to a column.