OracleTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. /**
  22. * @see Zend_Db_Adapter_TestCommon
  23. */
  24. require_once 'Zend/Db/Adapter/TestCommon.php';
  25. /**
  26. * @see Zend_Db_Adapter_Oracle
  27. */
  28. require_once 'Zend/Db/Adapter/Oracle.php';
  29. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  30. class Zend_Db_Adapter_OracleTest extends Zend_Db_Adapter_TestCommon
  31. {
  32. protected $_numericDataTypes = array(
  33. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  34. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  35. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  36. 'BINARY_DOUBLE' => Zend_Db::FLOAT_TYPE,
  37. 'BINARY_FLOAT' => Zend_Db::FLOAT_TYPE,
  38. 'NUMBER' => Zend_Db::FLOAT_TYPE,
  39. );
  40. public function testAdapterDescribeTablePrimaryAuto()
  41. {
  42. $this->markTestSkipped('Oracle does not support auto-increment');
  43. }
  44. public function testAdapterDescribeTablePrimaryKeyColumn()
  45. {
  46. $desc = $this->_db->describeTable('zfproducts');
  47. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME']);
  48. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME']);
  49. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION']);
  50. $this->assertEquals('', $desc['product_id']['DEFAULT']);
  51. $this->assertFalse( $desc['product_id']['NULLABLE']);
  52. $this->assertEquals(0, $desc['product_id']['SCALE']);
  53. // Oracle reports precsion 11 for integers
  54. $this->assertEquals(11, $desc['product_id']['PRECISION']);
  55. $this->assertTrue( $desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  56. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  57. $this->assertFalse( $desc['product_id']['IDENTITY']);
  58. }
  59. /**
  60. * Test the Adapter's fetchAll() method.
  61. */
  62. public function testAdapterFetchAll()
  63. {
  64. $products = $this->_db->quoteIdentifier('zfproducts');
  65. $product_id = $this->_db->quoteIdentifier('product_id');
  66. $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id ASC", array(":id"=>1));
  67. $this->assertEquals(2, count($result));
  68. $this->assertThat($result[0], $this->arrayHasKey('product_id'));
  69. $this->assertEquals('2', $result[0]['product_id']);
  70. }
  71. /**
  72. * ZF-4330: Oracle binds variables by name
  73. * Test that fetchAssoc() still fetched an associative array
  74. * after the adapter's default fetch mode is set to something else.
  75. */
  76. public function testAdapterFetchAllOverrideFetchMode()
  77. {
  78. $products = $this->_db->quoteIdentifier('zfproducts');
  79. $product_id = $this->_db->quoteIdentifier('product_id');
  80. $col_name = $this->_db->foldCase('product_id');
  81. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  82. // Test associative array
  83. $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id ASC", array(":id"=>1), Zend_Db::FETCH_ASSOC);
  84. $this->assertEquals(2, count($result));
  85. $this->assertType('array', $result[0]);
  86. $this->assertEquals(2, count($result[0])); // count columns
  87. $this->assertEquals(2, $result[0][$col_name]);
  88. // Test numeric and associative array
  89. // OCI8 driver does not support fetchAll(FETCH_BOTH), use fetch() in a loop instead
  90. // Ensure original fetch mode has been retained
  91. $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id", array(":id"=>1));
  92. $this->assertEquals(2, count($result));
  93. $this->assertType('object', $result[0]);
  94. $this->assertEquals(2, $result[0]->$col_name);
  95. }
  96. /**
  97. * Test the Adapter's fetchAssoc() method.
  98. */
  99. public function testAdapterFetchAssoc()
  100. {
  101. $products = $this->_db->quoteIdentifier('zfproducts');
  102. $product_id = $this->_db->quoteIdentifier('product_id');
  103. $result = $this->_db->fetchAssoc("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id DESC", array(":id"=>1));
  104. foreach ($result as $idKey => $row) {
  105. $this->assertThat($row, $this->arrayHasKey('product_id'));
  106. $this->assertEquals($idKey, $row['product_id']);
  107. }
  108. }
  109. /**
  110. * ZF-4275: Oracle binds variables by name
  111. * Test that fetchAssoc() still fetched an associative array
  112. * after the adapter's default fetch mode is set to something else.
  113. */
  114. public function testAdapterFetchAssocAfterSetFetchMode()
  115. {
  116. $products = $this->_db->quoteIdentifier('zfproducts');
  117. $product_id = $this->_db->quoteIdentifier('product_id');
  118. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  119. $result = $this->_db->fetchAssoc("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id DESC", array(":id"=>1));
  120. $this->assertType('array', $result);
  121. $this->assertEquals(array('product_id', 'product_name'), array_keys(current($result)));
  122. }
  123. /**
  124. * Test the Adapter's fetchCol() method.
  125. */
  126. public function testAdapterFetchCol()
  127. {
  128. $products = $this->_db->quoteIdentifier('zfproducts');
  129. $product_id = $this->_db->quoteIdentifier('product_id');
  130. $result = $this->_db->fetchCol("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id ASC", array(":id"=>1));
  131. $this->assertEquals(2, count($result)); // count rows
  132. $this->assertEquals(2, $result[0]);
  133. $this->assertEquals(3, $result[1]);
  134. }
  135. /**
  136. * ZF-4275: Oracle binds variables by name
  137. * Test that fetchCol() still fetched an associative array
  138. * after the adapter's default fetch mode is set to something else.
  139. */
  140. public function testAdapterFetchColAfterSetFetchMode()
  141. {
  142. $products = $this->_db->quoteIdentifier('zfproducts');
  143. $product_id = $this->_db->quoteIdentifier('product_id');
  144. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  145. $result = $this->_db->fetchCol("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id ASC", array(":id"=>1));
  146. $this->assertType('array', $result);
  147. $this->assertEquals(2, count($result)); // count rows
  148. $this->assertEquals(2, $result[0]);
  149. $this->assertEquals(3, $result[1]);
  150. }
  151. /**
  152. * Test the Adapter's fetchOne() method.
  153. */
  154. public function testAdapterFetchOne()
  155. {
  156. $products = $this->_db->quoteIdentifier('zfproducts');
  157. $product_id = $this->_db->quoteIdentifier('product_id');
  158. $product_name = $this->_db->quoteIdentifier('product_name');
  159. $prod = 'Linux';
  160. $result = $this->_db->fetchOne("SELECT $product_name FROM $products WHERE $product_id > :id ORDER BY $product_id", array(":id"=>1));
  161. $this->assertEquals($prod, $result);
  162. }
  163. /**
  164. * ZF-4275: Oracle binds variables by name
  165. * Test that fetchCol() still fetched an associative array
  166. * after the adapter's default fetch mode is set to something else.
  167. */
  168. public function testAdapterFetchOneAfterSetFetchMode()
  169. {
  170. $products = $this->_db->quoteIdentifier('zfproducts');
  171. $product_id = $this->_db->quoteIdentifier('product_id');
  172. $product_name = $this->_db->quoteIdentifier('product_name');
  173. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  174. $prod = 'Linux';
  175. $result = $this->_db->fetchOne("SELECT $product_name FROM $products WHERE $product_id > :id ORDER BY $product_id", array(":id"=>1));
  176. $this->assertType('string', $result);
  177. $this->assertEquals($prod, $result);
  178. }
  179. /**
  180. * Test the Adapter's fetchPairs() method.
  181. */
  182. public function testAdapterFetchPairs()
  183. {
  184. $products = $this->_db->quoteIdentifier('zfproducts');
  185. $product_id = $this->_db->quoteIdentifier('product_id');
  186. $product_name = $this->_db->quoteIdentifier('product_name');
  187. $prod = 'Linux';
  188. $result = $this->_db->fetchPairs("SELECT $product_id, $product_name FROM $products WHERE $product_id > :id ORDER BY $product_id ASC", array(":id"=>1));
  189. $this->assertEquals(2, count($result)); // count rows
  190. $this->assertEquals($prod, $result[2]);
  191. }
  192. /**
  193. * ZF-4275: Oracle binds variables by name
  194. * Test the Adapter's fetchPairs() method.
  195. */
  196. public function testAdapterFetchPairsAfterSetFetchMode()
  197. {
  198. $products = $this->_db->quoteIdentifier('zfproducts');
  199. $product_id = $this->_db->quoteIdentifier('product_id');
  200. $product_name = $this->_db->quoteIdentifier('product_name');
  201. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  202. $prod = 'Linux';
  203. $result = $this->_db->fetchPairs("SELECT $product_id, $product_name FROM $products WHERE $product_id > :id ORDER BY $product_id ASC", array(":id"=>1));
  204. $this->assertType('array', $result);
  205. $this->assertEquals(2, count($result)); // count rows
  206. $this->assertEquals($prod, $result[2]);
  207. }
  208. /**
  209. * Test the Adapter's fetchRow() method.
  210. */
  211. public function testAdapterFetchRow()
  212. {
  213. $products = $this->_db->quoteIdentifier('zfproducts');
  214. $product_id = $this->_db->quoteIdentifier('product_id');
  215. $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id", array(":id"=>1));
  216. $this->assertEquals(2, count($result)); // count columns
  217. $this->assertEquals(2, $result['product_id']);
  218. }
  219. /**
  220. * ZF-4330: Oracle binds variables by name
  221. * Test that fetchAssoc() still fetched an associative array
  222. * after the adapter's default fetch mode is set to something else.
  223. */
  224. public function testAdapterFetchRowOverrideFetchMode()
  225. {
  226. $products = $this->_db->quoteIdentifier('zfproducts');
  227. $product_id = $this->_db->quoteIdentifier('product_id');
  228. $col_name = $this->_db->foldCase('product_id');
  229. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  230. // Test associative array
  231. $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id", array(":id"=>1), Zend_Db::FETCH_ASSOC);
  232. $this->assertType('array', $result);
  233. $this->assertEquals(2, count($result)); // count columns
  234. $this->assertEquals(2, $result['product_id']);
  235. // Test numeric and associative array
  236. // OCI8 driver does not support fetchAll(FETCH_BOTH), use fetch() in a loop instead
  237. // Ensure original fetch mode has been retained
  238. $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > :id ORDER BY $product_id", array(":id"=>1));
  239. $this->assertType('object', $result);
  240. $this->assertEquals(2, $result->$col_name);
  241. }
  242. public function testAdapterInsert()
  243. {
  244. $row = array (
  245. 'product_id' => new Zend_Db_Expr($this->_db->quoteIdentifier('zfproducts_seq').'.NEXTVAL'),
  246. 'product_name' => 'Solaris',
  247. );
  248. $rowsAffected = $this->_db->insert('zfproducts', $row);
  249. $this->assertEquals(1, $rowsAffected);
  250. $lastInsertId = $this->_db->lastInsertId('zfproducts', null); // implies 'zfproducts_seq'
  251. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  252. $this->assertEquals('4', (string) $lastInsertId, 'Expected new id to be 4');
  253. $this->assertEquals('4', (string) $lastSequenceId, 'Expected new id to be 4');
  254. }
  255. public function testAdapterInsertDbExpr()
  256. {
  257. $row = array (
  258. 'product_id' => new Zend_Db_Expr($this->_db->quoteIdentifier('zfproducts_seq').'.NEXTVAL'),
  259. 'product_name' => new Zend_Db_Expr('UPPER(\'Solaris\')')
  260. );
  261. $rowsAffected = $this->_db->insert('zfproducts', $row);
  262. $this->assertEquals(1, $rowsAffected);
  263. $product_id = $this->_db->quoteIdentifier('product_id', true);
  264. $select = $this->_db->select()
  265. ->from('zfproducts')
  266. ->where("$product_id = 4");
  267. $result = $this->_db->fetchAll($select);
  268. $this->assertType('array', $result);
  269. $this->assertEquals('SOLARIS', $result[0]['product_name']);
  270. }
  271. /**
  272. * Test that quote() takes an array and returns
  273. * an imploded string of comma-separated, quoted elements.
  274. */
  275. public function testAdapterQuoteArray()
  276. {
  277. $array = array("it's", 'all', 'right!');
  278. $value = $this->_db->quote($array);
  279. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  280. }
  281. /**
  282. * test that quote() escapes a double-quote
  283. * character in a string.
  284. */
  285. public function testAdapterQuoteDoubleQuote()
  286. {
  287. $value = $this->_db->quote('St John"s Wort');
  288. $this->assertEquals("'St John\"s Wort'", $value);
  289. }
  290. /**
  291. * test that quote() escapes a single-quote
  292. * character in a string.
  293. */
  294. public function testAdapterQuoteSingleQuote()
  295. {
  296. $string = "St John's Wort";
  297. $value = $this->_db->quote($string);
  298. $this->assertEquals("'St John''s Wort'", $value);
  299. }
  300. /**
  301. * test that quoteInto() escapes a double-quote
  302. * character in a string.
  303. */
  304. public function testAdapterQuoteIntoDoubleQuote()
  305. {
  306. $value = $this->_db->quoteInto('id=?', 'St John"s Wort');
  307. $this->assertEquals("id='St John\"s Wort'", $value);
  308. }
  309. /**
  310. * test that quoteInto() escapes a single-quote
  311. * character in a string.
  312. */
  313. public function testAdapterQuoteIntoSingleQuote()
  314. {
  315. $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort');
  316. $this->assertEquals("id = 'St John''s Wort'", $value);
  317. }
  318. /**
  319. * test that quoteTableAs() accepts a string and an alias,
  320. * and returns each as delimited identifiers.
  321. * Oracle does not want the 'AS' in between.
  322. */
  323. public function testAdapterQuoteTableAs()
  324. {
  325. $string = "foo";
  326. $alias = "bar";
  327. $value = $this->_db->quoteTableAs($string, $alias);
  328. $this->assertEquals('"foo" "bar"', $value);
  329. }
  330. /**
  331. * @group ZF-5146
  332. */
  333. public function testAdapterLobAsString()
  334. {
  335. $this->assertFalse($this->_db->getLobAsString());
  336. $this->_db->setLobAsString(true);
  337. $this->assertTrue($this->_db->getLobAsString());
  338. }
  339. /**
  340. * @group ZF-5146
  341. */
  342. public function testAdapterLobAsStringFromDriverOptions()
  343. {
  344. $params = $this->_util->getParams();
  345. $params['driver_options'] = array(
  346. 'lob_as_string' => true
  347. );
  348. $db = Zend_Db::factory($this->getDriver(), $params);
  349. $this->assertTrue($db->getLobAsString());
  350. }
  351. /**
  352. * @group ZF-5146
  353. */
  354. public function testAdapterReadClobFetchRow()
  355. {
  356. $documents = $this->_db->quoteIdentifier('zfdocuments');
  357. $document_id = $this->_db->quoteIdentifier('doc_id');
  358. $value = $this->_db->fetchRow("SELECT * FROM $documents WHERE $document_id = 1");
  359. $this->assertType('OCI-Lob', $value['doc_clob']);
  360. $expected = 'this is the clob that never ends...'.
  361. 'this is the clob that never ends...'.
  362. 'this is the clob that never ends...';
  363. $lob = $value['doc_clob'];
  364. $this->assertEquals($expected, $lob->read($lob->size()));
  365. }
  366. /**
  367. * @group ZF-5146
  368. */
  369. public function testAdapterReadClobFetchRowLobAsString()
  370. {
  371. $this->_db->setLobAsString(true);
  372. parent::testAdapterReadClobFetchRow();
  373. }
  374. /**
  375. * @group ZF-5146
  376. */
  377. public function testAdapterReadClobFetchAssoc()
  378. {
  379. $documents = $this->_db->quoteIdentifier('zfdocuments');
  380. $document_id = $this->_db->quoteIdentifier('doc_id');
  381. $value = $this->_db->fetchAssoc("SELECT * FROM $documents WHERE $document_id = 1");
  382. $this->assertType('OCI-Lob', $value[1]['doc_clob']);
  383. $expected = 'this is the clob that never ends...'.
  384. 'this is the clob that never ends...'.
  385. 'this is the clob that never ends...';
  386. $lob = $value[1]['doc_clob'];
  387. $this->assertEquals($expected, $lob->read($lob->size()));
  388. }
  389. /**
  390. * @group ZF-5146
  391. */
  392. public function testAdapterReadClobFetchAssocLobAsString()
  393. {
  394. $this->_db->setLobAsString(true);
  395. parent::testAdapterReadClobFetchAssoc();
  396. }
  397. /**
  398. * @group ZF-5146
  399. */
  400. public function testAdapterReadClobFetchOne()
  401. {
  402. $documents = $this->_db->quoteIdentifier('zfdocuments');
  403. $document_id = $this->_db->quoteIdentifier('doc_id');
  404. $document_clob = $this->_db->quoteIdentifier('doc_clob');
  405. $value = $this->_db->fetchOne("SELECT $document_clob FROM $documents WHERE $document_id = 1");
  406. $this->assertType('OCI-Lob', $value);
  407. $expected = 'this is the clob that never ends...'.
  408. 'this is the clob that never ends...'.
  409. 'this is the clob that never ends...';
  410. $lob = $value;
  411. $this->assertEquals($expected, $lob->read($lob->size()));
  412. }
  413. /**
  414. * @group ZF-5146
  415. */
  416. public function testAdapterReadClobFetchOneLobAsString()
  417. {
  418. $this->_db->setLobAsString(true);
  419. parent::testAdapterReadClobFetchOne();
  420. }
  421. /**
  422. * Used by _testAdapterOptionCaseFoldingNatural()
  423. * DB2 and Oracle return identifiers in uppercase naturally,
  424. * so those test suites will override this method.
  425. */
  426. protected function _testAdapterOptionCaseFoldingNaturalIdentifier()
  427. {
  428. return 'CASE_FOLDED_IDENTIFIER';
  429. }
  430. public function testAdapterOptionCaseFoldingUpper()
  431. {
  432. $this->markTestIncomplete($this->getDriver() . ' does not support case-folding array keys yet.');
  433. }
  434. public function testAdapterOptionCaseFoldingLower()
  435. {
  436. $this->markTestIncomplete($this->getDriver() . ' does not support case-folding array keys yet.');
  437. }
  438. public function testAdapterTransactionCommit()
  439. {
  440. $this->markTestIncomplete($this->getDriver() . ' is having trouble with transactions');
  441. }
  442. public function testAdapterTransactionRollback()
  443. {
  444. $this->markTestIncomplete($this->getDriver() . ' is having trouble with transactions');
  445. }
  446. public function testAdapterAlternateStatement()
  447. {
  448. $this->_testAdapterAlternateStatement('Test_OracleStatement');
  449. }
  450. public function getDriver()
  451. {
  452. return 'Oracle';
  453. }
  454. }