Db2.php 27 KB

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