Db2.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. *
  22. */
  23. /**
  24. * @see Zend_Db
  25. */
  26. require_once 'Zend/Db.php';
  27. /**
  28. * @see Zend_Db_Adapter_Abstract
  29. */
  30. require_once 'Zend/Db/Adapter/Abstract.php';
  31. /**
  32. * @see Zend_Db_Statement_Db2
  33. */
  34. require_once 'Zend/Db/Statement/Db2.php';
  35. /**
  36. * @package Zend_Db
  37. * @copyright Copyright (c) 2005-2009 Zend Technologies Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Db_Adapter_Db2 extends Zend_Db_Adapter_Abstract
  41. {
  42. /**
  43. * User-provided configuration.
  44. *
  45. * Basic keys are:
  46. *
  47. * username => (string) Connect to the database as this username.
  48. * password => (string) Password associated with the username.
  49. * host => (string) What host to connect to (default 127.0.0.1)
  50. * dbname => (string) The name of the database to user
  51. * protocol => (string) Protocol to use, defaults to "TCPIP"
  52. * port => (integer) Port number to use for TCP/IP if protocol is "TCPIP"
  53. * persistent => (boolean) Set TRUE to use a persistent connection (db2_pconnect)
  54. * os => (string) This should be set to 'i5' if the db is on an os400/i5
  55. * schema => (string) The default schema the connection should use
  56. *
  57. * @var array
  58. */
  59. protected $_config = array(
  60. 'dbname' => null,
  61. 'username' => null,
  62. 'password' => null,
  63. 'host' => 'localhost',
  64. 'port' => '50000',
  65. 'protocol' => 'TCPIP',
  66. 'persistent' => false,
  67. 'os' => null,
  68. 'schema' => null
  69. );
  70. /**
  71. * Execution mode
  72. *
  73. * @var int execution flag (DB2_AUTOCOMMIT_ON or DB2_AUTOCOMMIT_OFF)
  74. */
  75. protected $_execute_mode = DB2_AUTOCOMMIT_ON;
  76. /**
  77. * Default class name for a DB statement.
  78. *
  79. * @var string
  80. */
  81. protected $_defaultStmtClass = 'Zend_Db_Statement_Db2';
  82. protected $_isI5 = false;
  83. /**
  84. * Keys are UPPERCASE SQL datatypes or the constants
  85. * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
  86. *
  87. * Values are:
  88. * 0 = 32-bit integer
  89. * 1 = 64-bit integer
  90. * 2 = float or decimal
  91. *
  92. * @var array Associative array of datatypes to values 0, 1, or 2.
  93. */
  94. protected $_numericDataTypes = array(
  95. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  96. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  97. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  98. 'INTEGER' => Zend_Db::INT_TYPE,
  99. 'SMALLINT' => Zend_Db::INT_TYPE,
  100. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  101. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  102. 'NUMERIC' => Zend_Db::FLOAT_TYPE
  103. );
  104. /**
  105. * Creates a connection resource.
  106. *
  107. * @return void
  108. */
  109. protected function _connect()
  110. {
  111. if (is_resource($this->_connection)) {
  112. // connection already exists
  113. return;
  114. }
  115. if (!extension_loaded('ibm_db2')) {
  116. /**
  117. * @see Zend_Db_Adapter_Db2_Exception
  118. */
  119. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  120. throw new Zend_Db_Adapter_Db2_Exception('The IBM DB2 extension is required for this adapter but the extension is not loaded');
  121. }
  122. $this->_determineI5();
  123. if ($this->_config['persistent']) {
  124. // use persistent connection
  125. $conn_func_name = 'db2_pconnect';
  126. } else {
  127. // use "normal" connection
  128. $conn_func_name = 'db2_connect';
  129. }
  130. if (!isset($this->_config['driver_options']['autocommit'])) {
  131. // set execution mode
  132. $this->_config['driver_options']['autocommit'] = &$this->_execute_mode;
  133. }
  134. if (isset($this->_config['options'][Zend_Db::CASE_FOLDING])) {
  135. $caseAttrMap = array(
  136. Zend_Db::CASE_NATURAL => DB2_CASE_NATURAL,
  137. Zend_Db::CASE_UPPER => DB2_CASE_UPPER,
  138. Zend_Db::CASE_LOWER => DB2_CASE_LOWER
  139. );
  140. $this->_config['driver_options']['DB2_ATTR_CASE'] = $caseAttrMap[$this->_config['options'][Zend_Db::CASE_FOLDING]];
  141. }
  142. if ($this->_config['host'] !== 'localhost' && !$this->_isI5) {
  143. // if the host isn't localhost, use extended connection params
  144. $dbname = 'DRIVER={IBM DB2 ODBC DRIVER}' .
  145. ';DATABASE=' . $this->_config['dbname'] .
  146. ';HOSTNAME=' . $this->_config['host'] .
  147. ';PORT=' . $this->_config['port'] .
  148. ';PROTOCOL=' . $this->_config['protocol'] .
  149. ';UID=' . $this->_config['username'] .
  150. ';PWD=' . $this->_config['password'] .';';
  151. $this->_connection = $conn_func_name(
  152. $dbname,
  153. null,
  154. null,
  155. $this->_config['driver_options']
  156. );
  157. } else {
  158. // host is localhost, so use standard connection params
  159. $this->_connection = $conn_func_name(
  160. $this->_config['dbname'],
  161. $this->_config['username'],
  162. $this->_config['password'],
  163. $this->_config['driver_options']
  164. );
  165. }
  166. // check the connection
  167. if (!$this->_connection) {
  168. /**
  169. * @see Zend_Db_Adapter_Db2_Exception
  170. */
  171. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  172. throw new Zend_Db_Adapter_Db2_Exception(db2_conn_errormsg(), db2_conn_error());
  173. }
  174. }
  175. /**
  176. * Test if a connection is active
  177. *
  178. * @return boolean
  179. */
  180. public function isConnected()
  181. {
  182. return ((bool) (is_resource($this->_connection)
  183. && get_resource_type($this->_connection) == 'DB2 Connection'));
  184. }
  185. /**
  186. * Force the connection to close.
  187. *
  188. * @return void
  189. */
  190. public function closeConnection()
  191. {
  192. if ($this->isConnected()) {
  193. db2_close($this->_connection);
  194. }
  195. $this->_connection = null;
  196. }
  197. /**
  198. * Returns an SQL statement for preparation.
  199. *
  200. * @param string $sql The SQL statement with placeholders.
  201. * @return Zend_Db_Statement_Db2
  202. */
  203. public function prepare($sql)
  204. {
  205. $this->_connect();
  206. $stmtClass = $this->_defaultStmtClass;
  207. if (!class_exists($stmtClass)) {
  208. require_once 'Zend/Loader.php';
  209. Zend_Loader::loadClass($stmtClass);
  210. }
  211. $stmt = new $stmtClass($this, $sql);
  212. $stmt->setFetchMode($this->_fetchMode);
  213. return $stmt;
  214. }
  215. /**
  216. * Gets the execution mode
  217. *
  218. * @return int the execution mode (DB2_AUTOCOMMIT_ON or DB2_AUTOCOMMIT_OFF)
  219. */
  220. public function _getExecuteMode()
  221. {
  222. return $this->_execute_mode;
  223. }
  224. /**
  225. * @param integer $mode
  226. * @return void
  227. */
  228. public function _setExecuteMode($mode)
  229. {
  230. switch ($mode) {
  231. case DB2_AUTOCOMMIT_OFF:
  232. case DB2_AUTOCOMMIT_ON:
  233. $this->_execute_mode = $mode;
  234. db2_autocommit($this->_connection, $mode);
  235. break;
  236. default:
  237. /**
  238. * @see Zend_Db_Adapter_Db2_Exception
  239. */
  240. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  241. throw new Zend_Db_Adapter_Db2_Exception("execution mode not supported");
  242. break;
  243. }
  244. }
  245. /**
  246. * Quote a raw string.
  247. *
  248. * @param string $value Raw string
  249. * @return string Quoted string
  250. */
  251. protected function _quote($value)
  252. {
  253. if (is_int($value) || is_float($value)) {
  254. return $value;
  255. }
  256. /**
  257. * Use db2_escape_string() if it is present in the IBM DB2 extension.
  258. * But some supported versions of PHP do not include this function,
  259. * so fall back to default quoting in the parent class.
  260. */
  261. if (function_exists('db2_escape_string')) {
  262. return "'" . db2_escape_string($value) . "'";
  263. }
  264. return parent::_quote($value);
  265. }
  266. /**
  267. * @return string
  268. */
  269. public function getQuoteIdentifierSymbol()
  270. {
  271. $this->_connect();
  272. $info = db2_server_info($this->_connection);
  273. if ($info) {
  274. $identQuote = $info->IDENTIFIER_QUOTE_CHAR;
  275. } else {
  276. // db2_server_info() does not return result on some i5 OS version
  277. if ($this->_isI5) {
  278. $identQuote ="'";
  279. }
  280. }
  281. return $identQuote;
  282. }
  283. /**
  284. * Returns a list of the tables in the database.
  285. * @param string $schema OPTIONAL
  286. * @return array
  287. */
  288. public function listTables($schema = null)
  289. {
  290. $this->_connect();
  291. if ($schema === null && $this->_config['schema'] != null) {
  292. $schema = $this->_config['schema'];
  293. }
  294. $tables = array();
  295. if (!$this->_isI5) {
  296. if ($schema) {
  297. $stmt = db2_tables($this->_connection, null, $schema);
  298. } else {
  299. $stmt = db2_tables($this->_connection);
  300. }
  301. while ($row = db2_fetch_assoc($stmt)) {
  302. $tables[] = $row['TABLE_NAME'];
  303. }
  304. } else {
  305. $tables = $this->_i5listTables($schema);
  306. }
  307. return $tables;
  308. }
  309. /**
  310. * Returns the column descriptions for a table.
  311. *
  312. * The return value is an associative array keyed by the column name,
  313. * as returned by the RDBMS.
  314. *
  315. * The value of each array element is an associative array
  316. * with the following keys:
  317. *
  318. * SCHEMA_NAME => string; name of database or schema
  319. * TABLE_NAME => string;
  320. * COLUMN_NAME => string; column name
  321. * COLUMN_POSITION => number; ordinal position of column in table
  322. * DATA_TYPE => string; SQL datatype name of column
  323. * DEFAULT => string; default expression of column, null if none
  324. * NULLABLE => boolean; true if column can have nulls
  325. * LENGTH => number; length of CHAR/VARCHAR
  326. * SCALE => number; scale of NUMERIC/DECIMAL
  327. * PRECISION => number; precision of NUMERIC/DECIMAL
  328. * UNSIGNED => boolean; unsigned property of an integer type
  329. * DB2 not supports UNSIGNED integer.
  330. * PRIMARY => boolean; true if column is part of the primary key
  331. * PRIMARY_POSITION => integer; position of column in primary key
  332. * IDENTITY => integer; true if column is auto-generated with unique values
  333. *
  334. * @param string $tableName
  335. * @param string $schemaName OPTIONAL
  336. * @return array
  337. */
  338. public function describeTable($tableName, $schemaName = null)
  339. {
  340. // Ensure the connection is made so that _isI5 is set
  341. $this->_connect();
  342. if ($schemaName === null && $this->_config['schema'] != null) {
  343. $schemaName = $this->_config['schema'];
  344. }
  345. if (!$this->_isI5) {
  346. $sql = "SELECT DISTINCT c.tabschema, c.tabname, c.colname, c.colno,
  347. c.typename, c.default, c.nulls, c.length, c.scale,
  348. c.identity, tc.type AS tabconsttype, k.colseq
  349. FROM syscat.columns c
  350. LEFT JOIN (syscat.keycoluse k JOIN syscat.tabconst tc
  351. ON (k.tabschema = tc.tabschema
  352. AND k.tabname = tc.tabname
  353. AND tc.type = 'P'))
  354. ON (c.tabschema = k.tabschema
  355. AND c.tabname = k.tabname
  356. AND c.colname = k.colname)
  357. WHERE "
  358. . $this->quoteInto('UPPER(c.tabname) = UPPER(?)', $tableName);
  359. if ($schemaName) {
  360. $sql .= $this->quoteInto(' AND UPPER(c.tabschema) = UPPER(?)', $schemaName);
  361. }
  362. $sql .= " ORDER BY c.colno";
  363. } else {
  364. // DB2 On I5 specific query
  365. $sql = "SELECT DISTINCT C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME, C.ORDINAL_POSITION,
  366. C.DATA_TYPE, C.COLUMN_DEFAULT, C.NULLS ,C.LENGTH, C.SCALE, LEFT(C.IDENTITY,1),
  367. LEFT(tc.TYPE, 1) AS tabconsttype, k.COLSEQ
  368. FROM QSYS2.SYSCOLUMNS C
  369. LEFT JOIN (QSYS2.syskeycst k JOIN QSYS2.SYSCST tc
  370. ON (k.TABLE_SCHEMA = tc.TABLE_SCHEMA
  371. AND k.TABLE_NAME = tc.TABLE_NAME
  372. AND LEFT(tc.type,1) = 'P'))
  373. ON (C.TABLE_SCHEMA = k.TABLE_SCHEMA
  374. AND C.TABLE_NAME = k.TABLE_NAME
  375. AND C.COLUMN_NAME = k.COLUMN_NAME)
  376. WHERE "
  377. . $this->quoteInto('UPPER(C.TABLE_NAME) = UPPER(?)', $tableName);
  378. if ($schemaName) {
  379. $sql .= $this->quoteInto(' AND UPPER(C.TABLE_SCHEMA) = UPPER(?)', $schemaName);
  380. }
  381. $sql .= " ORDER BY C.ORDINAL_POSITION FOR FETCH ONLY";
  382. }
  383. $desc = array();
  384. $stmt = $this->query($sql);
  385. /**
  386. * To avoid case issues, fetch using FETCH_NUM
  387. */
  388. $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  389. /**
  390. * The ordering of columns is defined by the query so we can map
  391. * to variables to improve readability
  392. */
  393. $tabschema = 0;
  394. $tabname = 1;
  395. $colname = 2;
  396. $colno = 3;
  397. $typename = 4;
  398. $default = 5;
  399. $nulls = 6;
  400. $length = 7;
  401. $scale = 8;
  402. $identityCol = 9;
  403. $tabconstType = 10;
  404. $colseq = 11;
  405. foreach ($result as $key => $row) {
  406. list ($primary, $primaryPosition, $identity) = array(false, null, false);
  407. if ($row[$tabconstType] == 'P') {
  408. $primary = true;
  409. $primaryPosition = $row[$colseq];
  410. }
  411. /**
  412. * In IBM DB2, an column can be IDENTITY
  413. * even if it is not part of the PRIMARY KEY.
  414. */
  415. if ($row[$identityCol] == 'Y') {
  416. $identity = true;
  417. }
  418. // only colname needs to be case adjusted
  419. $desc[$this->foldCase($row[$colname])] = array(
  420. 'SCHEMA_NAME' => $this->foldCase($row[$tabschema]),
  421. 'TABLE_NAME' => $this->foldCase($row[$tabname]),
  422. 'COLUMN_NAME' => $this->foldCase($row[$colname]),
  423. 'COLUMN_POSITION' => (!$this->_isI5) ? $row[$colno]+1 : $row[$colno],
  424. 'DATA_TYPE' => $row[$typename],
  425. 'DEFAULT' => $row[$default],
  426. 'NULLABLE' => (bool) ($row[$nulls] == 'Y'),
  427. 'LENGTH' => $row[$length],
  428. 'SCALE' => $row[$scale],
  429. 'PRECISION' => ($row[$typename] == 'DECIMAL' ? $row[$length] : 0),
  430. 'UNSIGNED' => false,
  431. 'PRIMARY' => $primary,
  432. 'PRIMARY_POSITION' => $primaryPosition,
  433. 'IDENTITY' => $identity
  434. );
  435. }
  436. return $desc;
  437. }
  438. /**
  439. * Return the most recent value from the specified sequence in the database.
  440. * This is supported only on RDBMS brands that support sequences
  441. * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
  442. *
  443. * @param string $sequenceName
  444. * @return string
  445. */
  446. public function lastSequenceId($sequenceName)
  447. {
  448. $this->_connect();
  449. if (!$this->_isI5) {
  450. $quotedSequenceName = $this->quoteIdentifier($sequenceName, true);
  451. $sql = 'SELECT PREVVAL FOR ' . $quotedSequenceName . ' AS VAL FROM SYSIBM.SYSDUMMY1';
  452. } else {
  453. $quotedSequenceName = $sequenceName;
  454. $sql = 'SELECT PREVVAL FOR ' . $this->quoteIdentifier($sequenceName, true) . ' AS VAL FROM QSYS2.QSQPTABL';
  455. }
  456. $value = $this->fetchOne($sql);
  457. return (string) $value;
  458. }
  459. /**
  460. * Generate a new value from the specified sequence in the database, and return it.
  461. * This is supported only on RDBMS brands that support sequences
  462. * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
  463. *
  464. * @param string $sequenceName
  465. * @return string
  466. */
  467. public function nextSequenceId($sequenceName)
  468. {
  469. $this->_connect();
  470. $sql = 'SELECT NEXTVAL FOR '.$this->quoteIdentifier($sequenceName, true).' AS VAL FROM SYSIBM.SYSDUMMY1';
  471. $value = $this->fetchOne($sql);
  472. return (string) $value;
  473. }
  474. /**
  475. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  476. *
  477. * As a convention, on RDBMS brands that support sequences
  478. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  479. * from the arguments and returns the last id generated by that sequence.
  480. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  481. * returns the last value generated for such a column, and the table name
  482. * argument is disregarded.
  483. *
  484. * The IDENTITY_VAL_LOCAL() function gives the last generated identity value
  485. * in the current process, even if it was for a GENERATED column.
  486. *
  487. * @param string $tableName OPTIONAL
  488. * @param string $primaryKey OPTIONAL
  489. * @param string $idType OPTIONAL used for i5 platform to define sequence/idenity unique value
  490. * @return string
  491. */
  492. public function lastInsertId($tableName = null, $primaryKey = null, $idType = null)
  493. {
  494. $this->_connect();
  495. if ($this->_isI5) {
  496. return (string) $this->_i5LastInsertId($tableName, $idType);
  497. }
  498. if ($tableName !== null) {
  499. $sequenceName = $tableName;
  500. if ($primaryKey) {
  501. $sequenceName .= "_$primaryKey";
  502. }
  503. $sequenceName .= '_seq';
  504. return $this->lastSequenceId($sequenceName);
  505. }
  506. $sql = 'SELECT IDENTITY_VAL_LOCAL() AS VAL FROM SYSIBM.SYSDUMMY1';
  507. $value = $this->fetchOne($sql);
  508. return (string) $value;
  509. }
  510. /**
  511. * Begin a transaction.
  512. *
  513. * @return void
  514. */
  515. protected function _beginTransaction()
  516. {
  517. $this->_setExecuteMode(DB2_AUTOCOMMIT_OFF);
  518. }
  519. /**
  520. * Commit a transaction.
  521. *
  522. * @return void
  523. */
  524. protected function _commit()
  525. {
  526. if (!db2_commit($this->_connection)) {
  527. /**
  528. * @see Zend_Db_Adapter_Db2_Exception
  529. */
  530. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  531. throw new Zend_Db_Adapter_Db2_Exception(
  532. db2_conn_errormsg($this->_connection),
  533. db2_conn_error($this->_connection));
  534. }
  535. $this->_setExecuteMode(DB2_AUTOCOMMIT_ON);
  536. }
  537. /**
  538. * Rollback a transaction.
  539. *
  540. * @return void
  541. */
  542. protected function _rollBack()
  543. {
  544. if (!db2_rollback($this->_connection)) {
  545. /**
  546. * @see Zend_Db_Adapter_Db2_Exception
  547. */
  548. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  549. throw new Zend_Db_Adapter_Db2_Exception(
  550. db2_conn_errormsg($this->_connection),
  551. db2_conn_error($this->_connection));
  552. }
  553. $this->_setExecuteMode(DB2_AUTOCOMMIT_ON);
  554. }
  555. /**
  556. * Set the fetch mode.
  557. *
  558. * @param integer $mode
  559. * @return void
  560. * @throws Zend_Db_Adapter_Db2_Exception
  561. */
  562. public function setFetchMode($mode)
  563. {
  564. switch ($mode) {
  565. case Zend_Db::FETCH_NUM: // seq array
  566. case Zend_Db::FETCH_ASSOC: // assoc array
  567. case Zend_Db::FETCH_BOTH: // seq+assoc array
  568. case Zend_Db::FETCH_OBJ: // object
  569. $this->_fetchMode = $mode;
  570. break;
  571. case Zend_Db::FETCH_BOUND: // bound to PHP variable
  572. /**
  573. * @see Zend_Db_Adapter_Db2_Exception
  574. */
  575. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  576. throw new Zend_Db_Adapter_Db2_Exception('FETCH_BOUND is not supported yet');
  577. break;
  578. default:
  579. /**
  580. * @see Zend_Db_Adapter_Db2_Exception
  581. */
  582. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  583. throw new Zend_Db_Adapter_Db2_Exception("Invalid fetch mode '$mode' specified");
  584. break;
  585. }
  586. }
  587. /**
  588. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  589. *
  590. * @param string $sql
  591. * @param integer $count
  592. * @param integer $offset OPTIONAL
  593. * @return string
  594. */
  595. public function limit($sql, $count, $offset = 0)
  596. {
  597. $count = intval($count);
  598. if ($count <= 0) {
  599. /**
  600. * @see Zend_Db_Adapter_Db2_Exception
  601. */
  602. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  603. throw new Zend_Db_Adapter_Db2_Exception("LIMIT argument count=$count is not valid");
  604. }
  605. $offset = intval($offset);
  606. if ($offset < 0) {
  607. /**
  608. * @see Zend_Db_Adapter_Db2_Exception
  609. */
  610. require_once 'Zend/Db/Adapter/Db2/Exception.php';
  611. throw new Zend_Db_Adapter_Db2_Exception("LIMIT argument offset=$offset is not valid");
  612. }
  613. if ($offset == 0) {
  614. $limit_sql = $sql . " FETCH FIRST $count ROWS ONLY";
  615. return $limit_sql;
  616. }
  617. /**
  618. * DB2 does not implement the LIMIT clause as some RDBMS do.
  619. * We have to simulate it with subqueries and ROWNUM.
  620. * Unfortunately because we use the column wildcard "*",
  621. * this puts an extra column into the query result set.
  622. */
  623. $limit_sql = "SELECT z2.*
  624. FROM (
  625. SELECT ROW_NUMBER() OVER() AS \"ZEND_DB_ROWNUM\", z1.*
  626. FROM (
  627. " . $sql . "
  628. ) z1
  629. ) z2
  630. WHERE z2.zend_db_rownum BETWEEN " . ($offset+1) . " AND " . ($offset+$count);
  631. return $limit_sql;
  632. }
  633. /**
  634. * Check if the adapter supports real SQL parameters.
  635. *
  636. * @param string $type 'positional' or 'named'
  637. * @return bool
  638. */
  639. public function supportsParameters($type)
  640. {
  641. if ($type == 'positional') {
  642. return true;
  643. }
  644. // if its 'named' or anything else
  645. return false;
  646. }
  647. /**
  648. * Retrieve server version in PHP style
  649. *
  650. * @return string
  651. */
  652. public function getServerVersion()
  653. {
  654. $this->_connect();
  655. $server_info = db2_server_info($this->_connection);
  656. if ($server_info !== false) {
  657. $version = $server_info->DBMS_VER;
  658. if ($this->_isI5) {
  659. $version = (int) substr($version, 0, 2) . '.' . (int) substr($version, 2, 2) . '.' . (int) substr($version, 4);
  660. }
  661. return $version;
  662. } else {
  663. return null;
  664. }
  665. }
  666. /**
  667. * Return whether or not this is running on i5
  668. *
  669. * @return bool
  670. */
  671. public function isI5()
  672. {
  673. if ($this->_isI5 === null) {
  674. $this->_determineI5();
  675. }
  676. return (bool) $this->_isI5;
  677. }
  678. /**
  679. * Check the connection parameters according to verify
  680. * type of used OS
  681. *
  682. * @return void
  683. */
  684. protected function _determineI5()
  685. {
  686. // first us the compiled flag.
  687. $this->_isI5 = (php_uname('s') == 'OS400') ? true : false;
  688. // if this is set, then us it
  689. if (isset($this->_config['os'])){
  690. if (strtolower($this->_config['os']) === 'i5') {
  691. $this->_isI5 = true;
  692. } else {
  693. // any other value passed in, its null
  694. $this->_isI5 = false;
  695. }
  696. }
  697. }
  698. /**
  699. * Db2 On I5 specific method
  700. *
  701. * Returns a list of the tables in the database .
  702. * Used only for DB2/400.
  703. *
  704. * @return array
  705. */
  706. protected function _i5listTables($schema = null)
  707. {
  708. //list of i5 libraries.
  709. $tables = array();
  710. if ($schema) {
  711. $tablesStatement = db2_tables($this->_connection, null, $schema);
  712. while ($rowTables = db2_fetch_assoc($tablesStatement) ) {
  713. if ($rowTables['TABLE_NAME'] !== null) {
  714. $tables[] = $rowTables['TABLE_NAME'];
  715. }
  716. }
  717. } else {
  718. $schemaStatement = db2_tables($this->_connection);
  719. while ($schema = db2_fetch_assoc($schemaStatement)) {
  720. if ($schema['TABLE_SCHEM'] !== null) {
  721. // list of the tables which belongs to the selected library
  722. $tablesStatement = db2_tables($this->_connection, NULL, $schema['TABLE_SCHEM']);
  723. if (is_resource($tablesStatement)) {
  724. while ($rowTables = db2_fetch_assoc($tablesStatement) ) {
  725. if ($rowTables['TABLE_NAME'] !== null) {
  726. $tables[] = $rowTables['TABLE_NAME'];
  727. }
  728. }
  729. }
  730. }
  731. }
  732. }
  733. return $tables;
  734. }
  735. protected function _i5LastInsertId($objectName = null, $idType = null)
  736. {
  737. if ($objectName === null) {
  738. $sql = 'SELECT IDENTITY_VAL_LOCAL() AS VAL FROM QSYS2.QSQPTABL';
  739. $value = $this->fetchOne($sql);
  740. return $value;
  741. }
  742. if (strtoupper($idType) === 'S'){
  743. //check i5_lib option
  744. $sequenceName = $objectName;
  745. return $this->lastSequenceId($sequenceName);
  746. }
  747. //returns last identity value for the specified table
  748. //if (strtoupper($idType) === 'I') {
  749. $tableName = $objectName;
  750. return $this->fetchOne('SELECT IDENTITY_VAL_LOCAL() from ' . $this->quoteIdentifier($tableName));
  751. }
  752. }