baseline
[omp.pkp.sfu.ca.git] / lib / pkp / lib / adodb / drivers / adodb-ado5.inc.php
blob6fac23a254338abae1bd525113939ae83d52c6a1
1 <?php
2 /*
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;
35 var $_lock_type = -1;
36 var $_execute_option = -1;
37 var $poorAffectedRows = true;
38 var $charPage;
40 function ADODB_ado()
42 $this->_affectedRows = new VARIANT;
45 function ServerInfo()
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= '')
63 // two modes
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
68 if ($argProvider) {
69 $argDatabasename = $argDBorProvider;
70 } else {
71 $argDatabasename = '';
72 if ($argDBorProvider) $argProvider = $argDBorProvider;
73 else $argProvider = 'MSDASQL';
77 try {
78 $u = 'UID';
79 $p = 'PWD';
81 if (!empty($this->charPage))
82 $dbc = new COM('ADODB.Connection',null,$this->charPage);
83 else
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
91 $p = 'Password';
92 $argProvider = "SQLOLEDB"; // SQL Server Provider
94 // not yet
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) {
122 return false;
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,
135 adSchemaColumns = 4,
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,
151 adSchemaTables = 20,
152 adSchemaTranslations = 21,
153 adSchemaProviderTypes = 22,
154 adSchemaViews = 23,
155 adSchemaViewColumnUsage = 24,
156 adSchemaViewTableUsage = 25,
157 adSchemaProcedureParameters = 26,
158 adSchemaForeignKeys = 27,
159 adSchemaPrimaryKeys = 28,
160 adSchemaProcedureColumns = 29,
161 adSchemaDBInfoKeywords = 30,
162 adSchemaDBInfoLiterals = 31,
163 adSchemaCubes = 32,
164 adSchemaDimensions = 33,
165 adSchemaHierarchies = 34,
166 adSchemaLevels = 35,
167 adSchemaMeasures = 36,
168 adSchemaProperties = 37,
169 adSchemaMembers = 38
173 function &MetaTables()
175 $arr= array();
176 $dbc = $this->_connectionID;
178 $adors=@$dbc->OpenSchema(20);//tables
179 if ($adors){
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')
185 $arr[]=$f->value;
186 //print $f->value . ' ' . $t->value.'<br>';
187 $adors->MoveNext();
189 $adors->Close();
192 return $arr;
195 function &MetaColumns($table)
197 $table = strtoupper($table);
198 $arr= array();
199 $dbc = $this->_connectionID;
201 $adors=@$dbc->OpenSchema(4);//tables
203 if ($adors){
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;
218 $adors->MoveNext();
220 $adors->Close();
223 return $arr;
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;
233 // return rs
235 $false = false;
237 if ($inputarr) {
239 if (!empty($this->charPage))
240 $oCmd = new COM('ADODB.Command',null,$this->charPage);
241 else
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);
254 $p = false;
255 $rs = $oCmd->Execute();
256 $e = $dbc->Errors;
257 if ($dbc->Errors->Count > 0) return $false;
258 return $rs;
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) {
267 $true = true;
268 return $true; // 0 = adStateClosed means no records returned
270 return $rs;
272 } catch (exception $e) {
275 return $false;
279 function BeginTrans()
281 if ($this->transOff) return true;
283 if (isset($this->_thisTransactions))
284 if (!$this->_thisTransactions) return false;
285 else {
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;
292 return true;
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;
301 return true;
303 function RollbackTrans() {
304 if ($this->transOff) return true;
305 @$this->_connectionID->RollbackTrans();
306 if ($this->transCnt) @$this->transCnt -= 1;
307 return true;
310 /* Returns: the last error message from previous database operation */
312 function ErrorMsg()
314 if (!$this->_connectionID) return "No connection established";
315 $errmsg = '';
317 try {
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) {
325 return $errmsg;
328 function ErrorNo()
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
337 function _close()
339 if ($this->_connectionID) $this->_connectionID->Close();
340 $this->_connectionID = false;
341 return true;
347 /*--------------------------------------------------------------------------------------
348 Class Name: Recordset
349 --------------------------------------------------------------------------------------*/
351 class ADORecordSet_ado extends ADORecordSet {
353 var $bind = false;
354 var $databaseType = "ado";
355 var $dataProvider = "ado";
356 var $_tarr = false; // caches the types
357 var $_flds; // and field objects
358 var $canSeek = true;
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);
379 $o->name = $f->Name;
380 $t = $f->Type;
381 $o->type = $this->MetaType($t);
382 $o->max_length = $f->DefinedSize;
383 $o->ado_type = $t;
386 //print "off=$off name=$o->name type=$o->type len=$o->max_length<br>";
387 return $o;
390 /* Use associative array to get fields array */
391 function Fields($colname)
393 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
394 if (!$this->bind) {
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)]];
406 function _initrs()
408 $rs = $this->_queryID;
409 $this->_numOfRows = $rs->RecordCount;
411 $f = $rs->Fields;
412 $this->_numOfFields = $f->Count;
416 // should only be used to move forward as we normally use forward-only cursors
417 function _seek($row)
419 $rs = $this->_queryID;
420 // absoluteposition doesn't work -- my maths is wrong ?
421 // $rs->AbsolutePosition->$row-2;
422 // return true;
423 if ($this->_currentRow > $row) return false;
424 @$rs->Move((integer)$row - $this->_currentRow-1); //adBookmarkFirst
425 return true;
429 OLEDB types
431 enum DBTYPEENUM
432 { DBTYPE_EMPTY = 0,
433 DBTYPE_NULL = 1,
434 DBTYPE_I2 = 2,
435 DBTYPE_I4 = 3,
436 DBTYPE_R4 = 4,
437 DBTYPE_R8 = 5,
438 DBTYPE_CY = 6,
439 DBTYPE_DATE = 7,
440 DBTYPE_BSTR = 8,
441 DBTYPE_IDISPATCH = 9,
442 DBTYPE_ERROR = 10,
443 DBTYPE_BOOL = 11,
444 DBTYPE_VARIANT = 12,
445 DBTYPE_IUNKNOWN = 13,
446 DBTYPE_DECIMAL = 14,
447 DBTYPE_UI1 = 17,
448 DBTYPE_ARRAY = 0x2000,
449 DBTYPE_BYREF = 0x4000,
450 DBTYPE_I1 = 16,
451 DBTYPE_UI2 = 18,
452 DBTYPE_UI4 = 19,
453 DBTYPE_I8 = 20,
454 DBTYPE_UI8 = 21,
455 DBTYPE_GUID = 72,
456 DBTYPE_VECTOR = 0x1000,
457 DBTYPE_RESERVED = 0x8000,
458 DBTYPE_BYTES = 128,
459 DBTYPE_STR = 129,
460 DBTYPE_WSTR = 130,
461 DBTYPE_NUMERIC = 131,
462 DBTYPE_UDT = 132,
463 DBTYPE_DBDATE = 133,
464 DBTYPE_DBTIME = 134,
465 DBTYPE_DBTIMESTAMP = 135
467 ADO Types
469 adEmpty = 0,
470 adTinyInt = 16,
471 adSmallInt = 2,
472 adInteger = 3,
473 adBigInt = 20,
474 adUnsignedTinyInt = 17,
475 adUnsignedSmallInt = 18,
476 adUnsignedInt = 19,
477 adUnsignedBigInt = 21,
478 adSingle = 4,
479 adDouble = 5,
480 adCurrency = 6,
481 adDecimal = 14,
482 adNumeric = 131,
483 adBoolean = 11,
484 adError = 10,
485 adUserDefined = 132,
486 adVariant = 12,
487 adIDispatch = 9,
488 adIUnknown = 13,
489 adGUID = 72,
490 adDate = 7,
491 adDBDate = 133,
492 adDBTime = 134,
493 adDBTimeStamp = 135,
494 adBSTR = 8,
495 adChar = 129,
496 adVarChar = 200,
497 adLongVarChar = 201,
498 adWChar = 130,
499 adVarWChar = 202,
500 adLongVarWChar = 203,
501 adBinary = 128,
502 adVarBinary = 204,
503 adLongVarBinary = 205,
504 adChapter = 136,
505 adFileTime = 64,
506 adDBFileTime = 137,
507 adPropVariant = 138,
508 adVarNumeric = 139
510 function MetaType($t,$len=-1,$fieldobj=false)
512 if (is_object($t)) {
513 $fieldobj = $t;
514 $t = $fieldobj->type;
515 $len = $fieldobj->max_length;
518 if (!is_numeric($t)) return $t;
520 switch ($t) {
521 case 0:
522 case 12: // variant
523 case 8: // bstr
524 case 129: //char
525 case 130: //wc
526 case 200: // varc
527 case 202:// varWC
528 case 128: // bin
529 case 204: // varBin
530 case 72: // guid
531 if ($len <= $this->blobSize) return 'C';
533 case 201:
534 case 203:
535 return 'X';
536 case 128:
537 case 204:
538 case 205:
539 return 'B';
540 case 7:
541 case 133: return 'D';
543 case 134:
544 case 135: return 'T';
546 case 11: return 'L';
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,
556 return 'I';
557 default: return 'N';
561 // time stamp not supported yet
562 function _fetch()
564 $rs = $this->_queryID;
565 if (!$rs or $rs->EOF) {
566 $this->fields = false;
567 return false;
569 $this->fields = array();
571 if (!$this->_tarr) {
572 $tarr = array();
573 $flds = array();
574 for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
575 $f = $rs->Fields($i);
576 $flds[] = $f;
577 $tarr[] = $f->Type;
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>';
589 switch($t) {
590 case 135: // timestamp
591 if (!strlen((string)$f->value)) $this->fields[] = false;
592 else {
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;
596 else
597 $val = $f->value;
598 $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
600 break;
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);
604 } else
605 $this->fields[] = false;
606 break;
607 case 7: // adDate
608 if (!strlen((string)$f->value)) $this->fields[] = false;
609 else {
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);
616 break;
617 case 1: // null
618 $this->fields[] = false;
619 break;
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;
623 break;
624 default:
625 $this->fields[] = $f->value;
626 break;
628 //print " $f->value $t, ";
629 $f = next($this->_flds);
630 $t = next($this->_tarr);
631 } // for
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);
638 return true;
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;
650 $this->bind = false;
651 $this->fields = false;
652 $this->_flds = false;
653 $this->_tarr = false;
655 $this->_inited = false;
656 $this->Init();
657 return true;
660 function _close() {
661 $this->_flds = false;
662 @$this->_queryID->Close();// by Pete Dishman (peterd@telephonetics.co.uk)
663 $this->_queryID = false;