3 V4.98 13 Feb 2008 (c) 2000-2008 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.
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, $argProvider= 'MSDASQL')
66 if (!empty($this->charPage
))
67 $dbc = new COM('ADODB.Connection',null,$this->charPage
);
69 $dbc = new COM('ADODB.Connection');
71 if (! $dbc) return false;
73 /* special support if provider is mssql or access */
74 if ($argProvider=='mssql') {
75 $u = 'User Id'; //User parameter name for OLEDB
77 $argProvider = "SQLOLEDB"; // SQL Server Provider
80 //if ($argDatabasename) $argHostname .= ";Initial Catalog=$argDatabasename";
82 //use trusted conection for SQL if username not specified
83 if (!$argUsername) $argHostname .= ";Trusted_Connection=Yes";
84 } else if ($argProvider=='access')
85 $argProvider = "Microsoft.Jet.OLEDB.4.0"; // Microsoft Jet Provider
87 if ($argProvider) $dbc->Provider
= $argProvider;
89 if ($argUsername) $argHostname .= ";$u=$argUsername";
90 if ($argPassword)$argHostname .= ";$p=$argPassword";
92 if ($this->debug
) ADOConnection
::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
93 // @ added below for php 4.0.1 and earlier
94 @$dbc->Open((string) $argHostname);
96 $this->_connectionID
= $dbc;
98 $dbc->CursorLocation
= $this->_cursor_location
;
99 return $dbc->State
> 0;
102 // returns true or false
103 function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
105 return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
109 adSchemaCatalogs = 1,
110 adSchemaCharacterSets = 2,
111 adSchemaCollations = 3,
113 adSchemaCheckConstraints = 5,
114 adSchemaConstraintColumnUsage = 6,
115 adSchemaConstraintTableUsage = 7,
116 adSchemaKeyColumnUsage = 8,
117 adSchemaReferentialContraints = 9,
118 adSchemaTableConstraints = 10,
119 adSchemaColumnsDomainUsage = 11,
120 adSchemaIndexes = 12,
121 adSchemaColumnPrivileges = 13,
122 adSchemaTablePrivileges = 14,
123 adSchemaUsagePrivileges = 15,
124 adSchemaProcedures = 16,
125 adSchemaSchemata = 17,
126 adSchemaSQLLanguages = 18,
127 adSchemaStatistics = 19,
129 adSchemaTranslations = 21,
130 adSchemaProviderTypes = 22,
132 adSchemaViewColumnUsage = 24,
133 adSchemaViewTableUsage = 25,
134 adSchemaProcedureParameters = 26,
135 adSchemaForeignKeys = 27,
136 adSchemaPrimaryKeys = 28,
137 adSchemaProcedureColumns = 29,
138 adSchemaDBInfoKeywords = 30,
139 adSchemaDBInfoLiterals = 31,
141 adSchemaDimensions = 33,
142 adSchemaHierarchies = 34,
144 adSchemaMeasures = 36,
145 adSchemaProperties = 37,
150 function &MetaTables()
153 $dbc = $this->_connectionID
;
155 $adors=@$dbc->OpenSchema(20);//tables
157 $f = $adors->Fields(2);//table/view name
158 $t = $adors->Fields(3);//table type
159 while (!$adors->EOF
){
160 $tt=substr($t->value
,0,6);
161 if ($tt!='SYSTEM' && $tt !='ACCESS')
163 //print $f->value . ' ' . $t->value.'<br>';
172 function &MetaColumns($table)
174 $table = strtoupper($table);
176 $dbc = $this->_connectionID
;
178 $adors=@$dbc->OpenSchema(4);//tables
181 $t = $adors->Fields(2);//table/view name
182 while (!$adors->EOF
){
185 if (strtoupper($t->Value
) == $table) {
187 $fld = new ADOFieldObject();
188 $c = $adors->Fields(3);
189 $fld->name
= $c->Value
;
190 $fld->type
= 'CHAR'; // cannot discover type in ADO!
191 $fld->max_length
= -1;
192 $arr[strtoupper($fld->name
)]=$fld;
200 return empty($arr) ?
$false : $arr;
206 /* returns queryID or false */
207 function &_query($sql,$inputarr=false)
210 $dbc = $this->_connectionID
;
216 if (!empty($this->charPage
))
217 $oCmd = new COM('ADODB.Command',null,$this->charPage
);
219 $oCmd = new COM('ADODB.Command');
220 $oCmd->ActiveConnection
= $dbc;
221 $oCmd->CommandText
= $sql;
222 $oCmd->CommandType
= 1;
224 foreach($inputarr as $val) {
225 // name, type, direction 1 = input, len,
226 $this->adoParameterType
= 130;
227 $p = $oCmd->CreateParameter('name',$this->adoParameterType
,1,strlen($val),$val);
228 //print $p->Type.' '.$p->value;
229 $oCmd->Parameters
->Append($p);
232 $rs = $oCmd->Execute();
234 if ($dbc->Errors
->Count
> 0) return $false;
238 $rs = @$dbc->Execute($sql,$this->_affectedRows
, $this->_execute_option
);
240 if ($dbc->Errors
->Count
> 0) return $false;
241 if (! $rs) return $false;
243 if ($rs->State
== 0) {
245 return $true; // 0 = adStateClosed means no records returned
251 function BeginTrans()
253 if ($this->transOff
) return true;
255 if (isset($this->_thisTransactions
))
256 if (!$this->_thisTransactions
) return false;
258 $o = $this->_connectionID
->Properties("Transaction DDL");
259 $this->_thisTransactions
= $o ?
true : false;
260 if (!$o) return false;
262 @$this->_connectionID
->BeginTrans();
263 $this->transCnt +
= 1;
267 function CommitTrans($ok=true)
269 if (!$ok) return $this->RollbackTrans();
270 if ($this->transOff
) return true;
272 @$this->_connectionID
->CommitTrans();
273 if ($this->transCnt
) @$this->transCnt
-= 1;
276 function RollbackTrans() {
277 if ($this->transOff
) return true;
278 @$this->_connectionID
->RollbackTrans();
279 if ($this->transCnt
) @$this->transCnt
-= 1;
283 /* Returns: the last error message from previous database operation */
287 if (!$this->_connectionID
) return "No connection established";
288 $errc = $this->_connectionID
->Errors
;
289 if (!$errc) return "No Errors object found";
290 if ($errc->Count
== 0) return '';
291 $err = $errc->Item($errc->Count
-1);
292 return $err->Description
;
297 $errc = $this->_connectionID
->Errors
;
298 if ($errc->Count
== 0) return 0;
299 $err = $errc->Item($errc->Count
-1);
300 return $err->NativeError
;
303 // returns true or false
306 if ($this->_connectionID
) $this->_connectionID
->Close();
307 $this->_connectionID
= false;
314 /*--------------------------------------------------------------------------------------
315 Class Name: Recordset
316 --------------------------------------------------------------------------------------*/
318 class ADORecordSet_ado
extends ADORecordSet
{
321 var $databaseType = "ado";
322 var $dataProvider = "ado";
323 var $_tarr = false; // caches the types
324 var $_flds; // and field objects
326 var $hideErrors = true;
328 function ADORecordSet_ado($id,$mode=false)
330 if ($mode === false) {
331 global $ADODB_FETCH_MODE;
332 $mode = $ADODB_FETCH_MODE;
334 $this->fetchMode
= $mode;
335 return $this->ADORecordSet($id,$mode);
339 // returns the field object
340 function &FetchField($fieldOffset = -1) {
341 $off=$fieldOffset+
1; // offsets begin at 1
343 $o= new ADOFieldObject();
344 $rs = $this->_queryID
;
345 $f = $rs->Fields($fieldOffset);
348 $o->type
= $this->MetaType($t);
349 $o->max_length
= $f->DefinedSize
;
352 //print "off=$off name=$o->name type=$o->type len=$o->max_length<br>";
356 /* Use associative array to get fields array */
357 function Fields($colname)
359 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) return $this->fields
[$colname];
361 $this->bind
= array();
362 for ($i=0; $i < $this->_numOfFields
; $i++
) {
363 $o = $this->FetchField($i);
364 $this->bind
[strtoupper($o->name
)] = $i;
368 return $this->fields
[$this->bind
[strtoupper($colname)]];
374 $rs = $this->_queryID
;
375 $this->_numOfRows
= $rs->RecordCount
;
378 $this->_numOfFields
= $f->Count
;
382 // should only be used to move forward as we normally use forward-only cursors
385 $rs = $this->_queryID
;
386 // absoluteposition doesn't work -- my maths is wrong ?
387 // $rs->AbsolutePosition->$row-2;
389 if ($this->_currentRow
> $row) return false;
390 @$rs->Move((integer)$row - $this->_currentRow
-1); //adBookmarkFirst
407 DBTYPE_IDISPATCH = 9,
411 DBTYPE_IUNKNOWN = 13,
414 DBTYPE_ARRAY = 0x2000,
415 DBTYPE_BYREF = 0x4000,
422 DBTYPE_VECTOR = 0x1000,
423 DBTYPE_RESERVED = 0x8000,
427 DBTYPE_NUMERIC = 131,
431 DBTYPE_DBTIMESTAMP = 135
440 adUnsignedTinyInt = 17,
441 adUnsignedSmallInt = 18,
443 adUnsignedBigInt = 21,
466 adLongVarWChar = 203,
469 adLongVarBinary = 205,
476 function MetaType($t,$len=-1,$fieldobj=false)
480 $t = $fieldobj->type
;
481 $len = $fieldobj->max_length
;
484 if (!is_numeric($t)) return $t;
497 if ($len <= $this->blobSize
) return 'C';
507 case 133: return 'D';
510 case 135: return 'T';
514 case 16:// adTinyInt = 16,
515 case 2://adSmallInt = 2,
516 case 3://adInteger = 3,
517 case 4://adBigInt = 20,
518 case 17://adUnsignedTinyInt = 17,
519 case 18://adUnsignedSmallInt = 18,
520 case 19://adUnsignedInt = 19,
521 case 20://adUnsignedBigInt = 21,
527 // time stamp not supported yet
530 $rs = $this->_queryID
;
531 if (!$rs or $rs->EOF
) {
532 $this->fields
= false;
535 $this->fields
= array();
540 for ($i=0,$max = $this->_numOfFields
; $i < $max; $i++
) {
541 $f = $rs->Fields($i);
545 // bind types and flds only once
546 $this->_tarr
= $tarr;
547 $this->_flds
= $flds;
549 $t = reset($this->_tarr
);
550 $f = reset($this->_flds
);
552 if ($this->hideErrors
) $olde = error_reporting(E_ERROR|E_CORE_ERROR
);// sometimes $f->value be null
553 for ($i=0,$max = $this->_numOfFields
; $i < $max; $i++
) {
554 //echo "<p>",$t,' ';var_dump($f->value); echo '</p>';
556 case 135: // timestamp
557 if (!strlen((string)$f->value
)) $this->fields
[] = false;
559 if (!is_numeric($f->value
)) # $val = variant_date_to_timestamp($f->value);
560 // VT_DATE stores dates as (float) fractional days since 1899/12/30 00:00:00
561 $val=(float) variant_cast($f->value
,VT_R8
)*3600*24-2209161600;
564 $this->fields
[] = adodb_date('Y-m-d H:i:s',$val);
567 case 133:// A date value (yyyymmdd)
568 if ($val = $f->value
) {
569 $this->fields
[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
571 $this->fields
[] = false;
574 if (!strlen((string)$f->value
)) $this->fields
[] = false;
576 if (!is_numeric($f->value
)) $val = variant_date_to_timestamp($f->value
);
577 else $val = $f->value
;
579 if (($val %
86400) == 0) $this->fields
[] = adodb_date('Y-m-d',$val);
580 else $this->fields
[] = adodb_date('Y-m-d H:i:s',$val);
584 $this->fields
[] = false;
586 case 6: // currency is not supported properly;
587 ADOConnection
::outp( '<b>'.$f->Name
.': currency type not supported by PHP</b>');
588 $this->fields
[] = (float) $f->value
;
592 if(is_bool($f->value
)) {
593 if($f->value
==true) $val = 1;
596 if(is_null($f->value
)) $val = null;
598 $this->fields
[] = $val;
602 $this->fields
[] = $f->value
;
605 //print " $f->value $t, ";
606 $f = next($this->_flds
);
607 $t = next($this->_tarr
);
609 if ($this->hideErrors
) error_reporting($olde);
610 @$rs->MoveNext(); // @ needed for some versions of PHP!
612 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) {
613 $this->fields
= &$this->GetRowAssoc(ADODB_ASSOC_CASE
);
618 function NextRecordSet()
620 $rs = $this->_queryID
;
621 $this->_queryID
= $rs->NextRecordSet();
622 //$this->_queryID = $this->_QueryId->NextRecordSet();
623 if ($this->_queryID
== null) return false;
625 $this->_currentRow
= -1;
626 $this->_currentPage
= -1;
628 $this->fields
= false;
629 $this->_flds
= false;
630 $this->_tarr
= false;
632 $this->_inited
= false;
638 $this->_flds
= false;
639 @$this->_queryID
->Close();// by Pete Dishman (peterd@telephonetics.co.uk)
640 $this->_queryID
= false;