Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.1.2

Bug #945 [Patch] Return result object
Submitted: 2004-03-04 11:48 UTC
From: dostick at ctco dot lv Assigned: quipo
Status: Closed Package: DB_QueryTool
PHP Version: 4.3.4 OS: UNIX
Roadmaps: (Not assigned)    
Subscription  


 [2004-03-04 11:48 UTC] dostick at ctco dot lv
Description: ------------ This is extension to make QueryTool return object result, just like PEAR_DB. This extension allow to transparently switch from PEAR_DB to QueryTool. <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Roman Dostovalov, Com-tec-so S.A. <roman.dostovalov@ctco.lv> // +----------------------------------------------------------------------+ // // $Id: ResultObject.php,v 1.1 2004/03/04 11:40:32 dostick Exp $ // /** * Include parent class */ require_once 'DB/QueryTool/Result.php'; /** * Result row class */ Class DB_QueryTool_Result_Row { /** * create object properties form array * */ function DB_QueryTool_Result_Row($arr) { foreach ($arr as $key=>$value) { $this->$key = $value; } } // end func } /** * oh. * * @access public */ class DB_QueryTool_Result_Object extends DB_QueryTool_Result { /** * Emulates PEAR DB numrows * * @param * @access public * @return void */ function numRows() { return $this->getCount(); } // end func /** * This function emulates PEAR_DB function FetchRow * With this function DB_QueryTool can transparently replace PEAR_DB * * @todo implement fetchmode support? * @access public * @return void */ function fetchRow($fetchmode) { $arr=$this->getNext(); if (!PEAR::isError($arr)) { $row=new DB_QueryTool_Result_Row($arr); return $row; } return false; } // end func /** * Wrapper for getNext function * * @access public * @return void */ function getNext() { if (!is_array($this->_dataKeys)) { $this->_dataKeys = array_keys($this->_data); } return parent::getNext(); } // end func } // end class ?> Added functions to Query.php: /** * * * @version 2004/04/04 * @access public * @author Roman Dostovalov <roman.dostovalov@ctco.lv> * @param * @return */ function useResultObject( $doit=true ) { $this->_useResultObject = $doit; if( $doit ) require_once 'DB/QueryTool/ResultObject.php'; } Modified function in Query.php: /** * * * @version 2004/04/04 * @access public * @author Wolfram Kriesing <wk@visionp.de> * @param * @return */ function returnResult( &$result ) { if( $this->_useResult ) { if( $result==false ) return false; return new DB_QueryTool_Result( $result ); } if( $this->_useResultObject ) { if( $result==false ) return false; return new DB_QueryTool_Result_Object( $result ); } return $result; } Reproduce code: --------------- Tutorial: Before QueryTool: $db=DB::connect(....); $query = "SELECT * FROM users WHERE users.level=3"; $result = $db->query($query); if ($result->numRows()) { while ($user = $result->fetchRow()) { echo $user->name; } } With QueryTool: $db=DB::connect(....); $q=new DB_Query_Tool; $q->setDbInstance($db); $q->useResultObject(true); $q->setWhere('users.level=3'); $q->getAll(); And the rest of your code is unchanged! it still works with PEAR_DB -style object methods.

Comments

 [2004-04-27 19:41 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [2004-04-27 22:24 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [2004-04-27 23:05 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!