PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/adodb/drivers/adodb-db2.inc.php

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