PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 860 lines | 627 code | 119 blank | 114 comment | 167 complexity | c7be04c3ea2dc0337f26da43bf83dc6d MD5 | raw file
  1. <?php
  2. /*
  3. V5.06 16 Oct 2008 (c) 2006 John Lim (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. if (!empty($this->host) && ADODB_PHPVER >= 0x4300) {
  174. $dsn = strtoupper($this->host);
  175. $first = true;
  176. $found = false;
  177. if (!function_exists('db2_data_source')) return false;
  178. while(true) {
  179. $rez = @db2_data_source($this->_connectionID,
  180. $first ? SQL_FETCH_FIRST : SQL_FETCH_NEXT);
  181. $first = false;
  182. if (!is_array($rez)) break;
  183. if (strtoupper($rez['server']) == $dsn) {
  184. $found = true;
  185. break;
  186. }
  187. }
  188. if (!$found) return ADOConnection::ServerInfo();
  189. if (!isset($rez['version'])) $rez['version'] = '';
  190. return $rez;
  191. } else {
  192. return ADOConnection::ServerInfo();
  193. }
  194. }
  195. function CreateSequence($seqname='adodbseq',$start=1)
  196. {
  197. if (empty($this->_genSeqSQL)) return false;
  198. $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname,$start));
  199. if (!$ok) return false;
  200. return true;
  201. }
  202. function DropSequence($seqname)
  203. {
  204. if (empty($this->_dropSeqSQL)) return false;
  205. return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
  206. }
  207. function SelectLimit($sql,$nrows=-1,$offset=-1,$inputArr=false)
  208. {
  209. $nrows = (integer) $nrows;
  210. if ($offset <= 0) {
  211. // could also use " OPTIMIZE FOR $nrows ROWS "
  212. if ($nrows >= 0) $sql .= " FETCH FIRST $nrows ROWS ONLY ";
  213. $rs = $this->Execute($sql,$inputArr);
  214. } else {
  215. if ($offset > 0 && $nrows < 0);
  216. else {
  217. $nrows += $offset;
  218. $sql .= " FETCH FIRST $nrows ROWS ONLY ";
  219. }
  220. $rs = ADOConnection::SelectLimit($sql,-1,$offset,$inputArr);
  221. }
  222. return $rs;
  223. }
  224. /*
  225. This algorithm is not very efficient, but works even if table locking
  226. is not available.
  227. Will return false if unable to generate an ID after $MAXLOOPS attempts.
  228. */
  229. function GenID($seq='adodbseq',$start=1)
  230. {
  231. // if you have to modify the parameter below, your database is overloaded,
  232. // or you need to implement generation of id's yourself!
  233. $num = $this->GetOne("VALUES NEXTVAL FOR $seq");
  234. return $num;
  235. }
  236. function ErrorMsg()
  237. {
  238. if ($this->_haserrorfunctions) {
  239. if ($this->_errorMsg !== false) return $this->_errorMsg;
  240. if (empty($this->_connectionID)) return @db2_conn_errormsg();
  241. return @db2_conn_errormsg($this->_connectionID);
  242. } else return ADOConnection::ErrorMsg();
  243. }
  244. function ErrorNo()
  245. {
  246. if ($this->_haserrorfunctions) {
  247. if ($this->_errorCode !== false) {
  248. // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
  249. return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode;
  250. }
  251. if (empty($this->_connectionID)) $e = @db2_conn_error();
  252. else $e = @db2_conn_error($this->_connectionID);
  253. // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
  254. // so we check and patch
  255. if (strlen($e)<=2) return 0;
  256. return $e;
  257. } else return ADOConnection::ErrorNo();
  258. }
  259. function BeginTrans()
  260. {
  261. if (!$this->hasTransactions) return false;
  262. if ($this->transOff) return true;
  263. $this->transCnt += 1;
  264. $this->_autocommit = false;
  265. return db2_autocommit($this->_connectionID,false);
  266. }
  267. function CommitTrans($ok=true)
  268. {
  269. if ($this->transOff) return true;
  270. if (!$ok) return $this->RollbackTrans();
  271. if ($this->transCnt) $this->transCnt -= 1;
  272. $this->_autocommit = true;
  273. $ret = db2_commit($this->_connectionID);
  274. db2_autocommit($this->_connectionID,true);
  275. return $ret;
  276. }
  277. function RollbackTrans()
  278. {
  279. if ($this->transOff) return true;
  280. if ($this->transCnt) $this->transCnt -= 1;
  281. $this->_autocommit = true;
  282. $ret = db2_rollback($this->_connectionID);
  283. db2_autocommit($this->_connectionID,true);
  284. return $ret;
  285. }
  286. function MetaPrimaryKeys($table)
  287. {
  288. global $ADODB_FETCH_MODE;
  289. if ($this->uCaseTables) $table = strtoupper($table);
  290. $schema = '';
  291. $this->_findschema($table,$schema);
  292. $savem = $ADODB_FETCH_MODE;
  293. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  294. $qid = @db2_primarykeys($this->_connectionID,'',$schema,$table);
  295. if (!$qid) {
  296. $ADODB_FETCH_MODE = $savem;
  297. return false;
  298. }
  299. $rs = new ADORecordSet_db2($qid);
  300. $ADODB_FETCH_MODE = $savem;
  301. if (!$rs) return false;
  302. $arr = $rs->GetArray();
  303. $rs->Close();
  304. $arr2 = array();
  305. for ($i=0; $i < sizeof($arr); $i++) {
  306. if ($arr[$i][3]) $arr2[] = $arr[$i][3];
  307. }
  308. return $arr2;
  309. }
  310. function MetaForeignKeys($table, $owner = FALSE, $upper = FALSE, $asociative = FALSE )
  311. {
  312. global $ADODB_FETCH_MODE;
  313. if ($this->uCaseTables) $table = strtoupper($table);
  314. $schema = '';
  315. $this->_findschema($table,$schema);
  316. $savem = $ADODB_FETCH_MODE;
  317. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  318. $qid = @db2_foreign_keys($this->_connectionID,'',$schema,$table);
  319. if (!$qid) {
  320. $ADODB_FETCH_MODE = $savem;
  321. return false;
  322. }
  323. $rs = new ADORecordSet_db2($qid);
  324. $ADODB_FETCH_MODE = $savem;
  325. /*
  326. $rs->fields indices
  327. 0 PKTABLE_CAT
  328. 1 PKTABLE_SCHEM
  329. 2 PKTABLE_NAME
  330. 3 PKCOLUMN_NAME
  331. 4 FKTABLE_CAT
  332. 5 FKTABLE_SCHEM
  333. 6 FKTABLE_NAME
  334. 7 FKCOLUMN_NAME
  335. */
  336. if (!$rs) return false;
  337. $foreign_keys = array();
  338. while (!$rs->EOF) {
  339. if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) {
  340. if (!is_array($foreign_keys[$rs->fields[5].'.'.$rs->fields[6]]))
  341. $foreign_keys[$rs->fields[5].'.'.$rs->fields[6]] = array();
  342. $foreign_keys[$rs->fields[5].'.'.$rs->fields[6]][$rs->fields[7]] = $rs->fields[3];
  343. }
  344. $rs->MoveNext();
  345. }
  346. $rs->Close();
  347. return $foreign_key;
  348. }
  349. function MetaTables($ttype=false,$schema=false)
  350. {
  351. global $ADODB_FETCH_MODE;
  352. $savem = $ADODB_FETCH_MODE;
  353. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  354. $qid = db2_tables($this->_connectionID);
  355. $rs = new ADORecordSet_db2($qid);
  356. $ADODB_FETCH_MODE = $savem;
  357. if (!$rs) {
  358. $false = false;
  359. return $false;
  360. }
  361. $arr = $rs->GetArray();
  362. $rs->Close();
  363. $arr2 = array();
  364. if ($ttype) {
  365. $isview = strncmp($ttype,'V',1) === 0;
  366. }
  367. for ($i=0; $i < sizeof($arr); $i++) {
  368. if (!$arr[$i][2]) continue;
  369. $type = $arr[$i][3];
  370. $owner = $arr[$i][1];
  371. $schemaval = ($schema) ? $arr[$i][1].'.' : '';
  372. if ($ttype) {
  373. if ($isview) {
  374. if (strncmp($type,'V',1) === 0) $arr2[] = $schemaval.$arr[$i][2];
  375. } else if (strncmp($owner,'SYS',3) !== 0) $arr2[] = $schemaval.$arr[$i][2];
  376. } else if (strncmp($owner,'SYS',3) !== 0) $arr2[] = $schemaval.$arr[$i][2];
  377. }
  378. return $arr2;
  379. }
  380. /*
  381. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/db2/htm/db2datetime_data_type_changes.asp
  382. / SQL data type codes /
  383. #define SQL_UNKNOWN_TYPE 0
  384. #define SQL_CHAR 1
  385. #define SQL_NUMERIC 2
  386. #define SQL_DECIMAL 3
  387. #define SQL_INTEGER 4
  388. #define SQL_SMALLINT 5
  389. #define SQL_FLOAT 6
  390. #define SQL_REAL 7
  391. #define SQL_DOUBLE 8
  392. #if (DB2VER >= 0x0300)
  393. #define SQL_DATETIME 9
  394. #endif
  395. #define SQL_VARCHAR 12
  396. / One-parameter shortcuts for date/time data types /
  397. #if (DB2VER >= 0x0300)
  398. #define SQL_TYPE_DATE 91
  399. #define SQL_TYPE_TIME 92
  400. #define SQL_TYPE_TIMESTAMP 93
  401. #define SQL_UNICODE (-95)
  402. #define SQL_UNICODE_VARCHAR (-96)
  403. #define SQL_UNICODE_LONGVARCHAR (-97)
  404. */
  405. function DB2Types($t)
  406. {
  407. switch ((integer)$t) {
  408. case 1:
  409. case 12:
  410. case 0:
  411. case -95:
  412. case -96:
  413. return 'C';
  414. case -97:
  415. case -1: //text
  416. return 'X';
  417. case -4: //image
  418. return 'B';
  419. case 9:
  420. case 91:
  421. return 'D';
  422. case 10:
  423. case 11:
  424. case 92:
  425. case 93:
  426. return 'T';
  427. case 4:
  428. case 5:
  429. case -6:
  430. return 'I';
  431. case -11: // uniqidentifier
  432. return 'R';
  433. case -7: //bit
  434. return 'L';
  435. default:
  436. return 'N';
  437. }
  438. }
  439. function MetaColumns($table, $normalize=true)
  440. {
  441. global $ADODB_FETCH_MODE;
  442. $false = false;
  443. if ($this->uCaseTables) $table = strtoupper($table);
  444. $schema = '';
  445. $this->_findschema($table,$schema);
  446. $savem = $ADODB_FETCH_MODE;
  447. $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
  448. $colname = "%";
  449. $qid = db2_columns($this->_connectionID, "", $schema, $table, $colname);
  450. if (empty($qid)) return $false;
  451. $rs = new ADORecordSet_db2($qid);
  452. $ADODB_FETCH_MODE = $savem;
  453. if (!$rs) return $false;
  454. $rs->_fetch();
  455. $retarr = array();
  456. /*
  457. $rs->fields indices
  458. 0 TABLE_QUALIFIER
  459. 1 TABLE_SCHEM
  460. 2 TABLE_NAME
  461. 3 COLUMN_NAME
  462. 4 DATA_TYPE
  463. 5 TYPE_NAME
  464. 6 PRECISION
  465. 7 LENGTH
  466. 8 SCALE
  467. 9 RADIX
  468. 10 NULLABLE
  469. 11 REMARKS
  470. */
  471. while (!$rs->EOF) {
  472. if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) {
  473. $fld = new ADOFieldObject();
  474. $fld->name = $rs->fields[3];
  475. $fld->type = $this->DB2Types($rs->fields[4]);
  476. // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp
  477. // access uses precision to store length for char/varchar
  478. if ($fld->type == 'C' or $fld->type == 'X') {
  479. if ($rs->fields[4] <= -95) // UNICODE
  480. $fld->max_length = $rs->fields[7]/2;
  481. else
  482. $fld->max_length = $rs->fields[7];
  483. } else
  484. $fld->max_length = $rs->fields[7];
  485. $fld->not_null = !empty($rs->fields[10]);
  486. $fld->scale = $rs->fields[8];
  487. $fld->primary_key = false;
  488. $retarr[strtoupper($fld->name)] = $fld;
  489. } else if (sizeof($retarr)>0)
  490. break;
  491. $rs->MoveNext();
  492. }
  493. $rs->Close();
  494. if (empty($retarr)) $retarr = false;
  495. $qid = db2_primary_keys($this->_connectionID, "", $schema, $table);
  496. if (empty($qid)) return $false;
  497. $rs = new ADORecordSet_db2($qid);
  498. $ADODB_FETCH_MODE = $savem;
  499. if (!$rs) return $retarr;
  500. $rs->_fetch();
  501. /*
  502. $rs->fields indices
  503. 0 TABLE_CAT
  504. 1 TABLE_SCHEM
  505. 2 TABLE_NAME
  506. 3 COLUMN_NAME
  507. 4 KEY_SEQ
  508. 5 PK_NAME
  509. */
  510. while (!$rs->EOF) {
  511. if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) {
  512. $retarr[strtoupper($rs->fields[3])]->primary_key = true;
  513. } else if (sizeof($retarr)>0)
  514. break;
  515. $rs->MoveNext();
  516. }
  517. $rs->Close();
  518. if (empty($retarr)) $retarr = false;
  519. return $retarr;
  520. }
  521. function Prepare($sql)
  522. {
  523. if (! $this->_bindInputArray) return $sql; // no binding
  524. $stmt = db2_prepare($this->_connectionID,$sql);
  525. if (!$stmt) {
  526. // we don't know whether db2 driver is parsing prepared stmts, so just return sql
  527. return $sql;
  528. }
  529. return array($sql,$stmt,false);
  530. }
  531. /* returns queryID or false */
  532. function _query($sql,$inputarr=false)
  533. {
  534. GLOBAL $php_errormsg;
  535. if (isset($php_errormsg)) $php_errormsg = '';
  536. $this->_error = '';
  537. if ($inputarr) {
  538. if (is_array($sql)) {
  539. $stmtid = $sql[1];
  540. } else {
  541. $stmtid = db2_prepare($this->_connectionID,$sql);
  542. if ($stmtid == false) {
  543. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  544. return false;
  545. }
  546. }
  547. if (! db2_execute($stmtid,$inputarr)) {
  548. if ($this->_haserrorfunctions) {
  549. $this->_errorMsg = db2_stmt_errormsg();
  550. $this->_errorCode = db2_stmt_error();
  551. }
  552. return false;
  553. }
  554. } else if (is_array($sql)) {
  555. $stmtid = $sql[1];
  556. if (!db2_execute($stmtid)) {
  557. if ($this->_haserrorfunctions) {
  558. $this->_errorMsg = db2_stmt_errormsg();
  559. $this->_errorCode = db2_stmt_error();
  560. }
  561. return false;
  562. }
  563. } else
  564. $stmtid = @db2_exec($this->_connectionID,$sql);
  565. $this->_lastAffectedRows = 0;
  566. if ($stmtid) {
  567. if (@db2_num_fields($stmtid) == 0) {
  568. $this->_lastAffectedRows = db2_num_rows($stmtid);
  569. $stmtid = true;
  570. } else {
  571. $this->_lastAffectedRows = 0;
  572. }
  573. if ($this->_haserrorfunctions) {
  574. $this->_errorMsg = '';
  575. $this->_errorCode = 0;
  576. } else
  577. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  578. } else {
  579. if ($this->_haserrorfunctions) {
  580. $this->_errorMsg = db2_stmt_errormsg();
  581. $this->_errorCode = db2_stmt_error();
  582. } else
  583. $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
  584. }
  585. return $stmtid;
  586. }
  587. /*
  588. Insert a null into the blob field of the table first.
  589. Then use UpdateBlob to store the blob.
  590. Usage:
  591. $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
  592. $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
  593. */
  594. function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
  595. {
  596. return $this->Execute("UPDATE $table SET $column=? WHERE $where",array($val)) != false;
  597. }
  598. // returns true or false
  599. function _close()
  600. {
  601. $ret = @db2_close($this->_connectionID);
  602. $this->_connectionID = false;
  603. return $ret;
  604. }
  605. function _affectedrows()
  606. {
  607. return $this->_lastAffectedRows;
  608. }
  609. }
  610. /*--------------------------------------------------------------------------------------
  611. Class Name: Recordset
  612. --------------------------------------------------------------------------------------*/
  613. class ADORecordSet_db2 extends ADORecordSet {
  614. var $bind = false;
  615. var $databaseType = "db2";
  616. var $dataProvider = "db2";
  617. var $useFetchArray;
  618. function ADORecordSet_db2($id,$mode=false)
  619. {
  620. if ($mode === false) {
  621. global $ADODB_FETCH_MODE;
  622. $mode = $ADODB_FETCH_MODE;
  623. }
  624. $this->fetchMode = $mode;
  625. $this->_queryID = $id;
  626. }
  627. // returns the field object
  628. function FetchField($offset = -1)
  629. {
  630. $o= new ADOFieldObject();
  631. $o->name = @db2_field_name($this->_queryID,$offset);
  632. $o->type = @db2_field_type($this->_queryID,$offset);
  633. $o->max_length = db2_field_width($this->_queryID,$offset);
  634. if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
  635. else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
  636. return $o;
  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. return $this->fields[$this->bind[strtoupper($colname)]];
  650. }
  651. function _initrs()
  652. {
  653. global $ADODB_COUNTRECS;
  654. $this->_numOfRows = ($ADODB_COUNTRECS) ? @db2_num_rows($this->_queryID) : -1;
  655. $this->_numOfFields = @db2_num_fields($this->_queryID);
  656. // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0
  657. if ($this->_numOfRows == 0) $this->_numOfRows = -1;
  658. }
  659. function _seek($row)
  660. {
  661. return false;
  662. }
  663. // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated
  664. function GetArrayLimit($nrows,$offset=-1)
  665. {
  666. if ($offset <= 0) {
  667. $rs = $this->GetArray($nrows);
  668. return $rs;
  669. }
  670. $savem = $this->fetchMode;
  671. $this->fetchMode = ADODB_FETCH_NUM;
  672. $this->Move($offset);
  673. $this->fetchMode = $savem;
  674. if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  675. $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE);
  676. }
  677. $results = array();
  678. $cnt = 0;
  679. while (!$this->EOF && $nrows != $cnt) {
  680. $results[$cnt++] = $this->fields;
  681. $this->MoveNext();
  682. }
  683. return $results;
  684. }
  685. function MoveNext()
  686. {
  687. if ($this->_numOfRows != 0 && !$this->EOF) {
  688. $this->_currentRow++;
  689. $this->fields = @db2_fetch_array($this->_queryID);
  690. if ($this->fields) {
  691. if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  692. $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE);
  693. }
  694. return true;
  695. }
  696. }
  697. $this->fields = false;
  698. $this->EOF = true;
  699. return false;
  700. }
  701. function _fetch()
  702. {
  703. $this->fields = db2_fetch_array($this->_queryID);
  704. if ($this->fields) {
  705. if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  706. $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE);
  707. }
  708. return true;
  709. }
  710. $this->fields = false;
  711. return false;
  712. }
  713. function _close()
  714. {
  715. return @db2_free_result($this->_queryID);
  716. }
  717. }
  718. ?>