Db2Test.php 14 KB

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