Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
1 <?php 2 /* 3 @version v5.21.0 2021-02-27 4 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved. 5 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community 6 Released under both BSD license and Lesser GPL library license. 7 Whenever there is any discrepancy between the two licenses, 8 the BSD license will take precedence. 9 Set tabs to 4 for best viewing. 10 11 Latest version is available at https://adodb.org/ 12 13 Requires ODBC. Works on Windows and Unix. 14 */ 15 // security - hide paths 16 if (!defined('ADODB_DIR')) die(); 17 18 define("_ADODB_ODBC_LAYER", 2 ); 19 20 /* 21 * These constants are used to set define MetaColumns() method's behavior. 22 * - METACOLUMNS_RETURNS_ACTUAL makes the driver return the actual type, 23 * like all other drivers do (default) 24 * - METACOLUMNS_RETURNS_META is provided for legacy compatibility (makes 25 * driver behave as it did prior to v5.21) 26 * 27 * @see $metaColumnsReturnType 28 */ 29 DEFINE('METACOLUMNS_RETURNS_ACTUAL', 0); 30 DEFINE('METACOLUMNS_RETURNS_META', 1); 31 32 /*-------------------------------------------------------------------------------------- 33 --------------------------------------------------------------------------------------*/ 34 35 36 class ADODB_odbc extends ADOConnection { 37 var $databaseType = "odbc"; 38 var $fmtDate = "'Y-m-d'"; 39 var $fmtTimeStamp = "'Y-m-d, h:i:sA'"; 40 var $replaceQuote = "''"; // string to use to replace quotes 41 var $dataProvider = "odbc"; 42 var $hasAffectedRows = true; 43 var $binmode = ODBC_BINMODE_RETURN; 44 var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive 45 // breaking backward-compat 46 //var $longreadlen = 8000; // default number of chars to return for a Blob/Long field 47 var $_bindInputArray = false; 48 var $curmode = SQL_CUR_USE_DRIVER; // See sqlext.h, SQL_CUR_DEFAULT == SQL_CUR_USE_DRIVER == 2L 49 var $_genSeqSQL = "create table %s (id integer)"; 50 var $_autocommit = true; 51 var $_lastAffectedRows = 0; 52 var $uCaseTables = true; // for meta* functions, uppercase table names 53 54 /* 55 * Tells the metaColumns feature whether to return actual or meta type 56 */ 57 public $metaColumnsReturnType = METACOLUMNS_RETURNS_ACTUAL; 58 59 function __construct() {} 60 61 // returns true or false 62 function _connect($argDSN, $argUsername, $argPassword, $argDatabasename) 63 { 64 if (!function_exists('odbc_connect')) return null; 65 66 if (!empty($argDatabasename) && stristr($argDSN, 'Database=') === false) { 67 $argDSN = trim($argDSN); 68 $endDSN = substr($argDSN, strlen($argDSN) - 1); 69 if ($endDSN != ';') $argDSN .= ';'; 70 $argDSN .= 'Database='.$argDatabasename; 71 } 72 73 $last_php_error = $this->resetLastError(); 74 if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword); 75 else $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword,$this->curmode); 76 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error); 77 if (isset($this->connectStmt)) $this->Execute($this->connectStmt); 78 79 return $this->_connectionID != false; 80 } 81 82 // returns true or false 83 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename) 84 { 85 if (!function_exists('odbc_connect')) return null; 86 87 $last_php_error = $this->resetLastError(); 88 $this->_errorMsg = ''; 89 if ($this->debug && $argDatabasename) { 90 ADOConnection::outp("For odbc PConnect(), $argDatabasename is not used. Place dsn in 1st parameter."); 91 } 92 // print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush(); 93 if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword); 94 else $this->_connectionID = odbc_pconnect($argDSN,$argUsername,$argPassword,$this->curmode); 95 96 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error); 97 if ($this->_connectionID && $this->autoRollback) @odbc_rollback($this->_connectionID); 98 if (isset($this->connectStmt)) $this->Execute($this->connectStmt); 99 100 return $this->_connectionID != false; 101 } 102 103 104 function ServerInfo() 105 { 106 107 if (!empty($this->host)) { 108 $dsn = strtoupper($this->host); 109 $first = true; 110 $found = false; 111 112 if (!function_exists('odbc_data_source')) return false; 113 114 while(true) { 115 116 $rez = @odbc_data_source($this->_connectionID, 117 $first ? SQL_FETCH_FIRST : SQL_FETCH_NEXT); 118 $first = false; 119 if (!is_array($rez)) break; 120 if (strtoupper($rez['server']) == $dsn) { 121 $found = true; 122 break; 123 } 124 } 125 if (!$found) return ADOConnection::ServerInfo(); 126 if (!isset($rez['version'])) $rez['version'] = ''; 127 return $rez; 128 } else { 129 return ADOConnection::ServerInfo(); 130 } 131 } 132 133 134 function CreateSequence($seqname='adodbseq',$start=1) 135 { 136 if (empty($this->_genSeqSQL)) return false; 137 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname)); 138 if (!$ok) return false; 139 $start -= 1; 140 return $this->Execute("insert into $seqname values($start)"); 141 } 142 143 var $_dropSeqSQL = 'drop table %s'; 144 function DropSequence($seqname = 'adodbseq') 145 { 146 if (empty($this->_dropSeqSQL)) return false; 147 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname)); 148 } 149 150 /* 151 This algorithm is not very efficient, but works even if table locking 152 is not available. 153 154 Will return false if unable to generate an ID after $MAXLOOPS attempts. 155 */ 156 function GenID($seq='adodbseq',$start=1) 157 { 158 // if you have to modify the parameter below, your database is overloaded, 159 // or you need to implement generation of id's yourself! 160 $MAXLOOPS = 100; 161 //$this->debug=1; 162 while (--$MAXLOOPS>=0) { 163 $num = $this->GetOne("select id from $seq"); 164 if ($num === false) { 165 $this->Execute(sprintf($this->_genSeqSQL ,$seq)); 166 $start -= 1; 167 $num = '0'; 168 $ok = $this->Execute("insert into $seq values($start)"); 169 if (!$ok) return false; 170 } 171 $this->Execute("update $seq set id=id+1 where id=$num"); 172 173 if ($this->affected_rows() > 0) { 174 $num += 1; 175 $this->genID = $num; 176 return $num; 177 } elseif ($this->affected_rows() == 0) { 178 // some drivers do not return a valid value => try with another method 179 $value = $this->GetOne("select id from $seq"); 180 if ($value == $num + 1) { 181 return $value; 182 } 183 } 184 } 185 if ($fn = $this->raiseErrorFn) { 186 $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num); 187 } 188 return false; 189 } 190 191 192 function ErrorMsg() 193 { 194 if ($this->_errorMsg !== false) return $this->_errorMsg; 195 if (empty($this->_connectionID)) return @odbc_errormsg(); 196 return @odbc_errormsg($this->_connectionID); 197 } 198 199 function ErrorNo() 200 { 201 if ($this->_errorCode !== false) { 202 // bug in 4.0.6, error number can be corrupted string (should be 6 digits) 203 return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode; 204 } 205 206 if (empty($this->_connectionID)) $e = @odbc_error(); 207 else $e = @odbc_error($this->_connectionID); 208 209 // bug in 4.0.6, error number can be corrupted string (should be 6 digits) 210 // so we check and patch 211 if (strlen($e)<=2) return 0; 212 return $e; 213 } 214 215 216 217 function BeginTrans() 218 { 219 if (!$this->hasTransactions) return false; 220 if ($this->transOff) return true; 221 $this->transCnt += 1; 222 $this->_autocommit = false; 223 return odbc_autocommit($this->_connectionID,false); 224 } 225 226 function CommitTrans($ok=true) 227 { 228 if ($this->transOff) return true; 229 if (!$ok) return $this->RollbackTrans(); 230 if ($this->transCnt) $this->transCnt -= 1; 231 $this->_autocommit = true; 232 $ret = odbc_commit($this->_connectionID); 233 odbc_autocommit($this->_connectionID,true); 234 return $ret; 235 } 236 237 function RollbackTrans() 238 { 239 if ($this->transOff) return true; 240 if ($this->transCnt) $this->transCnt -= 1; 241 $this->_autocommit = true; 242 $ret = odbc_rollback($this->_connectionID); 243 odbc_autocommit($this->_connectionID,true); 244 return $ret; 245 } 246 247 function MetaPrimaryKeys($table,$owner=false) 248 { 249 global $ADODB_FETCH_MODE; 250 251 if ($this->uCaseTables) $table = strtoupper($table); 252 $schema = ''; 253 $this->_findschema($table,$schema); 254 255 $savem = $ADODB_FETCH_MODE; 256 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 257 $qid = @odbc_primarykeys($this->_connectionID,'',$schema,$table); 258 259 if (!$qid) { 260 $ADODB_FETCH_MODE = $savem; 261 return false; 262 } 263 $rs = new ADORecordSet_odbc($qid); 264 $ADODB_FETCH_MODE = $savem; 265 266 if (!$rs) return false; 267 268 $arr = $rs->GetArray(); 269 $rs->Close(); 270 //print_r($arr); 271 $arr2 = array(); 272 for ($i=0; $i < sizeof($arr); $i++) { 273 if ($arr[$i][3]) $arr2[] = $arr[$i][3]; 274 } 275 return $arr2; 276 } 277 278 279 280 function MetaTables($ttype=false,$showSchema=false,$mask=false) 281 { 282 global $ADODB_FETCH_MODE; 283 284 $savem = $ADODB_FETCH_MODE; 285 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 286 $qid = odbc_tables($this->_connectionID); 287 288 $rs = new ADORecordSet_odbc($qid); 289 290 $ADODB_FETCH_MODE = $savem; 291 if (!$rs) { 292 $false = false; 293 return $false; 294 } 295 296 $arr = $rs->GetArray(); 297 //print_r($arr); 298 299 $rs->Close(); 300 $arr2 = array(); 301 302 if ($ttype) { 303 $isview = strncmp($ttype,'V',1) === 0; 304 } 305 for ($i=0; $i < sizeof($arr); $i++) { 306 if (!$arr[$i][2]) continue; 307 $type = $arr[$i][3]; 308 if ($ttype) { 309 if ($isview) { 310 if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2]; 311 } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2]; 312 } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2]; 313 } 314 return $arr2; 315 } 316 317 /* 318 See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcdatetime_data_type_changes.asp 319 / SQL data type codes / 320 #define SQL_UNKNOWN_TYPE 0 321 #define SQL_CHAR 1 322 #define SQL_NUMERIC 2 323 #define SQL_DECIMAL 3 324 #define SQL_INTEGER 4 325 #define SQL_SMALLINT 5 326 #define SQL_FLOAT 6 327 #define SQL_REAL 7 328 #define SQL_DOUBLE 8 329 #if (ODBCVER >= 0x0300) 330 #define SQL_DATETIME 9 331 #endif 332 #define SQL_VARCHAR 12 333 334 335 / One-parameter shortcuts for date/time data types / 336 #if (ODBCVER >= 0x0300) 337 #define SQL_TYPE_DATE 91 338 #define SQL_TYPE_TIME 92 339 #define SQL_TYPE_TIMESTAMP 93 340 341 #define SQL_UNICODE (-95) 342 #define SQL_UNICODE_VARCHAR (-96) 343 #define SQL_UNICODE_LONGVARCHAR (-97) 344 */ 345 function ODBCTypes($t) 346 { 347 switch ((integer)$t) { 348 case 1: 349 case 12: 350 case 0: 351 case -95: 352 case -96: 353 return 'C'; 354 case -97: 355 case -1: //text 356 return 'X'; 357 case -4: //image 358 return 'B'; 359 360 case 9: 361 case 91: 362 return 'D'; 363 364 case 10: 365 case 11: 366 case 92: 367 case 93: 368 return 'T'; 369 370 case 4: 371 case 5: 372 case -6: 373 return 'I'; 374 375 case -11: // uniqidentifier 376 return 'R'; 377 case -7: //bit 378 return 'L'; 379 380 default: 381 return 'N'; 382 } 383 } 384 385 function MetaColumns($table, $normalize=true) 386 { 387 global $ADODB_FETCH_MODE; 388 389 $false = false; 390 if ($this->uCaseTables) $table = strtoupper($table); 391 $schema = ''; 392 $this->_findschema($table,$schema); 393 394 $savem = $ADODB_FETCH_MODE; 395 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 396 397 /*if (false) { // after testing, confirmed that the following does not work because of a bug 398 $qid2 = odbc_tables($this->_connectionID); 399 $rs = new ADORecordSet_odbc($qid2); 400 $ADODB_FETCH_MODE = $savem; 401 if (!$rs) return false; 402 $rs->_fetch(); 403 404 while (!$rs->EOF) { 405 if ($table == strtoupper($rs->fields[2])) { 406 $q = $rs->fields[0]; 407 $o = $rs->fields[1]; 408 break; 409 } 410 $rs->MoveNext(); 411 } 412 $rs->Close(); 413 414 $qid = odbc_columns($this->_connectionID,$q,$o,strtoupper($table),'%'); 415 } */ 416 417 switch ($this->databaseType) { 418 case 'access': 419 case 'vfp': 420 $qid = odbc_columns($this->_connectionID);#,'%','',strtoupper($table),'%'); 421 break; 422 423 424 case 'db2': 425 $colname = "%"; 426 $qid = odbc_columns($this->_connectionID, "", $schema, $table, $colname); 427 break; 428 429 default: 430 $qid = @odbc_columns($this->_connectionID,'%','%',strtoupper($table),'%'); 431 if (empty($qid)) $qid = odbc_columns($this->_connectionID); 432 break; 433 } 434 if (empty($qid)) return $false; 435 436 $rs = new ADORecordSet_odbc($qid); 437 $ADODB_FETCH_MODE = $savem; 438 439 if (!$rs) return $false; 440 $rs->_fetch(); 441 442 $retarr = array(); 443 444 /* 445 $rs->fields indices 446 0 TABLE_QUALIFIER 447 1 TABLE_SCHEM 448 2 TABLE_NAME 449 3 COLUMN_NAME 450 4 DATA_TYPE 451 5 TYPE_NAME 452 6 PRECISION 453 7 LENGTH 454 8 SCALE 455 9 RADIX 456 10 NULLABLE 457 11 REMARKS 458 */ 459 while (!$rs->EOF) { 460 // adodb_pr($rs->fields); 461 if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) { 462 $fld = new ADOFieldObject(); 463 $fld->name = $rs->fields[3]; 464 if ($this->metaColumnsReturnType == METACOLUMNS_RETURNS_META) 465 /* 466 * This is the broken, original value 467 */ 468 $fld->type = $this->ODBCTypes($rs->fields[4]); 469 else 470 /* 471 * This is the correct new value 472 */ 473 $fld->type = $rs->fields[4]; 474 475 // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp 476 // access uses precision to store length for char/varchar 477 if ($fld->type == 'C' or $fld->type == 'X') { 478 if ($this->databaseType == 'access') 479 $fld->max_length = $rs->fields[6]; 480 else if ($rs->fields[4] <= -95) // UNICODE 481 $fld->max_length = $rs->fields[7]/2; 482 else 483 $fld->max_length = $rs->fields[7]; 484 } else 485 $fld->max_length = $rs->fields[7]; 486 $fld->not_null = !empty($rs->fields[10]); 487 $fld->scale = $rs->fields[8]; 488 $retarr[strtoupper($fld->name)] = $fld; 489 } else if (sizeof($retarr)>0) 490 break; 491 $rs->MoveNext(); 492 } 493 $rs->Close(); //-- crashes 4.03pl1 -- why? 494 495 if (empty($retarr)) $retarr = false; 496 return $retarr; 497 } 498 499 function Prepare($sql) 500 { 501 if (! $this->_bindInputArray) return $sql; // no binding 502 $stmt = odbc_prepare($this->_connectionID,$sql); 503 if (!$stmt) { 504 // we don't know whether odbc driver is parsing prepared stmts, so just return sql 505 return $sql; 506 } 507 return array($sql,$stmt,false); 508 } 509 510 /* returns queryID or false */ 511 function _query($sql,$inputarr=false) 512 { 513 $last_php_error = $this->resetLastError(); 514 $this->_errorMsg = ''; 515 516 if ($inputarr) { 517 if (is_array($sql)) { 518 $stmtid = $sql[1]; 519 } else { 520 $stmtid = odbc_prepare($this->_connectionID,$sql); 521 522 if ($stmtid == false) { 523 $this->_errorMsg = $this->getChangedErrorMsg($last_php_error); 524 return false; 525 } 526 } 527 528 if (! odbc_execute($stmtid,$inputarr)) { 529 //@odbc_free_result($stmtid); 530 $this->_errorMsg = odbc_errormsg(); 531 $this->_errorCode = odbc_error(); 532 return false; 533 } 534 535 } else if (is_array($sql)) { 536 $stmtid = $sql[1]; 537 if (!odbc_execute($stmtid)) { 538 //@odbc_free_result($stmtid); 539 $this->_errorMsg = odbc_errormsg(); 540 $this->_errorCode = odbc_error(); 541 return false; 542 } 543 } else 544 $stmtid = odbc_exec($this->_connectionID,$sql); 545 546 $this->_lastAffectedRows = 0; 547 if ($stmtid) { 548 if (@odbc_num_fields($stmtid) == 0) { 549 $this->_lastAffectedRows = odbc_num_rows($stmtid); 550 $stmtid = true; 551 } else { 552 $this->_lastAffectedRows = 0; 553 odbc_binmode($stmtid,$this->binmode); 554 odbc_longreadlen($stmtid,$this->maxblobsize); 555 } 556 557 $this->_errorMsg = ''; 558 $this->_errorCode = 0; 559 } else { 560 $this->_errorMsg = odbc_errormsg(); 561 $this->_errorCode = odbc_error(); 562 } 563 return $stmtid; 564 } 565 566 /* 567 Insert a null into the blob field of the table first. 568 Then use UpdateBlob to store the blob. 569 570 Usage: 571 572 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)'); 573 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1'); 574 */ 575 function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB') 576 { 577 return $this->Execute("UPDATE $table SET $column=? WHERE $where",array($val)) != false; 578 } 579 580 // returns true or false 581 function _close() 582 { 583 $ret = @odbc_close($this->_connectionID); 584 $this->_connectionID = false; 585 return $ret; 586 } 587 588 function _affectedrows() 589 { 590 return $this->_lastAffectedRows; 591 } 592 593 } 594 595 /*-------------------------------------------------------------------------------------- 596 Class Name: Recordset 597 --------------------------------------------------------------------------------------*/ 598 599 class ADORecordSet_odbc extends ADORecordSet { 600 601 var $bind = false; 602 var $databaseType = "odbc"; 603 var $dataProvider = "odbc"; 604 var $useFetchArray; 605 606 function __construct($id,$mode=false) 607 { 608 if ($mode === false) { 609 global $ADODB_FETCH_MODE; 610 $mode = $ADODB_FETCH_MODE; 611 } 612 $this->fetchMode = $mode; 613 614 $this->_queryID = $id; 615 616 // the following is required for mysql odbc driver in 4.3.1 -- why? 617 $this->EOF = false; 618 $this->_currentRow = -1; 619 //parent::__construct($id); 620 } 621 622 623 // returns the field object 624 function FetchField($fieldOffset = -1) 625 { 626 627 $off=$fieldOffset+1; // offsets begin at 1 628 629 $o= new ADOFieldObject(); 630 $o->name = @odbc_field_name($this->_queryID,$off); 631 $o->type = @odbc_field_type($this->_queryID,$off); 632 $o->max_length = @odbc_field_len($this->_queryID,$off); 633 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name); 634 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name); 635 return $o; 636 } 637 638 /* Use associative array to get fields array */ 639 function Fields($colname) 640 { 641 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname]; 642 if (!$this->bind) { 643 $this->bind = array(); 644 for ($i=0; $i < $this->_numOfFields; $i++) { 645 $o = $this->FetchField($i); 646 $this->bind[strtoupper($o->name)] = $i; 647 } 648 } 649 650 return $this->fields[$this->bind[strtoupper($colname)]]; 651 } 652 653 654 function _initrs() 655 { 656 global $ADODB_COUNTRECS; 657 $this->_numOfRows = ($ADODB_COUNTRECS) ? @odbc_num_rows($this->_queryID) : -1; 658 $this->_numOfFields = @odbc_num_fields($this->_queryID); 659 // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0 660 if ($this->_numOfRows == 0) $this->_numOfRows = -1; 661 //$this->useFetchArray = $this->connection->useFetchArray; 662 } 663 664 function _seek($row) 665 { 666 return false; 667 } 668 669 // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated 670 function GetArrayLimit($nrows,$offset=-1) 671 { 672 if ($offset <= 0) { 673 $rs = $this->GetArray($nrows); 674 return $rs; 675 } 676 $savem = $this->fetchMode; 677 $this->fetchMode = ADODB_FETCH_NUM; 678 $this->Move($offset); 679 $this->fetchMode = $savem; 680 681 if ($this->fetchMode & ADODB_FETCH_ASSOC) { 682 $this->fields = $this->GetRowAssoc(); 683 } 684 685 $results = array(); 686 $cnt = 0; 687 while (!$this->EOF && $nrows != $cnt) { 688 $results[$cnt++] = $this->fields; 689 $this->MoveNext(); 690 } 691 692 return $results; 693 } 694 695 696 function MoveNext() 697 { 698 if ($this->_numOfRows != 0 && !$this->EOF) { 699 $this->_currentRow++; 700 if( $this->_fetch() ) { 701 return true; 702 } 703 } 704 $this->fields = false; 705 $this->EOF = true; 706 return false; 707 } 708 709 function _fetch() 710 { 711 $this->fields = false; 712 $rez = @odbc_fetch_into($this->_queryID,$this->fields); 713 if ($rez) { 714 if ($this->fetchMode & ADODB_FETCH_ASSOC) { 715 $this->fields = $this->GetRowAssoc(); 716 } 717 return true; 718 } 719 return false; 720 } 721 722 function _close() 723 { 724 return @odbc_free_result($this->_queryID); 725 } 726 727 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body