3 V4.94 23 Jan 2007 (c) 2000-2007 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 ($argDatabasename) $argHostname .= ";DATABASE=$argDatabasename";
105 if ($argUsername) $argHostname .= ";$u=$argUsername";
106 if ($argPassword)$argHostname .= ";$p=$argPassword";
108 if ($this->debug
) ADOConnection
::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
109 // @ added below for php 4.0.1 and earlier
110 @$dbc->Open((string) $argHostname);
112 $this->_connectionID
= $dbc;
114 $dbc->CursorLocation
= $this->_cursor_location
;
115 return $dbc->State
> 0;
116 } catch (exception
$e) {
122 // returns true or false
123 function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
125 return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
129 adSchemaCatalogs = 1,
130 adSchemaCharacterSets = 2,
131 adSchemaCollations = 3,
133 adSchemaCheckConstraints = 5,
134 adSchemaConstraintColumnUsage = 6,
135 adSchemaConstraintTableUsage = 7,
136 adSchemaKeyColumnUsage = 8,
137 adSchemaReferentialContraints = 9,
138 adSchemaTableConstraints = 10,
139 adSchemaColumnsDomainUsage = 11,
140 adSchemaIndexes = 12,
141 adSchemaColumnPrivileges = 13,
142 adSchemaTablePrivileges = 14,
143 adSchemaUsagePrivileges = 15,
144 adSchemaProcedures = 16,
145 adSchemaSchemata = 17,
146 adSchemaSQLLanguages = 18,
147 adSchemaStatistics = 19,
149 adSchemaTranslations = 21,
150 adSchemaProviderTypes = 22,
152 adSchemaViewColumnUsage = 24,
153 adSchemaViewTableUsage = 25,
154 adSchemaProcedureParameters = 26,
155 adSchemaForeignKeys = 27,
156 adSchemaPrimaryKeys = 28,
157 adSchemaProcedureColumns = 29,
158 adSchemaDBInfoKeywords = 30,
159 adSchemaDBInfoLiterals = 31,
161 adSchemaDimensions = 33,
162 adSchemaHierarchies = 34,
164 adSchemaMeasures = 36,
165 adSchemaProperties = 37,
170 function &MetaTables()
173 $dbc = $this->_connectionID
;
175 $adors=@$dbc->OpenSchema(20);//tables
177 $f = $adors->Fields(2);//table/view name
178 $t = $adors->Fields(3);//table type
179 while (!$adors->EOF
){
180 $tt=substr($t->value
,0,6);
181 if ($tt!='SYSTEM' && $tt !='ACCESS')
183 //print $f->value . ' ' . $t->value.'<br>';
192 function &MetaColumns($table)
194 $table = strtoupper($table);
196 $dbc = $this->_connectionID
;
198 $adors=@$dbc->OpenSchema(4);//tables
201 $t = $adors->Fields(2);//table/view name
202 while (!$adors->EOF
){
205 if (strtoupper($t->Value
) == $table) {
207 $fld = new ADOFieldObject();
208 $c = $adors->Fields(3);
209 $fld->name
= $c->Value
;
210 $fld->type
= 'CHAR'; // cannot discover type in ADO!
211 $fld->max_length
= -1;
212 $arr[strtoupper($fld->name
)]=$fld;
223 /* returns queryID or false */
224 function &_query($sql,$inputarr=false)
226 try { // In PHP5, all COM errors are exceptions, so to maintain old behaviour...
228 $dbc = $this->_connectionID
;
236 if (!empty($this->charPage
))
237 $oCmd = new COM('ADODB.Command',null,$this->charPage
);
239 $oCmd = new COM('ADODB.Command');
240 $oCmd->ActiveConnection
= $dbc;
241 $oCmd->CommandText
= $sql;
242 $oCmd->CommandType
= 1;
244 foreach($inputarr as $val) {
245 // name, type, direction 1 = input, len,
246 $this->adoParameterType
= 130;
247 $p = $oCmd->CreateParameter('name',$this->adoParameterType
,1,strlen($val),$val);
248 //print $p->Type.' '.$p->value;
249 $oCmd->Parameters
->Append($p);
252 $rs = $oCmd->Execute();
254 if ($dbc->Errors
->Count
> 0) return $false;
258 $rs = @$dbc->Execute($sql,$this->_affectedRows
, $this->_execute_option
);
260 if ($dbc->Errors
->Count
> 0) return $false;
261 if (! $rs) return $false;
263 if ($rs->State
== 0) {
265 return $true; // 0 = adStateClosed means no records returned
269 } catch (exception
$e) {
276 function BeginTrans()
278 if ($this->transOff
) return true;
280 if (isset($this->_thisTransactions
))
281 if (!$this->_thisTransactions
) return false;
283 $o = $this->_connectionID
->Properties("Transaction DDL");
284 $this->_thisTransactions
= $o ?
true : false;
285 if (!$o) return false;
287 @$this->_connectionID
->BeginTrans();
288 $this->transCnt +
= 1;
291 function CommitTrans($ok=true)
293 if (!$ok) return $this->RollbackTrans();
294 if ($this->transOff
) return true;
296 @$this->_connectionID
->CommitTrans();
297 if ($this->transCnt
) @$this->transCnt
-= 1;
300 function RollbackTrans() {
301 if ($this->transOff
) return true;
302 @$this->_connectionID
->RollbackTrans();
303 if ($this->transCnt
) @$this->transCnt
-= 1;
307 /* Returns: the last error message from previous database operation */
311 if (!$this->_connectionID
) return "No connection established";
315 $errc = $this->_connectionID
->Errors
;
316 if (!$errc) return "No Errors object found";
317 if ($errc->Count
== 0) return '';
318 $err = $errc->Item($errc->Count
-1);
319 $errmsg = $err->Description
;
320 }catch(exception
$e) {
327 $errc = $this->_connectionID
->Errors
;
328 if ($errc->Count
== 0) return 0;
329 $err = $errc->Item($errc->Count
-1);
330 return $err->NativeError
;
333 // returns true or false
336 if ($this->_connectionID
) $this->_connectionID
->Close();
337 $this->_connectionID
= false;
344 /*--------------------------------------------------------------------------------------
345 Class Name: Recordset
346 --------------------------------------------------------------------------------------*/
348 class ADORecordSet_ado
extends ADORecordSet
{
351 var $databaseType = "ado";
352 var $dataProvider = "ado";
353 var $_tarr = false; // caches the types
354 var $_flds; // and field objects
356 var $hideErrors = true;
358 function ADORecordSet_ado($id,$mode=false)
360 if ($mode === false) {
361 global $ADODB_FETCH_MODE;
362 $mode = $ADODB_FETCH_MODE;
364 $this->fetchMode
= $mode;
365 return $this->ADORecordSet($id,$mode);
369 // returns the field object
370 function &FetchField($fieldOffset = -1) {
371 $off=$fieldOffset+
1; // offsets begin at 1
373 $o= new ADOFieldObject();
374 $rs = $this->_queryID
;
375 $f = $rs->Fields($fieldOffset);
378 $o->type
= $this->MetaType($t);
379 $o->max_length
= $f->DefinedSize
;
383 //print "off=$off name=$o->name type=$o->type len=$o->max_length<br>";
387 /* Use associative array to get fields array */
388 function Fields($colname)
390 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) return $this->fields
[$colname];
392 $this->bind
= array();
393 for ($i=0; $i < $this->_numOfFields
; $i++
) {
394 $o = $this->FetchField($i);
395 $this->bind
[strtoupper($o->name
)] = $i;
399 return $this->fields
[$this->bind
[strtoupper($colname)]];
405 $rs = $this->_queryID
;
406 $this->_numOfRows
= $rs->RecordCount
;
409 $this->_numOfFields
= $f->Count
;
413 // should only be used to move forward as we normally use forward-only cursors
416 $rs = $this->_queryID
;
417 // absoluteposition doesn't work -- my maths is wrong ?
418 // $rs->AbsolutePosition->$row-2;
420 if ($this->_currentRow
> $row) return false;
421 @$rs->Move((integer)$row - $this->_currentRow
-1); //adBookmarkFirst
438 DBTYPE_IDISPATCH = 9,
442 DBTYPE_IUNKNOWN = 13,
445 DBTYPE_ARRAY = 0x2000,
446 DBTYPE_BYREF = 0x4000,
453 DBTYPE_VECTOR = 0x1000,
454 DBTYPE_RESERVED = 0x8000,
458 DBTYPE_NUMERIC = 131,
462 DBTYPE_DBTIMESTAMP = 135
471 adUnsignedTinyInt = 17,
472 adUnsignedSmallInt = 18,
474 adUnsignedBigInt = 21,
497 adLongVarWChar = 203,
500 adLongVarBinary = 205,
507 function MetaType($t,$len=-1,$fieldobj=false)
511 $t = $fieldobj->type
;
512 $len = $fieldobj->max_length
;
515 if (!is_numeric($t)) return $t;
528 if ($len <= $this->blobSize
) return 'C';
538 case 133: return 'D';
541 case 135: return 'T';
545 case 16:// adTinyInt = 16,
546 case 2://adSmallInt = 2,
547 case 3://adInteger = 3,
548 case 4://adBigInt = 20,
549 case 17://adUnsignedTinyInt = 17,
550 case 18://adUnsignedSmallInt = 18,
551 case 19://adUnsignedInt = 19,
552 case 20://adUnsignedBigInt = 21,
558 // time stamp not supported yet
561 $rs = $this->_queryID
;
562 if (!$rs or $rs->EOF
) {
563 $this->fields
= false;
566 $this->fields
= array();
571 for ($i=0,$max = $this->_numOfFields
; $i < $max; $i++
) {
572 $f = $rs->Fields($i);
576 // bind types and flds only once
577 $this->_tarr
= $tarr;
578 $this->_flds
= $flds;
580 $t = reset($this->_tarr
);
581 $f = reset($this->_flds
);
583 if ($this->hideErrors
) $olde = error_reporting(E_ERROR|E_CORE_ERROR
);// sometimes $f->value be null
584 for ($i=0,$max = $this->_numOfFields
; $i < $max; $i++
) {
585 //echo "<p>",$t,' ';var_dump($f->value); echo '</p>';
587 case 135: // timestamp
588 if (!strlen((string)$f->value
)) $this->fields
[] = false;
590 if (!is_numeric($f->value
)) # $val = variant_date_to_timestamp($f->value);
591 // VT_DATE stores dates as (float) fractional days since 1899/12/30 00:00:00
592 $val= (float) variant_cast($f->value
,VT_R8
)*3600*24-2209161600;
595 $this->fields
[] = adodb_date('Y-m-d H:i:s',$val);
598 case 133:// A date value (yyyymmdd)
599 if ($val = $f->value
) {
600 $this->fields
[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
602 $this->fields
[] = false;
605 if (!strlen((string)$f->value
)) $this->fields
[] = false;
607 if (!is_numeric($f->value
)) $val = variant_date_to_timestamp($f->value
);
608 else $val = $f->value
;
610 if (($val %
86400) == 0) $this->fields
[] = adodb_date('Y-m-d',$val);
611 else $this->fields
[] = adodb_date('Y-m-d H:i:s',$val);
615 $this->fields
[] = false;
617 case 6: // currency is not supported properly;
618 ADOConnection
::outp( '<b>'.$f->Name
.': currency type not supported by PHP</b>');
619 $this->fields
[] = (float) $f->value
;
622 $this->fields
[] = $f->value
;
625 //print " $f->value $t, ";
626 $f = next($this->_flds
);
627 $t = next($this->_tarr
);
629 if ($this->hideErrors
) error_reporting($olde);
630 @$rs->MoveNext(); // @ needed for some versions of PHP!
632 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) {
633 $this->fields
= &$this->GetRowAssoc(ADODB_ASSOC_CASE
);
638 function NextRecordSet()
640 $rs = $this->_queryID
;
641 $this->_queryID
= $rs->NextRecordSet();
642 //$this->_queryID = $this->_QueryId->NextRecordSet();
643 if ($this->_queryID
== null) return false;
645 $this->_currentRow
= -1;
646 $this->_currentPage
= -1;
648 $this->fields
= false;
649 $this->_flds
= false;
650 $this->_tarr
= false;
652 $this->_inited
= false;
658 $this->_flds
= false;
659 @$this->_queryID
->Close();// by Pete Dishman (peterd@telephonetics.co.uk)
660 $this->_queryID
= false;