PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/adodb/drivers/adodb-db2.inc.php

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