From fa0481609c3728d056f173f04dc742b9d5ec7120 Mon Sep 17 00:00:00 2001 From: "Tomas V.V.Cox" Date: Fri, 3 Aug 2001 14:13:52 +0000 Subject: [PATCH] * Added the new fetch row mode: DB_FETCHMODE_OBJECT that returns the columns of a fetched row as object properties * Added a new class DB_row (the default type of a fetched row object) * Updated some phpdocs --- pear/DB.php | 58 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/pear/DB.php b/pear/DB.php index 33a6bfa259..1710f3997f 100644 --- a/pear/DB.php +++ b/pear/DB.php @@ -123,6 +123,12 @@ define('DB_FETCHMODE_ORDERED', 1); define('DB_FETCHMODE_ASSOC', 2); +/** +* Column data as object properties +*/ + +define('DB_FETCHMODE_OBJECT', 3); + /** * For multi-dimensional results: normally the first level of arrays * is the row number, and the second level indexed by column number or name. @@ -612,40 +618,58 @@ class DB_result } /** - * Fetch and return a row of data (it uses fetchInto for that) - * @param $fetchmode format of fetched row array - * @param $rownum the absolute row number to fetch + * Fetch and return a row of data (it uses backend->fetchInto for that) + * @param $fetchmode format of fetched row + * @param $rownum the row number to fetch * - * @return array a row of data, or false on error + * @return array a row of data, NULL on no more rows or PEAR_Error on error */ function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { if ($fetchmode === DB_FETCHMODE_DEFAULT) { $fetchmode = $this->dbh->fetchmode; } - + if ($fetchmode === DB_FETCHMODE_OBJECT) { + $fetchmode = DB_FETCHMODE_ASSOC; + $return_object = true; + } $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); if ($res !== DB_OK) { return $res; } + if (isset($return_object)) { + $class = $this->dbh->fetchmode_object_class; + $ret =& new $class($arr); + return $ret; + } return $arr; } /** - * Fetch a row of data into an existing array. + * Fetch a row of data into an existing variable. * - * @param $arr reference to data array - * @param $fetchmode format of fetched row array - * @param $rownum the absolute row number to fetch + * @param $arr reference to data containing the row + * @param $fetchmode format of fetched row + * @param $rownum the row number to fetch * - * @return int error code + * @return mixed DB_OK on success, NULL on no more rows or + * a DB_Error object on errors */ function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { if ($fetchmode === DB_FETCHMODE_DEFAULT) { $fetchmode = $this->dbh->fetchmode; } - return $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); + if ($fetchmode === DB_FETCHMODE_OBJECT) { + $fetchmode = DB_FETCHMODE_ASSOC; + $return_object = true; + } + $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); + if (($res === DB_OK) && isset($return_object)) { + $class = $this->dbh->fetchmode_object_class; + $arr = new $class($arr); + } + return $res; } /** @@ -688,4 +712,14 @@ class DB_result } } -?> +class DB_row +{ + function DB_row(&$arr) + { + for (reset($arr); $key = key($arr); next($arr)) { + $this->$key = &$arr[$key]; + } + } +} + +?> \ No newline at end of file -- 2.50.1