Upgraded to ADOdb 4.5.1. Contains a lot of small fixes and speed-ups.

This commit is contained in:
moodler 2004-08-07 14:10:19 +00:00
parent f5f346a594
commit fab7e07e19
90 changed files with 1555 additions and 832 deletions

View file

@ -224,7 +224,7 @@
var $substr = 'substr'; /// substring operator
var $length = 'length'; /// string length operator
var $random = 'rand()'; /// random function
var $upperCase = false; /// uppercase function
var $upperCase = 'upper'; /// uppercase function
var $fmtDate = "'Y-m-d'"; /// used by DBDate() as the default date format used by the database
var $fmtTimeStamp = "'Y-m-d, h:i:s A'"; /// used by DBTimeStamp as the default timestamp fmt.
var $true = '1'; /// string that represents TRUE for a database
@ -348,15 +348,17 @@
if ($newline) $msg .= "<br>\n";
if (isset($HTTP_SERVER_VARS['HTTP_USER_AGENT'])) echo $msg;
if (isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']) || !$newline) echo $msg;
else echo strip_tags($msg);
if (!empty($ADODB_FLUSH) && ob_get_length() !== false) flush(); // do not flush if output buffering enabled - useless - thx to Jesse Mullan
}
function Time()
{
$rs =& $this->Execute("select $this->sysTimeStamp");
$rs =& $this->_Execute("select $this->sysTimeStamp");
if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
return false;
@ -617,7 +619,7 @@
For databases that require positioned params, eg $1, $2, $3 for postgresql,
pass in Param(false) before setting the first parameter.
*/
function Param($name)
function Param($name,$type='C')
{
return '?';
}
@ -754,7 +756,9 @@
$ret =& $fn($this,$sql,$inputarr);
if (isset($ret)) return $ret;
}
if ($inputarr && is_array($inputarr)) {
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));
@ -782,7 +786,7 @@
if ($i+1 != sizeof($sqlarr))
ADOConnection::outp( "Input Array does not match ?: ".htmlspecialchars($sql));
$ret =& $this->_Execute($sql,false);
$ret =& $this->_Execute($sql);
if (!$ret) return $ret;
}
} else {
@ -803,60 +807,23 @@
return $ret;
}
function& _Execute($sql,$inputarr=false)
{
if ($this->debug) {
global $HTTP_SERVER_VARS;
$ss = '';
if ($inputarr) {
foreach($inputarr as $kk=>$vv) {
if (is_string($vv) && strlen($vv)>64) $vv = substr($vv,0,64).'...';
$ss .= "($kk=>'$vv') ";
}
$ss = "[ $ss ]";
}
$sqlTxt = str_replace(',',', ',is_array($sql) ?$sql[0] : $sql);
// check if running from browser or command-line
$inBrowser = isset($HTTP_SERVER_VARS['HTTP_USER_AGENT']);
if ($inBrowser) {
if ($this->debug === -1)
ADOConnection::outp( "<br>\n($this->databaseType): ".htmlspecialchars($sqlTxt)." &nbsp; <code>$ss</code>\n<br>\n",false);
else
ADOConnection::outp( "<hr>\n($this->databaseType): ".htmlspecialchars($sqlTxt)." &nbsp; <code>$ss</code>\n<hr>\n",false);
} else {
ADOConnection::outp("-----\n($this->databaseType): ".($sqlTxt)." \n-----\n",false);
}
$this->_queryID = $this->_query($sql,$inputarr);
/*
Alexios Fakios notes that ErrorMsg() must be called before ErrorNo() for mssql
because ErrorNo() calls Execute('SELECT @ERROR'), causing recursion
*/
if ($this->databaseType == 'mssql') {
// ErrorNo is a slow function call in mssql, and not reliable in PHP 4.0.6
if($emsg = $this->ErrorMsg()) {
if ($err = $this->ErrorNo()) ADOConnection::outp($err.': '.$emsg);
}
} else if (!$this->_queryID) {
ADOConnection::outp($this->ErrorNo() .': '. $this->ErrorMsg());
}
global $ADODB_INCLUDED_LIB;
if (empty($ADODB_INCLUDED_LIB)) include_once(ADODB_DIR.'/adodb-lib.inc.php');
$this->_queryID = _adodb_debug_execute($this, $sql,$inputarr);
} else {
//****************************
// non-debug version of query
//****************************
$this->_queryID =@$this->_query($sql,$inputarr);
$this->_queryID = @$this->_query($sql,$inputarr);
}
/************************
// OK, query executed
*************************/
if ($this->_queryID === false) {
// error handling if query fails
if ($this->_queryID === false) { // error handling if query fails
if ($this->debug == 99) adodb_backtrace(true,5);
$fn = $this->raiseErrorFn;
if ($fn) {
@ -866,12 +833,8 @@
return false;
}
if ($this->_queryID === true) {
// return simplified empty recordset for inserts/updates/deletes with lower overhead
if ($this->_queryID === true) { // return simplified recordset for inserts/updates/deletes with lower overhead
$rs =& new ADORecordSet_empty();
#if (is_array($sql)) $rs->sql = $sql[0];
#else $rs->sql = $sql;
return $rs;
}
@ -885,7 +848,7 @@
if ($rs->_numOfRows <= 0) {
global $ADODB_COUNTRECS;
if ($ADODB_COUNTRECS) {
if (!$rs->EOF){
if (!$rs->EOF) {
$rs = &$this->_rs2rs($rs,-1,-1,!is_array($sql));
$rs->_queryID = $this->_queryID;
} else
@ -1241,7 +1204,7 @@
if ($rs) {
if (!$rs->EOF) $ret = reset($rs->fields);
$rs->Close();
}
}
$ADODB_COUNTRECS = $crecs;
return $ret;
}
@ -1481,6 +1444,10 @@
}
return;
}
global $ADODB_INCLUDED_CSV;
if (empty($ADODB_INCLUDED_CSV)) include_once(ADODB_DIR.'/adodb-csvlib.inc.php');
$f = $this->_gencachename($sql.serialize($inputarr),false);
adodb_write_file($f,''); // is adodb_write_file needed?
if (!@unlink($f)) {
@ -1631,7 +1598,7 @@
$forcenulls = defined('ADODB_FORCE_NULLS') ? true : false;
}
if (empty($ADODB_INCLUDED_LIB)) include_once(ADODB_DIR.'/adodb-lib.inc.php');
return _adodb_getupdatesql($this,$rs,$arrFields,$forceUpdate,$magicq);
return _adodb_getupdatesql($this,$rs,$arrFields,$forceUpdate,$magicq,$forcenulls);
}
@ -1650,7 +1617,7 @@
$forcenulls = defined('ADODB_FORCE_NULLS') ? true : false;
}
if (empty($ADODB_INCLUDED_LIB)) include_once(ADODB_DIR.'/adodb-lib.inc.php');
return _adodb_getinsertsql($this,$rs,$arrFields,$magicq);
return _adodb_getinsertsql($this,$rs,$arrFields,$magicq,$forcenulls);
}
@ -2505,7 +2472,10 @@
* @return an associative array indexed by the first column of the array,
* or false if the data has less than 2 cols.
*/
function &GetAssoc($force_array = false, $first2cols = false) {
function &GetAssoc($force_array = false, $first2cols = false)
{
global $ADODB_EXTENSION;
$cols = $this->_numOfFields;
if ($cols < 2) {
return false;
@ -2514,32 +2484,64 @@
$results = array();
if (!$first2cols && ($cols > 2 || $force_array)) {
if ($numIndex) {
while (!$this->EOF) {
$results[trim($this->fields[0])] = array_slice($this->fields, 1);
$this->MoveNext();
if ($ADODB_EXTENSION) {
if ($numIndex) {
while (!$this->EOF) {
$results[trim($this->fields[0])] = array_slice($this->fields, 1);
adodb_movenext($this);
}
} else {
while (!$this->EOF) {
$results[trim(reset($this->fields))] = array_slice($this->fields, 1);
adodb_movenext($this);
}
}
} else {
while (!$this->EOF) {
$results[trim(reset($this->fields))] = array_slice($this->fields, 1);
$this->MoveNext();
if ($numIndex) {
while (!$this->EOF) {
$results[trim($this->fields[0])] = array_slice($this->fields, 1);
$this->MoveNext();
}
} else {
while (!$this->EOF) {
$results[trim(reset($this->fields))] = array_slice($this->fields, 1);
$this->MoveNext();
}
}
}
} else {
// return scalar values
if ($numIndex) {
while (!$this->EOF) {
// some bug in mssql PHP 4.02 -- doesn't handle references properly so we FORCE creating a new string
$results[trim(($this->fields[0]))] = $this->fields[1];
$this->MoveNext();
if ($ADODB_EXTENSION) {
// return scalar values
if ($numIndex) {
while (!$this->EOF) {
// some bug in mssql PHP 4.02 -- doesn't handle references properly so we FORCE creating a new string
$results[trim(($this->fields[0]))] = $this->fields[1];
adodb_movenext($this);
}
} else {
while (!$this->EOF) {
// some bug in mssql PHP 4.02 -- doesn't handle references properly so we FORCE creating a new string
$v1 = trim(reset($this->fields));
$v2 = ''.next($this->fields);
$results[$v1] = $v2;
adodb_movenext($this);
}
}
} else {
while (!$this->EOF) {
// some bug in mssql PHP 4.02 -- doesn't handle references properly so we FORCE creating a new string
$v1 = trim(reset($this->fields));
$v2 = ''.next($this->fields);
$results[$v1] = $v2;
$this->MoveNext();
if ($numIndex) {
while (!$this->EOF) {
// some bug in mssql PHP 4.02 -- doesn't handle references properly so we FORCE creating a new string
$results[trim(($this->fields[0]))] = $this->fields[1];
$this->MoveNext();
}
} else {
while (!$this->EOF) {
// some bug in mssql PHP 4.02 -- doesn't handle references properly so we FORCE creating a new string
$v1 = trim(reset($this->fields));
$v2 = ''.next($this->fields);
$results[$v1] = $v2;
$this->MoveNext();
}
}
}
}
@ -2728,7 +2730,8 @@
}
*/
return false;
}
}
/**
* Random access to a specific row in the recordset. Some databases do not support
@ -2989,7 +2992,8 @@
*/
function &FetchNextObj()
{
return $this->FetchNextObject(false);
$o = $this->FetchNextObject(false);
return $o;
}
@ -3415,6 +3419,7 @@
if (!$dbType) return false;
$db = strtolower($dbType);
switch ($db) {
case 'ifx':
case 'maxsql': $db = 'mysqlt'; break;
case 'postgres':
case 'pgsql': $db = 'postgres7'; break;
@ -3456,44 +3461,111 @@
if (!defined('ADODB_ASSOC_CASE')) define('ADODB_ASSOC_CASE',2);
$errorfn = (defined('ADODB_ERROR_HANDLER')) ? ADODB_ERROR_HANDLER : false;
if (strpos($db,'://')) {
$origdsn = $db;
$dsna = @parse_url($db);
if (!$dsna) {
// special handling of oracle, which might not have host
$db = str_replace('@/','@adodb-fakehost/',$db);
$dsna = parse_url($db);
if (!$dsna) return false;
$dsna['host'] = '';
}
$db = @$dsna['scheme'];
if (!$db) return false;
$dsna['host'] = isset($dsna['host']) ? rawurldecode($dsna['host']) : '';
$dsna['user'] = isset($dsna['user']) ? rawurldecode($dsna['user']) : '';
$dsna['pass'] = isset($dsna['pass']) ? rawurldecode($dsna['pass']) : '';
$dsna['path'] = isset($dsna['path']) ? rawurldecode(substr($dsna['path'],1)) : '';
if (isset($dsna['query'])) {
$opt1 = explode('&',$dsna['query']);
foreach($opt1 as $k => $v) {
$arr = explode('=',$v);
$opt[$arr[0]] = isset($arr[1]) ? rawurldecode($arr[1]) : 1;
}
} else $opt = array();
}
/*
* phptype: Database backend used in PHP (mysql, odbc etc.)
* dbsyntax: Database used with regards to SQL syntax etc.
* protocol: Communication protocol to use (tcp, unix etc.)
* hostspec: Host specification (hostname[:port])
* database: Database to use on the DBMS server
* username: User name for login
* password: Password for login
*/
if (!empty($ADODB_NEWCONNECTION)) {
$obj = $ADODB_NEWCONNECTION($db);
if ($obj) {
if ($errorfn) $obj->raiseErrorFn = $errorfn;
return $obj;
} else {
if (!isset($ADODB_LASTDB)) $ADODB_LASTDB = '';
if (empty($db)) $db = $ADODB_LASTDB;
if ($db != $ADODB_LASTDB) $db = ADOLoadCode($db);
if (!$db) {
if (isset($origdsn)) $db = $origdsn;
if ($errorfn) {
// raise an error
$ignore = false;
$errorfn('ADONewConnection', 'ADONewConnection', -998,
"could not load the database driver for '$db'",
$db,false,$ignore);
} else
ADOConnection::outp( "<p>ADONewConnection: Unable to load database driver '$db'</p>",false);
return false;
}
$cls = 'ADODB_'.$db;
if (!class_exists($cls)) {
adodb_backtrace();
return false;
}
$obj =& new $cls();
}
# constructor should not fail
if ($obj) {
if ($errorfn) $obj->raiseErrorFn = $errorfn;
if (isset($dsna)) {
foreach($opt as $k => $v) {
switch(strtolower($k)) {
case 'persist':
case 'persistent': $persist = $v; break;
case 'debug': $obj->debug = (integer) $v; break;
#ibase
case 'dialect': $obj->dialect = (integer) $v; break;
case 'charset': $obj->charset = $v; break;
case 'buffers': $obj->buffers = $v; break;
case 'fetchmode': $obj->SetFetchMode($v); break;
#ado
case 'charpage': $obj->charPage = $v; break;
#mysql, mysqli
case 'clientflags': $obj->clientFlags = $v; break;
#mysqli
case 'port': $obj->port = $v; break;
case 'socket': $obj->socket = $v; break;
}
}
if (empty($persist))
$ok = $obj->Connect($dsna['host'], $dsna['user'], $dsna['pass'], $dsna['path']);
else
$ok = $obj->PConnect($dsna['host'], $dsna['user'], $dsna['pass'], $dsna['path']);
if (!$ok) return false;
}
}
if (!isset($ADODB_LASTDB)) $ADODB_LASTDB = '';
if (empty($db)) $db = $ADODB_LASTDB;
if ($db != $ADODB_LASTDB) $db = ADOLoadCode($db);
if (!$db) {
if ($errorfn) {
// raise an error
$ignore = false;
$errorfn('ADONewConnection', 'ADONewConnection', -998,
"could not load the database driver for '$db",
$db,false,$ignore);
} else
ADOConnection::outp( "<p>ADONewConnection: Unable to load database driver '$db'</p>",false);
return false;
}
$cls = 'ADODB_'.$db;
if (!class_exists($cls)) {
adodb_backtrace();
return false;
}
$obj =& new $cls();
if ($errorfn) $obj->raiseErrorFn = $errorfn;
return $obj;
}
// $perf == true means called by NewPerfMonitor()
function _adodb_getdriver($provider,$drivername,$perf=false)
{
@ -3559,56 +3631,6 @@
}
/**
* Save a file $filename and its $contents (normally for caching) with file locking
*/
function adodb_write_file($filename, $contents,$debug=false)
{
# http://www.php.net/bugs.php?id=9203 Bug that flock fails on Windows
# So to simulate locking, we assume that rename is an atomic operation.
# First we delete $filename, then we create a $tempfile write to it and
# rename to the desired $filename. If the rename works, then we successfully
# modified the file exclusively.
# What a stupid need - having to simulate locking.
# Risks:
# 1. $tempfile name is not unique -- very very low
# 2. unlink($filename) fails -- ok, rename will fail
# 3. adodb reads stale file because unlink fails -- ok, $rs timeout occurs
# 4. another process creates $filename between unlink() and rename() -- ok, rename() fails and cache updated
if (strncmp(PHP_OS,'WIN',3) === 0) {
// skip the decimal place
$mtime = substr(str_replace(' ','_',microtime()),2);
// getmypid() actually returns 0 on Win98 - never mind!
$tmpname = $filename.uniqid($mtime).getmypid();
if (!($fd = fopen($tmpname,'a'))) return false;
$ok = ftruncate($fd,0);
if (!fwrite($fd,$contents)) $ok = false;
fclose($fd);
chmod($tmpname,0644);
// the tricky moment
@unlink($filename);
if (!@rename($tmpname,$filename)) {
unlink($tmpname);
$ok = false;
}
if (!$ok) {
if ($debug) ADOConnection::outp( " Rename $tmpname ".($ok? 'ok' : 'failed'));
}
return $ok;
}
if (!($fd = fopen($filename, 'a'))) return false;
if (flock($fd, LOCK_EX) && ftruncate($fd, 0)) {
$ok = fwrite( $fd, $contents );
fclose($fd);
chmod($filename,0644);
}else {
fclose($fd);
if ($debug)ADOConnection::outp( " Failed acquiring lock for $filename<br>\n");
$ok = false;
}
return $ok;
}
/*
Perform a print_r, with pre tags for better formatting.
@ -3629,54 +3651,9 @@
*/
function adodb_backtrace($printOrArr=true,$levels=9999)
{
$s = '';
if (PHPVERSION() < 4.3) return;
$html = (isset($_SERVER['HTTP_USER_AGENT']));
$fmt = ($html) ? "</font><font color=#808080 size=-1> %% line %4d, file: <a href=\"file:/%s\">%s</a></font>" : "%% line %4d, file: %s";
$MAXSTRLEN = 64;
$s = ($html) ? '<pre align=left>' : '';
if (is_array($printOrArr)) $traceArr = $printOrArr;
else $traceArr = debug_backtrace();
array_shift($traceArr);
$tabs = sizeof($traceArr)-1;
foreach ($traceArr as $arr) {
$levels -= 1;
if ($levels < 0) break;
$args = array();
for ($i=0; $i < $tabs; $i++) $s .= ($html) ? ' &nbsp; ' : "\t";
$tabs -= 1;
if ($html) $s .= '<font face="Courier New,Courier">';
if (isset($arr['class'])) $s .= $arr['class'].'.';
if (isset($arr['args']))
foreach($arr['args'] as $v) {
if (is_null($v)) $args[] = 'null';
else if (is_array($v)) $args[] = 'Array['.sizeof($v).']';
else if (is_object($v)) $args[] = 'Object:'.get_class($v);
else if (is_bool($v)) $args[] = $v ? 'true' : 'false';
else {
$v = (string) @$v;
$str = htmlspecialchars(substr($v,0,$MAXSTRLEN));
if (strlen($v) > $MAXSTRLEN) $str .= '...';
$args[] = $str;
}
}
$s .= $arr['function'].'('.implode(', ',$args).')';
$s .= @sprintf($fmt, $arr['line'],$arr['file'],basename($arr['file']));
$s .= "\n";
}
if ($html) $s .= '</pre>';
if ($printOrArr) print $s;
return $s;
global $ADODB_INCLUDED_LIB;
if (empty($ADODB_INCLUDED_LIB)) include_once(ADODB_DIR.'/adodb-lib.inc.php');
return _adodb_backtrace($printOrArr,$levels);
}
} // defined