Home » Database » DB_QueryTool » Bug #945
[Patch] Return result object
Details
| 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) |
Comments
[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.