Db2.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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-2015 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. /** @see Zend_Db_Adapter_Pdo_Ibm */
  23. require_once 'Zend/Db/Adapter/Pdo/Ibm.php';
  24. /** @see Zend_Db_Statement_Pdo_Ibm */
  25. require_once 'Zend/Db/Statement/Pdo/Ibm.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage Adapter
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Db_Adapter_Pdo_Ibm_Db2
  34. {
  35. /**
  36. * @var Zend_Db_Adapter_Abstract
  37. */
  38. protected $_adapter = null;
  39. /**
  40. * Construct the data server class.
  41. *
  42. * It will be used to generate non-generic SQL
  43. * for a particular data server
  44. *
  45. * @param Zend_Db_Adapter_Abstract $adapter
  46. */
  47. public function __construct($adapter)
  48. {
  49. $this->_adapter = $adapter;
  50. }
  51. /**
  52. * Returns a list of the tables in the database.
  53. *
  54. * @return array
  55. */
  56. public function listTables()
  57. {
  58. $sql = "SELECT tabname "
  59. . "FROM SYSCAT.TABLES ";
  60. return $this->_adapter->fetchCol($sql);
  61. }
  62. /**
  63. * DB2 catalog lookup for describe table
  64. *
  65. * @param string $tableName
  66. * @param string $schemaName OPTIONAL
  67. * @return array
  68. */
  69. public function describeTable($tableName, $schemaName = null)
  70. {
  71. $sql = "SELECT DISTINCT c.tabschema, c.tabname, c.colname, c.colno,
  72. c.typename, c.default, c.nulls, c.length, c.scale,
  73. c.identity, tc.type AS tabconsttype, k.colseq
  74. FROM syscat.columns c
  75. LEFT JOIN (syscat.keycoluse k JOIN syscat.tabconst tc
  76. ON (k.tabschema = tc.tabschema
  77. AND k.tabname = tc.tabname
  78. AND tc.type = 'P'))
  79. ON (c.tabschema = k.tabschema
  80. AND c.tabname = k.tabname
  81. AND c.colname = k.colname)
  82. WHERE "
  83. . $this->_adapter->quoteInto('UPPER(c.tabname) = UPPER(?)', $tableName);
  84. if ($schemaName) {
  85. $sql .= $this->_adapter->quoteInto(' AND UPPER(c.tabschema) = UPPER(?)', $schemaName);
  86. }
  87. $sql .= " ORDER BY c.colno";
  88. $desc = array();
  89. $stmt = $this->_adapter->query($sql);
  90. /**
  91. * To avoid case issues, fetch using FETCH_NUM
  92. */
  93. $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  94. /**
  95. * The ordering of columns is defined by the query so we can map
  96. * to variables to improve readability
  97. */
  98. $tabschema = 0;
  99. $tabname = 1;
  100. $colname = 2;
  101. $colno = 3;
  102. $typename = 4;
  103. $default = 5;
  104. $nulls = 6;
  105. $length = 7;
  106. $scale = 8;
  107. $identityCol = 9;
  108. $tabconstype = 10;
  109. $colseq = 11;
  110. foreach ($result as $key => $row) {
  111. list ($primary, $primaryPosition, $identity) = array(false, null, false);
  112. if ($row[$tabconstype] == 'P') {
  113. $primary = true;
  114. $primaryPosition = $row[$colseq];
  115. }
  116. /**
  117. * In IBM DB2, an column can be IDENTITY
  118. * even if it is not part of the PRIMARY KEY.
  119. */
  120. if ($row[$identityCol] == 'Y') {
  121. $identity = true;
  122. }
  123. $desc[$this->_adapter->foldCase($row[$colname])] = array(
  124. 'SCHEMA_NAME' => $this->_adapter->foldCase($row[$tabschema]),
  125. 'TABLE_NAME' => $this->_adapter->foldCase($row[$tabname]),
  126. 'COLUMN_NAME' => $this->_adapter->foldCase($row[$colname]),
  127. 'COLUMN_POSITION' => $row[$colno]+1,
  128. 'DATA_TYPE' => $row[$typename],
  129. 'DEFAULT' => $row[$default],
  130. 'NULLABLE' => (bool) ($row[$nulls] == 'Y'),
  131. 'LENGTH' => $row[$length],
  132. 'SCALE' => $row[$scale],
  133. 'PRECISION' => ($row[$typename] == 'DECIMAL' ? $row[$length] : 0),
  134. 'UNSIGNED' => false,
  135. 'PRIMARY' => $primary,
  136. 'PRIMARY_POSITION' => $primaryPosition,
  137. 'IDENTITY' => $identity
  138. );
  139. }
  140. return $desc;
  141. }
  142. /**
  143. * Adds a DB2-specific LIMIT clause to the SELECT statement.
  144. *
  145. * @param string $sql
  146. * @param integer $count
  147. * @param integer $offset OPTIONAL
  148. * @throws Zend_Db_Adapter_Exception
  149. * @return string
  150. */
  151. public function limit($sql, $count, $offset = 0)
  152. {
  153. $count = intval($count);
  154. if ($count < 0) {
  155. /** @see Zend_Db_Adapter_Exception */
  156. require_once 'Zend/Db/Adapter/Exception.php';
  157. throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
  158. } else {
  159. $offset = intval($offset);
  160. if ($offset < 0) {
  161. /** @see Zend_Db_Adapter_Exception */
  162. require_once 'Zend/Db/Adapter/Exception.php';
  163. throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
  164. }
  165. if ($offset == 0 && $count > 0) {
  166. $limit_sql = $sql . " FETCH FIRST $count ROWS ONLY";
  167. return $limit_sql;
  168. }
  169. /**
  170. * DB2 does not implement the LIMIT clause as some RDBMS do.
  171. * We have to simulate it with subqueries and ROWNUM.
  172. * Unfortunately because we use the column wildcard "*",
  173. * this puts an extra column into the query result set.
  174. */
  175. $limit_sql = "SELECT z2.*
  176. FROM (
  177. SELECT ROW_NUMBER() OVER() AS \"ZEND_DB_ROWNUM\", z1.*
  178. FROM (
  179. " . $sql . "
  180. ) z1
  181. ) z2
  182. WHERE z2.zend_db_rownum BETWEEN " . ($offset+1) . " AND " . ($offset+$count);
  183. }
  184. return $limit_sql;
  185. }
  186. /**
  187. * DB2-specific last sequence id
  188. *
  189. * @param string $sequenceName
  190. * @return integer
  191. */
  192. public function lastSequenceId($sequenceName)
  193. {
  194. $sql = 'SELECT PREVVAL FOR '.$this->_adapter->quoteIdentifier($sequenceName).' AS VAL FROM SYSIBM.SYSDUMMY1';
  195. $value = $this->_adapter->fetchOne($sql);
  196. return $value;
  197. }
  198. /**
  199. * DB2-specific sequence id value
  200. *
  201. * @param string $sequenceName
  202. * @return integer
  203. */
  204. public function nextSequenceId($sequenceName)
  205. {
  206. $sql = 'SELECT NEXTVAL FOR '.$this->_adapter->quoteIdentifier($sequenceName).' AS VAL FROM SYSIBM.SYSDUMMY1';
  207. $value = $this->_adapter->fetchOne($sql);
  208. return $value;
  209. }
  210. }