OracleTest.php 19 KB

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