OracleTest.php 19 KB

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