[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 @version v5.20.3 01-Jan-2016 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 7 This is a version of the ADODB driver for DB2. It uses the 'ibm_db2' PECL extension 8 for PHP (http://pecl.php.net/package/ibm_db2), which in turn requires DB2 V8.2.2 or 9 higher. 10 11 Originally tested with PHP 5.1.1 and Apache 2.0.55 on Windows XP SP2. 12 More recently tested with PHP 5.1.2 and Apache 2.0.55 on Windows XP SP2. 13 14 This file was ported from "adodb-odbc.inc.php" by Larry Menard, "larry.menard#rogers.com". 15 I ripped out what I believed to be a lot of redundant or obsolete code, but there are 16 probably still some remnants of the ODBC support in this file; I'm relying on reviewers 17 of this code to point out any other things that can be removed. 18 */ 19 20 // security - hide paths 21 if (!defined('ADODB_DIR')) die(); 22 23 define("_ADODB_DB2_LAYER", 2 ); 24 25 /*-------------------------------------------------------------------------------------- 26 --------------------------------------------------------------------------------------*/ 27 28 29 30 31 32 class ADODB_db2 extends ADOConnection { 33 var $databaseType = "db2"; 34 var $fmtDate = "'Y-m-d'"; 35 var $concat_operator = '||'; 36 37 var $sysTime = 'CURRENT TIME'; 38 var $sysDate = 'CURRENT DATE'; 39 var $sysTimeStamp = 'CURRENT TIMESTAMP'; 40 41 var $fmtTimeStamp = "'Y-m-d H:i:s'"; 42 var $replaceQuote = "''"; // string to use to replace quotes 43 var $dataProvider = "db2"; 44 var $hasAffectedRows = true; 45 46 var $binmode = DB2_BINARY; 47 48 var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive 49 // breaking backward-compat 50 var $_bindInputArray = false; 51 var $_genIDSQL = "VALUES NEXTVAL FOR %s"; 52 var $_genSeqSQL = "CREATE SEQUENCE %s START WITH %s NO MAXVALUE NO CYCLE"; 53 var $_dropSeqSQL = "DROP SEQUENCE %s"; 54 var $_autocommit = true; 55 var $_haserrorfunctions = true; 56 var $_lastAffectedRows = 0; 57 var $uCaseTables = true; // for meta* functions, uppercase table names 58 var $hasInsertID = true; 59 60 61 function _insertid() 62 { 63 return ADOConnection::GetOne('VALUES IDENTITY_VAL_LOCAL()'); 64 } 65 66 function __construct() 67 { 68 $this->_haserrorfunctions = ADODB_PHPVER >= 0x4050; 69 } 70 71 // returns true or false 72 function _connect($argDSN, $argUsername, $argPassword, $argDatabasename) 73 { 74 global $php_errormsg; 75 76 if (!function_exists('db2_connect')) { 77 ADOConnection::outp("Warning: The old ODBC based DB2 driver has been renamed 'odbc_db2'. This ADOdb driver calls PHP's native db2 extension which is not installed."); 78 return null; 79 } 80 // This needs to be set before the connect(). 81 // Replaces the odbc_binmode() call that was in Execute() 82 ini_set('ibm_db2.binmode', $this->binmode); 83 84 if ($argDatabasename && empty($argDSN)) { 85 86 if (stripos($argDatabasename,'UID=') && stripos($argDatabasename,'PWD=')) $this->_connectionID = db2_connect($argDatabasename,null,null); 87 else $this->_connectionID = db2_connect($argDatabasename,$argUsername,$argPassword); 88 } else { 89 if ($argDatabasename) $schema = $argDatabasename; 90 if (stripos($argDSN,'UID=') && stripos($argDSN,'PWD=')) $this->_connectionID = db2_connect($argDSN,null,null); 91 else $this->_connectionID = db2_connect($argDSN,$argUsername,$argPassword); 92 } 93 if (isset($php_errormsg)) $php_errormsg = ''; 94 95 // For db2_connect(), there is an optional 4th arg. If present, it must be 96 // an array of valid options. So far, we don't use them. 97 98 $this->_errorMsg = @db2_conn_errormsg(); 99 if (isset($this->connectStmt)) $this->Execute($this->connectStmt); 100 101 if ($this->_connectionID && isset($schema)) $this->Execute("SET SCHEMA=$schema"); 102 return $this->_connectionID != false; 103 } 104 105 // returns true or false 106 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename) 107 { 108 global $php_errormsg; 109 110 if (!function_exists('db2_connect')) return null; 111 112 // This needs to be set before the connect(). 113 // Replaces the odbc_binmode() call that was in Execute() 114 ini_set('ibm_db2.binmode', $this->binmode); 115 116 if (isset($php_errormsg)) $php_errormsg = ''; 117 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; 118 119 if ($argDatabasename && empty($argDSN)) { 120 121 if (stripos($argDatabasename,'UID=') && stripos($argDatabasename,'PWD=')) $this->_connectionID = db2_pconnect($argDatabasename,null,null); 122 else $this->_connectionID = db2_pconnect($argDatabasename,$argUsername,$argPassword); 123 } else { 124 if ($argDatabasename) $schema = $argDatabasename; 125 if (stripos($argDSN,'UID=') && stripos($argDSN,'PWD=')) $this->_connectionID = db2_pconnect($argDSN,null,null); 126 else $this->_connectionID = db2_pconnect($argDSN,$argUsername,$argPassword); 127 } 128 if (isset($php_errormsg)) $php_errormsg = ''; 129 130 $this->_errorMsg = @db2_conn_errormsg(); 131 if ($this->_connectionID && $this->autoRollback) @db2_rollback($this->_connectionID); 132 if (isset($this->connectStmt)) $this->Execute($this->connectStmt); 133 134 if ($this->_connectionID && isset($schema)) $this->Execute("SET SCHEMA=$schema"); 135 return $this->_connectionID != false; 136 } 137 138 // format and return date string in database timestamp format 139 function DBTimeStamp($ts, $isfld = false) 140 { 141 if (empty($ts) && $ts !== 0) return 'null'; 142 if (is_string($ts)) $ts = ADORecordSet::UnixTimeStamp($ts); 143 return 'TO_DATE('.adodb_date($this->fmtTimeStamp,$ts).",'YYYY-MM-DD HH24:MI:SS')"; 144 } 145 146 // Format date column in sql string given an input format that understands Y M D 147 function SQLDate($fmt, $col=false) 148 { 149 // use right() and replace() ? 150 if (!$col) $col = $this->sysDate; 151 152 /* use TO_CHAR() if $fmt is TO_CHAR() allowed fmt */ 153 if ($fmt== 'Y-m-d H:i:s') 154 return 'TO_CHAR('.$col.", 'YYYY-MM-DD HH24:MI:SS')"; 155 156 $s = ''; 157 158 $len = strlen($fmt); 159 for ($i=0; $i < $len; $i++) { 160 if ($s) $s .= $this->concat_operator; 161 $ch = $fmt[$i]; 162 switch($ch) { 163 case 'Y': 164 case 'y': 165 if ($len==1) return "year($col)"; 166 $s .= "char(year($col))"; 167 break; 168 case 'M': 169 if ($len==1) return "monthname($col)"; 170 $s .= "substr(monthname($col),1,3)"; 171 break; 172 case 'm': 173 if ($len==1) return "month($col)"; 174 $s .= "right(digits(month($col)),2)"; 175 break; 176 case 'D': 177 case 'd': 178 if ($len==1) return "day($col)"; 179 $s .= "right(digits(day($col)),2)"; 180 break; 181 case 'H': 182 case 'h': 183 if ($len==1) return "hour($col)"; 184 if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)"; 185 else $s .= "''"; 186 break; 187 case 'i': 188 case 'I': 189 if ($len==1) return "minute($col)"; 190 if ($col != $this->sysDate) 191 $s .= "right(digits(minute($col)),2)"; 192 else $s .= "''"; 193 break; 194 case 'S': 195 case 's': 196 if ($len==1) return "second($col)"; 197 if ($col != $this->sysDate) 198 $s .= "right(digits(second($col)),2)"; 199 else $s .= "''"; 200 break; 201 default: 202 if ($ch == '\\') { 203 $i++; 204 $ch = substr($fmt,$i,1); 205 } 206 $s .= $this->qstr($ch); 207 } 208 } 209 return $s; 210 } 211 212 213 function ServerInfo() 214 { 215 $row = $this->GetRow("SELECT service_level, fixpack_num FROM TABLE(sysproc.env_get_inst_info()) 216 as INSTANCEINFO"); 217 218 219 if ($row) { 220 $info['version'] = $row[0].':'.$row[1]; 221 $info['fixpack'] = $row[1]; 222 $info['description'] = ''; 223 } else { 224 return ADOConnection::ServerInfo(); 225 } 226 227 return $info; 228 } 229 230 function CreateSequence($seqname='adodbseq',$start=1) 231 { 232 if (empty($this->_genSeqSQL)) return false; 233 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname,$start)); 234 if (!$ok) return false; 235 return true; 236 } 237 238 function DropSequence($seqname = 'adodbseq') 239 { 240 if (empty($this->_dropSeqSQL)) return false; 241 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname)); 242 } 243 244 function SelectLimit($sql, $nrows = -1, $offset = -1, $inputArr = false, $secs2cache = 0) 245 { 246 $nrows = (integer) $nrows; 247 if ($offset <= 0) { 248 // could also use " OPTIMIZE FOR $nrows ROWS " 249 if ($nrows >= 0) $sql .= " FETCH FIRST $nrows ROWS ONLY "; 250 $rs = $this->Execute($sql,$inputArr); 251 } else { 252 if ($offset > 0 && $nrows < 0); 253 else { 254 $nrows += $offset; 255 $sql .= " FETCH FIRST $nrows ROWS ONLY "; 256 } 257 $rs = ADOConnection::SelectLimit($sql,-1,$offset,$inputArr); 258 } 259 260 return $rs; 261 } 262 263 /* 264 This algorithm is not very efficient, but works even if table locking 265 is not available. 266 267 Will return false if unable to generate an ID after $MAXLOOPS attempts. 268 */ 269 function GenID($seq='adodbseq',$start=1) 270 { 271 // if you have to modify the parameter below, your database is overloaded, 272 // or you need to implement generation of id's yourself! 273 $num = $this->GetOne("VALUES NEXTVAL FOR $seq"); 274 return $num; 275 } 276 277 278 function ErrorMsg() 279 { 280 if ($this->_haserrorfunctions) { 281 if ($this->_errorMsg !== false) return $this->_errorMsg; 282 if (empty($this->_connectionID)) return @db2_conn_errormsg(); 283 return @db2_conn_errormsg($this->_connectionID); 284 } else return ADOConnection::ErrorMsg(); 285 } 286 287 function ErrorNo() 288 { 289 290 if ($this->_haserrorfunctions) { 291 if ($this->_errorCode !== false) { 292 // bug in 4.0.6, error number can be corrupted string (should be 6 digits) 293 return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode; 294 } 295 296 if (empty($this->_connectionID)) $e = @db2_conn_error(); 297 else $e = @db2_conn_error($this->_connectionID); 298 299 // bug in 4.0.6, error number can be corrupted string (should be 6 digits) 300 // so we check and patch 301 if (strlen($e)<=2) return 0; 302 return $e; 303 } else return ADOConnection::ErrorNo(); 304 } 305 306 307 308 function BeginTrans() 309 { 310 if (!$this->hasTransactions) return false; 311 if ($this->transOff) return true; 312 $this->transCnt += 1; 313 $this->_autocommit = false; 314 return db2_autocommit($this->_connectionID,false); 315 } 316 317 function CommitTrans($ok=true) 318 { 319 if ($this->transOff) return true; 320 if (!$ok) return $this->RollbackTrans(); 321 if ($this->transCnt) $this->transCnt -= 1; 322 $this->_autocommit = true; 323 $ret = db2_commit($this->_connectionID); 324 db2_autocommit($this->_connectionID,true); 325 return $ret; 326 } 327 328 function RollbackTrans() 329 { 330 if ($this->transOff) return true; 331 if ($this->transCnt) $this->transCnt -= 1; 332 $this->_autocommit = true; 333 $ret = db2_rollback($this->_connectionID); 334 db2_autocommit($this->_connectionID,true); 335 return $ret; 336 } 337 338 function MetaPrimaryKeys($table, $owner = false) 339 { 340 global $ADODB_FETCH_MODE; 341 342 if ($this->uCaseTables) $table = strtoupper($table); 343 $schema = ''; 344 $this->_findschema($table,$schema); 345 346 $savem = $ADODB_FETCH_MODE; 347 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 348 $qid = @db2_primarykeys($this->_connectionID,'',$schema,$table); 349 350 if (!$qid) { 351 $ADODB_FETCH_MODE = $savem; 352 return false; 353 } 354 $rs = new ADORecordSet_db2($qid); 355 $ADODB_FETCH_MODE = $savem; 356 357 if (!$rs) return false; 358 359 $arr = $rs->GetArray(); 360 $rs->Close(); 361 $arr2 = array(); 362 for ($i=0; $i < sizeof($arr); $i++) { 363 if ($arr[$i][3]) $arr2[] = $arr[$i][3]; 364 } 365 return $arr2; 366 } 367 368 function MetaForeignKeys($table, $owner = FALSE, $upper = FALSE, $asociative = FALSE ) 369 { 370 global $ADODB_FETCH_MODE; 371 372 if ($this->uCaseTables) $table = strtoupper($table); 373 $schema = ''; 374 $this->_findschema($table,$schema); 375 376 $savem = $ADODB_FETCH_MODE; 377 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 378 $qid = @db2_foreign_keys($this->_connectionID,'',$schema,$table); 379 if (!$qid) { 380 $ADODB_FETCH_MODE = $savem; 381 return false; 382 } 383 $rs = new ADORecordSet_db2($qid); 384 385 $ADODB_FETCH_MODE = $savem; 386 /* 387 $rs->fields indices 388 0 PKTABLE_CAT 389 1 PKTABLE_SCHEM 390 2 PKTABLE_NAME 391 3 PKCOLUMN_NAME 392 4 FKTABLE_CAT 393 5 FKTABLE_SCHEM 394 6 FKTABLE_NAME 395 7 FKCOLUMN_NAME 396 */ 397 if (!$rs) return false; 398 399 $foreign_keys = array(); 400 while (!$rs->EOF) { 401 if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) { 402 if (!is_array($foreign_keys[$rs->fields[5].'.'.$rs->fields[6]])) 403 $foreign_keys[$rs->fields[5].'.'.$rs->fields[6]] = array(); 404 $foreign_keys[$rs->fields[5].'.'.$rs->fields[6]][$rs->fields[7]] = $rs->fields[3]; 405 } 406 $rs->MoveNext(); 407 } 408 409 $rs->Close(); 410 return $foreign_key; 411 } 412 413 414 function MetaTables($ttype = false, $schema = false, $mask = false) 415 { 416 global $ADODB_FETCH_MODE; 417 418 $savem = $ADODB_FETCH_MODE; 419 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 420 $qid = db2_tables($this->_connectionID); 421 422 $rs = new ADORecordSet_db2($qid); 423 424 $ADODB_FETCH_MODE = $savem; 425 if (!$rs) { 426 $false = false; 427 return $false; 428 } 429 430 $arr = $rs->GetArray(); 431 $rs->Close(); 432 $arr2 = array(); 433 434 if ($ttype) { 435 $isview = strncmp($ttype,'V',1) === 0; 436 } 437 for ($i=0; $i < sizeof($arr); $i++) { 438 if (!$arr[$i][2]) continue; 439 $type = $arr[$i][3]; 440 $owner = $arr[$i][1]; 441 $schemaval = ($schema) ? $arr[$i][1].'.' : ''; 442 if ($ttype) { 443 if ($isview) { 444 if (strncmp($type,'V',1) === 0) $arr2[] = $schemaval.$arr[$i][2]; 445 } else if (strncmp($owner,'SYS',3) !== 0) $arr2[] = $schemaval.$arr[$i][2]; 446 } else if (strncmp($owner,'SYS',3) !== 0) $arr2[] = $schemaval.$arr[$i][2]; 447 } 448 return $arr2; 449 } 450 451 /* 452 See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/db2/htm/db2datetime_data_type_changes.asp 453 / SQL data type codes / 454 #define SQL_UNKNOWN_TYPE 0 455 #define SQL_CHAR 1 456 #define SQL_NUMERIC 2 457 #define SQL_DECIMAL 3 458 #define SQL_INTEGER 4 459 #define SQL_SMALLINT 5 460 #define SQL_FLOAT 6 461 #define SQL_REAL 7 462 #define SQL_DOUBLE 8 463 #if (DB2VER >= 0x0300) 464 #define SQL_DATETIME 9 465 #endif 466 #define SQL_VARCHAR 12 467 468 469 / One-parameter shortcuts for date/time data types / 470 #if (DB2VER >= 0x0300) 471 #define SQL_TYPE_DATE 91 472 #define SQL_TYPE_TIME 92 473 #define SQL_TYPE_TIMESTAMP 93 474 475 #define SQL_UNICODE (-95) 476 #define SQL_UNICODE_VARCHAR (-96) 477 #define SQL_UNICODE_LONGVARCHAR (-97) 478 */ 479 function DB2Types($t) 480 { 481 switch ((integer)$t) { 482 case 1: 483 case 12: 484 case 0: 485 case -95: 486 case -96: 487 return 'C'; 488 case -97: 489 case -1: //text 490 return 'X'; 491 case -4: //image 492 return 'B'; 493 494 case 9: 495 case 91: 496 return 'D'; 497 498 case 10: 499 case 11: 500 case 92: 501 case 93: 502 return 'T'; 503 504 case 4: 505 case 5: 506 case -6: 507 return 'I'; 508 509 case -11: // uniqidentifier 510 return 'R'; 511 case -7: //bit 512 return 'L'; 513 514 default: 515 return 'N'; 516 } 517 } 518 519 function MetaColumns($table, $normalize=true) 520 { 521 global $ADODB_FETCH_MODE; 522 523 $false = false; 524 if ($this->uCaseTables) $table = strtoupper($table); 525 $schema = ''; 526 $this->_findschema($table,$schema); 527 528 $savem = $ADODB_FETCH_MODE; 529 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 530 531 $colname = "%"; 532 $qid = db2_columns($this->_connectionID, "", $schema, $table, $colname); 533 if (empty($qid)) return $false; 534 535 $rs = new ADORecordSet_db2($qid); 536 $ADODB_FETCH_MODE = $savem; 537 538 if (!$rs) return $false; 539 $rs->_fetch(); 540 541 $retarr = array(); 542 543 /* 544 $rs->fields indices 545 0 TABLE_QUALIFIER 546 1 TABLE_SCHEM 547 2 TABLE_NAME 548 3 COLUMN_NAME 549 4 DATA_TYPE 550 5 TYPE_NAME 551 6 PRECISION 552 7 LENGTH 553 8 SCALE 554 9 RADIX 555 10 NULLABLE 556 11 REMARKS 557 */ 558 while (!$rs->EOF) { 559 if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) { 560 $fld = new ADOFieldObject(); 561 $fld->name = $rs->fields[3]; 562 $fld->type = $this->DB2Types($rs->fields[4]); 563 564 // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp 565 // access uses precision to store length for char/varchar 566 if ($fld->type == 'C' or $fld->type == 'X') { 567 if ($rs->fields[4] <= -95) // UNICODE 568 $fld->max_length = $rs->fields[7]/2; 569 else 570 $fld->max_length = $rs->fields[7]; 571 } else 572 $fld->max_length = $rs->fields[7]; 573 $fld->not_null = !empty($rs->fields[10]); 574 $fld->scale = $rs->fields[8]; 575 $fld->primary_key = false; 576 $retarr[strtoupper($fld->name)] = $fld; 577 } else if (sizeof($retarr)>0) 578 break; 579 $rs->MoveNext(); 580 } 581 $rs->Close(); 582 if (empty($retarr)) $retarr = false; 583 584 $qid = db2_primary_keys($this->_connectionID, "", $schema, $table); 585 if (empty($qid)) return $false; 586 587 $rs = new ADORecordSet_db2($qid); 588 $ADODB_FETCH_MODE = $savem; 589 590 if (!$rs) return $retarr; 591 $rs->_fetch(); 592 593 /* 594 $rs->fields indices 595 0 TABLE_CAT 596 1 TABLE_SCHEM 597 2 TABLE_NAME 598 3 COLUMN_NAME 599 4 KEY_SEQ 600 5 PK_NAME 601 */ 602 while (!$rs->EOF) { 603 if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) { 604 $retarr[strtoupper($rs->fields[3])]->primary_key = true; 605 } else if (sizeof($retarr)>0) 606 break; 607 $rs->MoveNext(); 608 } 609 $rs->Close(); 610 611 if (empty($retarr)) $retarr = false; 612 return $retarr; 613 } 614 615 616 function Prepare($sql) 617 { 618 if (! $this->_bindInputArray) return $sql; // no binding 619 $stmt = db2_prepare($this->_connectionID,$sql); 620 if (!$stmt) { 621 // we don't know whether db2 driver is parsing prepared stmts, so just return sql 622 return $sql; 623 } 624 return array($sql,$stmt,false); 625 } 626 627 /* returns queryID or false */ 628 function _query($sql,$inputarr=false) 629 { 630 GLOBAL $php_errormsg; 631 if (isset($php_errormsg)) $php_errormsg = ''; 632 $this->_error = ''; 633 634 if ($inputarr) { 635 if (is_array($sql)) { 636 $stmtid = $sql[1]; 637 } else { 638 $stmtid = db2_prepare($this->_connectionID,$sql); 639 640 if ($stmtid == false) { 641 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; 642 return false; 643 } 644 } 645 646 if (! db2_execute($stmtid,$inputarr)) { 647 if ($this->_haserrorfunctions) { 648 $this->_errorMsg = db2_stmt_errormsg(); 649 $this->_errorCode = db2_stmt_error(); 650 } 651 return false; 652 } 653 654 } else if (is_array($sql)) { 655 $stmtid = $sql[1]; 656 if (!db2_execute($stmtid)) { 657 if ($this->_haserrorfunctions) { 658 $this->_errorMsg = db2_stmt_errormsg(); 659 $this->_errorCode = db2_stmt_error(); 660 } 661 return false; 662 } 663 } else 664 $stmtid = @db2_exec($this->_connectionID,$sql); 665 666 $this->_lastAffectedRows = 0; 667 if ($stmtid) { 668 if (@db2_num_fields($stmtid) == 0) { 669 $this->_lastAffectedRows = db2_num_rows($stmtid); 670 $stmtid = true; 671 } else { 672 $this->_lastAffectedRows = 0; 673 } 674 675 if ($this->_haserrorfunctions) { 676 $this->_errorMsg = ''; 677 $this->_errorCode = 0; 678 } else 679 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; 680 } else { 681 if ($this->_haserrorfunctions) { 682 $this->_errorMsg = db2_stmt_errormsg(); 683 $this->_errorCode = db2_stmt_error(); 684 } else 685 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; 686 687 } 688 return $stmtid; 689 } 690 691 /* 692 Insert a null into the blob field of the table first. 693 Then use UpdateBlob to store the blob. 694 695 Usage: 696 697 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)'); 698 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1'); 699 */ 700 function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB') 701 { 702 return $this->Execute("UPDATE $table SET $column=? WHERE $where",array($val)) != false; 703 } 704 705 // returns true or false 706 function _close() 707 { 708 $ret = @db2_close($this->_connectionID); 709 $this->_connectionID = false; 710 return $ret; 711 } 712 713 function _affectedrows() 714 { 715 return $this->_lastAffectedRows; 716 } 717 718 } 719 720 /*-------------------------------------------------------------------------------------- 721 Class Name: Recordset 722 --------------------------------------------------------------------------------------*/ 723 724 class ADORecordSet_db2 extends ADORecordSet { 725 726 var $bind = false; 727 var $databaseType = "db2"; 728 var $dataProvider = "db2"; 729 var $useFetchArray; 730 731 function __construct($id,$mode=false) 732 { 733 if ($mode === false) { 734 global $ADODB_FETCH_MODE; 735 $mode = $ADODB_FETCH_MODE; 736 } 737 $this->fetchMode = $mode; 738 739 $this->_queryID = $id; 740 } 741 742 743 // returns the field object 744 function FetchField($offset = -1) 745 { 746 $o= new ADOFieldObject(); 747 $o->name = @db2_field_name($this->_queryID,$offset); 748 $o->type = @db2_field_type($this->_queryID,$offset); 749 $o->max_length = db2_field_width($this->_queryID,$offset); 750 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name); 751 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name); 752 return $o; 753 } 754 755 /* Use associative array to get fields array */ 756 function Fields($colname) 757 { 758 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname]; 759 if (!$this->bind) { 760 $this->bind = array(); 761 for ($i=0; $i < $this->_numOfFields; $i++) { 762 $o = $this->FetchField($i); 763 $this->bind[strtoupper($o->name)] = $i; 764 } 765 } 766 767 return $this->fields[$this->bind[strtoupper($colname)]]; 768 } 769 770 771 function _initrs() 772 { 773 global $ADODB_COUNTRECS; 774 $this->_numOfRows = ($ADODB_COUNTRECS) ? @db2_num_rows($this->_queryID) : -1; 775 $this->_numOfFields = @db2_num_fields($this->_queryID); 776 // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0 777 if ($this->_numOfRows == 0) $this->_numOfRows = -1; 778 } 779 780 function _seek($row) 781 { 782 return false; 783 } 784 785 // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated 786 function GetArrayLimit($nrows,$offset=-1) 787 { 788 if ($offset <= 0) { 789 $rs = $this->GetArray($nrows); 790 return $rs; 791 } 792 $savem = $this->fetchMode; 793 $this->fetchMode = ADODB_FETCH_NUM; 794 $this->Move($offset); 795 $this->fetchMode = $savem; 796 797 if ($this->fetchMode & ADODB_FETCH_ASSOC) { 798 $this->fields = $this->GetRowAssoc(); 799 } 800 801 $results = array(); 802 $cnt = 0; 803 while (!$this->EOF && $nrows != $cnt) { 804 $results[$cnt++] = $this->fields; 805 $this->MoveNext(); 806 } 807 808 return $results; 809 } 810 811 812 function MoveNext() 813 { 814 if ($this->_numOfRows != 0 && !$this->EOF) { 815 $this->_currentRow++; 816 817 $this->fields = @db2_fetch_array($this->_queryID); 818 if ($this->fields) { 819 if ($this->fetchMode & ADODB_FETCH_ASSOC) { 820 $this->fields = $this->GetRowAssoc(); 821 } 822 return true; 823 } 824 } 825 $this->fields = false; 826 $this->EOF = true; 827 return false; 828 } 829 830 function _fetch() 831 { 832 833 $this->fields = db2_fetch_array($this->_queryID); 834 if ($this->fields) { 835 if ($this->fetchMode & ADODB_FETCH_ASSOC) { 836 $this->fields = $this->GetRowAssoc(); 837 } 838 return true; 839 } 840 $this->fields = false; 841 return false; 842 } 843 844 function _close() 845 { 846 return @db2_free_result($this->_queryID); 847 } 848 849 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |