3 V4.90 8 June 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence.
7 Set tabs to 4 for best viewing.
9 Latest version is available at http://adodb.sourceforge.net
11 Microsoft ADO data driver. Requires ADO. Works only on MS Windows. PHP5 compat version.
14 // security - hide paths
15 if (!defined('ADODB_DIR')) die();
17 define("_ADODB_ADO_LAYER", 1 );
18 /*--------------------------------------------------------------------------------------
19 --------------------------------------------------------------------------------------*/
22 class ADODB_ado
extends ADOConnection
{
23 var $databaseType = "ado";
24 var $_bindInputArray = false;
25 var $fmtDate = "'Y-m-d'";
26 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
27 var $replaceQuote = "''"; // string to use to replace quotes
28 var $dataProvider = "ado";
29 var $hasAffectedRows = true;
30 var $adoParameterType = 201; // 201 = long varchar, 203=long wide varchar, 205 = long varbinary
31 var $_affectedRows = false;
32 var $_thisTransactions;
33 var $_cursor_type = 3; // 3=adOpenStatic,0=adOpenForwardOnly,1=adOpenKeyset,2=adOpenDynamic
34 var $_cursor_location = 3; // 2=adUseServer, 3 = adUseClient;
36 var $_execute_option = -1;
37 var $poorAffectedRows = true;
42 $this->_affectedRows
= new VARIANT
;
47 if (!empty($this->_connectionID
)) $desc = $this->_connectionID
->provider
;
48 return array('description' => $desc, 'version' => '');
51 function _affectedrows()
53 if (PHP_VERSION
>= 5) return $this->_affectedRows
;
55 return $this->_affectedRows
->value
;
58 // you can also pass a connection string like this:
60 // $DB->Connect('USER ID=sa;PASSWORD=pwd;SERVER=mangrove;DATABASE=ai',false,false,'SQLOLEDB');
61 function _connect($argHostname, $argUsername, $argPassword,$argDBorProvider, $argProvider= '')
64 // - if $argProvider is empty, we assume that $argDBorProvider holds provider -- this is for backward compat
65 // - if $argProvider is not empty, then $argDBorProvider holds db
69 $argDatabasename = $argDBorProvider;
71 $argDatabasename = '';
72 if ($argDBorProvider) $argProvider = $argDBorProvider;
73 else $argProvider = 'MSDASQL';
81 if (!empty($this->charPage
))
82 $dbc = new COM('ADODB.Connection',null,$this->charPage
);
84 $dbc = new COM('ADODB.Connection');
86 if (! $dbc) return false;
88 /* special support if provider is mssql or access */
89 if ($argProvider=='mssql') {
90 $u = 'User Id'; //User parameter name for OLEDB
92 $argProvider = "SQLOLEDB"; // SQL Server Provider
95 //if ($argDatabasename) $argHostname .= ";Initial Catalog=$argDatabasename";
97 //use trusted conection for SQL if username not specified
98 if (!$argUsername) $argHostname .= ";Trusted_Connection=Yes";
99 } else if ($argProvider=='access')
100 $argProvider = "Microsoft.Jet.OLEDB.4.0"; // Microsoft Jet Provider
102 if ($argProvider) $dbc->Provider
= $argProvider;
104 if ($argProvider) $argHostname = "PROVIDER=$argProvider;DRIVER={SQL Server};SERVER=$argHostname";
107 if ($argDatabasename) $argHostname .= ";DATABASE=$argDatabasename";
108 if ($argUsername) $argHostname .= ";$u=$argUsername";
109 if ($argPassword)$argHostname .= ";$p=$argPassword";
111 if ($this->debug
) ADOConnection
::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
112 // @ added below for php 4.0.1 and earlier
113 @$dbc->Open((string) $argHostname);
115 $this->_connectionID
= $dbc;
117 $dbc->CursorLocation
= $this->_cursor_location
;
118 return $dbc->State
> 0;
119 } catch (exception
$e) {
125 // returns true or false
126 function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
128 return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
132 adSchemaCatalogs = 1,
133 adSchemaCharacterSets = 2,
134 adSchemaCollations = 3,
136 adSchemaCheckConstraints = 5,
137 adSchemaConstraintColumnUsage = 6,
138 adSchemaConstraintTableUsage = 7,
139 adSchemaKeyColumnUsage = 8,
140 adSchemaReferentialContraints = 9,
141 adSchemaTableConstraints = 10,
142 adSchemaColumnsDomainUsage = 11,
143 adSchemaIndexes = 12,
144 adSchemaColumnPrivileges = 13,
145 adSchemaTablePrivileges = 14,
146 adSchemaUsagePrivileges = 15,
147 adSchemaProcedures = 16,
148 adSchemaSchemata = 17,
149 adSchemaSQLLanguages = 18,
150 adSchemaStatistics = 19,
152 adSchemaTranslations = 21,
153 adSchemaProviderTypes = 22,
155 adSchemaViewColumnUsage = 24,
156 adSchemaViewTableUsage = 25,
157 adSchemaProcedureParameters = 26,
158 adSchemaForeignKeys = 27,
159 adSchemaPrimaryKeys = 28,
160 adSchemaProcedureColumns = 29,
161 adSchemaDBInfoKeywords = 30,
162 adSchemaDBInfoLiterals = 31,
164 adSchemaDimensions = 33,
165 adSchemaHierarchies = 34,
167 adSchemaMeasures = 36,
168 adSchemaProperties = 37,
173 function &MetaTables()
176 $dbc = $this->_connectionID
;
178 $adors=@$dbc->OpenSchema(20);//tables
180 $f = $adors->Fields(2);//table/view name
181 $t = $adors->Fields(3);//table type
182 while (!$adors->EOF
){
183 $tt=substr($t->value
,0,6);
184 if ($tt!='SYSTEM' && $tt !='ACCESS')
186 //print $f->value . ' ' . $t->value.'<br>';
195 function &MetaColumns($table)
197 $table = strtoupper($table);
199 $dbc = $this->_connectionID
;
201 $adors=@$dbc->OpenSchema(4);//tables
204 $t = $adors->Fields(2);//table/view name
205 while (!$adors->EOF
){
208 if (strtoupper($t->Value
) == $table) {
210 $fld = new ADOFieldObject();
211 $c = $adors->Fields(3);
212 $fld->name
= $c->Value
;
213 $fld->type
= 'CHAR'; // cannot discover type in ADO!
214 $fld->max_length
= -1;
215 $arr[strtoupper($fld->name
)]=$fld;
226 /* returns queryID or false */
227 function &_query($sql,$inputarr=false)
229 try { // In PHP5, all COM errors are exceptions, so to maintain old behaviour...
231 $dbc = $this->_connectionID
;
239 if (!empty($this->charPage
))
240 $oCmd = new COM('ADODB.Command',null,$this->charPage
);
242 $oCmd = new COM('ADODB.Command');
243 $oCmd->ActiveConnection
= $dbc;
244 $oCmd->CommandText
= $sql;
245 $oCmd->CommandType
= 1;
247 foreach($inputarr as $val) {
248 // name, type, direction 1 = input, len,
249 $this->adoParameterType
= 130;
250 $p = $oCmd->CreateParameter('name',$this->adoParameterType
,1,strlen($val),$val);
251 //print $p->Type.' '.$p->value;
252 $oCmd->Parameters
->Append($p);
255 $rs = $oCmd->Execute();
257 if ($dbc->Errors
->Count
> 0) return $false;
261 $rs = @$dbc->Execute($sql,$this->_affectedRows
, $this->_execute_option
);
263 if ($dbc->Errors
->Count
> 0) return $false;
264 if (! $rs) return $false;
266 if ($rs->State
== 0) {
268 return $true; // 0 = adStateClosed means no records returned
272 } catch (exception
$e) {
279 function BeginTrans()
281 if ($this->transOff
) return true;
283 if (isset($this->_thisTransactions
))
284 if (!$this->_thisTransactions
) return false;
286 $o = $this->_connectionID
->Properties("Transaction DDL");
287 $this->_thisTransactions
= $o ?
true : false;
288 if (!$o) return false;
290 @$this->_connectionID
->BeginTrans();
291 $this->transCnt +
= 1;
294 function CommitTrans($ok=true)
296 if (!$ok) return $this->RollbackTrans();
297 if ($this->transOff
) return true;
299 @$this->_connectionID
->CommitTrans();
300 if ($this->transCnt
) @$this->transCnt
-= 1;
303 function RollbackTrans() {
304 if ($this->transOff
) return true;
305 @$this->_connectionID
->RollbackTrans();
306 if ($this->transCnt
) @$this->transCnt
-= 1;
310 /* Returns: the last error message from previous database operation */
314 if (!$this->_connectionID
) return "No connection established";
318 $errc = $this->_connectionID
->Errors
;
319 if (!$errc) return "No Errors object found";
320 if ($errc->Count
== 0) return '';
321 $err = $errc->Item($errc->Count
-1);
322 $errmsg = $err->Description
;
323 }catch(exception
$e) {
330 $errc = $this->_connectionID
->Errors
;
331 if ($errc->Count
== 0) return 0;
332 $err = $errc->Item($errc->Count
-1);
333 return $err->NativeError
;
336 // returns true or false
339 if ($this->_connectionID
) $this->_connectionID
->Close();
340 $this->_connectionID
= false;
347 /*--------------------------------------------------------------------------------------
348 Class Name: Recordset
349 --------------------------------------------------------------------------------------*/
351 class ADORecordSet_ado
extends ADORecordSet
{
354 var $databaseType = "ado";
355 var $dataProvider = "ado";
356 var $_tarr = false; // caches the types
357 var $_flds; // and field objects
359 var $hideErrors = true;
361 function ADORecordSet_ado($id,$mode=false)
363 if ($mode === false) {
364 global $ADODB_FETCH_MODE;
365 $mode = $ADODB_FETCH_MODE;
367 $this->fetchMode
= $mode;
368 return $this->ADORecordSet($id,$mode);
372 // returns the field object
373 function &FetchField($fieldOffset = -1) {
374 $off=$fieldOffset+
1; // offsets begin at 1
376 $o= new ADOFieldObject();
377 $rs = $this->_queryID
;
378 $f = $rs->Fields($fieldOffset);
381 $o->type
= $this->MetaType($t);
382 $o->max_length
= $f->DefinedSize
;
386 //print "off=$off name=$o->name type=$o->type len=$o->max_length<br>";
390 /* Use associative array to get fields array */
391 function Fields($colname)
393 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) return $this->fields
[$colname];
395 $this->bind
= array();
396 for ($i=0; $i < $this->_numOfFields
; $i++
) {
397 $o = $this->FetchField($i);
398 $this->bind
[strtoupper($o->name
)] = $i;
402 return $this->fields
[$this->bind
[strtoupper($colname)]];
408 $rs = $this->_queryID
;
409 $this->_numOfRows
= $rs->RecordCount
;
412 $this->_numOfFields
= $f->Count
;
416 // should only be used to move forward as we normally use forward-only cursors
419 $rs = $this->_queryID
;
420 // absoluteposition doesn't work -- my maths is wrong ?
421 // $rs->AbsolutePosition->$row-2;
423 if ($this->_currentRow
> $row) return false;
424 @$rs->Move((integer)$row - $this->_currentRow
-1); //adBookmarkFirst
441 DBTYPE_IDISPATCH = 9,
445 DBTYPE_IUNKNOWN = 13,
448 DBTYPE_ARRAY = 0x2000,
449 DBTYPE_BYREF = 0x4000,
456 DBTYPE_VECTOR = 0x1000,
457 DBTYPE_RESERVED = 0x8000,
461 DBTYPE_NUMERIC = 131,
465 DBTYPE_DBTIMESTAMP = 135
474 adUnsignedTinyInt = 17,
475 adUnsignedSmallInt = 18,
477 adUnsignedBigInt = 21,
500 adLongVarWChar = 203,
503 adLongVarBinary = 205,
510 function MetaType($t,$len=-1,$fieldobj=false)
514 $t = $fieldobj->type
;
515 $len = $fieldobj->max_length
;
518 if (!is_numeric($t)) return $t;
531 if ($len <= $this->blobSize
) return 'C';
541 case 133: return 'D';
544 case 135: return 'T';
548 case 16:// adTinyInt = 16,
549 case 2://adSmallInt = 2,
550 case 3://adInteger = 3,
551 case 4://adBigInt = 20,
552 case 17://adUnsignedTinyInt = 17,
553 case 18://adUnsignedSmallInt = 18,
554 case 19://adUnsignedInt = 19,
555 case 20://adUnsignedBigInt = 21,
561 // time stamp not supported yet
564 $rs = $this->_queryID
;
565 if (!$rs or $rs->EOF
) {
566 $this->fields
= false;
569 $this->fields
= array();
574 for ($i=0,$max = $this->_numOfFields
; $i < $max; $i++
) {
575 $f = $rs->Fields($i);
579 // bind types and flds only once
580 $this->_tarr
= $tarr;
581 $this->_flds
= $flds;
583 $t = reset($this->_tarr
);
584 $f = reset($this->_flds
);
586 if ($this->hideErrors
) $olde = error_reporting(E_ERROR|E_CORE_ERROR
);// sometimes $f->value be null
587 for ($i=0,$max = $this->_numOfFields
; $i < $max; $i++
) {
588 //echo "<p>",$t,' ';var_dump($f->value); echo '</p>';
590 case 135: // timestamp
591 if (!strlen((string)$f->value
)) $this->fields
[] = false;
593 if (!is_numeric($f->value
)) # $val = variant_date_to_timestamp($f->value);
594 // VT_DATE stores dates as (float) fractional days since 1899/12/30 00:00:00
595 $val= (float) variant_cast($f->value
,VT_R8
)*3600*24-2209161600;
598 $this->fields
[] = adodb_date('Y-m-d H:i:s',$val);
601 case 133:// A date value (yyyymmdd)
602 if ($val = $f->value
) {
603 $this->fields
[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
605 $this->fields
[] = false;
608 if (!strlen((string)$f->value
)) $this->fields
[] = false;
610 if (!is_numeric($f->value
)) $val = variant_date_to_timestamp($f->value
);
611 else $val = $f->value
;
613 if (($val %
86400) == 0) $this->fields
[] = adodb_date('Y-m-d',$val);
614 else $this->fields
[] = adodb_date('Y-m-d H:i:s',$val);
618 $this->fields
[] = false;
620 case 6: // currency is not supported properly;
621 ADOConnection
::outp( '<b>'.$f->Name
.': currency type not supported by PHP</b>');
622 $this->fields
[] = (float) $f->value
;
625 $this->fields
[] = $f->value
;
628 //print " $f->value $t, ";
629 $f = next($this->_flds
);
630 $t = next($this->_tarr
);
632 if ($this->hideErrors
) error_reporting($olde);
633 @$rs->MoveNext(); // @ needed for some versions of PHP!
635 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) {
636 $this->fields
= &$this->GetRowAssoc(ADODB_ASSOC_CASE
);
641 function NextRecordSet()
643 $rs = $this->_queryID
;
644 $this->_queryID
= $rs->NextRecordSet();
645 //$this->_queryID = $this->_QueryId->NextRecordSet();
646 if ($this->_queryID
== null) return false;
648 $this->_currentRow
= -1;
649 $this->_currentPage
= -1;
651 $this->fields
= false;
652 $this->_flds
= false;
653 $this->_tarr
= false;
655 $this->_inited
= false;
661 $this->_flds
= false;
662 @$this->_queryID
->Close();// by Pete Dishman (peterd@telephonetics.co.uk)
663 $this->_queryID
= false;