[Linpha-cvs] SF.net SVN: linpha: [4855] trunk/linpha2/lib
Status: Inactive
Brought to you by:
bzrudi
From: <fan...@us...> - 2008-02-07 20:22:40
|
Revision: 4855 http://linpha.svn.sourceforge.net/linpha/?rev=4855&view=rev Author: fangehrn Date: 2008-02-07 12:22:33 -0800 (Thu, 07 Feb 2008) Log Message: ----------- updated to adodb503 Modified Paths: -------------- trunk/linpha2/lib/adodb/adodb-active-record.inc.php trunk/linpha2/lib/adodb/adodb-csvlib.inc.php trunk/linpha2/lib/adodb/adodb-datadict.inc.php trunk/linpha2/lib/adodb/adodb-error.inc.php trunk/linpha2/lib/adodb/adodb-errorhandler.inc.php trunk/linpha2/lib/adodb/adodb-errorpear.inc.php trunk/linpha2/lib/adodb/adodb-exceptions.inc.php trunk/linpha2/lib/adodb/adodb-iterator.inc.php trunk/linpha2/lib/adodb/adodb-lib.inc.php trunk/linpha2/lib/adodb/adodb-pager.inc.php trunk/linpha2/lib/adodb/adodb-pear.inc.php trunk/linpha2/lib/adodb/adodb-perf.inc.php trunk/linpha2/lib/adodb/adodb-php4.inc.php trunk/linpha2/lib/adodb/adodb-time.inc.php trunk/linpha2/lib/adodb/adodb-xmlschema.inc.php trunk/linpha2/lib/adodb/adodb-xmlschema03.inc.php trunk/linpha2/lib/adodb/adodb.inc.php trunk/linpha2/lib/adodb/drivers/adodb-access.inc.php trunk/linpha2/lib/adodb/drivers/adodb-ado.inc.php trunk/linpha2/lib/adodb/drivers/adodb-ado5.inc.php trunk/linpha2/lib/adodb/drivers/adodb-ado_access.inc.php trunk/linpha2/lib/adodb/drivers/adodb-ado_mssql.inc.php trunk/linpha2/lib/adodb/drivers/adodb-borland_ibase.inc.php trunk/linpha2/lib/adodb/drivers/adodb-csv.inc.php trunk/linpha2/lib/adodb/drivers/adodb-db2.inc.php trunk/linpha2/lib/adodb/drivers/adodb-fbsql.inc.php trunk/linpha2/lib/adodb/drivers/adodb-firebird.inc.php trunk/linpha2/lib/adodb/drivers/adodb-ibase.inc.php trunk/linpha2/lib/adodb/drivers/adodb-informix.inc.php trunk/linpha2/lib/adodb/drivers/adodb-informix72.inc.php trunk/linpha2/lib/adodb/drivers/adodb-ldap.inc.php trunk/linpha2/lib/adodb/drivers/adodb-mssql.inc.php trunk/linpha2/lib/adodb/drivers/adodb-mssqlpo.inc.php trunk/linpha2/lib/adodb/drivers/adodb-mysql.inc.php trunk/linpha2/lib/adodb/drivers/adodb-mysqli.inc.php trunk/linpha2/lib/adodb/drivers/adodb-mysqlt.inc.php trunk/linpha2/lib/adodb/drivers/adodb-netezza.inc.php trunk/linpha2/lib/adodb/drivers/adodb-oci8.inc.php trunk/linpha2/lib/adodb/drivers/adodb-oci805.inc.php trunk/linpha2/lib/adodb/drivers/adodb-oci8po.inc.php trunk/linpha2/lib/adodb/drivers/adodb-odbc.inc.php trunk/linpha2/lib/adodb/drivers/adodb-odbc_db2.inc.php trunk/linpha2/lib/adodb/drivers/adodb-odbc_mssql.inc.php trunk/linpha2/lib/adodb/drivers/adodb-odbc_oracle.inc.php trunk/linpha2/lib/adodb/drivers/adodb-odbtp.inc.php trunk/linpha2/lib/adodb/drivers/adodb-odbtp_unicode.inc.php trunk/linpha2/lib/adodb/drivers/adodb-oracle.inc.php trunk/linpha2/lib/adodb/drivers/adodb-pdo.inc.php trunk/linpha2/lib/adodb/drivers/adodb-pdo_mssql.inc.php trunk/linpha2/lib/adodb/drivers/adodb-pdo_mysql.inc.php trunk/linpha2/lib/adodb/drivers/adodb-pdo_oci.inc.php trunk/linpha2/lib/adodb/drivers/adodb-pdo_pgsql.inc.php trunk/linpha2/lib/adodb/drivers/adodb-postgres.inc.php trunk/linpha2/lib/adodb/drivers/adodb-postgres64.inc.php trunk/linpha2/lib/adodb/drivers/adodb-postgres7.inc.php trunk/linpha2/lib/adodb/drivers/adodb-postgres8.inc.php trunk/linpha2/lib/adodb/drivers/adodb-proxy.inc.php trunk/linpha2/lib/adodb/drivers/adodb-sapdb.inc.php trunk/linpha2/lib/adodb/drivers/adodb-sqlanywhere.inc.php trunk/linpha2/lib/adodb/drivers/adodb-sqlite.inc.php trunk/linpha2/lib/adodb/drivers/adodb-sqlitepo.inc.php trunk/linpha2/lib/adodb/drivers/adodb-sybase.inc.php trunk/linpha2/lib/adodb/drivers/adodb-sybase_ase.inc.php trunk/linpha2/lib/adodb/drivers/adodb-vfp.inc.php trunk/linpha2/lib/adodb/lang/adodb-ar.inc.php trunk/linpha2/lib/adodb/lang/adodb-bg.inc.php trunk/linpha2/lib/adodb/lang/adodb-bgutf8.inc.php trunk/linpha2/lib/adodb/lang/adodb-ca.inc.php trunk/linpha2/lib/adodb/lang/adodb-en.inc.php trunk/linpha2/lib/adodb/lang/adodb-pl.inc.php trunk/linpha2/lib/adodb/lang/adodb-pt-br.inc.php trunk/linpha2/lib/adodb/lang/adodb-ro.inc.php trunk/linpha2/lib/adodb/lang/adodb-uk1251.inc.php trunk/linpha2/lib/adodb/pivottable.inc.php trunk/linpha2/lib/adodb/rsfilter.inc.php trunk/linpha2/lib/adodb/server.php trunk/linpha2/lib/adodb/toexport.inc.php trunk/linpha2/lib/adodb/tohtml.inc.php trunk/linpha2/lib/classes/adodb-errorhandler.inc.php trunk/linpha2/lib/classes/linpha.sql.class.php Added Paths: ----------- trunk/linpha2/lib/adodb/adodb-memcache.lib.inc.php trunk/linpha2/lib/adodb/drivers/adodb-mssql_n.inc.php trunk/linpha2/lib/adodb/drivers/adodb-mysqlpo.inc.php trunk/linpha2/lib/adodb/lang/adodb_th.inc.php trunk/linpha2/lib/classes/adodb-exceptions.inc.php Removed Paths: ------------- trunk/linpha2/lib/adodb/adodb-time.zip Modified: trunk/linpha2/lib/adodb/adodb-active-record.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-active-record.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-active-record.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,7 +1,7 @@ <?php /* -@version V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. +@version V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. Latest version is available at http://adodb.sourceforge.net Released under both BSD license and Lesser GPL library license. @@ -10,7 +10,7 @@ Active Record implementation. Superset of Zend Framework's. - Version 0.04 + Version 0.08 See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord for info on Ruby on Rails Active Record implementation @@ -18,11 +18,14 @@ global $_ADODB_ACTIVE_DBS; global $ADODB_ACTIVE_CACHESECS; // set to true to enable caching of metadata such as field info +global $ACTIVE_RECORD_SAFETY; // set to false to disable safety checks +global $ADODB_ACTIVE_DEFVALS; // use default values of table definition when creating new active record. // array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat $_ADODB_ACTIVE_DBS = array(); +$ACTIVE_RECORD_SAFETY = true; +$ADODB_ACTIVE_DEFVALS = false; - class ADODB_Active_DB { var $db; // ADOConnection var $tables; // assoc array of ADODB_Active_Table objects, indexed by tablename @@ -41,11 +44,16 @@ global $_ADODB_ACTIVE_DBS; foreach($_ADODB_ACTIVE_DBS as $k => $d) { - if ($d->db == $db) return $k; + if (PHP_VERSION >= 5) { + if ($d->db === $db) return $k; + } else { + if ($d->db->_connectionID === $db->_connectionID && $db->database == $d->db->database) + return $k; + } } $obj = new ADODB_Active_DB(); - $obj->db =& $db; + $obj->db = $db; $obj->tables = array(); $_ADODB_ACTIVE_DBS[] = $obj; @@ -63,16 +71,24 @@ var $_lasterr = false; // last error message var $_original = false; // the original values loaded or inserted, refreshed on update + static function UseDefaultValues($bool=null) + { + global $ADODB_ACTIVE_DEFVALS; + if (isset($bool)) $ADODB_ACTIVE_DEFVALS = $bool; + return $ADODB_ACTIVE_DEFVALS; + } + // should be static - function SetDatabaseAdapter(&$db) + static function SetDatabaseAdapter(&$db) { return ADODB_SetDatabaseAdapter($db); } - // php4 constructor - function ADODB_Active_Record($table = false, $pkeyarr=false, $db=false) + + public function __set($name, $value) { - ADODB_Active_Record::__construct($table,$pkeyarr,$db); + $name = str_replace(' ', '_', $name); + $this->$name = $value; } // php5 constructor @@ -102,6 +118,12 @@ $this->UpdateActiveTable($pkeyarr); } + function __wakeup() + { + $class = get_class($this); + new $class; + } + function _pluralize($table) { $ut = strtoupper($table); @@ -129,20 +151,26 @@ function UpdateActiveTable($pkeys=false,$forceUpdate=false) { global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS; - - $activedb =& $_ADODB_ACTIVE_DBS[$this->_dbat]; + global $ADODB_ACTIVE_DEFVALS; + $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; + $table = $this->_table; $tables = $activedb->tables; $tableat = $this->_tableat; if (!$forceUpdate && !empty($tables[$tableat])) { - $tobj =& $tables[$tableat]; - foreach($tobj->flds as $name => $fld) + + $tobj = $tables[$tableat]; + foreach($tobj->flds as $name => $fld) { + if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) + $this->$name = $fld->default_value; + else $this->$name = null; + } return; } - $db =& $activedb->db; + $db = $activedb->db; $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_'. $table . '.cache'; if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) { $fp = fopen($fname,'r'); @@ -191,7 +219,10 @@ case 0: foreach($cols as $name => $fldobj) { $name = strtolower($name); - $this->$name = null; + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $this->$name = null; $attr[$name] = $fldobj; } foreach($pkeys as $k => $name) { @@ -202,7 +233,11 @@ case 1: foreach($cols as $name => $fldobj) { $name = strtoupper($name); - $this->$name = null; + + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $this->$name = null; $attr[$name] = $fldobj; } @@ -212,12 +247,16 @@ break; default: foreach($cols as $name => $fldobj) { - $name = ($name); - $this->$name = null; + $name = ($fldobj->name); + + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) + $this->$name = $fldobj->default_value; + else + $this->$name = null; $attr[$name] = $fldobj; } foreach($pkeys as $k => $name) { - $keys[$name] = ($name); + $keys[$name] = $cols[$name]->name; } break; } @@ -250,7 +289,7 @@ if ($this->_dbat < 0) $db = false; else { $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; - $db =& $activedb->db; + $db = $activedb->db; } if (function_exists('adodb_throw')) { @@ -274,8 +313,17 @@ return $this->_lasterr; } + function ErrorNo() + { + if ($this->_dbat < 0) return -9999; // no database connection... + $db = $this->DB(); + + return (int) $db->ErrorNo(); + } + + // retrieve ADOConnection from _ADODB_Active_DBs - function &DB() + function DB() { global $_ADODB_ACTIVE_DBS; @@ -285,24 +333,26 @@ return $false; } $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; - $db =& $activedb->db; + $db = $activedb->db; return $db; } // retrieve ADODB_Active_Table - function &TableInfo() + function TableInfo() { global $_ADODB_ACTIVE_DBS; $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; - $table =& $activedb->tables[$this->_tableat]; + $table = $activedb->tables[$this->_tableat]; return $table; } // set a numeric array (using natural table field ordering) as object properties function Set(&$row) { - $db =& $this->DB(); + global $ACTIVE_RECORD_SAFETY; + + $db = $this->DB(); if (!$row) { $this->_saved = false; @@ -311,18 +361,34 @@ $this->_saved = true; - $table =& $this->TableInfo(); - if (sizeof($table->flds) != sizeof($row)) { + $table = $this->TableInfo(); + if ($ACTIVE_RECORD_SAFETY && sizeof($table->flds) != sizeof($row)) { + # <AP> + $bad_size = TRUE; + if (sizeof($row) == 2 * sizeof($table->flds)) { + // Only keep string keys + $keys = array_filter(array_keys($row), 'is_string'); + if (sizeof($keys) == sizeof($table->flds)) + $bad_size = FALSE; + } + if ($bad_size) { $this->Error("Table structure of $this->_table has changed","Load"); return false; } - - $cnt = 0; + # </AP> + } + else + $keys = array_keys($row); + # <AP> + reset($keys); + $this->_original = array(); foreach($table->flds as $name=>$fld) { - $this->$name = $row[$cnt]; - $cnt += 1; + $value = $row[current($keys)]; + $this->$name = $value; + $this->_original[] = $value; + next($keys); } - $this->_original = $row; + # </AP> return true; } @@ -383,7 +449,7 @@ function Load($where,$bindarr=false) { - $db =& $this->DB(); if (!$db) return false; + $db = $this->DB(); if (!$db) return false; $this->_where = $where; $save = $db->SetFetchMode(ADODB_FETCH_NUM); @@ -405,9 +471,9 @@ // false on error function Insert() { - $db =& $this->DB(); if (!$db) return false; + $db = $this->DB(); if (!$db) return false; $cnt = 0; - $table =& $this->TableInfo(); + $table = $this->TableInfo(); $valarr = array(); $names = array(); @@ -455,8 +521,8 @@ function Delete() { - $db =& $this->DB(); if (!$db) return false; - $table =& $this->TableInfo(); + $db = $this->DB(); if (!$db) return false; + $table = $this->TableInfo(); $where = $this->GenWhere($db,$table); $sql = 'DELETE FROM '.$this->_table.' WHERE '.$where; @@ -466,10 +532,10 @@ } // returns an array of active record objects - function &Find($whereOrderBy,$bindarr=false,$pkeysArr=false) + function Find($whereOrderBy,$bindarr=false,$pkeysArr=false) { - $db =& $this->DB(); if (!$db || empty($this->_table)) return false; - $arr =& $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr); + $db = $this->DB(); if (!$db || empty($this->_table)) return false; + $arr = $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr); return $arr; } @@ -478,8 +544,8 @@ { global $ADODB_ASSOC_CASE; - $db =& $this->DB(); if (!$db) return false; - $table =& $this->TableInfo(); + $db = $this->DB(); if (!$db) return false; + $table = $this->TableInfo(); $pkey = $table->keys; @@ -509,7 +575,7 @@ if ($ADODB_ASSOC_CASE == 0) foreach($pkey as $k => $v) $pkey[$k] = strtolower($v); - elseif ($ADODB_ASSOC_CASE == 0) + elseif ($ADODB_ASSOC_CASE == 1) foreach($pkey as $k => $v) $pkey[$k] = strtoupper($v); @@ -530,7 +596,7 @@ } } - $this->_original =& $valarr; + $this->_original = $valarr; } return $ok; } @@ -538,8 +604,8 @@ // returns 0 on error, 1 on update, -1 if no change in data (no update) function Update() { - $db =& $this->DB(); if (!$db) return false; - $table =& $this->TableInfo(); + $db = $this->DB(); if (!$db) return false; + $table = $this->TableInfo(); $where = $this->GenWhere($db, $table); @@ -584,7 +650,7 @@ $sql = 'UPDATE '.$this->_table." SET ".implode(",",$pairs)." WHERE ".$where; $ok = $db->Execute($sql,$valarr); if ($ok) { - $this->_original =& $neworig; + $this->_original = $neworig; return 1; } return 0; @@ -592,7 +658,7 @@ function GetAttributeNames() { - $table =& $this->TableInfo(); + $table = $this->TableInfo(); if (!$table) return false; return array_keys($table->flds); } Modified: trunk/linpha2/lib/adodb/adodb-csvlib.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-csvlib.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-csvlib.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -8,7 +8,7 @@ /* - V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. See License.txt. @@ -54,7 +54,7 @@ $line = "====1,$tt,$sql\n"; if ($rs->databaseType == 'array') { - $rows =& $rs->_array; + $rows = $rs->_array; } else { $rows = array(); while (!$rs->EOF) { @@ -64,7 +64,7 @@ } for($i=0; $i < $max; $i++) { - $o =& $rs->FetchField($i); + $o = $rs->FetchField($i); $flds[] = $o; } @@ -90,7 +90,7 @@ * error occurred in sql INSERT/UPDATE/DELETE, * empty recordset is returned */ - function &csv2rs($url,&$err,$timeout=0, $rsclass='ADORecordSet_array') + function csv2rs($url,&$err,$timeout=0, $rsclass='ADORecordSet_array') { $false = false; $err = false; @@ -261,6 +261,7 @@ /** * Save a file $filename and its $contents (normally for caching) with file locking + * Returns true if ok, false if fopen/fwrite error, 0 if rename error (eg. file is locked) */ function adodb_write_file($filename, $contents,$debug=false) { @@ -280,25 +281,29 @@ $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; + if (!($fd = @fopen($tmpname,'w'))) return false; + if (fwrite($fd,$contents)) $ok = true; + else $ok = false; fclose($fd); - chmod($tmpname,0644); - // the tricky moment - @unlink($filename); - if (!@rename($tmpname,$filename)) { - unlink($tmpname); - $ok = false; + + if ($ok) { + chmod($tmpname,0644); + // the tricky moment + @unlink($filename); + if (!@rename($tmpname,$filename)) { + unlink($tmpname); + $ok = 0; + } + if (!$ok) { + if ($debug) ADOConnection::outp( " Rename $tmpname ".($ok? 'ok' : 'failed')); + } } - 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 ); + if (fwrite( $fd, $contents )) $ok = true; + else $ok = false; fclose($fd); chmod($filename,0644); }else { Modified: trunk/linpha2/lib/adodb/adodb-datadict.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-datadict.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-datadict.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,7 +1,7 @@ <?php /** - V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -216,8 +216,115 @@ } function MetaType($t,$len=-1,$fieldobj=false) - { - return ADORecordSet::MetaType($t,$len,$fieldobj); + { + static $typeMap = array( + 'VARCHAR' => 'C', + 'VARCHAR2' => 'C', + 'CHAR' => 'C', + 'C' => 'C', + 'STRING' => 'C', + 'NCHAR' => 'C', + 'NVARCHAR' => 'C', + 'VARYING' => 'C', + 'BPCHAR' => 'C', + 'CHARACTER' => 'C', + 'INTERVAL' => 'C', # Postgres + 'MACADDR' => 'C', # postgres + ## + 'LONGCHAR' => 'X', + 'TEXT' => 'X', + 'NTEXT' => 'X', + 'M' => 'X', + 'X' => 'X', + 'CLOB' => 'X', + 'NCLOB' => 'X', + 'LVARCHAR' => 'X', + ## + 'BLOB' => 'B', + 'IMAGE' => 'B', + 'BINARY' => 'B', + 'VARBINARY' => 'B', + 'LONGBINARY' => 'B', + 'B' => 'B', + ## + 'YEAR' => 'D', // mysql + 'DATE' => 'D', + 'D' => 'D', + ## + 'UNIQUEIDENTIFIER' => 'C', # MS SQL Server + ## + 'TIME' => 'T', + 'TIMESTAMP' => 'T', + 'DATETIME' => 'T', + 'TIMESTAMPTZ' => 'T', + 'T' => 'T', + 'TIMESTAMP WITHOUT TIME ZONE' => 'T', // postgresql + ## + 'BOOL' => 'L', + 'BOOLEAN' => 'L', + 'BIT' => 'L', + 'L' => 'L', + ## + 'COUNTER' => 'R', + 'R' => 'R', + 'SERIAL' => 'R', // ifx + 'INT IDENTITY' => 'R', + ## + 'INT' => 'I', + 'INT2' => 'I', + 'INT4' => 'I', + 'INT8' => 'I', + 'INTEGER' => 'I', + 'INTEGER UNSIGNED' => 'I', + 'SHORT' => 'I', + 'TINYINT' => 'I', + 'SMALLINT' => 'I', + 'I' => 'I', + ## + 'LONG' => 'N', // interbase is numeric, oci8 is blob + 'BIGINT' => 'N', // this is bigger than PHP 32-bit integers + 'DECIMAL' => 'N', + 'DEC' => 'N', + 'REAL' => 'N', + 'DOUBLE' => 'N', + 'DOUBLE PRECISION' => 'N', + 'SMALLFLOAT' => 'N', + 'FLOAT' => 'N', + 'NUMBER' => 'N', + 'NUM' => 'N', + 'NUMERIC' => 'N', + 'MONEY' => 'N', + + ## informix 9.2 + 'SQLINT' => 'I', + 'SQLSERIAL' => 'I', + 'SQLSMINT' => 'I', + 'SQLSMFLOAT' => 'N', + 'SQLFLOAT' => 'N', + 'SQLMONEY' => 'N', + 'SQLDECIMAL' => 'N', + 'SQLDATE' => 'D', + 'SQLVCHAR' => 'C', + 'SQLCHAR' => 'C', + 'SQLDTIME' => 'T', + 'SQLINTERVAL' => 'N', + 'SQLBYTES' => 'B', + 'SQLTEXT' => 'X', + ## informix 10 + "SQLINT8" => 'I8', + "SQLSERIAL8" => 'I8', + "SQLNCHAR" => 'C', + "SQLNVCHAR" => 'C', + "SQLLVARCHAR" => 'X', + "SQLBOOL" => 'L' + ); + + if (!$this->connection->IsConnected()) { + $t = strtoupper($t); + if (isset($typeMap[$t])) return $typeMap[$t]; + return 'N'; + } + return $this->connection->MetaType($t,$len,$fieldobj); } function NameQuote($name = NULL,$allowBrackets=false) @@ -261,7 +368,7 @@ function ExecuteSQLArray($sql, $continueOnError = true) { $rez = 2; - $conn = &$this->connection; + $conn = $this->connection; $saved = $conn->debug; foreach($sql as $line) { @@ -277,7 +384,7 @@ return $rez; } - /* + /** Returns the actual type given a character code. C: varchar @@ -344,11 +451,19 @@ { $tabname = $this->TableName ($tabname); $sql = array(); - list($lines,$pkey) = $this->_GenFields($flds); + list($lines,$pkey,$idxs) = $this->_GenFields($flds); + // genfields can return FALSE at times + if ($lines == null) $lines = array(); $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' '; foreach($lines as $v) { $sql[] = $alter . $v; } + if (is_array($idxs)) { + foreach($idxs as $idx => $idxdef) { + $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); + $sql = array_merge($sql, $sql_idxs); + } + } return $sql; } @@ -367,11 +482,20 @@ { $tabname = $this->TableName ($tabname); $sql = array(); - list($lines,$pkey) = $this->_GenFields($flds); + list($lines,$pkey,$idxs) = $this->_GenFields($flds); + // genfields can return FALSE at times + if ($lines == null) $lines = array(); $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' '; foreach($lines as $v) { $sql[] = $alter . $v; } + if (is_array($idxs)) { + foreach($idxs as $idx => $idxdef) { + $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); + $sql = array_merge($sql, $sql_idxs); + } + + } return $sql; } @@ -389,7 +513,9 @@ { $tabname = $this->TableName ($tabname); if ($flds) { - list($lines,$pkey) = $this->_GenFields($flds); + list($lines,$pkey,$idxs) = $this->_GenFields($flds); + // genfields can return FALSE at times + if ($lines == null) $lines = array(); list(,$first) = each($lines); list(,$column_def) = split("[\t ]+",$first,2); } @@ -429,22 +555,36 @@ return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname))); } - /* + /** Generate the SQL to create table. Returns an array of sql strings. */ - function CreateTableSQL($tabname, $flds, $tableoptions=false) + function CreateTableSQL($tabname, $flds, $tableoptions=array()) { - if (!$tableoptions) $tableoptions = array(); + list($lines,$pkey,$idxs) = $this->_GenFields($flds, true); + // genfields can return FALSE at times + if ($lines == null) $lines = array(); - list($lines,$pkey) = $this->_GenFields($flds, true); - $taboptions = $this->_Options($tableoptions); $tabname = $this->TableName ($tabname); $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions); + // ggiunta - 2006/10/12 - KLUDGE: + // if we are on autoincrement, and table options includes REPLACE, the + // autoincrement sequence has already been dropped on table creation sql, so + // we avoid passing REPLACE to trigger creation code. This prevents + // creating sql that double-drops the sequence + if ($this->autoIncrement && isset($taboptions['REPLACE'])) + unset($taboptions['REPLACE']); $tsql = $this->_Triggers($tabname,$taboptions); foreach($tsql as $s) $sql[] = $s; + if (is_array($idxs)) { + foreach($idxs as $idx => $idxdef) { + $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); + $sql = array_merge($sql, $sql_idxs); + } + } + return $sql; } @@ -460,6 +600,9 @@ $f1 = array(); foreach($f0 as $token) { switch (strtoupper($token)) { + case 'INDEX': + $f1['INDEX'] = ''; + // fall through intentionally case 'CONSTRAINT': case 'DEFAULT': $hasparam = $token; @@ -471,6 +614,20 @@ break; } } + // 'index' token without a name means single column index: name it after column + if (array_key_exists('INDEX', $f1) && $f1['INDEX'] == '') { + $f1['INDEX'] = isset($f0['NAME']) ? $f0['NAME'] : $f0[0]; + // check if column name used to create an index name was quoted + if (($f1['INDEX'][0] == '"' || $f1['INDEX'][0] == "'" || $f1['INDEX'][0] == "`") && + ($f1['INDEX'][0] == substr($f1['INDEX'], -1))) { + $f1['INDEX'] = $f1['INDEX'][0].'idx_'.substr($f1['INDEX'], 1, -1).$f1['INDEX'][0]; + } + else + $f1['INDEX'] = 'idx_'.$f1['INDEX']; + } + // reset it, so we don't get next field 1st token as INDEX... + $hasparam = false; + $flds[] = $f1; } @@ -478,9 +635,10 @@ $this->autoIncrement = false; $lines = array(); $pkey = array(); + $idxs = array(); foreach($flds as $fld) { $fld = _array_change_key_case($fld); - + $fname = false; $fdefault = false; $fautoinc = false; @@ -494,6 +652,8 @@ $fconstraint = false; $fnotnull = false; $funsigned = false; + $findex = ''; + $funiqueindex = false; //----------------- // Parse attributes @@ -519,7 +679,8 @@ case 'AUTOINCREMENT': case 'AUTO': $fautoinc = true; $fnotnull = true; break; case 'KEY': - case 'PRIMARY': $fprimary = $v; $fnotnull = true; break; + // a primary key col can be non unique in itself (if key spans many cols...) + case 'PRIMARY': $fprimary = $v; $fnotnull = true; /*$funiqueindex = true;*/ break; case 'DEF': case 'DEFAULT': $fdefault = $v; break; case 'NOTNULL': $fnotnull = $v; break; @@ -527,6 +688,9 @@ case 'DEFDATE': $fdefdate = $v; break; case 'DEFTIMESTAMP': $fdefts = $v; break; case 'CONSTRAINT': $fconstraint = $v; break; + // let INDEX keyword create a 'very standard' index on column + case 'INDEX': $findex = $v; break; + case 'UNIQUE': $funiqueindex = true; break; } //switch } // foreach $fld @@ -556,6 +720,27 @@ // some databases do not allow blobs to have defaults if ($ty == 'X') $fdefault = false; + // build list of indexes + if ($findex != '') { + if (array_key_exists($findex, $idxs)) { + $idxs[$findex]['cols'][] = ($fname); + if (in_array('UNIQUE', $idxs[$findex]['opts']) != $funiqueindex) { + if ($this->debug) ADOConnection::outp("Index $findex defined once UNIQUE and once not"); + } + if ($funiqueindex && !in_array('UNIQUE', $idxs[$findex]['opts'])) + $idxs[$findex]['opts'][] = 'UNIQUE'; + } + else + { + $idxs[$findex] = array(); + $idxs[$findex]['cols'] = array($fname); + if ($funiqueindex) + $idxs[$findex]['opts'] = array('UNIQUE'); + else + $idxs[$findex]['opts'] = array(); + } + } + //-------------------- // CONSTRUCT FIELD SQL if ($fdefts) { @@ -570,24 +755,47 @@ } else { $fdefault = $this->connection->sysDate; } - } else if ($fdefault !== false && !$fnoquote) + } else if ($fdefault !== false && !$fnoquote) { if ($ty == 'C' or $ty == 'X' or - ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) + ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) { + + if (($ty == 'D' || $ty == 'T') && strtolower($fdefault) != 'null') { + // convert default date into database-aware code + if ($ty == 'T') + { + $fdefault = $this->connection->DBTimeStamp($fdefault); + } + else + { + $fdefault = $this->connection->DBDate($fdefault); + } + } + else if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ') $fdefault = trim($fdefault); else if (strtolower($fdefault) != 'null') $fdefault = $this->connection->qstr($fdefault); + } + } $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned); + // add index creation if ($widespacing) $fname = str_pad($fname,24); + + // check for field names appearing twice + if (array_key_exists($fid, $lines)) { + ADOConnection::outp("Field '$fname' defined twice"); + } + $lines[$fid] = $fname.' '.$ftype.$suffix; if ($fautoinc) $this->autoIncrement = true; } // foreach $flds - return array($lines,$pkey); + return array($lines,$pkey,$idxs); } - /* + + /** GENERATE THE SIZE PART OF THE DATATYPE $ftype is the actual type $ty is the type defined originally in the DDL @@ -680,7 +888,7 @@ return $sql; } - /* + /** GENERATE TRIGGERS IF NEEDED used when table has auto-incrementing field that is emulated using triggers */ @@ -689,7 +897,7 @@ return array(); } - /* + /** Sanitize options, so that array elements with no keys are promoted to keys */ function _Options($opts) @@ -703,7 +911,7 @@ return $newopts; } - /* + /** "Florian Buzin [ easywe ]" <florian.buzin#easywe.de> This function changes/adds new fields to your table. You don't @@ -760,7 +968,9 @@ // already exists, alter table instead - list($lines,$pkey) = $this->_GenFields($flds); + list($lines,$pkey,$idxs) = $this->_GenFields($flds); + // genfields can return FALSE at times + if ($lines == null) $lines = array(); $alter = 'ALTER TABLE ' . $this->TableName($tablename); $sql = array(); @@ -770,8 +980,13 @@ $flds = Lens_ParseArgs($v,','); // We are trying to change the size of the field, if not allowed, simply ignore the request. - if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) continue; - + // $flds[1] holds the type, $flds[2] holds the size -postnuke addition + if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4) + && (isset($flds[0][2]) && is_numeric($flds[0][2]))) { + if ($this->debug) ADOConnection::outp(sprintf("<h3>%s cannot be changed to %s currently</h3>", $flds[0][0], $flds[0][1])); + #echo "<h3>$this->alterCol cannot be changed to $flds currently</h3>"; + continue; + } $sql[] = $alter . $this->alterCol . ' ' . $v; } else { $sql[] = $alter . $this->addCol . ' ' . $v; Modified: trunk/linpha2/lib/adodb/adodb-error.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-error.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-error.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,6 +1,6 @@ <?php /** - * @version V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + * @version V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. * Released under both BSD license and Lesser GPL library license. * Whenever there is any discrepancy between the two licenses, * the BSD license will take precedence. @@ -92,14 +92,14 @@ { if (is_numeric($errormsg)) return (integer) $errormsg; static $error_regexps = array( - '/(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$/' => DB_ERROR_NOSUCHTABLE, - '/Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*/' => DB_ERROR_ALREADY_EXISTS, - '/divide by zero$/' => DB_ERROR_DIVZERO, - '/pg_atoi: error in .*: can\'t parse /' => DB_ERROR_INVALID_NUMBER, - '/ttribute [\"\'].*[\"\'] not found|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']/' => DB_ERROR_NOSUCHFIELD, - '/parser: parse error at or near \"/' => DB_ERROR_SYNTAX, - '/referential integrity violation/' => DB_ERROR_CONSTRAINT, - '/Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*|duplicate key violates unique constraint/' + '/(Table does not exist\.|Relation [\"\'].*[\"\'] does not exist|sequence does not exist|class ".+" not found)$/i' => DB_ERROR_NOSUCHTABLE, + '/Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*/i' => DB_ERROR_ALREADY_EXISTS, + '/divide by zero$/i' => DB_ERROR_DIVZERO, + '/pg_atoi: error in .*: can\'t parse /i' => DB_ERROR_INVALID_NUMBER, + '/ttribute [\"\'].*[\"\'] not found|Relation [\"\'].*[\"\'] does not have attribute [\"\'].*[\"\']/i' => DB_ERROR_NOSUCHFIELD, + '/parser: parse error at or near \"/i' => DB_ERROR_SYNTAX, + '/referential integrity violation/i' => DB_ERROR_CONSTRAINT, + '/Relation [\"\'].*[\"\'] already exists|Cannot insert a duplicate key into (a )?unique index.*|duplicate key violates unique constraint/i' => DB_ERROR_ALREADY_EXISTS ); reset($error_regexps); Modified: trunk/linpha2/lib/adodb/adodb-errorhandler.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-errorhandler.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-errorhandler.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,6 +1,6 @@ <?php /** - * @version V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + * @version V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. * Released under both BSD license and Lesser GPL library license. * Whenever there is any discrepancy between the two licenses, * the BSD license will take precedence. @@ -30,7 +30,6 @@ */ function ADODB_Error_Handler($dbms, $fn, $errno, $errmsg, $p1, $p2, &$thisConnection) { - echo 'hi'; if (error_reporting() == 0) return; // obey @ protocol switch($fn) { case 'EXECUTE': @@ -65,26 +64,16 @@ * and optionally, port number, of the socket receiving the debug information. * 3 message is appended to the file destination */ - /*if (defined('ADODB_ERROR_LOG_TYPE')) { + if (defined('ADODB_ERROR_LOG_TYPE')) { $t = date('Y-m-d H:i:s'); if (defined('ADODB_ERROR_LOG_DEST')) error_log("($t) $s", ADODB_ERROR_LOG_TYPE, ADODB_ERROR_LOG_DEST); else error_log("($t) $s", ADODB_ERROR_LOG_TYPE); - }*/ + } - /** - * if adodb errorhandler is used, the function $db->ErrorMsg() does not work - * anymore - * -> output the error to screen - * -> no need anymore for : - * if(!$create) { echo $GLOBALS['db']->ErrorMsg().'<br />'; } - */ - echo $s.'<br />'; - - linLog(LOG_TYPE_DB,LOG_ERR,'db',$s); //print "<p>$s</p>"; - //trigger_error($s,ADODB_ERROR_HANDLER_TYPE); + trigger_error($s,ADODB_ERROR_HANDLER_TYPE); } ?> Modified: trunk/linpha2/lib/adodb/adodb-errorpear.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-errorpear.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-errorpear.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,6 +1,6 @@ <?php /** - * @version V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + * @version V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. * Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -78,7 +78,7 @@ * Returns last PEAR_Error object. This error might be for an error that * occured several sql statements ago. */ -function &ADODB_PEAR_Error() +function ADODB_PEAR_Error() { global $ADODB_Last_PEAR_Error; Modified: trunk/linpha2/lib/adodb/adodb-exceptions.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-exceptions.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-exceptions.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,7 +1,7 @@ <?php /** - * @version V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + * @version V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. * Released under both BSD license and Lesser GPL library license. * Whenever there is any discrepancy between the two licenses, * the BSD license will take precedence. Modified: trunk/linpha2/lib/adodb/adodb-iterator.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-iterator.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-iterator.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,7 +1,7 @@ <?php /* - V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -18,68 +18,13 @@ Iterator code based on http://cvs.php.net/cvs.php/php-src/ext/spl/examples/cachingiterator.inc?login=2 + + + Moved to adodb.inc.php to improve performance. */ - class ADODB_Iterator implements Iterator { - private $rs; - function __construct($rs) - { - $this->rs = $rs; - } - function rewind() - { - $this->rs->MoveFirst(); - } - function valid() - { - return !$this->rs->EOF; - } - - function key() - { - return $this->rs->_currentRow; - } - - function current() - { - return $this->rs->fields; - } - - function next() - { - $this->rs->MoveNext(); - } - - function __call($func, $params) - { - return call_user_func_array(array($this->rs, $func), $params); - } - - - function hasMore() - { - return !$this->rs->EOF; - } - -} - - -class ADODB_BASE_RS implements IteratorAggregate { - function getIterator() { - return new ADODB_Iterator($this); - } - - /* this is experimental - i don't really know what to return... */ - function __toString() - { - include_once(ADODB_DIR.'/toexport.inc.php'); - return _adodb_export($this,',',',',false,true); - } -} - - ?> \ No newline at end of file Modified: trunk/linpha2/lib/adodb/adodb-lib.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-lib.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-lib.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,5 +1,8 @@ <?php + + + // security - hide paths if (!defined('ADODB_DIR')) die(); @@ -7,7 +10,7 @@ $ADODB_INCLUDED_LIB = 1; /* - @version V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim\@natsoft.com.my). All rights reserved. + @version V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim\@natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. See License.txt. @@ -16,7 +19,106 @@ Less commonly used functions are placed here to reduce size of adodb.inc.php. */ +function adodb_strip_order_by($sql) +{ + $rez = preg_match('/(\sORDER\s+BY\s[^)]*)/is',$sql,$arr); + if ($arr) + if (strpos($arr[0],'(') !== false) { + $at = strpos($sql,$arr[0]); + $cntin = 0; + for ($i=$at, $max=strlen($sql); $i < $max; $i++) { + $ch = $sql[$i]; + if ($ch == '(') { + $cntin += 1; + } elseif($ch == ')') { + $cntin -= 1; + if ($cntin < 0) { + break; + } + } + } + $sql = substr($sql,0,$at).substr($sql,$i); + } else + $sql = str_replace($arr[0], '', $sql); + return $sql; + } +if (false) { + $sql = 'select * from (select a from b order by a(b),b(c) desc)'; + $sql = '(select * from abc order by 1)'; + die(adodb_strip_order_by($sql)); +} + +function adodb_probetypes(&$array,&$types,$probe=8) +{ +// probe and guess the type + $types = array(); + if ($probe > sizeof($array)) $max = sizeof($array); + else $max = $probe; + + + for ($j=0;$j < $max; $j++) { + $row = $array[$j]; + if (!$row) break; + $i = -1; + foreach($row as $v) { + $i += 1; + + if (isset($types[$i]) && $types[$i]=='C') continue; + + //print " ($i ".$types[$i]. "$v) "; + $v = trim($v); + + if (!preg_match('/^[+-]{0,1}[0-9\.]+$/',$v)) { + $types[$i] = 'C'; // once C, always C + + continue; + } + if ($j == 0) { + // If empty string, we presume is character + // test for integer for 1st row only + // after that it is up to testing other rows to prove + // that it is not an integer + if (strlen($v) == 0) $types[$i] = 'C'; + if (strpos($v,'.') !== false) $types[$i] = 'N'; + else $types[$i] = 'I'; + continue; + } + + if (strpos($v,'.') !== false) $types[$i] = 'N'; + + } + } + +} + +function adodb_transpose(&$arr, &$newarr, &$hdr, &$fobjs) +{ + $oldX = sizeof(reset($arr)); + $oldY = sizeof($arr); + + if ($hdr) { + $startx = 1; + $hdr = array('Fields'); + for ($y = 0; $y < $oldY; $y++) { + $hdr[] = $arr[$y][0]; + } + } else + $startx = 0; + + for ($x = $startx; $x < $oldX; $x++) { + if ($fobjs) { + $o = $fobjs[$x]; + $newarr[] = array($o->name); + } else + $newarr[] = array(); + + for ($y = 0; $y < $oldY; $y++) { + $newarr[$x-$startx][] = $arr[$y][$x]; + } + } +} + // Force key to upper. // See also http://www.php.net/manual/en/function.array-change-key-case.php function _array_change_key_case($an_array) @@ -42,7 +144,10 @@ $keyCol = array($keyCol); } foreach($fieldArray as $k => $v) { - if ($autoQuote && !is_numeric($v) and strncmp($v,"'",1) !== 0 and strcasecmp($v,'null')!=0) { + if ($v === null) { + $v = 'NULL'; + $fieldArray[$k] = $v; + } else if ($autoQuote && !is_numeric($v) /*and strncmp($v,"'",1) !== 0 -- sql injection risk*/ and strcasecmp($v,$zthis->null2null)!=0) { $v = $zthis->qstr($v); $fieldArray[$k] = $v; } @@ -297,15 +402,15 @@ { $qryRecs = 0; - if (preg_match("/^\s*SELECT\s+DISTINCT/is", $sql) || + if (!empty($zthis->_nestedSQL) || preg_match("/^\s*SELECT\s+DISTINCT/is", $sql) || preg_match('/\s+GROUP\s+BY\s+/is',$sql) || preg_match('/\s+UNION\s+/is',$sql)) { + + $rewritesql = adodb_strip_order_by($sql); + // ok, has SELECT DISTINCT or GROUP BY so see if we can use a table alias // but this is only supported by oracle and postgresql... if ($zthis->dataProvider == 'oci8') { - - $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is','',$sql); - // Allow Oracle hints to be used for query optimization, Chris Wrye if (preg_match('#/\\*+.*?\\*\\/#', $sql, $hint)) { $rewritesql = "SELECT ".$hint[0]." COUNT(*) FROM (".$rewritesql.")"; @@ -313,33 +418,22 @@ $rewritesql = "SELECT COUNT(*) FROM (".$rewritesql.")"; } else if (strncmp($zthis->databaseType,'postgres',8) == 0) { - - $info = $zthis->ServerInfo(); - if (substr($info['version'],0,3) >= 7.1) { // good till version 999 - $rewritesql = preg_replace('/(\sORDER\s+BY\s[^)]*)/is','',$sql); - $rewritesql = "SELECT COUNT(*) FROM ($rewritesql) _ADODB_ALIAS_"; - } + $rewritesql = "SELECT COUNT(*) FROM ($rewritesql) _ADODB_ALIAS_"; + } else { + $rewritesql = "SELECT COUNT(*) FROM ($rewritesql)"; } } else { // now replace SELECT ... FROM with SELECT COUNT(*) FROM $rewritesql = preg_replace( '/^\s*SELECT\s.*\s+FROM\s/Uis','SELECT COUNT(*) FROM ',$sql); - - - // fix by alexander zhukov, alex#unipack.ru, because count(*) and 'order by' fails // with mssql, access and postgresql. Also a good speedup optimization - skips sorting! // also see http://phplens.com/lens/lensforum/msgs.php?id=12752 - if (preg_match('/\sORDER\s+BY\s*\(/i',$rewritesql)) - $rewritesql = preg_replace('/(\sORDER\s+BY\s.*)/is','',$rewritesql); - else - $rewritesql = preg_replace('/(\sORDER\s+BY\s[^)]*)/is','',$rewritesql); + $rewritesql = adodb_strip_order_by($rewritesql); } - - if (isset($rewritesql) && $rewritesql != $sql) { - if (preg_match('/\sLIMIT\s+[0-9]+/i',$sql,$limitarr)) $rewritesql .= $limitarr[1]; + if (preg_match('/\sLIMIT\s+[0-9]+/i',$sql,$limitarr)) $rewritesql .= $limitarr[0]; if ($secs2cache) { // we only use half the time of secs2cache because the count can quickly @@ -361,7 +455,7 @@ if (preg_match('/\sLIMIT\s+[0-9]+/i',$sql,$limitarr)) $rewritesql .= $limitarr[0]; - $rstest = &$zthis->Execute($rewritesql,$inputarr); + $rstest = $zthis->Execute($rewritesql,$inputarr); if (!$rstest) $rstest = $zthis->Execute($sql,$inputarr); if ($rstest) { @@ -383,7 +477,6 @@ $rstest->Close(); if ($qryRecs == -1) return 0; } - return $qryRecs; } @@ -396,7 +489,7 @@ data will get out of synch. use CachePageExecute() only with tables that rarely change. */ -function &_adodb_pageexecute_all_rows(&$zthis, $sql, $nrows, $page, +function _adodb_pageexecute_all_rows(&$zthis, $sql, $nrows, $page, $inputarr=false, $secs2cache=0) { $atfirstpage = false; @@ -432,9 +525,9 @@ // We get the data we want $offset = $nrows * ($page-1); if ($secs2cache > 0) - $rsreturn = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr); + $rsreturn = $zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr); else - $rsreturn = &$zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache); + $rsreturn = $zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache); // Before returning the RecordSet, we set the pagination properties we need @@ -450,7 +543,7 @@ } // Iv\xE1n Oliva version -function &_adodb_pageexecute_no_last_page(&$zthis, $sql, $nrows, $page, $inputarr=false, $secs2cache=0) +function _adodb_pageexecute_no_last_page(&$zthis, $sql, $nrows, $page, $inputarr=false, $secs2cache=0) { $atfirstpage = false; @@ -466,16 +559,16 @@ // the last page number. $pagecounter = $page + 1; $pagecounteroffset = ($pagecounter * $nrows) - $nrows; - if ($secs2cache>0) $rstest = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr); - else $rstest = &$zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $secs2cache); + if ($secs2cache>0) $rstest = $zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr); + else $rstest = $zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $secs2cache); if ($rstest) { while ($rstest && $rstest->EOF && $pagecounter>0) { $atlastpage = true; $pagecounter--; $pagecounteroffset = $nrows * ($pagecounter - 1); $rstest->Close(); - if ($secs2cache>0) $rstest = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr); - else $rstest = &$zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $secs2cache); + if ($secs2cache>0) $rstest = $zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $pagecounteroffset, $inputarr); + else $rstest = $zthis->SelectLimit($sql, $nrows, $pagecounteroffset, $inputarr, $secs2cache); } if ($rstest) $rstest->Close(); } @@ -487,8 +580,8 @@ // We get the data we want $offset = $nrows * ($page-1); - if ($secs2cache > 0) $rsreturn = &$zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr); - else $rsreturn = &$zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache); + if ($secs2cache > 0) $rsreturn = $zthis->CacheSelectLimit($secs2cache, $sql, $nrows, $offset, $inputarr); + else $rsreturn = $zthis->SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache); // Before returning the RecordSet, we set the pagination properties we need if ($rsreturn) { @@ -502,6 +595,8 @@ function _adodb_getupdatesql(&$zthis,&$rs, $arrFields,$forceUpdate=false,$magicq=false,$force=2) { + global $ADODB_QUOTE_FIELDNAMES; + if (!$rs) { printf(ADODB_BAD_RS,'GetUpdateSQL'); return false; @@ -548,7 +643,7 @@ $type = 'C'; } - if (strpos($upperfname,' ') !== false) + if ((strpos($upperfname,' ') !== false) || ($ADODB_QUOTE_FIELDNAMES)) $fnameq = $zthis->nameQuote.$upperfname.$zthis->nameQuote; else $fnameq = $upperfname; @@ -558,7 +653,7 @@ //********************************************************// if (is_null($arrFields[$upperfname]) || (empty($arrFields[$upperfname]) && strlen($arrFields[$upperfname]) == 0) - || $arrFields[$upperfname] === 'null' + || $arrFields[$upperfname] === $zthis->null2null ) { switch ($force) { @@ -580,7 +675,7 @@ default: case 3: //Set the value that was given in array, so you can give both null and empty values - if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === 'null') { + if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === $zthis->null2null) { $setFields .= $field->name . " = null, "; } else { $setFields .= _adodb_column_sql($zthis, 'U', $type, $upperfname, $fnameq,$arrFields, $magicq); @@ -662,6 +757,7 @@ static $cacheRS = false; static $cacheSig = 0; static $cacheCols; + global $ADODB_QUOTE_FIELDNAMES; $tableName = ''; $values = ''; @@ -680,10 +776,10 @@ //php can't do a $rsclass::MetaType() $rsclass = $zthis->rsPrefix.$zthis->databaseType; $recordSet = new $rsclass(-1,$zthis->fetchMode); - $recordSet->connection = &$zthis; + $recordSet->connection = $zthis; if (is_string($cacheRS) && $cacheRS == $rs) { - $columns =& $cacheCols; + $columns = $cacheCols; } else { $columns = $zthis->MetaColumns( $tableName ); $cacheRS = $tableName; @@ -691,7 +787,7 @@ } } else if (is_subclass_of($rs, 'adorecordset')) { if (isset($rs->insertSig) && is_integer($cacheRS) && $cacheRS == $rs->insertSig) { - $columns =& $cacheCols; + $columns = $cacheCols; } else { for ($i=0, $max=$rs->FieldCount(); $i < $max; $i++) $columns[] = $rs->FetchField($i); @@ -699,7 +795,7 @@ $cacheCols = $columns; $rs->insertSig = $cacheSig++; } - $recordSet =& $rs; + $recordSet = $rs; } else { printf(ADODB_BAD_RS,'GetInsertSQL'); @@ -711,7 +807,7 @@ $upperfname = strtoupper($field->name); if (adodb_key_exists($upperfname,$arrFields,$force)) { $bad = false; - if (strpos($upperfname,' ') !== false) + if ((strpos($upperfname,' ') !== false) || ($ADODB_QUOTE_FIELDNAMES)) $fnameq = $zthis->nameQuote.$upperfname.$zthis->nameQuote; else $fnameq = $upperfname; @@ -721,7 +817,7 @@ /********************************************************/ if (is_null($arrFields[$upperfname]) || (empty($arrFields[$upperfname]) && strlen($arrFields[$upperfname]) == 0) - || $arrFields[$upperfname] === 'null' + || $arrFields[$upperfname] === $zthis->null2null ) { switch ($force) { @@ -743,7 +839,7 @@ default: case 3: //Set the value that was given in array, so you can give both null and empty values - if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === 'null') { + if (is_null($arrFields[$upperfname]) || $arrFields[$upperfname] === $zthis->null2null) { $values .= "null, "; } else { $values .= _adodb_column_sql($zthis, 'I', $type, $upperfname, $fnameq, $arrFields, $magicq); @@ -904,9 +1000,21 @@ case "T": $val = $zthis->DBTimeStamp($arrFields[$fname]); break; + + case "N": + $val = $arrFields[$fname]; + if (!is_numeric($val)) $val = str_replace(',', '.', (float)$val); + break; + case "I": + case "R": + $val = $arrFields[$fname]; + if (!is_numeric($val)) $val = (integer) $val; + break; + + default: - $val = $arrFields[$fname]; + $val = str_replace(array("'"," ","("),"",$arrFields[$fname]); // basic sql injection defence if (empty($val)) $val = '0'; break; } @@ -926,7 +1034,8 @@ if ($inputarr) { foreach($inputarr as $kk=>$vv) { if (is_string($vv) && strlen($vv)>64) $vv = substr($vv,0,64).'...'; - $ss .= "($kk=>'$vv') "; + if (is_null($vv)) $ss .= "($kk=>null) "; + else $ss .= "($kk=>'$vv') "; } $ss = "[ $ss ]"; } Added: trunk/linpha2/lib/adodb/adodb-memcache.lib.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-memcache.lib.inc.php (rev 0) +++ trunk/linpha2/lib/adodb/adodb-memcache.lib.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -0,0 +1,118 @@ +<?php + +// security - hide paths +if (!defined('ADODB_DIR')) die(); + +global $ADODB_INCLUDED_MEMCACHE; +$ADODB_INCLUDED_MEMCACHE = 1; + +/* + + V4.90 8 June 2006 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. + Released under both BSD license and Lesser GPL library license. + Whenever there is any discrepancy between the two licenses, + the BSD license will take precedence. See License.txt. + Set tabs to 4 for best viewing. + + Latest version is available at http://adodb.sourceforge.net + +*/ + + function getmemcache($key,&$err, $timeout=0, $host, $port) + { + $false = false; + $err = false; + + if (!function_exists('memcache_pconnect')) { + $err = 'Memcache module PECL extension not found!'; + return $false; + } + + $memcache = new Memcache; + if (!@$memcache->pconnect($host, $port)) { + $err = 'Can\'t connect to memcache server on: '.$host.':'.$port; + return $false; + } + + $rs = $memcache->get($key); + if (!$rs) { + $err = 'Item with such key doesn\'t exists on the memcached server.'; + return $false; + } + + $tdiff = intval($rs->timeCreated+$timeout - time()); + if ($tdiff <= 2) { + switch($tdiff) { + case 2: + if ((rand() & 15) == 0) { + $err = "Timeout 2"; + return $false; + } + break; + case 1: + if ((rand() & 3) == 0) { + $err = "Timeout 1"; + return $false; + } + break; + default: + $err = "Timeout 0"; + return $false; + } + } + return $rs; + } + + function putmemcache($key, $rs, $host, $port, $compress, $debug=false) + { + $false = false; + $true = true; + + if (!function_exists('memcache_pconnect')) { + if ($debug) ADOConnection::outp(" Memcache module PECL extension not found!<br>\n"); + return $false; + } + + $memcache = new Memcache; + if (!@$memcache->pconnect($host, $port)) { + if ($debug) ADOConnection::outp(" Can't connect to memcache server on: $host:$port<br>\n"); + return $false; + } + + $rs->timeCreated = time(); + if (!$memcache->set($key, $rs, $compress, 0)) { + if ($debug) ADOConnection::outp(" Failed to save data at the memcached server!<br>\n"); + return $false; + } + return $true; + } + + function flushmemcache($key=false, $host, $port, $debug=false) + { + if (!function_exists('memcache_pconnect')) { + if ($debug) ADOConnection::outp(" Memcache module PECL extension not found!<br>\n"); + return; + } + + $memcache = new Memcache; + if (!@$memcache->pconnect($host, $port)) { + if ($debug) ADOConnection::outp(" Can't connect to memcache server on: $host:$port<br>\n"); + return; + } + + if ($key) { + if (!$memcache->delete($key)) { + if ($debug) ADOConnection::outp("CacheFlush: $key entery doesn't exist on memcached server!<br>\n"); + } else { + if ($debug) ADOConnection::outp("CacheFlush: $key entery flushed from memcached server!<br>\n"); + } + } else { + if (!$memcache->flush()) { + if ($debug) ADOConnection::outp("CacheFlush: Failure flushing all enteries from memcached server!<br>\n"); + } else { + if ($debug) ADOConnection::outp("CacheFlush: All enteries flushed from memcached server!<br>\n"); + } + } + return; + } +?> Modified: trunk/linpha2/lib/adodb/adodb-pager.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-pager.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-pager.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,7 +1,7 @@ <?php /* - V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. @@ -247,12 +247,12 @@ $savec = $ADODB_COUNTRECS; if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true; if ($this->cache) - $rs = &$this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page); + $rs = $this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page); else - $rs = &$this->db->PageExecute($this->sql,$rows,$this->curr_page); + $rs = $this->db->PageExecute($this->sql,$rows,$this->curr_page); $ADODB_COUNTRECS = $savec; - $this->rs = &$rs; + $this->rs = $rs; if (!$rs) { print "<h3>Query failed: $this->sql</h3>"; return; Modified: trunk/linpha2/lib/adodb/adodb-pear.inc.php =================================================================== --- trunk/linpha2/lib/adodb/adodb-pear.inc.php 2008-02-06 23:25:59 UTC (rev 4854) +++ trunk/linpha2/lib/adodb/adodb-pear.inc.php 2008-02-07 20:22:33 UTC (rev 4855) @@ -1,6 +1,6 @@ <?php /** - * @version V4.91 2 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved. + * @version V5.03 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved. * Released under both BSD license and Lesser GPL library license. * Whenever there is any discrepancy between the two licenses, * the BSD license will take precedence. @@ -109,11 +109,11 @@ * error */ - function &factory($type) + function factory($type) { include_once(ADODB_DIR."/drivers/adodb-$type.inc.php"); - $obj = &NewADOConnection($type); - if (!is_object($obj)) $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1); + $obj = NewADOConnection($type); + if (!is_object($obj)) $obj = new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1); return $obj; } @@ -136,7 +136,7 @@ * @see DB::parseDSN * @see DB::isError */ - function &connect($dsn, $options = false) + function connect($dsn, $options = false) { if (is_array($dsn)) { $dsninfo = $dsn; @@ -157,9 +157,9 @@ @include_once("adodb-$type.inc.php"); } - @$obj =& NewADOConnection($type); + @$obj = NewADOConnection($type); if (!is_object($obj)) { - $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1); + $obj = new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1); return $obj; } if (is_array($options)) { Modified: trunk/linpha2/lib/adodb/adodb-perf.inc.php ===========================================... [truncated message content] |