Firebird.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Firebird.php 6847 2007-11-18 05:24:21Z peptolab $
  21. */
  22. /**
  23. * @see Zend_Db_TestUtil_Common
  24. */
  25. require_once 'Zend/Db/TestUtil/Common.php';
  26. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  27. /**
  28. * @category Zend
  29. * @package Zend_Db
  30. * @subpackage Table
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Db_TestUtil_Firebird extends Zend_Db_TestUtil_Common
  35. {
  36. public function tearDown()
  37. {
  38. $this->_setUpDatabase(false);
  39. }
  40. private function _getConnString()
  41. {
  42. return '"' .
  43. TESTS_ZEND_DB_ADAPTER_FIREBIRD_HOSTNAME .
  44. ( TESTS_ZEND_DB_ADAPTER_FIREBIRD_PORT ? '/' . TESTS_ZEND_DB_ADAPTER_FIREBIRD_PORT : '' ) .
  45. TESTS_ZEND_DB_ADAPTER_FIREBIRD_DATABASE .
  46. '" USER "' .
  47. TESTS_ZEND_DB_ADAPTER_FIREBIRD_USERNAME .
  48. '" PASSWORD "' .
  49. TESTS_ZEND_DB_ADAPTER_FIREBIRD_PASSWORD .
  50. '";';
  51. }
  52. private function _setUpDatabase($create = true)
  53. {
  54. $temp_file = tempnam(sys_get_temp_dir(), 'fbtest.sql');
  55. $cmd = 'CONNECT ' . $this->_getConnString() .
  56. 'DROP DATABASE;' .
  57. ( $create ? 'CREATE DATABASE ' . $this->_getConnString() : '');
  58. file_put_contents($temp_file, $cmd);
  59. exec('"TESTS_ZEND_DB_ADAPTER_FIREBIRD_BINPATH" -i ' . $temp_file, $output, $return_var);
  60. unlink($temp_file);
  61. }
  62. public function setUp(Zend_Db_Adapter_Abstract $db)
  63. {
  64. $this->_setUpDatabase();
  65. parent::setUp($db);
  66. $this->createSequence('zfbugs_seq');
  67. $this->createSequence('zfproducts_seq');
  68. $zfbugs_seq = $this->_db->quoteIdentifier('zfbugs_seq');
  69. $zfproducts_seq = $this->_db->quoteIdentifier('zfproducts_seq');
  70. $this->_rawQuery("SET GENERATOR $zfbugs_seq TO 4");
  71. $this->_rawQuery("SET GENERATOR $zfproducts_seq TO 3");
  72. }
  73. protected function _getDataProducts()
  74. {
  75. return array(
  76. array('product_id' => 1, 'product_name' => 'Windows'),
  77. array('product_id' => 2, 'product_name' => 'Linux'),
  78. array('product_id' => 3, 'product_name' => 'OS X'),
  79. );
  80. }
  81. protected function _getColumnsBugs()
  82. {
  83. return array(
  84. 'bug_id' => 'IDENTITY',
  85. 'bug_description' => 'VARCHAR(100)',
  86. 'bug_status' => 'VARCHAR(20)',
  87. 'created_on' => 'TIMESTAMP',
  88. 'updated_on' => 'TIMESTAMP',
  89. 'reported_by' => 'VARCHAR(100)',
  90. 'assigned_to' => 'VARCHAR(100)',
  91. 'verified_by' => 'VARCHAR(100)'
  92. );
  93. }
  94. protected function _getDataBugs()
  95. {
  96. return array(
  97. array(
  98. 'bug_id' => 1,
  99. 'bug_description' => 'System needs electricity to run',
  100. 'bug_status' => 'NEW',
  101. 'created_on' => '2007-04-01',
  102. 'updated_on' => '2007-04-01',
  103. 'reported_by' => 'goofy',
  104. 'assigned_to' => 'mmouse',
  105. 'verified_by' => 'dduck'
  106. ),
  107. array(
  108. 'bug_id' => 2,
  109. 'bug_description' => 'Implement Do What I Mean function',
  110. 'bug_status' => 'VERIFIED',
  111. 'created_on' => '2007-04-02',
  112. 'updated_on' => '2007-04-02',
  113. 'reported_by' => 'goofy',
  114. 'assigned_to' => 'mmouse',
  115. 'verified_by' => 'dduck'
  116. ),
  117. array(
  118. 'bug_id' => 3,
  119. 'bug_description' => 'Where are my keys?',
  120. 'bug_status' => 'FIXED',
  121. 'created_on' => '2007-04-03',
  122. 'updated_on' => '2007-04-03',
  123. 'reported_by' => 'dduck',
  124. 'assigned_to' => 'mmouse',
  125. 'verified_by' => 'dduck'
  126. ),
  127. array(
  128. 'bug_id' => 4,
  129. 'bug_description' => 'Bug no product',
  130. 'bug_status' => 'INCOMPLETE',
  131. 'created_on' => '2007-04-04',
  132. 'updated_on' => '2007-04-04',
  133. 'reported_by' => 'mmouse',
  134. 'assigned_to' => 'goofy',
  135. 'verified_by' => 'dduck'
  136. )
  137. );
  138. }
  139. protected function _getColumnsDocuments()
  140. {
  141. return array(
  142. 'doc_id' => 'INTEGER NOT NULL',
  143. 'doc_clob' => 'BLOB',
  144. 'doc_blob' => 'BLOB',
  145. 'PRIMARY KEY' => 'doc_id'
  146. );
  147. }
  148. public function getParams(array $constants = array())
  149. {
  150. $constants = array(
  151. 'host' => 'TESTS_ZEND_DB_ADAPTER_FIREBIRD_HOSTNAME',
  152. 'username' => 'TESTS_ZEND_DB_ADAPTER_FIREBIRD_USERNAME',
  153. 'password' => 'TESTS_ZEND_DB_ADAPTER_FIREBIRD_PASSWORD',
  154. 'dbname' => 'TESTS_ZEND_DB_ADAPTER_FIREBIRD_DATABASE',
  155. 'port' => 'TESTS_ZEND_DB_ADAPTER_FIREBIRD_PORT'
  156. );
  157. return parent::getParams($constants);
  158. }
  159. public function getSqlType($type)
  160. {
  161. if ($type == 'IDENTITY') {
  162. return 'INTEGER NOT NULL PRIMARY KEY';
  163. }
  164. return $type;
  165. }
  166. protected function _getSqlCreateSequence($sequenceName)
  167. {
  168. $sequenceName = $this->_db->quoteIdentifier($sequenceName);
  169. return "CREATE GENERATOR $sequenceName";
  170. }
  171. protected function _getSqlDropSequence($sequenceName)
  172. {
  173. $sequenceName = $this->_db->quoteIdentifier($sequenceName);
  174. return "DROP GENERATOR $sequenceName";
  175. }
  176. protected function _rawQuery($sql)
  177. {
  178. $conn = $this->_db->getConnection();
  179. try {
  180. ibase_query($conn, $sql);
  181. ibase_commit($conn);
  182. } catch (Exception $e) {
  183. if (!stripos(' '.$sql, 'drop')){
  184. $e = ibase_errmsg();
  185. require_once 'Zend/Db/Exception.php';
  186. throw new Zend_Db_Exception("SQL parse error for \"$sql\": ".$e);
  187. }
  188. }
  189. }
  190. }