added fetchmode parameter to query() and set it to ADODB_FETCH_ASSOC for select(), also added comment about how to avoid cloning the global db object and looping over result sets via the returned result object

This commit is contained in:
Ralf Becker 2008-03-06 11:42:21 +00:00
parent f4b534904f
commit 9de6c2884f

View File

@ -1,43 +1,53 @@
<?php
/**************************************************************************\
* eGroupWare API - database support via ADOdb *
* ------------------------------------------------------------------------ *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation; either version 2.1 of the License, or *
* any later version. *
\**************************************************************************/
/**
* eGroupWare API: Database abstraction library
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage db
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2003-8 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @version $Id$
*/
/* $Id$ */
/**
/**
* Database abstraction library
*
* This allows eGroupWare to use multiple database backends via ADOdb
*
* @package api
* @subpackage db
* @author Ralf Becker <RalfBecker@outdoor-training.de>
* @license LGPL
* You only need to clone the global database object $GLOBALS['egw']->db if:
* - you use the old methods f(), next_record(), row(), num_fields(), num_rows()
* - you access an application table (non phpgwapi) and you want to call set_app()
*
* Otherwise you can simply use $GLOBALS['egw']->db or a reference to it.
*
* Avoiding next_record() or row() can be done by looping with the recordset returned by query() or select():
*
* a) foreach($db->query("SELECT * FROM $table",__LINE__,__FILE__) as $row)
*
* b) foreach($db->select($api_table,'*',$where,__LINE__,__FILE__) as $row)
*
* c) foreach($db->select($table,'*',$where,__LINE__,__FILE__,false,'',$app) as $row)
*/
// some constanst for pre php4.3
if (!defined('PHP_SHLIB_SUFFIX'))
{
// some constanst for pre php4.3
if (!defined('PHP_SHLIB_SUFFIX'))
{
define('PHP_SHLIB_SUFFIX',strtoupper(substr(PHP_OS, 0,3)) == 'WIN' ? 'dll' : 'so');
}
if (!defined('PHP_SHLIB_PREFIX'))
{
}
if (!defined('PHP_SHLIB_PREFIX'))
{
define('PHP_SHLIB_PREFIX',PHP_SHLIB_SUFFIX == 'dll' ? 'php_' : '');
}
if(empty($GLOBALS['egw_info']['server']['db_type']))
{
}
if(empty($GLOBALS['egw_info']['server']['db_type']))
{
$GLOBALS['egw_info']['server']['db_type'] = 'mysql';
}
include_once(EGW_API_INC.'/adodb/adodb.inc.php');
}
include_once(EGW_API_INC.'/adodb/adodb.inc.php');
class egw_db
{
class egw_db
{
/**
* @var string $type translated database type: mysqlt+mysqli ==> mysql, same for odbc-types
*/
@ -483,9 +493,10 @@
* @param int $offset row to start from, default 0
* @param int $num_rows number of rows to return (optional), default -1 = all, 0 will use $GLOBALS['egw_info']['user']['preferences']['common']['maxmatchs']
* @param array/boolean $inputarr array for binding variables to parameters or false (default)
* @param int $fetchmode=ADODB_FETCH_BOTH ADODB_FETCH_BOTH (default), ADODB_FETCH_ASSOC or ADODB_FETCH_NUM
* @return ADORecordSet or false, if the query fails
*/
function query($Query_String, $line = '', $file = '', $offset=0, $num_rows=-1,$inputarr=false)
function query($Query_String, $line = '', $file = '', $offset=0, $num_rows=-1,$inputarr=false,$fetchmode=ADODB_FETCH_BOTH)
{
if ($Query_String == '')
{
@ -501,9 +512,9 @@
{
$this->free();
}
if ($this->Link_ID->fetchMode != ADODB_FETCH_BOTH)
if ($this->Link_ID->fetchMode != $fetchmode)
{
$this->Link_ID->SetFetchMode(ADODB_FETCH_BOTH);
$this->Link_ID->SetFetchMode($fetchmode);
}
if (!$num_rows)
{
@ -1176,18 +1187,20 @@
}
/**
* Escape values before sending them to the database - prevents SQL injunction and SQL errors ;-)
* Escape values before sending them to the database - prevents SQL injection and SQL errors ;-)
*
* Please note that the quote function already returns necessary quotes: quote('Hello') === "'Hello'".
* Int and Auto types are casted to int: quote('1','int') === 1, quote('','int') === 0, quote('Hello','int') === 0
* Arrays of id's stored in strings: quote(array(1,2,3),'string') === "'1,2,3'"
*
* @param mixed $value the value to be escaped
* @param string/boolean $type=false string the type of the db-column, default False === varchar
* @param boolean $not_null=true is column NOT NULL, default true, else php null values are written as SQL NULL
* @param int $length=null length of the varchar column, to truncate it if the database requires it (eg. Postgres)
* @param string $glue=',' used to glue array values together for the string type
* @return string escaped sting
*/
function quote($value,$type=False,$not_null=true,$length=null)
function quote($value,$type=False,$not_null=true,$length=null,$glue=',')
{
if ($this->Debug) echo "<p>db::quote(".(is_null($value)?'NULL':"'$value'").",'$type','$not_null')</p>\n";
@ -1233,6 +1246,10 @@
case 'timestamp':
return $this->Link_ID->DBTimeStamp($value);
}
if (is_array($value))
{
$value = implode($glue,$value);
}
if (!is_null($length) && strlen($value) > $length)
{
$value = substr($value,0,$length);
@ -1706,7 +1723,7 @@
{
return $sql;
}
return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows);
return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows,false,ADODB_FETCH_ASSOC);
}
/**
@ -1750,6 +1767,6 @@
if ($this->Debug) echo "<p>sql='$sql'</p>";
return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows);
}
return $this->query($sql,$line,$file,$offset,$offset===False ? -1 : (int)$num_rows,false,ADODB_FETCH_ASSOC);
}
}