[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gallery-devel
Subject:    [Gallery-devel] Getting G2 to work using MS SQL via ADO
From:       "Larry Menard" <larry.menard () rogers ! com>
Date:       2006-05-08 8:52:03
Message-ID: 170001c6727c$ad5f4150$6401a8c0 () larryhome
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


   Folks,

   I apologize for posting this here, but I'm unable to contact any of the Gallery \
web sites (e.g., gallery.menalto.com), so I can't even access the forum thread I've \
been using for this topic.  I'm currently 99.44% sure I'm being blocked at the \
server, not at my end.

   Mindless,

   I have gotten G2 to work with the "adodb-ado-mssql.inc.php" and \
"adodb-ado5.inc.php" files.  (Attached... look for "LJM" to see my changes.)

   First, I added SEQUENCE support to "adodb-ado-mssql.inc.php".  That's \
straightforward, copied from "adodb-mssql.inc.php", I'm not overly concerned about \
that.

   I'm much more concerned with the amount of tweaking I did to the \
"adodb-ado5.inc.php" file... I don't know if these changes will fly outside of G2.

  a.. In DatabaseSetupStep::loadTemplateData(), G2 calls \
NConnect($this->_config['hostname'], $this->_config['username'], \
$this->_config['password'], $this->_config['database'])   b.. NConnect() in turn \
calls _connect($argHostname, $argUsername, $argPassword, $argDatabaseName, true)   \
c.. But _connect() is expecting args ($argHostname, $argUsername, $argPassword, \
$argProvider= 'MSDASQL'), so it is getting our databasename as the Providername, and \
as a result the Provider clause being passed along is incorrect ("gallery2" instead \
of "MSDASQL").  (Note that in the regular "adodb-mssql.inc.php" file, the 4th arg is \
$argDatabasename, not $argProvider (there is no $argProvider in _connect() in that \
file).)

   To resolve this, in the _connect() function in "adodb-ado5.inc.php":

  a.. I changed the parameter list from ($argHostname, $argUsername, $argPassword, \
$argProvider= 'MSDASQL') to ($argHostname, $argUsername, $argPassword, \
$argDatabasename, $argProvider= 'MSDASQL') (in other words, I injected an arg for the \
dbname before $argProvider)

   I also found that the _connect() function constructs a hostname string (by \
concatenating various other parms), which is the only thing it passes to the Open() \
function.  That hostname string does not include all the clauses it needs \
("Provider=...;DRIVER=...;SERVER=...;DATABASE=...;") so I added the missing clauses \
like this: 

  a.. if ($argProvider) $argHostname = "PROVIDER=$argProvider;DRIVER={SQL \
Server};SERVER=$argHostname";   b.. if ($argDatabasename) $argHostname .= \
";DATABASE=$argDatabasename";

   With these changes, G2 works using MS SQL Server.  However, I strongly suspect \
these ADOdb changes will break any other existing apps that use this file.

   A possible alternative might be to add the missing clauses to the hostname in G2 \
before calling NConnect(), and ignore the databasename/Provider problem in ADOdb.  I \
haven't tested this theory yet, but it would mean adding some more platform-specifics \
to G2, as this should only be done for MS SQL.

   Your thoughts?
--------------------
Larry Menard
"Defender of Geese and of All Things Natural"
E-mail and MSN Messenger: larry.menard@rogers.com
Web: http://ca.geocities.com/larry.menard@rogers.com


[Attachment #5 (text/html)]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; Folks,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; I apologize for posting this here, but 
I'm unable to contact any of the Gallery web sites (e.g., gallery.menalto.com), 
so I can't even access the forum thread I've been using for this topic.&nbsp; 
I'm currently 99.44% sure I'm being blocked at the server, not at my 
end.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; Mindless,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; I have gotten G2 to work with the 
"adodb-ado-mssql.inc.php" and "adodb-ado5.inc.php" files.&nbsp; (Attached... 
look for "LJM"&nbsp;to see&nbsp;my changes.)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; First, I added SEQUENCE support to 
"adodb-ado-mssql.inc.php".&nbsp; That's straightforward, copied from 
"adodb-mssql.inc.php",&nbsp;I'm not overly&nbsp;concerned about 
that.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; I'm much more concerned with the 
amount of tweaking&nbsp;I did to&nbsp;the "adodb-ado5.inc.php" file... I don't 
know if these changes will fly outside of G2.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<UL>
  <LI><FONT face=Arial size=2>In DatabaseSetupStep::loadTemplateData(), G2 calls 
  <FONT face=Fixedsys>NConnect($this-&gt;_config['hostname'], 
  $this-&gt;_config['username'], $this-&gt;_config['password'], 
  $this-&gt;_config['database'])</FONT></FONT> 
  <LI><FONT face=Arial size=2>NConnect() in turn calls <FONT 
  face=Fixedsys>_connect($argHostname, $argUsername, $argPassword, 
  $argDatabaseName, true)</FONT></FONT> 
  <LI><FONT face=Arial size=2>But _connect() is expecting args <FONT 
  face=Fixedsys>($argHostname, $argUsername, $argPassword, $argProvider= 
  'MSDASQL')</FONT></FONT><FONT face=Arial size=2>, <U>so it is getting our 
  databasename as the Providername</U>, and as a result the Provider clause 
  being passed along is incorrect ("gallery2" instead of "MSDASQL").&nbsp; (Note 
  that in the regular "adodb-mssql.inc.php" file, the 4th arg is 
  $argDatabasename, not $argProvider (there is no $argProvider in _connect() in 
  that file).)</FONT></LI></UL>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; To resolve this, in the _connect() 
function in&nbsp;"adodb-ado5.inc.php":</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<UL dir=ltr style="MARGIN-RIGHT: 0px">
  <LI><FONT face=Arial size=2>I changed the parameter list&nbsp;from <FONT 
  face=Fixedsys>($argHostname, $argUsername, $argPassword, $argProvider= 
  'MSDASQL')</FONT> to <FONT face=Fixedsys>($argHostname, $argUsername, 
  $argPassword, <FONT color=#0000ff>$argDatabasename,</FONT> $argProvider= 
  'MSDASQL')</FONT> (in other words, I injected an arg for the dbname before 
  $argProvider)</FONT></LI></UL>
<DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV dir=ltr><FONT face=Arial size=2>&nbsp;&nbsp; I also found that 
t</FONT><FONT face=Arial size=2>he _connect() function constructs&nbsp;a 
hostname string (by concatenating various other parms), which is the <U>only</U> 
thing&nbsp;it passes to the Open() function.&nbsp; That hostname string does not 
include all the clauses it needs 
("Provider=...;DRIVER=...;SERVER=...;DATABASE=...;")&nbsp;so I added the missing 
clauses like this:</FONT> </DIV>
<DIV dir=ltr><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV dir=ltr>
<UL>
  <LI><FONT face=Fixedsys size=2>if ($argProvider) $argHostname = 
  "PROVIDER=$argProvider;DRIVER={SQL Server};SERVER=$argHostname";</FONT> 
  <LI><FONT face=Fixedsys size=2>if ($argDatabasename) $argHostname .= 
  ";DATABASE=$argDatabasename";</FONT></LI></UL></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; With these changes, G2 works using MS 
SQL Server.&nbsp; However, I strongly suspect these ADOdb changes&nbsp;will 
break&nbsp;any other&nbsp;existing apps that use this file.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; A possible&nbsp;alternative&nbsp;might 
be to&nbsp;add the missing&nbsp;clauses to&nbsp;the&nbsp;hostname&nbsp;<U>in 
G2</U> before calling NConnect(), and ignore the databasename/Provider problem 
in ADOdb.&nbsp;&nbsp;I&nbsp;haven't tested this theory yet, but it would mean 
adding some more platform-specifics to G2, as this should only be done for MS 
SQL.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; Your thoughts?</FONT></DIV>
<DIV><FONT face=Arial size=2>--------------------<BR>Larry Menard<BR>"Defender 
of Geese and of All Things Natural"<BR>E-mail and MSN Messenger: <A 
href="mailto:larry.menard@rogers.com">larry.menard@rogers.com</A><BR>Web: <A 
href="http://ca.geocities.com/larry.menard@rogers.com">http://ca.geocities.com/larry.menard@rogers.com</A></FONT></DIV>
 <DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BODY></HTML>


["adodb-ado_mssql.inc.php" (application/octet-stream)]

<?php
/* 
V4.80 8 Mar 2006  (c) 2000-2006 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. 
Set tabs to 4 for best viewing.
  
  Latest version is available at http://adodb.sourceforge.net
  
  Microsoft SQL Server ADO data driver. Requires ADO and MSSQL client. 
  Works only on MS Windows.
  
  It is normally better to use the mssql driver directly because it is much faster. 
  This file is only a technology demonstration and for test purposes.
*/

// security - hide paths
if (!defined('ADODB_DIR')) die();

if (!defined('_ADODB_ADO_LAYER')) {
	if (PHP_VERSION >= 5) include(ADODB_DIR."/drivers/adodb-ado5.inc.php");
	else include(ADODB_DIR."/drivers/adodb-ado.inc.php");
}


class  ADODB_ado_mssql extends ADODB_ado {        
	var $databaseType = 'ado_mssql';
	var $hasTop = 'top';
	var $hasInsertID = true;
	var $sysDate = 'convert(datetime,convert(char,GetDate(),102),102)';
	var $sysTimeStamp = 'GetDate()';
	var $leftOuter = '*=';
	var $rightOuter = '=*';
	var $ansiOuter = true; // for mssql7 or later
	var $substr = "substring";
	var $length = 'len';
	
//LJM
// Looks like there's no built-in SEQUENCE support in MS SQL.
// Copy the emulation (tables) from adodb-mssql.inc.php.
var $_dropSeqSQL = "drop table %s";

	//var $_inTransaction = 1; // always open recordsets, so no transaction problems.
	
	function ADODB_ado_mssql()
	{
	        $this->ADODB_ado();
	}
	
	function _insertid()
	{
	        return $this->GetOne('select @@identity');
	}
	
	function _affectedrows()
	{
	        return $this->GetOne('select @@rowcount');
	}
	
	function MetaColumns($table)
	{
        $table = strtoupper($table);
        $arr= array();
        $dbc = $this->_connectionID;
        
        $osoptions = array();
        $osoptions[0] = null;
        $osoptions[1] = null;
        $osoptions[2] = $table;
        $osoptions[3] = null;
        
        $adors=@$dbc->OpenSchema(4, $osoptions);//tables

        if ($adors){
                while (!$adors->EOF){
                        $fld = new ADOFieldObject();
                        $c = $adors->Fields(3);
                        $fld->name = $c->Value;
                        $fld->type = 'CHAR'; // cannot discover type in ADO!
                        $fld->max_length = -1;
                        $arr[strtoupper($fld->name)]=$fld;
        
                        $adors->MoveNext();
                }
                $adors->Close();
        }
        $false = false;
		return empty($arr) ? $false : $arr;
	}
	
//LJM on
	function CreateSequence($seq='adodbseq',$start=1)
	{
		
		$this->Execute('BEGIN TRANSACTION adodbseq');
		$start -= 1;
		$this->Execute("create table $seq (id float(53))");
		$ok = $this->Execute("insert into $seq with (tablock,holdlock) values($start)");
		if (!$ok) {
				$this->Execute('ROLLBACK TRANSACTION adodbseq');
				return false;
		}
		$this->Execute('COMMIT TRANSACTION adodbseq'); 
		return true;
	}

	function GenID($seq='adodbseq',$start=1)
	{
		//$this->debug=1;
		$this->Execute('BEGIN TRANSACTION adodbseq');
		$ok = $this->Execute("update $seq with (tablock,holdlock) set id = id + 1");
		if (!$ok) {
			$this->Execute("create table $seq (id float(53))");
			$ok = $this->Execute("insert into $seq with (tablock,holdlock) values($start)");
			if (!$ok) {
				$this->Execute('ROLLBACK TRANSACTION adodbseq');
				return false;
			}
			$this->Execute('COMMIT TRANSACTION adodbseq'); 
			return $start;
		}
		$num = $this->GetOne("select id from $seq");
		$this->Execute('COMMIT TRANSACTION adodbseq'); 
		return $num;
		
		// in old implementation, pre 1.90, we returned GUID...
		//return $this->GetOne("SELECT CONVERT(varchar(255), NEWID()) AS 'Char'");
	}
//LJM off

	} // end class 
	
	class  ADORecordSet_ado_mssql extends ADORecordSet_ado {        
	
	var $databaseType = 'ado_mssql';
	
	function ADORecordSet_ado_mssql($id,$mode=false)
	{
	        return $this->ADORecordSet_ado($id,$mode);
	}
}
?>

["adodb-ado5.inc.php" (application/octet-stream)]

<?php
/* 
V4.80 8 Mar 2006  (c) 2000-2006 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. 
Set tabs to 4 for best viewing.
  
  Latest version is available at http://adodb.sourceforge.net
  
	Microsoft ADO data driver. Requires ADO. Works only on MS Windows. PHP5 compat version.
*/

// security - hide paths
if (!defined('ADODB_DIR')) die();
	
define("_ADODB_ADO_LAYER", 1 );
/*--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------*/

	
class ADODB_ado extends ADOConnection {
	var $databaseType = "ado";	
	var $_bindInputArray = false;
	var $fmtDate = "'Y-m-d'";
	var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
	var $replaceQuote = "''"; // string to use to replace quotes
	var $dataProvider = "ado";	
	var $hasAffectedRows = true;
	var $adoParameterType = 201; // 201 = long varchar, 203=long wide varchar, 205 = long varbinary
	var $_affectedRows = false;
	var $_thisTransactions;
	var $_cursor_type = 3; // 3=adOpenStatic,0=adOpenForwardOnly,1=adOpenKeyset,2=adOpenDynamic
	var $_cursor_location = 3; // 2=adUseServer, 3 = adUseClient;
	var $_lock_type = -1;
	var $_execute_option = -1;
	var $poorAffectedRows = true; 
	var $charPage;
		
	function ADODB_ado() 
	{ 	
		$this->_affectedRows = new VARIANT;
	}

	function ServerInfo()
	{
		if (!empty($this->_connectionID)) $desc = $this->_connectionID->provider;
		return array('description' => $desc, 'version' => '');
	}
	
	function _affectedrows()
	{
		if (PHP_VERSION >= 5) return $this->_affectedRows;
		
		return $this->_affectedRows->value;
	}
	
	// you can also pass a connection string like this:
	//
	// $DB->Connect('USER ID=sa;PASSWORD=pwd;SERVER=mangrove;DATABASE=ai',false,false,'SQLOLEDB');
//LJM
//	function _connect($argHostname, $argUsername, $argPassword, $argProvider= 'MSDASQL')
	function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $argProvider= 'MSDASQL')
	{
//LJM
//printf ("<br><br>In ADODB_ado::_connect(), \$argProvider = \"%s\".\n<br>", $argProvider);
		try {
		$u = 'UID';
		$p = 'PWD';
	
		if (!empty($this->charPage))
			$dbc = new COM('ADODB.Connection',null,$this->charPage);
		else
			$dbc = new COM('ADODB.Connection');
			
		if (! $dbc) return false;

		/* special support if provider is mssql or access */
		if ($argProvider=='mssql') {
			$u = 'User Id';  //User parameter name for OLEDB
			$p = 'Password'; 
			$argProvider = "SQLOLEDB"; // SQL Server Provider
			
			// not yet
			//if ($argDatabasename) $argHostname .= ";Initial Catalog=$argDatabasename";
			
			//use trusted conection for SQL if username not specified
			if (!$argUsername) $argHostname .= ";Trusted_Connection=Yes";
		} else if ($argProvider=='access')
			$argProvider = "Microsoft.Jet.OLEDB.4.0"; // Microsoft Jet Provider
		
		if ($argProvider) $dbc->Provider = $argProvider;	
//LJM
if ($argProvider) $argHostname = "PROVIDER=$argProvider;DRIVER={SQL Server};SERVER=$argHostname";	

//LJM
if ($argDatabasename) $argHostname .= ";DATABASE=$argDatabasename";		
		if ($argUsername) $argHostname .= ";$u=$argUsername";
		if ($argPassword)$argHostname .= ";$p=$argPassword";
		
		if ($this->debug) ADOConnection::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
		// @ added below for php 4.0.1 and earlier
//LJM
//printf ("<br><br>In ADODB_ado::_connect(), calling \$dbc->Open(%s).\n<br>", (string) $argHostname);
		@$dbc->Open((string) $argHostname);
		
		$this->_connectionID = $dbc;
		
		$dbc->CursorLocation = $this->_cursor_location;
		return  $dbc->State > 0;
		} catch (exception $e) {
		}
		
		return false;
	}
	
	// returns true or false
	function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
	{
		return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
	}	
	
/*
	adSchemaCatalogs	= 1,
	adSchemaCharacterSets	= 2,
	adSchemaCollations	= 3,
	adSchemaColumns	= 4,
	adSchemaCheckConstraints	= 5,
	adSchemaConstraintColumnUsage	= 6,
	adSchemaConstraintTableUsage	= 7,
	adSchemaKeyColumnUsage	= 8,
	adSchemaReferentialContraints	= 9,
	adSchemaTableConstraints	= 10,
	adSchemaColumnsDomainUsage	= 11,
	adSchemaIndexes	= 12,
	adSchemaColumnPrivileges	= 13,
	adSchemaTablePrivileges	= 14,
	adSchemaUsagePrivileges	= 15,
	adSchemaProcedures	= 16,
	adSchemaSchemata	= 17,
	adSchemaSQLLanguages	= 18,
	adSchemaStatistics	= 19,
	adSchemaTables	= 20,
	adSchemaTranslations	= 21,
	adSchemaProviderTypes	= 22,
	adSchemaViews	= 23,
	adSchemaViewColumnUsage	= 24,
	adSchemaViewTableUsage	= 25,
	adSchemaProcedureParameters	= 26,
	adSchemaForeignKeys	= 27,
	adSchemaPrimaryKeys	= 28,
	adSchemaProcedureColumns	= 29,
	adSchemaDBInfoKeywords	= 30,
	adSchemaDBInfoLiterals	= 31,
	adSchemaCubes	= 32,
	adSchemaDimensions	= 33,
	adSchemaHierarchies	= 34,
	adSchemaLevels	= 35,
	adSchemaMeasures	= 36,
	adSchemaProperties	= 37,
	adSchemaMembers	= 38

*/
	
	function &MetaTables()
	{
		$arr= array();
		$dbc = $this->_connectionID;
		
		$adors=@$dbc->OpenSchema(20);//tables
		if ($adors){
			$f = $adors->Fields(2);//table/view name
			$t = $adors->Fields(3);//table type
			while (!$adors->EOF){
				$tt=substr($t->value,0,6);
				if ($tt!='SYSTEM' && $tt !='ACCESS')
					$arr[]=$f->value;
				//print $f->value . ' ' . $t->value.'<br>';
				$adors->MoveNext();
			}
			$adors->Close();
		}
		
		return $arr;
	}
	
	function &MetaColumns($table)
	{
		$table = strtoupper($table);
		$arr= array();
		$dbc = $this->_connectionID;
		
		$adors=@$dbc->OpenSchema(4);//tables
	
		if ($adors){
			$t = $adors->Fields(2);//table/view name
			while (!$adors->EOF){
				
				
				if (strtoupper($t->Value) == $table) {
				
					$fld = new ADOFieldObject();
					$c = $adors->Fields(3);
					$fld->name = $c->Value;
					$fld->type = 'CHAR'; // cannot discover type in ADO!
					$fld->max_length = -1;
					$arr[strtoupper($fld->name)]=$fld;
				}
		
				$adors->MoveNext();
			}
			$adors->Close();
		}
		
		return $arr;
	}
	


	
	/* returns queryID or false */
	function &_query($sql,$inputarr=false) 
	{
		$false = false;
		try { // In PHP5, all COM errors are exceptions, so to maintain old behaviour...
		
		$dbc = $this->_connectionID;
		
	//	return rs	
		if ($inputarr) {
			
			if (!empty($this->charPage))
				$oCmd = new COM('ADODB.Command',null,$this->charPage);
			else
				$oCmd = new COM('ADODB.Command');
			$oCmd->ActiveConnection = $dbc;
			$oCmd->CommandText = $sql;
			$oCmd->CommandType = 1;

			foreach($inputarr as $val) {
				// name, type, direction 1 = input, len,
				$this->adoParameterType = 130;
				$p = $oCmd->CreateParameter('name',$this->adoParameterType,1,strlen($val),$val);
				//print $p->Type.' '.$p->value;
				$oCmd->Parameters->Append($p);
			}
			$p = false;
			$rs = $oCmd->Execute();
			$e = $dbc->Errors;
			if ($dbc->Errors->Count > 0) return $false;
			return $rs;
		}
		
		$rs = @$dbc->Execute($sql,$this->_affectedRows, $this->_execute_option);
			
		if ($dbc->Errors->Count > 0) return $false;
		if (! $rs) return $false;
		
		if ($rs->State == 0) {
			$true = true;
			return $true; // 0 = adStateClosed means no records returned
		}
		return $rs;
		
		} catch (exception $e) {
			
		}
		return $false;
	}

	
	function BeginTrans() 
	{ 
		if ($this->transOff) return true;
		
		if (isset($this->_thisTransactions))
			if (!$this->_thisTransactions) return false;
		else {
			$o = $this->_connectionID->Properties("Transaction DDL");
			$this->_thisTransactions = $o ? true : false;
			if (!$o) return false;
		}
		@$this->_connectionID->BeginTrans();
		$this->transCnt += 1;
		return true;
	}
	function CommitTrans($ok=true) 
	{ 
		if (!$ok) return $this->RollbackTrans();
		if ($this->transOff) return true;
		
		@$this->_connectionID->CommitTrans();
		if ($this->transCnt) @$this->transCnt -= 1;
		return true;
	}
	function RollbackTrans() {
		if ($this->transOff) return true;
		@$this->_connectionID->RollbackTrans();
		if ($this->transCnt) @$this->transCnt -= 1;
		return true;
	}
	
	/*	Returns: the last error message from previous database operation	*/	

	function ErrorMsg() 
	{
		$errc = $this->_connectionID->Errors;
		if ($errc->Count == 0) return '';
		$err = $errc->Item($errc->Count-1);
		return $err->Description;
	}
	
	function ErrorNo() 
	{
		$errc = $this->_connectionID->Errors;
		if ($errc->Count == 0) return 0;
		$err = $errc->Item($errc->Count-1);
		return $err->NativeError;
	}

	// returns true or false
	function _close()
	{
		if ($this->_connectionID) $this->_connectionID->Close();
		$this->_connectionID = false;
		return true;
	}
	
	
}
	
/*--------------------------------------------------------------------------------------
	 Class Name: Recordset
--------------------------------------------------------------------------------------*/

class ADORecordSet_ado extends ADORecordSet {	
	
	var $bind = false;
	var $databaseType = "ado";	
	var $dataProvider = "ado";	
	var $_tarr = false; // caches the types
	var $_flds; // and field objects
	var $canSeek = true;
  	var $hideErrors = true;
		  
	function ADORecordSet_ado($id,$mode=false)
	{
		if ($mode === false) { 
			global $ADODB_FETCH_MODE;
			$mode = $ADODB_FETCH_MODE;
		}
		$this->fetchMode = $mode;
		return $this->ADORecordSet($id,$mode);
	}


	// returns the field object
	function &FetchField($fieldOffset = -1) {
		$off=$fieldOffset+1; // offsets begin at 1
		
		$o= new ADOFieldObject();
		$rs = $this->_queryID;
		$f = $rs->Fields($fieldOffset);
		$o->name = $f->Name;
		$t = $f->Type;
		$o->type = $this->MetaType($t);
		$o->max_length = $f->DefinedSize;
		$o->ado_type = $t;
		

		//print "off=$off name=$o->name type=$o->type len=$o->max_length<br>";
		return $o;
	}
	
	/* Use associative array to get fields array */
	function Fields($colname)
	{
		if ($this->fetchMode & ADODB_FETCH_ASSOC) 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)]];
	}

		
	function _initrs()
	{
		$rs = $this->_queryID;
		$this->_numOfRows = $rs->RecordCount;
		
		$f = $rs->Fields;
		$this->_numOfFields = $f->Count;
	}
	
	
	 // should only be used to move forward as we normally use forward-only cursors
	function _seek($row)
	{
	   $rs = $this->_queryID; 
		// absoluteposition doesn't work -- my maths is wrong ?
		//	$rs->AbsolutePosition->$row-2;
		//	return true;
		if ($this->_currentRow > $row) return false;
		@$rs->Move((integer)$row - $this->_currentRow-1); //adBookmarkFirst
		return true;
	}
	
/*
	OLEDB types
	
	 enum DBTYPEENUM
	{	DBTYPE_EMPTY	= 0,
	DBTYPE_NULL	= 1,
	DBTYPE_I2	= 2,
	DBTYPE_I4	= 3,
	DBTYPE_R4	= 4,
	DBTYPE_R8	= 5,
	DBTYPE_CY	= 6,
	DBTYPE_DATE	= 7,
	DBTYPE_BSTR	= 8,
	DBTYPE_IDISPATCH	= 9,
	DBTYPE_ERROR	= 10,
	DBTYPE_BOOL	= 11,
	DBTYPE_VARIANT	= 12,
	DBTYPE_IUNKNOWN	= 13,
	DBTYPE_DECIMAL	= 14,
	DBTYPE_UI1	= 17,
	DBTYPE_ARRAY	= 0x2000,
	DBTYPE_BYREF	= 0x4000,
	DBTYPE_I1	= 16,
	DBTYPE_UI2	= 18,
	DBTYPE_UI4	= 19,
	DBTYPE_I8	= 20,
	DBTYPE_UI8	= 21,
	DBTYPE_GUID	= 72,
	DBTYPE_VECTOR	= 0x1000,
	DBTYPE_RESERVED	= 0x8000,
	DBTYPE_BYTES	= 128,
	DBTYPE_STR	= 129,
	DBTYPE_WSTR	= 130,
	DBTYPE_NUMERIC	= 131,
	DBTYPE_UDT	= 132,
	DBTYPE_DBDATE	= 133,
	DBTYPE_DBTIME	= 134,
	DBTYPE_DBTIMESTAMP	= 135
	
	ADO Types
	
   	adEmpty	= 0,
	adTinyInt	= 16,
	adSmallInt	= 2,
	adInteger	= 3,
	adBigInt	= 20,
	adUnsignedTinyInt	= 17,
	adUnsignedSmallInt	= 18,
	adUnsignedInt	= 19,
	adUnsignedBigInt	= 21,
	adSingle	= 4,
	adDouble	= 5,
	adCurrency	= 6,
	adDecimal	= 14,
	adNumeric	= 131,
	adBoolean	= 11,
	adError	= 10,
	adUserDefined	= 132,
	adVariant	= 12,
	adIDispatch	= 9,
	adIUnknown	= 13,	
	adGUID	= 72,
	adDate	= 7,
	adDBDate	= 133,
	adDBTime	= 134,
	adDBTimeStamp	= 135,
	adBSTR	= 8,
	adChar	= 129,
	adVarChar	= 200,
	adLongVarChar	= 201,
	adWChar	= 130,
	adVarWChar	= 202,
	adLongVarWChar	= 203,
	adBinary	= 128,
	adVarBinary	= 204,
	adLongVarBinary	= 205,
	adChapter	= 136,
	adFileTime	= 64,
	adDBFileTime	= 137,
	adPropVariant	= 138,
	adVarNumeric	= 139
*/
	function MetaType($t,$len=-1,$fieldobj=false)
	{
		if (is_object($t)) {
			$fieldobj = $t;
			$t = $fieldobj->type;
			$len = $fieldobj->max_length;
		}
		
		if (!is_numeric($t)) return $t;
		
		switch ($t) {
		case 0:
		case 12: // variant
		case 8: // bstr
		case 129: //char
		case 130: //wc
		case 200: // varc
		case 202:// varWC
		case 128: // bin
		case 204: // varBin
		case 72: // guid
			if ($len <= $this->blobSize) return 'C';
		
		case 201:
		case 203:
			return 'X';
		case 128:
		case 204:
		case 205:
			 return 'B';
		case 7:
		case 133: return 'D';
		
		case 134:
		case 135: return 'T';
		
		case 11: return 'L';
		
		case 16://	adTinyInt	= 16,
		case 2://adSmallInt	= 2,
		case 3://adInteger	= 3,
		case 4://adBigInt	= 20,
		case 17://adUnsignedTinyInt	= 17,
		case 18://adUnsignedSmallInt	= 18,
		case 19://adUnsignedInt	= 19,
		case 20://adUnsignedBigInt	= 21,
			return 'I';
		default: return 'N';
		}
	}
	
	// time stamp not supported yet
	function _fetch()
	{	
		$rs = $this->_queryID;
		if (!$rs or $rs->EOF) {
			$this->fields = false;
			return false;
		}
		$this->fields = array();
	
		if (!$this->_tarr) {
			$tarr = array();
			$flds = array();
			for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
				$f = $rs->Fields($i);
				$flds[] = $f;
				$tarr[] = $f->Type;
			}
			// bind types and flds only once
			$this->_tarr = $tarr; 
			$this->_flds = $flds;
		}
		$t = reset($this->_tarr);
		$f = reset($this->_flds);
		
		if ($this->hideErrors)  $olde = error_reporting(E_ERROR|E_CORE_ERROR);// sometimes $f->value be null
		for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
			//echo "<p>",$t,' ';var_dump($f->value); echo '</p>';
			switch($t) {
			case 135: // timestamp
				if (!strlen((string)$f->value)) $this->fields[] = false;
				else {
					if (!is_numeric($f->value)) # $val = variant_date_to_timestamp($f->value);
						// VT_DATE stores dates as (float) fractional days since 1899/12/30 00:00:00
						$val= (float) variant_cast($f->value,VT_R8)*3600*24-2209161600;
					else 
						$val = $f->value;
					$this->fields[] = adodb_date('Y-m-d H:i:s',$val);
				}
				break;			
			case 133:// A date value (yyyymmdd) 
				if ($val = $f->value) {
					$this->fields[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
				} else
					$this->fields[] = false;
				break;
			case 7: // adDate
				if (!strlen((string)$f->value)) $this->fields[] = false;
				else {
					if (!is_numeric($f->value)) $val = variant_date_to_timestamp($f->value);
					else $val = $f->value;
					
					if (($val % 86400) == 0) $this->fields[] = adodb_date('Y-m-d',$val);
					else $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
				}
				break;
			case 1: // null
				$this->fields[] = false;
				break;
			case 6: // currency is not supported properly;
				ADOConnection::outp( '<b>'.$f->Name.': currency type not supported by PHP</b>');
				$this->fields[] = (float) $f->value;
				break;
			default:
				$this->fields[] = $f->value; 
				break;
			}
			//print " $f->value $t, ";
			$f = next($this->_flds);
			$t = next($this->_tarr);
		} // for
		if ($this->hideErrors) error_reporting($olde);
		@$rs->MoveNext(); // @ needed for some versions of PHP!
		
		if ($this->fetchMode & ADODB_FETCH_ASSOC) {
			$this->fields = &$this->GetRowAssoc(ADODB_ASSOC_CASE);
		}
		return true;
	}
	
		function NextRecordSet()
		{
			$rs = $this->_queryID;
			$this->_queryID = $rs->NextRecordSet();
			//$this->_queryID = $this->_QueryId->NextRecordSet();
			if ($this->_queryID == null) return false;
			
			$this->_currentRow = -1;
			$this->_currentPage = -1;
			$this->bind = false;
			$this->fields = false;
			$this->_flds = false;
			$this->_tarr = false;
			
			$this->_inited = false;
			$this->Init();
			return true;
		}

	function _close() {
		$this->_flds = false;
		@$this->_queryID->Close();// by Pete Dishman (peterd@telephonetics.co.uk)
		$this->_queryID = false;	
	}

}

?>

-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
__[ g a l l e r y - d e v e l ]_________________________

[ list info/archive --> http://gallery.sf.net/lists.php ]
[ gallery info/FAQ/download --> http://gallery.sf.net ]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic