3 V4.22 15 Apr 2004 (c) 2000-2004 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://php.weblogs.com/
11 Microsoft ADO data driver. Requires ADO. Works only on MS Windows.
13 define("_ADODB_ADO_LAYER", 1 );
14 /*--------------------------------------------------------------------------------------
15 --------------------------------------------------------------------------------------*/
17 class ADODB_ado extends ADOConnection {
18 var $databaseType = "ado";
19 var $_bindInputArray = false;
20 var $fmtDate = "'Y-m-d'";
21 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
22 var $replaceQuote = "''"; // string to use to replace quotes
23 var $dataProvider = "ado";
24 var $hasAffectedRows = true;
25 var $adoParameterType = 201; // 201 = long varchar, 203=long wide varchar, 205 = long varbinary
26 var $_affectedRows = false;
27 var $_thisTransactions;
28 var $_cursor_type = 3; // 3=adOpenStatic,0=adOpenForwardOnly,1=adOpenKeyset,2=adOpenDynamic
29 var $_cursor_location = 3; // 2=adUseServer, 3 = adUseClient;
31 var $_execute_option = -1;
32 var $poorAffectedRows = true;
37 $this->_affectedRows = new VARIANT;
42 if (!empty($this->_connectionID)) $desc = $this->_connectionID->provider;
43 return array('description' => $desc, 'version' => '');
46 function _affectedrows()
48 if (PHP_VERSION >= 5) return $this->_affectedRows;
50 return $this->_affectedRows->value;
53 // you can also pass a connection string like this:
55 // $DB->Connect('USER ID=sa;PASSWORD=pwd;SERVER=mangrove;DATABASE=ai',false,false,'SQLOLEDB');
56 function _connect($argHostname, $argUsername, $argPassword, $argProvider= 'MSDASQL')
61 if (!empty($this->charPage))
62 $dbc = new COM('ADODB.Connection',null,$this->charPage);
64 $dbc = new COM('ADODB.Connection');
66 if (! $dbc) return false;
68 /* special support if provider is mssql or access */
69 if ($argProvider=='mssql') {
70 $u = 'User Id'; //User parameter name for OLEDB
72 $argProvider = "SQLOLEDB"; // SQL Server Provider
75 //if ($argDatabasename) $argHostname .= ";Initial Catalog=$argDatabasename";
77 //use trusted conection for SQL if username not specified
78 if (!$argUsername) $argHostname .= ";Trusted_Connection=Yes";
79 } else if ($argProvider=='access')
80 $argProvider = "Microsoft.Jet.OLEDB.4.0"; // Microsoft Jet Provider
82 if ($argProvider) $dbc->Provider = $argProvider;
84 if ($argUsername) $argHostname .= ";$u=$argUsername";
85 if ($argPassword)$argHostname .= ";$p=$argPassword";
87 if ($this->debug) ADOConnection::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
88 // @ added below for php 4.0.1 and earlier
89 @$dbc->Open((string) $argHostname);
91 $this->_connectionID = $dbc;
93 $dbc->CursorLocation = $this->_cursor_location;
94 return $dbc->State > 0;
97 // returns true or false
98 function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
100 return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
104 adSchemaCatalogs = 1,
105 adSchemaCharacterSets = 2,
106 adSchemaCollations = 3,
108 adSchemaCheckConstraints = 5,
109 adSchemaConstraintColumnUsage = 6,
110 adSchemaConstraintTableUsage = 7,
111 adSchemaKeyColumnUsage = 8,
112 adSchemaReferentialContraints = 9,
113 adSchemaTableConstraints = 10,
114 adSchemaColumnsDomainUsage = 11,
115 adSchemaIndexes = 12,
116 adSchemaColumnPrivileges = 13,
117 adSchemaTablePrivileges = 14,
118 adSchemaUsagePrivileges = 15,
119 adSchemaProcedures = 16,
120 adSchemaSchemata = 17,
121 adSchemaSQLLanguages = 18,
122 adSchemaStatistics = 19,
124 adSchemaTranslations = 21,
125 adSchemaProviderTypes = 22,
127 adSchemaViewColumnUsage = 24,
128 adSchemaViewTableUsage = 25,
129 adSchemaProcedureParameters = 26,
130 adSchemaForeignKeys = 27,
131 adSchemaPrimaryKeys = 28,
132 adSchemaProcedureColumns = 29,
133 adSchemaDBInfoKeywords = 30,
134 adSchemaDBInfoLiterals = 31,
136 adSchemaDimensions = 33,
137 adSchemaHierarchies = 34,
139 adSchemaMeasures = 36,
140 adSchemaProperties = 37,
145 function &MetaTables()
148 $dbc = $this->_connectionID;
150 $adors=@$dbc->OpenSchema(20);//tables
152 $f = $adors->Fields(2);//table/view name
153 $t = $adors->Fields(3);//table type
154 while (!$adors->EOF){
155 $tt=substr($t->value,0,6);
156 if ($tt!='SYSTEM' && $tt !='ACCESS')
158 //print $f->value . ' ' . $t->value.'<br>';
167 function &MetaColumns($table)
169 $table = strtoupper($table);
171 $dbc = $this->_connectionID;
173 $adors=@$dbc->OpenSchema(4);//tables
176 $t = $adors->Fields(2);//table/view name
177 while (!$adors->EOF){
180 if (strtoupper($t->Value) == $table) {
182 $fld = new ADOFieldObject();
183 $c = $adors->Fields(3);
184 $fld->name = $c->Value;
185 $fld->type = 'CHAR'; // cannot discover type in ADO!
186 $fld->max_length = -1;
187 $arr[strtoupper($fld->name)]=$fld;
201 /* returns queryID or false */
202 function &_query($sql,$inputarr=false)
205 $dbc = $this->_connectionID;
210 if (!empty($this->charPage))
211 $oCmd = new COM('ADODB.Command',null,$this->charPage);
213 $oCmd = new COM('ADODB.Command');
214 $oCmd->ActiveConnection = $dbc;
215 $oCmd->CommandText = $sql;
216 $oCmd->CommandType = 1;
218 foreach($inputarr as $val) {
219 // name, type, direction 1 = input, len,
220 $this->adoParameterType = 130;
221 $p = $oCmd->CreateParameter('name',$this->adoParameterType,1,strlen($val),$val);
222 //print $p->Type.' '.$p->value;
223 $oCmd->Parameters->Append($p);
226 $rs = $oCmd->Execute();
228 if ($dbc->Errors->Count > 0) return false;
232 $rs = @$dbc->Execute($sql,$this->_affectedRows, $this->_execute_option);
234 $rs = new COM('ADODB.Recordset');
236 $rs->Open ($sql, $dbc, $this->_cursor_type,$this->_lock_type, $this->_execute_option);
239 if ($dbc->Errors->Count > 0) return false;
240 if (! $rs) return false;
242 if ($rs->State == 0) return true; // 0 = adStateClosed means no records returned
247 function BeginTrans()
249 if ($this->transOff) return true;
251 if (isset($this->_thisTransactions))
252 if (!$this->_thisTransactions) return false;
254 $o = $this->_connectionID->Properties("Transaction DDL");
255 $this->_thisTransactions = $o ? true : false;
256 if (!$o) return false;
258 @$this->_connectionID->BeginTrans();
259 $this->transCnt += 1;
262 function CommitTrans($ok=true)
264 if (!$ok) return $this->RollbackTrans();
265 if ($this->transOff) return true;
267 @$this->_connectionID->CommitTrans();
268 if ($this->transCnt) @$this->transCnt -= 1;
271 function RollbackTrans() {
272 if ($this->transOff) return true;
273 @$this->_connectionID->RollbackTrans();
274 if ($this->transCnt) @$this->transCnt -= 1;
278 /* Returns: the last error message from previous database operation */
282 $errc = $this->_connectionID->Errors;
283 if ($errc->Count == 0) return '';
284 $err = $errc->Item($errc->Count-1);
285 return $err->Description;
290 $errc = $this->_connectionID->Errors;
291 if ($errc->Count == 0) return 0;
292 $err = $errc->Item($errc->Count-1);
293 return $err->NativeError;
296 // returns true or false
299 if ($this->_connectionID) $this->_connectionID->Close();
300 $this->_connectionID = false;
307 /*--------------------------------------------------------------------------------------
308 Class Name: Recordset
309 --------------------------------------------------------------------------------------*/
311 class ADORecordSet_ado extends ADORecordSet {
314 var $databaseType = "ado";
315 var $dataProvider = "ado";
316 var $_tarr = false; // caches the types
317 var $_flds; // and field objects
319 var $hideErrors = true;
321 function ADORecordSet_ado($id,$mode=false)
323 if ($mode === false) {
324 global $ADODB_FETCH_MODE;
325 $mode = $ADODB_FETCH_MODE;
327 $this->fetchMode = $mode;
328 return $this->ADORecordSet($id,$mode);
332 // returns the field object
333 function FetchField($fieldOffset = -1) {
334 $off=$fieldOffset+1; // offsets begin at 1
336 $o= new ADOFieldObject();
337 $rs = $this->_queryID;
338 $f = $rs->Fields($fieldOffset);
341 $o->type = $this->MetaType($t);
342 $o->max_length = $f->DefinedSize;
346 //print "off=$off name=$o->name type=$o->type len=$o->max_length<br>";
350 /* Use associative array to get fields array */
351 function Fields($colname)
353 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
355 $this->bind = array();
356 for ($i=0; $i < $this->_numOfFields; $i++) {
357 $o = $this->FetchField($i);
358 $this->bind[strtoupper($o->name)] = $i;
362 return $this->fields[$this->bind[strtoupper($colname)]];
368 $rs = $this->_queryID;
369 $this->_numOfRows = $rs->RecordCount;
372 $this->_numOfFields = $f->Count;
376 // should only be used to move forward as we normally use forward-only cursors
379 $rs = $this->_queryID;
380 // absoluteposition doesn't work -- my maths is wrong ?
381 // $rs->AbsolutePosition->$row-2;
383 if ($this->_currentRow > $row) return false;
384 @$rs->Move((integer)$row - $this->_currentRow-1); //adBookmarkFirst
401 DBTYPE_IDISPATCH = 9,
405 DBTYPE_IUNKNOWN = 13,
408 DBTYPE_ARRAY = 0x2000,
409 DBTYPE_BYREF = 0x4000,
416 DBTYPE_VECTOR = 0x1000,
417 DBTYPE_RESERVED = 0x8000,
421 DBTYPE_NUMERIC = 131,
425 DBTYPE_DBTIMESTAMP = 135
434 adUnsignedTinyInt = 17,
435 adUnsignedSmallInt = 18,
437 adUnsignedBigInt = 21,
460 adLongVarWChar = 203,
463 adLongVarBinary = 205,
470 function MetaType($t,$len=-1,$fieldobj=false)
474 $t = $fieldobj->type;
475 $len = $fieldobj->max_length;
478 if (!is_numeric($t)) return $t;
491 if ($len <= $this->blobSize) return 'C';
501 case 133: return 'D';
504 case 135: return 'T';
508 case 16:// adTinyInt = 16,
509 case 2://adSmallInt = 2,
510 case 3://adInteger = 3,
511 case 4://adBigInt = 20,
512 case 17://adUnsignedTinyInt = 17,
513 case 18://adUnsignedSmallInt = 18,
514 case 19://adUnsignedInt = 19,
515 case 20://adUnsignedBigInt = 21,
521 // time stamp not supported yet
524 $rs = $this->_queryID;
525 if (!$rs or $rs->EOF) {
526 $this->fields = false;
529 $this->fields = array();
534 for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
535 $f = $rs->Fields($i);
539 // bind types and flds only once
540 $this->_tarr = $tarr;
541 $this->_flds = $flds;
543 $t = reset($this->_tarr);
544 $f = reset($this->_flds);
546 if ($this->hideErrors) $olde = error_reporting(E_ERROR|E_CORE_ERROR);// sometimes $f->value be null
547 for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
550 case 135: // timestamp
551 if (!strlen((string)$f->value)) $this->fields[] = false;
552 else $this->fields[] = adodb_date('Y-m-d H:i:s',(float)$f->value);
554 case 133:// A date value (yyyymmdd)
555 if ($val = $f->value) {
556 $this->fields[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
558 $this->fields[] = false;
561 if (!strlen((string)$f->value)) $this->fields[] = false;
562 else $this->fields[] = adodb_date('Y-m-d',(float)$f->value);
565 $this->fields[] = false;
567 case 6: // currency is not supported properly;
568 ADOConnection::outp( '<b>'.$f->Name.': currency type not supported by PHP</b>');
569 $this->fields[] = (float) $f->value;
572 $this->fields[] = $f->value;
575 //print " $f->value $t, ";
576 $f = next($this->_flds);
577 $t = next($this->_tarr);
579 if ($this->hideErrors) error_reporting($olde);
580 @$rs->MoveNext(); // @ needed for some versions of PHP!
582 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
583 $this->fields = &$this->GetRowAssoc(ADODB_ASSOC_CASE);
588 function NextRecordSet()
590 $rs = $this->_queryID;
591 $this->_queryID = $rs->NextRecordSet();
592 //$this->_queryID = $this->_QueryId->NextRecordSet();
593 if ($this->_queryID == null) return false;
595 $this->_currentRow = -1;
596 $this->_currentPage = -1;
598 $this->fields = false;
599 $this->_flds = false;
600 $this->_tarr = false;
602 $this->_inited = false;
608 $this->_flds = false;
609 @$this->_queryID->Close();// by Pete Dishman (peterd@telephonetics.co.uk)
610 $this->_queryID = false;