Index: trunk/phpgwapi/inc/adodb/drivers/adodb-pdo.inc.php
===================================================================
--- trunk/phpgwapi/inc/adodb/drivers/adodb-pdo.inc.php	(revision 2)
+++ trunk/phpgwapi/inc/adodb/drivers/adodb-pdo.inc.php	(revision 34)
@@ -1,5 +1,5 @@
 <?php
 /* 
-V4.51 29 July 2004  (c) 2000-2004 John Lim (jlim#natsoft.com.my). All rights reserved.
+V4.94 23 Jan 2007  (c) 2000-2007 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, 
@@ -10,10 +10,92 @@
   
   Requires ODBC. Works on Windows and Unix.
+
+	Problems: 
+		Where is float/decimal type in pdo_param_type
+		LOB handling for CLOB/BLOB differs significantly
 */
 // security - hide paths
 if (!defined('ADODB_DIR')) die();
+
+
+/*
+enum pdo_param_type {
+PDO::PARAM_NULL, 0
+
+/* int as in long (the php native int type).
+ * If you mark a column as an int, PDO expects get_col to return
+ * a pointer to a long 
+PDO::PARAM_INT, 1
+
+/* get_col ptr should point to start of the string buffer 
+PDO::PARAM_STR, 2
+
+/* get_col: when len is 0 ptr should point to a php_stream *,
+ * otherwise it should behave like a string. Indicate a NULL field
+ * value by setting the ptr to NULL 
+PDO::PARAM_LOB, 3
+
+/* get_col: will expect the ptr to point to a new PDOStatement object handle,
+ * but this isn't wired up yet 
+PDO::PARAM_STMT, 4 /* hierarchical result set 
+
+/* get_col ptr should point to a zend_bool 
+PDO::PARAM_BOOL, 5
+
+
+/* magic flag to denote a parameter as being input/output 
+PDO::PARAM_INPUT_OUTPUT = 0x80000000
+};
+*/
+	
+function adodb_pdo_type($t)
+{
+	switch($t) {
+	case 2: return 'VARCHAR';
+	case 3: return 'BLOB';
+	default: return 'NUMERIC';
+	}
+}
 	 
 /*--------------------------------------------------------------------------------------
 --------------------------------------------------------------------------------------*/
+
+////////////////////////////////////////////////
+
+
+
+class ADODB_pdo_base extends ADODB_pdo {
+
+	var $sysDate = "'?'";
+	var $sysTimeStamp = "'?'";
+	
+
+	function _init($parentDriver)
+	{
+		$parentDriver->_bindInputArray = true;
+		#$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
+	}
+	
+	function ServerInfo()
+	{
+		return ADOConnection::ServerInfo();
+	}
+	
+	function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
+	{
+		$ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
+		return $ret;
+	}
+	
+	function MetaTables()
+	{
+		return false;
+	}
+	
+	function MetaColumns()
+	{
+		return false;
+	}
+}
 
 
@@ -31,30 +113,95 @@
 	var $_lastAffectedRows = 0;
 	
+	var $_errormsg = false;
+	var $_errorno = false;
+	
+	var $dsnType = '';
 	var $stmt = false;
 	
-	function ADODB_pdo() 
-	{
-	}
-	
-
+	function ADODB_pdo()
+	{
+	}
+	
+	function _UpdatePDO()
+	{
+		$d = &$this->_driver;
+		$this->fmtDate = $d->fmtDate;
+		$this->fmtTimeStamp = $d->fmtTimeStamp;
+		$this->replaceQuote = $d->replaceQuote;
+		$this->sysDate = $d->sysDate;
+		$this->sysTimeStamp = $d->sysTimeStamp;
+		$this->random = $d->random;
+		$this->concat_operator = $d->concat_operator;
+		$this->nameQuote = $d->nameQuote;
+				
+		$this->hasGenID = $d->hasGenID;
+		$this->_genIDSQL = $d->_genIDSQL;
+		$this->_genSeqSQL = $d->_genSeqSQL;
+		$this->_dropSeqSQL = $d->_dropSeqSQL;
+
+		$d->_init($this);
+	}
+	
+	function Time()
+	{
+		if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual";
+		else $sql = "select $this->sysTimeStamp";
+		
+		$rs =& $this->_Execute($sql);
+		if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
+		
+		return false;
+	}
+	
 	// returns true or false
 	function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
 	{
-		$this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
+		$at = strpos($argDSN,':');
+		$this->dsnType = substr($argDSN,0,$at);
+
+		if ($argDatabasename) {
+			$argDSN .= ';dbname='.$argDatabasename;
+		}
+		try {
+			$this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
+		} catch (Exception $e) {
+			$this->_connectionID = false;
+			$this->_errorno = -1;
+			//var_dump($e);
+			$this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
+			return false;
+		}
+		
 		if ($this->_connectionID) {
 			switch(ADODB_ASSOC_CASE){
-			case 0: $m = PDO_CASE_LOWER; break;
-			case 1: $m = PDO_CASE_UPPER; break;
+			case 0: $m = PDO::CASE_LOWER; break;
+			case 1: $m = PDO::CASE_UPPER; break;
 			default:
-			case 2: $m = PDO_CASE_NATURAL; break;
+			case 2: $m = PDO::CASE_NATURAL; break;
 			}
 			
-			//$this->_connectionID->setAttribute(PDO_ATTR_ERRMODE,PDO_ERRMODE_SILENT );
-			$this->_connectionID->setAttribute(PDO_ATTR_CASE,$m);
+			//$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
+			$this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
 			
-			//$this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true);
+			$class = 'ADODB_pdo_'.$this->dsnType;
+			//$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
+			switch($this->dsnType) {
+			case 'oci':
+			case 'mysql':
+			case 'pgsql':
+			case 'mssql':
+				include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
+				break;
+			}
+			if (class_exists($class))
+				$this->_driver = new $class();
+			else
+				$this->_driver = new ADODB_pdo_base();
 			
+			$this->_driver->_connectionID = $this->_connectionID;
+			$this->_UpdatePDO();
 			return true;
 		}
+		$this->_driver = new ADODB_pdo_base();
 		return false;
 	}
@@ -66,13 +213,31 @@
 	}
 	
-	function ErrorMsg()
-	{
-		if ($this->_stmt) $arr = $this->_stmt->errorInfo();
-		else $arr = $this->_connectionID->errorInfo();
-		
-		if ($arr) {
-			if ($arr[0]) return $arr[2];
-			else return '';
-		} else return '-1';
+	/*------------------------------------------------------------------------------*/
+	
+	
+	function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
+	{	
+		$save = $this->_driver->fetchMode;
+		$this->_driver->fetchMode = $this->fetchMode;
+	 	$this->_driver->debug = $this->debug;
+		$ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
+		$this->_driver->fetchMode = $save;
+		return $ret;
+	}
+	
+	
+	function ServerInfo()
+	{
+		return $this->_driver->ServerInfo();
+	}
+	
+	function MetaTables($ttype=false,$showSchema=false,$mask=false)
+	{
+		return $this->_driver->MetaTables($ttype,$showSchema,$mask);
+	}
+	
+	function MetaColumns($table,$normalize=true)
+	{
+		return $this->_driver->MetaColumns($table,$normalize);
 	}
 	
@@ -84,9 +249,34 @@
 	}
 	
+	
+	function ErrorMsg()
+	{
+		if ($this->_errormsg !== false) return $this->_errormsg;
+		if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo();
+		else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo();
+		else return 'No Connection Established';
+		
+		
+		if ($arr) {
+		 	if (sizeof($arr)<2) return '';
+			if ((integer)$arr[1]) return $arr[2];
+			else return '';
+		} else return '-1';
+	}
+	
+
 	function ErrorNo()
 	{
-		
-		if ($this->_stmt) return $this->_stmt->errorCode();
-		else return $this->_connectionID->errorInfo();
+		if ($this->_errorno !== false) return $this->_errorno;
+		if (!empty($this->_stmt)) $err = $this->_stmt->errorCode();
+		else if (!empty($this->_connectionID)) {
+			$arr = $this->_connectionID->errorInfo();
+			if (isset($arr[0])) $err = $arr[0];
+			else $err = -1;
+		} else
+			return 0;
+			
+		if ($err == '00000') return 0; // allows empty check
+		return $err;
 	}
 
@@ -97,5 +287,5 @@
 		$this->transCnt += 1;
 		$this->_autocommit = false;
-		$this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,false);
+		$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
 		return $this->_connectionID->beginTransaction();
 	}
@@ -103,4 +293,5 @@
 	function CommitTrans($ok=true) 
 	{ 
+		if (!$this->hasTransactions) return false;
 		if ($this->transOff) return true; 
 		if (!$ok) return $this->RollbackTrans();
@@ -109,5 +300,5 @@
 		
 		$ret = $this->_connectionID->commit();
-		$this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true);
+		$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
 		return $ret;
 	}
@@ -115,4 +306,5 @@
 	function RollbackTrans()
 	{
+		if (!$this->hasTransactions) return false;
 		if ($this->transOff) return true; 
 		if ($this->transCnt) $this->transCnt -= 1;
@@ -120,5 +312,5 @@
 		
 		$ret = $this->_connectionID->rollback();
-		$this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true);
+		$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
 		return $ret;
 	}
@@ -139,5 +331,6 @@
 		return $obj;
 	}
-
+	
+	
 	/* returns queryID or false */
 	function _query($sql,$inputarr=false) 
@@ -146,12 +339,36 @@
 			$stmt = $sql[1];
 		} else {
-			$stmt = $this->_connectionID->prepare($sql);		
-		}
+			$stmt = $this->_connectionID->prepare($sql);
+		}
+		#adodb_backtrace();
+		#var_dump($this->_bindInputArray);
 		if ($stmt) {
-			if ($inputarr) $stmt->execute($inputarr);
-			else $stmt->execute();
-		}
-		$this->_stmt = $stmt;
-		return $stmt;
+			$this->_driver->debug = $this->debug;
+			if ($inputarr) $ok = $stmt->execute($inputarr);
+			else $ok = $stmt->execute();
+		} 
+		
+		
+		$this->_errormsg = false;
+		$this->_errorno = false;
+			
+		if ($ok) {
+			$this->_stmt = $stmt;
+			return $stmt;
+		}
+		
+		if ($stmt) {
+			
+			$arr = $stmt->errorinfo();
+			if ((integer)$arr[1]) {
+				$this->_errormsg = $arr[2];
+				$this->_errorno = $arr[1];
+			}
+
+		} else {
+			$this->_errormsg = false;
+			$this->_errorno = false;
+		}
+		return false;
 	}
 
@@ -211,9 +428,14 @@
 		if ($this->_stmt) $arr = $this->_stmt->errorInfo();
 		else $arr = $this->_connectionID->errorInfo();
-		print_r($arr);
-		if ($arr) {
-			if ($arr[0]) return $arr[2];
+
+		if (is_array($arr)) {
+			if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
 			else return '';
 		} else return '-1';
+	}
+	
+	function NumCols()
+	{
+		return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
 	}
 	
@@ -241,9 +463,11 @@
 			$mode = $ADODB_FETCH_MODE;
 		}
+		$this->adodbFetchMode = $mode;
 		switch($mode) {
-		default:
-		case ADODB_FETCH_BOTH: $mode = PDO_FETCH_BOTH; break;
-		case ADODB_FETCH_NUM: $mode = PDO_FETCH_NUM; break;
-		case ADODB_FETCH_ASSOC:  $mode = PDO_FETCH_ASSOC; break;
+		case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
+		case ADODB_FETCH_ASSOC:  $mode = PDO::FETCH_ASSOC; break;
+		
+		case ADODB_FETCH_BOTH: 
+		default: $mode = PDO::FETCH_BOTH; break;
 		}
 		$this->fetchMode = $mode;
@@ -253,19 +477,4 @@
 	}
 
-
-	// returns the field object
-	function &FetchField($fieldOffset = -1) 
-	{
-		
-		$off=$fieldOffset+1; // offsets begin at 1
-		
-		$o= new ADOFieldObject();
-		$o->name = @odbc_field_name($this->_queryID,$off);
-		$o->type = @odbc_field_type($this->_queryID,$off);
-		$o->max_length = @odbc_field_len($this->_queryID,$off);
-		if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
-		else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
-		return $o;
-	}
 	
 	function Init()
@@ -283,5 +492,4 @@
 				$this->_numOfRows = 0; // _numOfRows could be -1
 			}
-			$this->_numOfFields = sizeof($this->fields);
 		} else {
 			$this->EOF = true;
@@ -289,5 +497,4 @@
 	}
 	
-		
 	function _initrs()
 	{
@@ -296,6 +503,33 @@
 		$this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
 		if (!$this->_numOfRows) $this->_numOfRows = -1;
-		$this->_numOfFields =0;
-	}	
+		$this->_numOfFields = $this->_queryID->columnCount();
+	}
+
+	// returns the field object
+	function &FetchField($fieldOffset = -1) 
+	{
+		$off=$fieldOffset+1; // offsets begin at 1
+		
+		$o= new ADOFieldObject();
+		$arr = @$this->_queryID->getColumnMeta($fieldOffset);
+		if (!$arr) {
+			$o->name = 'bad getColumnMeta()';
+			$o->max_length = -1;
+			$o->type = 'VARCHAR';
+			$o->precision = 0;
+	#		$false = false;
+			return $o;
+		}
+		//adodb_pr($arr);
+		$o->name = $arr['name'];
+		if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type'];
+		else $o->type = adodb_pdo_type($arr['pdo_type']);
+		$o->max_length = $arr['len'];
+		$o->precision = $arr['precision'];
+		
+		if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
+		else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
+		return $o;
+	}
 	
 	function _seek($row)
@@ -306,4 +540,6 @@
 	function _fetch()
 	{
+		if (!$this->_queryID) return false;
+		
 		$this->fields = $this->_queryID->fetch($this->fetchMode);
 		return !empty($this->fields);
@@ -314,4 +550,18 @@
 		$this->_queryID = false;
 	}
+	
+	function Fields($colname)
+	{
+		if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
+		
+		if (!$this->bind) {
+			$this->bind = array();
+			for ($i=0; $i < $this->_numOfFields; $i++) {
+				$o = $this->FetchField($i);
+				$this->bind[strtoupper($o->name)] = $i;
+			}
+		}
+		 return $this->fields[$this->bind[strtoupper($colname)]];
+	}
 
 }
