RE: [PEAR-DEV] My contribute do DB/common.php - getMany()
| From: | Lukas Smith | Date: | Wed, 10 Jul 2002 12:59:33 +0000 |
| Subject: | RE: [PEAR-DEV] My contribute do DB/common.php - getMany() | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-7629@lists.php.net to get a copy of this message | ||
Well this is yet another "high level" method that does not add any
functionality but safes a bit of work for a programmer.
I did some fairly crude testing and it seems like these types of methods
don't really add much overhead. They do however make the API larger and
mean that more code needs to be maintained (although these type of
methods don't need all that much more since they are the same for all
RDBMS).
The question is more if we want more of these methods to be part of PEAR
DB/MDB or if they should be moved to a separate package.
Best regards,
Lukas
> -----Original Message-----
> From: Johann [mailto:programisci_listy@murator.com.pl]
> Sent: Wednesday, July 10, 2002 2:35 PM
> To: pear-dev@lists.php.net
> Subject: [PEAR-DEV] My contribute do DB/common.php - getMany()
> Importance: High
>
> Hi folks,
>
> I was inspired simplicity of Python my_db module for paging results
> from db, so I decided to write similar easy-to-use function for PHP.
> This function should be added to DB/commom.php of PEAR. It is based on
> getAll() method from DB/common.php but it has some advantages. Check
> examples below.
>
> My function could overwrite older getAll() code or could be added as
> another method with different name: getMany().
>
> Because I have no writing rights to CVS root I include my code below.
>
> // }}}
> // {{{ getMany()
>
> /**
> * Fetch chosen part of the rows returned from a query.
> * (usefull for paging results from database)
> *
> * @param int $from first row, note: first row is 0, not 1
> * @param int $limit number of rows to return
> * @param string $query the SQL query
> *
> * @param array $params array if supplied, prepare/execute will be
used
> * with this array as execute parameters
> * @param integer $fetchmode the fetch mode to use
> *
> * @access public
> * @return array an nested array, or a DB error
> *
> * @author Jaroslaw Zabiello <webmaster@watchtower.org.pl>
> *
> * Examples:
> * $db = new MyDB; // MyDB overloads DB_mssql or DB_mysql or another
class
> * $sql = "SELECT * FROM table";
> *
> * // the same as $db->getAll($sql):
> * print_r($db->getMany($sql));
> *
> * // prints all rows from 4 record:
> * print_r($db->getMany($sql, 3));
> *
> * // prints 5 rows from 4 record:
> * print_r($db->getMany($sql, 3, 5));
> */
> function &getMany($query, $from=null, $limit = null, $params = null,
> $fetchmode
> = DB_FETCHMODE_DEFAULT)
> {
> if (!isset($from) and !isset($limit)) {
> return $this->getAll($query, $params, $fetchmode);
> }
>
> // compat check, the params and fetchmode parameters used to
> // have the opposite order
> if (!is_array($params)) {
> if (is_array($fetchmode)) {
> $tmp = $params;
> $params = $fetchmode;
> $fetchmode = $tmp;
> } elseif ($params !== null) {
> $fetchmode = $params;
> $params = null;
> }
> }
> $params = (empty($params)) ? array() : $params;
> $fetchmode = (empty($fetchmode)) ? DB_FETCHMODE_DEFAULT :
$fetchmode;
> settype($params, "array");
> if (sizeof($params) > 0) {
> $sth = $this->prepare($query);
>
> if (DB::isError($sth)) {
> return $sth;
> }
>
> $res = $this->execute($sth, $params);
> } else {
> $res = $this->query($query);
> }
>
> if (DB::isError($res)) {
> return $res;
> }
>
> $results = array();
> $this->pushErrorHandling(PEAR_ERROR_RETURN);
>
> if (!isset($limit)) { // all remain rows from choosen point
($from)
> while ($row = $res->fetchRow($fetchmode, $from++)) {
> if ($fetchmode & DB_FETCHMODE_FLIPPED) {
> foreach ($row as $key => $val) {
> $results[$key][] = $val;
> }
> } else {
> $results[] = $row;
> }
> }
> } else { // only rows from $from to $from+$limit
> $max = $from + $limit;
> while ($row = $res->fetchRow($fetchmode, $from++) and $from <=
> $max) {
> if ($fetchmode & DB_FETCHMODE_FLIPPED) {
> foreach ($row as $key => $val) {
> $results[$key][] = $val;
> }
> } else {
> $results[] = $row;
> }
> }
> }
> $this->popErrorHandling();
>
> $res->free();
>
> if (isset($sth)) {
> $this->freeResult($sth);
> }
> if (DB::isError($row)) {
> return $this->raiseError($row);
> }
> return $results;
> }
>
>
> --
> PEAR Development Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php