Db2.php 6.7 KB

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