All pastes #448918 Raw Edit

Unnamed

public php v1 · immutable
·published 2007-04-19 23:03 UTC
rendered paste body
/**	 * Execute SQL 	 *	 * @param sql		SQL statement to execute, or possibly an array holding prepared statement ($sql[0] will hold sql text)	 * @param [inputarr]	holds the input data to bind to. Null elements will be set to null.	 * @return 		RecordSet or false	 */	function &Execute($sql,$inputarr=false) 	{		if ($inputarr) {			if (!is_array($inputarr)) $inputarr = array($inputarr);						$element0 = reset($inputarr);			# is_object check because oci8 descriptors can be passed in			$array_2d = is_array($element0) && !is_object(reset($element0));			//remove extra memory copy of input -mikefedyk			unset($element0);						if (!is_array($sql) && !$this->_bindInputArray) {				$sqlarr = explode('?',$sql);									if (!$array_2d) $inputarr = array($inputarr);				foreach($inputarr as $arr) {					$sql = ''; $i = 0;					//Use each() instead of foreach to reduce memory usage -mikefedyk					while(list(, $v) = each($arr)) {						$sql .= $sqlarr[$i];						// from Ron Baldwin <ron.baldwin#sourceprose.com>						// Only quote string types							$typ = gettype($v);						if ($typ == 'string')							//New memory copy of input created here -mikefedyk							$sql .= $this->qstr($v);						else if ($typ == 'double')							$sql .= str_replace(',','.',$v); // locales fix so 1.1 does not get converted to 1,1						else if ($typ == 'boolean')							$sql .= $v ? $this->true : $this->false;						else if ($typ == 'object') {							if (method_exists($v, '__toString')) $sql .= $this->qstr($v->__toString());							else $sql .= $this->qstr((string) $v);						} else if ($v === null)							$sql .= 'NULL';						else							$sql .= $v;						$i += 1;					}					if (isset($sqlarr[$i])) {						$sql .= $sqlarr[$i];						if ($i+1 != sizeof($sqlarr)) ADOConnection::outp( "Input Array does not match ?: ".htmlspecialchars($sql));					} else if ($i != sizeof($sqlarr))							ADOConnection::outp( "Input array does not match ?: ".htmlspecialchars($sql));							$ret =& $this->_Execute($sql);					if (!$ret) return $ret;				}				} else {				if ($array_2d) {					if (is_string($sql))						$stmt = $this->Prepare($sql);					else						$stmt = $sql;											foreach($inputarr as $arr) {						$ret =& $this->_Execute($stmt,$arr);						if (!$ret) return $ret;					}				} else {					$ret =& $this->_Execute($sql,$inputarr);				}			}		} else {			$ret =& $this->_Execute($sql,false);		}		return $ret;	}	function &_Execute($sql,$inputarr=false)	{		if ($this->debug) {			global $ADODB_INCLUDED_LIB;			if (empty($ADODB_INCLUDED_LIB)) include(ADODB_DIR.'/adodb-lib.inc.php');			$this->_queryID = _adodb_debug_execute($this, $sql,$inputarr);		} else {			$this->_queryID = @$this->_query($sql,$inputarr);		}				/************************		// OK, query executed		*************************/		if ($this->_queryID === false) { // error handling if query fails			if ($this->debug == 99) adodb_backtrace(true,5);				$fn = $this->raiseErrorFn;			if ($fn) {				$fn($this->databaseType,'EXECUTE',$this->ErrorNo(),$this->ErrorMsg(),$sql,$inputarr,$this);			} 			$false = false;			return $false;		} 				if ($this->_queryID === true) { // return simplified recordset for inserts/updates/deletes with lower overhead			$rs = new ADORecordSet_empty();			return $rs;		}				// return real recordset from select statement		$rsclass = $this->rsPrefix.$this->databaseType;		$rs = new $rsclass($this->_queryID,$this->fetchMode);		$rs->connection = &$this; // Pablo suggestion		$rs->Init();		if (is_array($sql)) $rs->sql = $sql[0];		else $rs->sql = $sql;		if ($rs->_numOfRows <= 0) {		global $ADODB_COUNTRECS;			if ($ADODB_COUNTRECS) {				if (!$rs->EOF) { 					$rs = &$this->_rs2rs($rs,-1,-1,!is_array($sql));					$rs->_queryID = $this->_queryID;				} else					$rs->_numOfRows = 0;			}		}		return $rs;	}	function _query($sql,$inputarr)	{	//global $ADODB_COUNTRECS;		//if($ADODB_COUNTRECS) 		return mysql_query($sql,$this->_connectionID);		//else return @mysql_unbuffered_query($sql,$this->_connectionID); // requires PHP >= 4.0.6	}