2
0

Db2Test.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. /**
  23. * @see Zend_Db_Adapter_TestCommon
  24. */
  25. require_once 'Zend/Db/Adapter/TestCommon.php';
  26. /**
  27. * @see Zend_Db_Adapter_Db2
  28. */
  29. require_once 'Zend/Db/Adapter/Db2.php';
  30. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  31. /**
  32. * @category Zend
  33. * @package Zend_Db
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Db
  38. * @group Zend_Db_Adapter
  39. */
  40. class Zend_Db_Adapter_Db2Test extends Zend_Db_Adapter_TestCommon
  41. {
  42. protected $_numericDataTypes = array(
  43. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  44. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  45. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  46. 'INTEGER' => Zend_Db::INT_TYPE,
  47. 'SMALLINT' => Zend_Db::INT_TYPE,
  48. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  49. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  50. 'NUMERIC' => Zend_Db::FLOAT_TYPE
  51. );
  52. public function testAdapterDescribeTablePrimaryAuto()
  53. {
  54. $desc = $this->_db->describeTable('zfbugs');
  55. $this->assertTrue($desc['bug_id']['PRIMARY']);
  56. $this->assertEquals(1, $desc['bug_id']['PRIMARY_POSITION']);
  57. $this->assertTrue($desc['bug_id']['IDENTITY']);
  58. }
  59. public function testAdapterDescribeTableAttributeColumn()
  60. {
  61. $desc = $this->_db->describeTable('zfproducts');
  62. $this->assertEquals('zfproducts', $desc['product_name']['TABLE_NAME'], 'Expected table name to be zfproducts');
  63. $this->assertEquals('product_name', $desc['product_name']['COLUMN_NAME'], 'Expected column name to be product_name');
  64. $this->assertEquals(2, $desc['product_name']['COLUMN_POSITION'], 'Expected column position to be 2');
  65. $this->assertRegExp('/varchar/i', $desc['product_name']['DATA_TYPE'], 'Expected data type to be VARCHAR');
  66. $this->assertEquals('', $desc['product_name']['DEFAULT'], 'Expected default to be empty string');
  67. $this->assertTrue( $desc['product_name']['NULLABLE'], 'Expected product_name to be nullable');
  68. if (!$this->_db->isI5()) {
  69. $this->assertEquals(0, $desc['product_name']['SCALE'], 'Expected scale to be 0');
  70. } else {
  71. $this->assertNull( $desc['product_name']['SCALE'], 'Expected scale to be 0');
  72. }
  73. $this->assertEquals(0, $desc['product_name']['PRECISION'], 'Expected precision to be 0');
  74. $this->assertFalse( $desc['product_name']['PRIMARY'], 'Expected product_name not to be a primary key');
  75. $this->assertNull( $desc['product_name']['PRIMARY_POSITION'], 'Expected product_name to return null for PRIMARY_POSITION');
  76. $this->assertFalse( $desc['product_name']['IDENTITY'], 'Expected product_name to return false for IDENTITY');
  77. }
  78. public function testAdapterDescribeTablePrimaryKeyColumn()
  79. {
  80. $desc = $this->_db->describeTable('zfproducts');
  81. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME'], 'Expected table name to be zfproducts');
  82. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME'], 'Expected column name to be product_id');
  83. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION'], 'Expected column position to be 1');
  84. $this->assertEquals('', $desc['product_id']['DEFAULT'], 'Expected default to be empty string');
  85. $this->assertFalse( $desc['product_id']['NULLABLE'], 'Expected product_id not to be nullable');
  86. $this->assertEquals(0, $desc['product_id']['SCALE'], 'Expected scale to be 0');
  87. $this->assertEquals(0, $desc['product_id']['PRECISION'], 'Expected precision to be 0');
  88. $this->assertTrue( $desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  89. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  90. }
  91. /**
  92. * Used by _testAdapterOptionCaseFoldingNatural()
  93. * DB2 and Oracle return identifiers in uppercase naturally,
  94. * so those test suites will override this method.
  95. */
  96. protected function _testAdapterOptionCaseFoldingNaturalIdentifier()
  97. {
  98. return 'CASE_FOLDED_IDENTIFIER';
  99. }
  100. public function testAdapterTransactionCommit()
  101. {
  102. $bugs = $this->_db->quoteIdentifier('zfbugs');
  103. $bug_id = $this->_db->quoteIdentifier('bug_id');
  104. // use our default connection as the Connection1
  105. $dbConnection1 = $this->_db;
  106. // create a second connection to the same database
  107. $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  108. $dbConnection2->getConnection();
  109. if ($dbConnection2->isI5()) {
  110. $dbConnection2->query('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
  111. } else {
  112. $dbConnection2->query('SET ISOLATION LEVEL = UR');
  113. }
  114. // notice the number of rows in connection 2
  115. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  116. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  117. // start an explicit transaction in connection 1
  118. $dbConnection1->beginTransaction();
  119. // delete a row in connection 1
  120. $rowsAffected = $dbConnection1->delete(
  121. 'zfbugs',
  122. "$bug_id = 1"
  123. );
  124. $this->assertEquals(1, $rowsAffected);
  125. // we should see one less row in connection 2
  126. // because it is doing an uncommitted read
  127. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  128. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table (step 2) because conn2 is doing an uncommitted read');
  129. // commit the DELETE
  130. $dbConnection1->commit();
  131. // now we should see one fewer rows in connection 2
  132. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  133. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
  134. // delete another row in connection 1
  135. $rowsAffected = $dbConnection1->delete(
  136. 'zfbugs',
  137. "$bug_id = 2"
  138. );
  139. $this->assertEquals(1, $rowsAffected);
  140. // we should see results immediately, because
  141. // the db connection returns to auto-commit mode
  142. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  143. $this->assertEquals(2, $count);
  144. }
  145. public function testAdapterTransactionRollback()
  146. {
  147. $bugs = $this->_db->quoteIdentifier('zfbugs');
  148. $bug_id = $this->_db->quoteIdentifier('bug_id');
  149. // use our default connection as the Connection1
  150. $dbConnection1 = $this->_db;
  151. // create a second connection to the same database
  152. $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  153. $dbConnection2->getConnection();
  154. if ($dbConnection2->isI5()) {
  155. $dbConnection2->query('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
  156. } else {
  157. $dbConnection2->query('SET ISOLATION LEVEL = UR');
  158. }
  159. // notice the number of rows in connection 2
  160. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  161. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  162. // start an explicit transaction in connection 1
  163. $dbConnection1->beginTransaction();
  164. // delete a row in connection 1
  165. $rowsAffected = $dbConnection1->delete(
  166. 'zfbugs',
  167. "$bug_id = 1"
  168. );
  169. $this->assertEquals(1, $rowsAffected);
  170. // we should see one less row in connection 2
  171. // because it is doing an uncommitted read
  172. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  173. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table (step 2) because conn2 is doing an uncommitted read');
  174. // rollback the DELETE
  175. $dbConnection1->rollback();
  176. // now we should see the same number of rows
  177. // because the DELETE was rolled back
  178. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  179. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table after DELETE is rolled back (step 3)');
  180. // delete another row in connection 1
  181. $rowsAffected = $dbConnection1->delete(
  182. 'zfbugs',
  183. "$bug_id = 2"
  184. );
  185. $this->assertEquals(1, $rowsAffected);
  186. // we should see results immediately, because
  187. // the db connection returns to auto-commit mode
  188. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  189. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 4)');
  190. }
  191. public function testAdapterAlternateStatement()
  192. {
  193. $this->_testAdapterAlternateStatement('Test_Db2Statement');
  194. }
  195. /**
  196. * OVERRIDDEN COMMON TEST CASE
  197. *
  198. * This test case will produce a value with two internally set values,
  199. * autocommit = 1
  200. * DB2_ATTR_CASE = 0
  201. */
  202. public function testAdapterZendConfigEmptyDriverOptions()
  203. {
  204. Zend_Loader::loadClass('Zend_Config');
  205. $params = $this->_util->getParams();
  206. $params['driver_options'] = '';
  207. $params = new Zend_Config($params);
  208. $db = Zend_Db::factory($this->getDriver(), $params);
  209. $db->getConnection();
  210. $config = $db->getConfig();
  211. $expectedValue = array(
  212. 'autocommit' => 1,
  213. 'DB2_ATTR_CASE' => 0
  214. );
  215. $this->assertEquals($expectedValue, $config['driver_options']);
  216. }
  217. /**
  218. * OVERRIDDEN COMMON TEST CASE
  219. *
  220. * Test that quote() takes an array and returns
  221. * an imploded string of comma-separated, quoted elements.
  222. */
  223. public function testAdapterQuoteArray()
  224. {
  225. $array = array("it's", 'all', 'right!');
  226. $value = $this->_db->quote($array);
  227. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  228. }
  229. /**
  230. * OVERRRIDEEN COMMON TEST CASE
  231. *
  232. * test that quote() escapes a double-quote
  233. * character in a string.
  234. */
  235. public function testAdapterQuoteDoubleQuote()
  236. {
  237. $string = 'St John"s Wort';
  238. $value = $this->_db->quote($string);
  239. $this->assertEquals("'St John\"s Wort'", $value);
  240. }
  241. /**
  242. * OVERRIDDEN FROM COMMON TEST CASE
  243. *
  244. * test that quote() escapes a single-quote
  245. * character in a string.
  246. */
  247. public function testAdapterQuoteSingleQuote()
  248. {
  249. $string = "St John's Wort";
  250. $value = $this->_db->quote($string);
  251. $this->assertEquals("'St John''s Wort'", $value);
  252. }
  253. /**
  254. * OVERRIDDEN FROM COMMON TEST CASE
  255. *
  256. * test that quoteInto() escapes a double-quote
  257. * character in a string.
  258. */
  259. public function testAdapterQuoteIntoDoubleQuote()
  260. {
  261. $string = 'id=?';
  262. $param = 'St John"s Wort';
  263. $value = $this->_db->quoteInto($string, $param);
  264. $this->assertEquals("id='St John\"s Wort'", $value);
  265. }
  266. /**
  267. * OVERRIDDEN FROM COMMON TEST CASE
  268. *
  269. * test that quoteInto() escapes a single-quote
  270. * character in a string.
  271. */
  272. public function testAdapterQuoteIntoSingleQuote()
  273. {
  274. $string = 'id = ?';
  275. $param = 'St John\'s Wort';
  276. $value = $this->_db->quoteInto($string, $param);
  277. $this->assertEquals("id = 'St John''s Wort'", $value);
  278. }
  279. /**
  280. * This is "related" to the issue. It appears the fix for
  281. * describeTable is relatively untestable due to the fact that
  282. * its primary focus is to reduce the query time, not the result
  283. * set.
  284. *
  285. * @group ZF-5169
  286. */
  287. public function testAdapterSchemaOptionInListTables()
  288. {
  289. $params = $this->_util->getParams();
  290. unset($params['schema']);
  291. $connection = Zend_Db::factory($this->getDriver(), $params);
  292. $tableCountNoSchema = count($connection->listTables());
  293. $dbConfig = $this->_db->getConfig();
  294. if ($this->_db->isI5()) {
  295. if (isset($dbConfig['driver_options']['i5_lib'])) {
  296. $schema = $dbConfig['driver_options']['i5_lib'];
  297. }
  298. } elseif (!$this->_db->isI5()) {
  299. $schema = $this->_util->getSchema();
  300. } else {
  301. $this->markTestSkipped('No valid schema to test against.');
  302. return;
  303. }
  304. $params = $this->_util->getParams();
  305. $params['schema'] = $schema;
  306. $connection = Zend_Db::factory($this->getDriver(), $params);
  307. $tableCountSchema = count($connection->listTables());
  308. $this->assertGreaterThan(0, $tableCountNoSchema, 'Adapter without schema should produce large result');
  309. $this->assertGreaterThan(0, $tableCountSchema, 'Adapter with schema should produce large result');
  310. $this->assertTrue(($tableCountNoSchema > $tableCountSchema), 'Table count with schema provided should be less than without.');
  311. }
  312. public function getDriver()
  313. {
  314. return 'Db2';
  315. }
  316. }