OracleTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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_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-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_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->assertTrue(is_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->assertTrue(is_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->assertTrue(is_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->assertTrue(is_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->assertTrue(is_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->assertTrue(is_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->assertTrue(is_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->assertTrue(is_object($result));
  249. $this->assertEquals(2, $result->$col_name);
  250. }
  251. /**
  252. * @group ZF-10829
  253. */
  254. public function testAdapterGetPersistentConnection()
  255. {
  256. $params = $this->_util->getParams();
  257. $params['persistent'] = 1;
  258. $db = Zend_Db::factory($this->getDriver(), $params);
  259. $db->getConnection();
  260. $this->assertTrue($db->isConnected());
  261. }
  262. public function testAdapterInsert()
  263. {
  264. $row = array (
  265. 'product_id' => new Zend_Db_Expr($this->_db->quoteIdentifier('zfproducts_seq').'.NEXTVAL'),
  266. 'product_name' => 'Solaris',
  267. );
  268. $rowsAffected = $this->_db->insert('zfproducts', $row);
  269. $this->assertEquals(1, $rowsAffected);
  270. $lastInsertId = $this->_db->lastInsertId('zfproducts', null); // implies 'zfproducts_seq'
  271. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  272. $this->assertEquals('4', (string) $lastInsertId, 'Expected new id to be 4');
  273. $this->assertEquals('4', (string) $lastSequenceId, 'Expected new id to be 4');
  274. }
  275. public function testAdapterInsertDbExpr()
  276. {
  277. $row = array (
  278. 'product_id' => new Zend_Db_Expr($this->_db->quoteIdentifier('zfproducts_seq').'.NEXTVAL'),
  279. 'product_name' => new Zend_Db_Expr('UPPER(\'Solaris\')')
  280. );
  281. $rowsAffected = $this->_db->insert('zfproducts', $row);
  282. $this->assertEquals(1, $rowsAffected);
  283. $product_id = $this->_db->quoteIdentifier('product_id', true);
  284. $select = $this->_db->select()
  285. ->from('zfproducts')
  286. ->where("$product_id = 4");
  287. $result = $this->_db->fetchAll($select);
  288. $this->assertTrue(is_array($result));
  289. $this->assertEquals('SOLARIS', $result[0]['product_name']);
  290. }
  291. /**
  292. * Test that quote() takes an array and returns
  293. * an imploded string of comma-separated, quoted elements.
  294. */
  295. public function testAdapterQuoteArray()
  296. {
  297. $array = array("it's", 'all', 'right!');
  298. $value = $this->_db->quote($array);
  299. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  300. }
  301. /**
  302. * test that quote() escapes a double-quote
  303. * character in a string.
  304. */
  305. public function testAdapterQuoteDoubleQuote()
  306. {
  307. $value = $this->_db->quote('St John"s Wort');
  308. $this->assertEquals("'St John\"s Wort'", $value);
  309. }
  310. /**
  311. * test that quote() escapes a single-quote
  312. * character in a string.
  313. */
  314. public function testAdapterQuoteSingleQuote()
  315. {
  316. $string = "St John's Wort";
  317. $value = $this->_db->quote($string);
  318. $this->assertEquals("'St John''s Wort'", $value);
  319. }
  320. /**
  321. * test that quoteInto() escapes a double-quote
  322. * character in a string.
  323. */
  324. public function testAdapterQuoteIntoDoubleQuote()
  325. {
  326. $value = $this->_db->quoteInto('id=?', 'St John"s Wort');
  327. $this->assertEquals("id='St John\"s Wort'", $value);
  328. }
  329. /**
  330. * test that quoteInto() escapes a single-quote
  331. * character in a string.
  332. */
  333. public function testAdapterQuoteIntoSingleQuote()
  334. {
  335. $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort');
  336. $this->assertEquals("id = 'St John''s Wort'", $value);
  337. }
  338. /**
  339. * test that quoteTableAs() accepts a string and an alias,
  340. * and returns each as delimited identifiers.
  341. * Oracle does not want the 'AS' in between.
  342. */
  343. public function testAdapterQuoteTableAs()
  344. {
  345. $string = "foo";
  346. $alias = "bar";
  347. $value = $this->_db->quoteTableAs($string, $alias);
  348. $this->assertEquals('"foo" "bar"', $value);
  349. }
  350. /**
  351. * @group ZF-5146
  352. */
  353. public function testAdapterLobAsString()
  354. {
  355. $this->assertFalse($this->_db->getLobAsString());
  356. $this->_db->setLobAsString(true);
  357. $this->assertTrue($this->_db->getLobAsString());
  358. }
  359. /**
  360. * @group ZF-5146
  361. */
  362. public function testAdapterLobAsStringFromDriverOptions()
  363. {
  364. $params = $this->_util->getParams();
  365. $params['driver_options'] = array(
  366. 'lob_as_string' => true
  367. );
  368. $db = Zend_Db::factory($this->getDriver(), $params);
  369. $this->assertTrue($db->getLobAsString());
  370. }
  371. /**
  372. * @group ZF-5146
  373. */
  374. public function testAdapterReadClobFetchRow()
  375. {
  376. $documents = $this->_db->quoteIdentifier('zfdocuments');
  377. $document_id = $this->_db->quoteIdentifier('doc_id');
  378. $value = $this->_db->fetchRow("SELECT * FROM $documents WHERE $document_id = 1");
  379. $class = 'OCI-Lob';
  380. $this->assertTrue($value['doc_clob'] instanceof $class);
  381. $expected = 'this is the clob that never ends...'.
  382. 'this is the clob that never ends...'.
  383. 'this is the clob that never ends...';
  384. $lob = $value['doc_clob'];
  385. $this->assertEquals($expected, $lob->read($lob->size()));
  386. }
  387. /**
  388. * @group ZF-5146
  389. */
  390. public function testAdapterReadClobFetchRowLobAsString()
  391. {
  392. $this->_db->setLobAsString(true);
  393. parent::testAdapterReadClobFetchRow();
  394. }
  395. /**
  396. * @group ZF-5146
  397. */
  398. public function testAdapterReadClobFetchAssoc()
  399. {
  400. $documents = $this->_db->quoteIdentifier('zfdocuments');
  401. $document_id = $this->_db->quoteIdentifier('doc_id');
  402. $value = $this->_db->fetchAssoc("SELECT * FROM $documents WHERE $document_id = 1");
  403. $class = 'OCI-Lob';
  404. $this->assertTrue($value[1]['doc_clob'] instanceof $class);
  405. $expected = 'this is the clob that never ends...'.
  406. 'this is the clob that never ends...'.
  407. 'this is the clob that never ends...';
  408. $lob = $value[1]['doc_clob'];
  409. $this->assertEquals($expected, $lob->read($lob->size()));
  410. }
  411. /**
  412. * @group ZF-5146
  413. */
  414. public function testAdapterReadClobFetchAssocLobAsString()
  415. {
  416. $this->_db->setLobAsString(true);
  417. parent::testAdapterReadClobFetchAssoc();
  418. }
  419. /**
  420. * @group ZF-5146
  421. */
  422. public function testAdapterReadClobFetchOne()
  423. {
  424. $documents = $this->_db->quoteIdentifier('zfdocuments');
  425. $document_id = $this->_db->quoteIdentifier('doc_id');
  426. $document_clob = $this->_db->quoteIdentifier('doc_clob');
  427. $value = $this->_db->fetchOne("SELECT $document_clob FROM $documents WHERE $document_id = 1");
  428. $class = 'OCI-Lob';
  429. $this->assertTrue($value instanceof $class);
  430. $expected = 'this is the clob that never ends...'.
  431. 'this is the clob that never ends...'.
  432. 'this is the clob that never ends...';
  433. $lob = $value;
  434. $this->assertEquals($expected, $lob->read($lob->size()));
  435. }
  436. /**
  437. * @group ZF-5146
  438. */
  439. public function testAdapterReadClobFetchOneLobAsString()
  440. {
  441. $this->_db->setLobAsString(true);
  442. parent::testAdapterReadClobFetchOne();
  443. }
  444. /**
  445. * Used by _testAdapterOptionCaseFoldingNatural()
  446. * DB2 and Oracle return identifiers in uppercase naturally,
  447. * so those test suites will override this method.
  448. */
  449. protected function _testAdapterOptionCaseFoldingNaturalIdentifier()
  450. {
  451. return 'CASE_FOLDED_IDENTIFIER';
  452. }
  453. public function testAdapterOptionCaseFoldingUpper()
  454. {
  455. $this->markTestIncomplete($this->getDriver() . ' does not support case-folding array keys yet.');
  456. }
  457. public function testAdapterOptionCaseFoldingLower()
  458. {
  459. $this->markTestIncomplete($this->getDriver() . ' does not support case-folding array keys yet.');
  460. }
  461. public function testAdapterTransactionCommit()
  462. {
  463. $this->markTestIncomplete($this->getDriver() . ' is having trouble with transactions');
  464. }
  465. public function testAdapterTransactionRollback()
  466. {
  467. $this->markTestIncomplete($this->getDriver() . ' is having trouble with transactions');
  468. }
  469. public function testAdapterAlternateStatement()
  470. {
  471. $this->_testAdapterAlternateStatement('Test_OracleStatement');
  472. }
  473. /**
  474. * @group ZF-8399
  475. */
  476. public function testLongQueryWithTextField()
  477. {
  478. $this->markTestSkipped($this->getDriver() . ' does not have TEXT field type');
  479. }
  480. public function getDriver()
  481. {
  482. return 'Oracle';
  483. }
  484. }