Db2.php 6.5 KB

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