Db2.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 UnitTests
  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. require_once 'Zend/Db/TestUtil/Common.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Db
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Db_TestUtil_Db2 extends Zend_Db_TestUtil_Common
  31. {
  32. public function setUp(Zend_Db_Adapter_Abstract $db)
  33. {
  34. $this->setAdapter($db);
  35. $this->createSequence('zfproducts_seq');
  36. parent::setUp($db);
  37. }
  38. public function getParams(array $constants = array())
  39. {
  40. $constants = array(
  41. 'host' => 'TESTS_ZEND_DB_ADAPTER_DB2_HOSTNAME',
  42. 'username' => 'TESTS_ZEND_DB_ADAPTER_DB2_USERNAME',
  43. 'password' => 'TESTS_ZEND_DB_ADAPTER_DB2_PASSWORD',
  44. 'dbname' => 'TESTS_ZEND_DB_ADAPTER_DB2_DATABASE',
  45. 'port' => 'TESTS_ZEND_DB_ADAPTER_DB2_PORT'
  46. );
  47. $params = parent::getParams($constants);
  48. if (isset($GLOBALS['TESTS_ZEND_DB_ADAPTER_DB2_DRIVER_OPTIONS'])) {
  49. $params['driver_options'] = $GLOBALS['TESTS_ZEND_DB_ADAPTER_DB2_DRIVER_OPTIONS'];
  50. }
  51. return $params;
  52. }
  53. public function getSchema()
  54. {
  55. $desc = $this->_db->describeTable('zfproducts');
  56. return $desc['product_id']['SCHEMA_NAME'];
  57. }
  58. /**
  59. * For DB2, override the Products table to use an
  60. * explicit sequence-based column.
  61. */
  62. protected function _getColumnsProducts()
  63. {
  64. return array(
  65. 'product_id' => 'INT NOT NULL PRIMARY KEY',
  66. 'product_name' => 'VARCHAR(100)'
  67. );
  68. }
  69. protected function _getDataProducts()
  70. {
  71. $data = parent::_getDataProducts();
  72. foreach ($data as &$row) {
  73. $row['product_id'] = new Zend_Db_Expr('NEXTVAL FOR '.$this->_db->quoteIdentifier('zfproducts_seq', true));
  74. }
  75. return $data;
  76. }
  77. protected function _getDataDocuments()
  78. {
  79. return array (
  80. array(
  81. 'doc_id' => 1,
  82. 'doc_clob' => 'this is the clob that never ends...'.
  83. 'this is the clob that never ends...'.
  84. 'this is the clob that never ends...',
  85. 'doc_blob' => new Zend_Db_Expr("BLOB('this is the blob that never ends...".
  86. "this is the blob that never ends...".
  87. "this is the blob that never ends...')")
  88. )
  89. );
  90. }
  91. public function getSqlType($type)
  92. {
  93. if ($type == 'IDENTITY') {
  94. return 'INT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY';
  95. }
  96. if ($type == 'DATETIME') {
  97. return 'DATE';
  98. }
  99. return $type;
  100. }
  101. protected function _getSqlCreateTable($tableName)
  102. {
  103. if ($this->_db->isI5()) {
  104. $tableList = $this->_db->fetchCol('SELECT UPPER(T.TABLE_NAME) FROM QSYS2.TABLES T '
  105. . $this->_db->quoteInto(' WHERE UPPER(T.TABLE_NAME) = UPPER(?)', $tableName)
  106. );
  107. } else {
  108. $tableList = $this->_db->fetchCol('SELECT UPPER(T.TABLE_NAME) FROM SYSIBM.TABLES T '
  109. . $this->_db->quoteInto(' WHERE UPPER(T.TABLE_NAME) = UPPER(?)', $tableName)
  110. );
  111. }
  112. if (in_array(strtoupper($tableName), $tableList)) {
  113. return null;
  114. }
  115. return 'CREATE TABLE ' . $this->_db->quoteIdentifier($tableName, true);
  116. }
  117. protected function _getSqlDropTable($tableName)
  118. {
  119. if ($this->_db->isI5()) {
  120. $tableList = $this->_db->fetchCol('SELECT UPPER(T.TABLE_NAME) FROM QSYS2.TABLES T '
  121. . $this->_db->quoteInto(' WHERE UPPER(T.TABLE_NAME) = UPPER(?)', $tableName)
  122. );
  123. } else {
  124. $tableList = $this->_db->fetchCol('SELECT UPPER(T.TABLE_NAME) FROM SYSIBM.TABLES T '
  125. . $this->_db->quoteInto(' WHERE UPPER(T.TABLE_NAME) = UPPER(?)', $tableName)
  126. );
  127. }
  128. if (in_array(strtoupper($tableName), $tableList)) {
  129. return 'DROP TABLE ' . $this->_db->quoteIdentifier($tableName, true);
  130. }
  131. return null;
  132. }
  133. protected function _getSqlCreateSequence($sequenceName)
  134. {
  135. if ($this->_db->isI5()) {
  136. $sequenceQuery = 'SELECT UPPER(S.SEQNAME) FROM QSYS2.SYSSEQUENCES S '
  137. . $this->_db->quoteInto(' WHERE UPPER(S.SEQNAME) = UPPER(?)', $sequenceName);
  138. } else {
  139. $sequenceQuery = 'SELECT UPPER(S.SEQNAME) FROM SYSIBM.SYSSEQUENCES S '
  140. . $this->_db->quoteInto(' WHERE UPPER(S.SEQNAME) = UPPER(?)', $sequenceName);
  141. }
  142. $seqList = $this->_db->fetchCol($sequenceQuery);
  143. if (in_array(strtoupper($sequenceName), $seqList)) {
  144. return null;
  145. }
  146. return 'CREATE SEQUENCE ' . $this->_db->quoteIdentifier($sequenceName, true) . ' AS INT START WITH 1 INCREMENT BY 1 MINVALUE 1';
  147. }
  148. protected function _getSqlDropSequence($sequenceName)
  149. {
  150. if ($this->_db->isI5()) {
  151. $sequenceQuery = 'SELECT UPPER(S.SEQNAME) FROM QSYS2.SYSSEQUENCES S '
  152. . $this->_db->quoteInto(' WHERE UPPER(S.SEQNAME) = UPPER(?)', $sequenceName);
  153. } else {
  154. $sequenceQuery = 'SELECT UPPER(S.SEQNAME) FROM SYSIBM.SYSSEQUENCES S '
  155. . $this->_db->quoteInto(' WHERE UPPER(S.SEQNAME) = UPPER(?)', $sequenceName);
  156. }
  157. $seqList = $this->_db->fetchCol($sequenceQuery);
  158. if (in_array(strtoupper($sequenceName), $seqList)) {
  159. return 'DROP SEQUENCE ' . $this->_db->quoteIdentifier($sequenceName, true) . ' RESTRICT';
  160. }
  161. return null;
  162. }
  163. protected function _rawQuery($sql)
  164. {
  165. $conn = $this->_db->getConnection();
  166. $result = @db2_exec($conn, $sql);
  167. if (!$result) {
  168. $e = db2_stmt_errormsg();
  169. require_once 'Zend/Db/Exception.php';
  170. throw new Zend_Db_Exception("SQL error for \"$sql\": $e");
  171. }
  172. }
  173. }