adodb-db2.inc.php 22 KB

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