From: Jan Schneider Date: Tue, 1 Feb 2011 14:53:18 +0000 (+0100) Subject: Add option to set fetchmode. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=222a114a4bb1afb2794d6d67b8094fd9035d062b;p=horde.git Add option to set fetchmode. --- diff --git a/framework/Db/lib/Horde/Db.php b/framework/Db/lib/Horde/Db.php new file mode 100644 index 000000000..aee966cf2 --- /dev/null +++ b/framework/Db/lib/Horde/Db.php @@ -0,0 +1,38 @@ + + * @license http://opensource.org/licenses/bsd-license.php + * @category Horde + * @package Db + */ + +/** + * @author Jan Schneider + * @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; +} diff --git a/framework/Db/lib/Horde/Db/Adapter/Mysql/Result.php b/framework/Db/lib/Horde/Db/Adapter/Mysql/Result.php index 176568ad2..873589145 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Mysql/Result.php +++ b/framework/Db/lib/Horde/Db/Adapter/Mysql/Result.php @@ -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]; + } } diff --git a/framework/Db/lib/Horde/Db/Adapter/Mysqli/Result.php b/framework/Db/lib/Horde/Db/Adapter/Mysqli/Result.php index 4465d3567..9318bb22d 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Mysqli/Result.php +++ b/framework/Db/lib/Horde/Db/Adapter/Mysqli/Result.php @@ -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]; + } } diff --git a/framework/Db/package.xml b/framework/Db/package.xml index 1c1dc5ea2..9e747fed2 100644 --- a/framework/Db/package.xml +++ b/framework/Db/package.xml @@ -16,8 +16,8 @@ chuck@horde.org yes - 2010-10-22 - + 2011-02-01 + 0.1.0 0.1.0 @@ -33,6 +33,9 @@ + + + @@ -85,6 +88,7 @@ + @@ -186,8 +190,10 @@ + + @@ -267,7 +273,7 @@ beta beta - 2010-10-22 + 2011-02-01 BSD * Add support for adding autoincrement to a column.