cvs: pear /MDB2/MDB2 Extended.php

From: Date: Wed, 07 Apr 2004 21:12:02 +0000
Subject: cvs: pear /MDB2/MDB2 Extended.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-17996@lists.php.net to get a copy of this message
lsmith Wed Apr 7 17:12:02 2004 EDT Modified files: /pear/MDB2/MDB2 Extended.php Log: - moved getOne, getRow, getCol and getAll back into the exteneded container (these methods are a misuse of prepare queries, if you want to separate your sql from your data use a proper DAO) - added getAssoc just to make DB switchers happy

http://cvs.php.net/diff.php/pear/MDB2/MDB2/Extended.php?r1=1.2&r2=1.3&ty=u Index: pear/MDB2/MDB2/Extended.php diff -u pear/MDB2/MDB2/Extended.php:1.2 pear/MDB2/MDB2/Extended.php:1.3 --- pear/MDB2/MDB2/Extended.php:1.2 Thu Jan 8 08:43:29 2004 +++ pear/MDB2/MDB2/Extended.php Wed Apr 7 17:12:02 2004 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@backendmedia.com> | // +----------------------------------------------------------------------+ // -// $Id: Extended.php,v 1.2 2004/01/08 13:43:29 lsmith Exp $ +// $Id: Extended.php,v 1.3 2004/04/07 21:12:02 lsmith Exp $ /** * @package MDB2 @@ -220,5 +220,284 @@ $result =& $db->query($query, $types, $result_class); return $result; } + + + // }}} + // {{{ getOne() + + /** + * Fetch the first column of the first row of data returned from + * a query. Takes care of doing the query and freeing the results + * when finished. + * + * @param string $query the SQL query + * @param string $type string that contains the type of the column in the + * result set + * @param array $params if supplied, prepare/execute will be used + * with this array as execute parameters + * @param array $param_types array that contains the types of the values + * defined in $params + * @return mixed MDB2_OK or value on success, a MDB2 error on failure + * @access public + */ + function getOne($query, $type = null, $params = array(), $param_types = null) + { + $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; + settype($params, 'array'); + if (count($params) == 0) { + return $db->queryOne($query, $type); + } + + $prepared_query = $db->prepare($query, $param_types); + if (MDB2::isError($prepared_query)) { + return $prepared_query; + } + + $result = $db->executeParams($prepared_query, $type, $params); + if ($result == MDB2_OK || MDB2::isError($result)) { + return $result; + } + + $one = $result->fetch(); + $db->freePrepared($prepared_query); + $result->free(); + return $one; + } + + // }}} + // {{{ getRow() + + /** + * Fetch the first row of data returned from a query. Takes care + * of doing the query and freeing the results when finished. + * + * @param string $query the SQL query + * @param array $types array that contains the types of the columns in + * the result set + * @param array $params array if supplied, prepare/execute will be used + * with this array as execute parameters + * @param array $param_types array that contains the types of the values + * defined in $params + * @param integer $fetchmode the fetch mode to use + * @return mixed MDB2_OK or data array on success, a MDB2 error on failure + * @access public + */ + function getRow($query, $types = null, $params = array(), + $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT) + { + $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; + settype($params, 'array'); + if (count($params) == 0) { + return $db->queryRow($query, $types, $fetchmode); + } + + $prepared_query = $db->prepare($query, $param_types); + if (MDB2::isError($prepared_query)) { + return $prepared_query; + } + + $result = $db->executeParams($prepared_query, $types, $params); + if ($result == MDB2_OK || MDB2::isError($result)) { + return $result; + } + + $row = $result->fetchRow($fetchmode); + $db->freePrepared($prepared_query); + $result->free(); + return $row; + } + + // }}} + // {{{ getCol() + + /** + * Fetch a single column from a result set and return it as an + * indexed array. + * + * @param string $query the SQL query + * @param string $type string that contains the type of the column in the + * result set + * @param array $params array if supplied, prepare/execute will be used + * with this array as execute parameters + * @param array $param_types array that contains the types of the values + * defined in $params + * @param mixed $colnum which column to return + * @return mixed MDB2_OK or data array on success, a MDB2 error on failure + * @access public + */ + function getCol($query, $type = null, $params = array(), + $param_types = null, $colnum = 0) + { + $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; + settype($params, 'array'); + if (count($params) > 0) { + $result = $db->queryCol($query, $type, $colnum); + } + + $prepared_query = $db->prepare($query, $param_types); + if (MDB2::isError($prepared_query)) { + return $prepared_query; + } + + $result = $db->executeParams($prepared_query, $type, $params); + if ($result == MDB2_OK || MDB2::isError($result)) { + return $result; + } + + $col = $result->fetchCol($colnum); + $db->freePrepared($prepared_query); + $result->free(); + return $col; + } + + // }}} + // {{{ getAll() + + /** + * Fetch all the rows returned from a query. + * + * @param string $query the SQL query + * @param array $types array that contains the types of the columns in + * the result set + * @param array $params array if supplied, prepare/execute will be used + * with this array as execute parameters + * @param array $param_types array that contains the types of the values + * defined in $params + * @param integer $fetchmode the fetch mode to use + * @param boolean $rekey if set to true, the $all will have the first + * column as its first dimension + * @param boolean $force_array used only when the query returns exactly + * two columns. If true, the values of the returned array will be + * one-element arrays instead of scalars. + * @param boolean $group if true, the values of the returned array is + * wrapped in another array. If the same key value (in the first + * column) repeats itself, the values will be appended to this array + * instead of overwriting the existing values. + * @return mixed MDB2_OK or data array on success, a MDB2 error on failure + * @access public + */ + function getAll($query, $types = null, $params = array(), + $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT, + $rekey = false, $force_array = false, $group = false) + { + $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; + settype($params, 'array'); + if (count($params) > 0) { + return $db->queryAll($query, $types, $fetchmode, $rekey, $force_array, $group); + } + + $prepared_query = $db->prepare($query, $param_types); + if (MDB2::isError($prepared_query)) { + return $prepared_query; + } + + $result = $db->executeParams($prepared_query, $types, $params); + if ($result == MDB2_OK || MDB2::isError($result)) { + return $result; + } + + $all = $result->fetchAll($result, $fetchmode, $rekey, $force_array, $group); + $db->freePrepared($prepared_query); + $result->free(); + return $all; + } + + // }}} + // {{{ getAssoc() + + /** + * Fetch the entire result set of a query and return it as an + * associative array using the first column as the key. + * + * If the result set contains more than two columns, the value + * will be an array of the values from column 2-n. If the result + * set contains only two columns, the returned value will be a + * scalar with the value of the second column (unless forced to an + * array with the $force_array parameter). A MDB error code is + * returned on errors. If the result set contains fewer than two + * columns, a MDB2_ERROR_TRUNCATED error is returned. + * + * For example, if the table 'mytable' contains: + * + * ID TEXT DATE + * -------------------------------- + * 1 'one' 944679408 + * 2 'two' 944679408 + * 3 'three' 944679408 + * + * Then the call getAssoc('SELECT id,text FROM mytable') returns: + * array( + * '1' => 'one', + * '2' => 'two', + * '3' => 'three', + * ) + * + * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns: + * array( + * '1' => array('one', '944679408'), + * '2' => array('two', '944679408'), + * '3' => array('three', '944679408') + * ) + * + * If the more than one row occurs with the same value in the + * first column, the last row overwrites all previous ones by + * default. Use the $group parameter if you don't want to + * overwrite like this. Example: + * + * getAssoc('SELECT category,id,name FROM mytable', null, null + * MDB2_FETCHMODE_ASSOC, false, true) returns: + * array( + * '1' => array(array('id' => '4', 'name' => 'number four'), + * array('id' => '6', 'name' => 'number six') + * ), + * '9' => array(array('id' => '4', 'name' => 'number four'), + * array('id' => '6', 'name' => 'number six') + * ) + * ) + * + * Keep in mind that database functions in PHP usually return string + * values for results regardless of the database's internal type. + * + * @param string $query the SQL query + * @param array $types array that contains the types of the columns in + * the result set + * @param array $params array if supplied, prepare/execute will be used + * with this array as execute parameters + * @param array $param_types array that contains the types of the values + * defined in $params + * @param boolean $force_array used only when the query returns + * exactly two columns. If TRUE, the values of the returned array + * will be one-element arrays instead of scalars. + * @param boolean $group if TRUE, the values of the returned array + * is wrapped in another array. If the same key value (in the first + * column) repeats itself, the values will be appended to this array + * instead of overwriting the existing values. + * @return array associative array with results from the query. + * @access public + */ + function getAssoc($query, $types = null, $params = array(), $param_types = null, + $fetchmode = MDB2_FETCHMODE_ORDERED, $force_array = false, $group = false) + { + $db =& $GLOBALS['_MDB2_databases'][$this->db_index]; + settype($params, 'array'); + if (count($params) > 0) { + return $db->queryAll($query, $types, $fetchmode, true, $force_array, $group); + } + + $prepared_query = $db->prepare($query, $param_types); + if (MDB2::isError($prepared_query)) { + return $prepared_query; + } + + $result = $db->executeParams($prepared_query, $types, $params); + if ($result == MDB2_OK || MDB2::isError($result)) { + return $result; + } + + $all = $result->fetchAll($fetchmode, true, $force_array, $group); + $db->freePrepared($prepared_query); + $result->free(); + return $all; + } } ?> \ No newline at end of file
« previous php.pear.cvs (#17996) next »