OciTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. require_once 'Zend/Db/Adapter/Pdo/TestCommon.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Db
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Db
  30. * @group Zend_Db_Adapter
  31. */
  32. class Zend_Db_Adapter_Pdo_OciTest extends Zend_Db_Adapter_Pdo_TestCommon
  33. {
  34. protected $_numericDataTypes = array(
  35. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  36. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  37. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  38. 'BINARY_DOUBLE' => Zend_Db::FLOAT_TYPE,
  39. 'BINARY_FLOAT' => Zend_Db::FLOAT_TYPE,
  40. 'NUMBER' => Zend_Db::FLOAT_TYPE
  41. );
  42. public function testAdapterDescribeTablePrimaryAuto()
  43. {
  44. $this->markTestSkipped('Oracle does not support auto-increment');
  45. }
  46. public function testAdapterDescribeTablePrimaryKeyColumn()
  47. {
  48. $desc = $this->_db->describeTable('zfproducts');
  49. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME']);
  50. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME']);
  51. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION']);
  52. $this->assertEquals('', $desc['product_id']['DEFAULT']);
  53. $this->assertFalse( $desc['product_id']['NULLABLE']);
  54. $this->assertEquals(0, $desc['product_id']['SCALE']);
  55. // Oracle reports precsion 11 for integers
  56. $this->assertEquals(11, $desc['product_id']['PRECISION']);
  57. $this->assertTrue( $desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  58. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  59. $this->assertFalse( $desc['product_id']['IDENTITY']);
  60. }
  61. public function testAdapterInsert()
  62. {
  63. $row = array (
  64. 'product_id' => new Zend_Db_Expr($this->_db->quoteIdentifier('zfproducts_seq').'.NEXTVAL'),
  65. 'product_name' => 'Solaris',
  66. );
  67. $rowsAffected = $this->_db->insert('zfproducts', $row);
  68. $this->assertEquals(1, $rowsAffected);
  69. $lastInsertId = $this->_db->lastInsertId('zfproducts', null); // implies 'products_seq'
  70. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  71. $this->assertTrue(is_string($lastInsertId));
  72. $this->assertTrue(is_string($lastSequenceId));
  73. $this->assertEquals('4', (string) $lastInsertId, 'Expected new id to be 4');
  74. $this->assertEquals('4', (string) $lastSequenceId, 'Expected new id to be 4');
  75. }
  76. public function testAdapterInsertDbExpr()
  77. {
  78. $row = array (
  79. 'product_id' => new Zend_Db_Expr($this->_db->quoteIdentifier('zfproducts_seq').'.NEXTVAL'),
  80. 'product_name' => new Zend_Db_Expr('UPPER(\'Solaris\')')
  81. );
  82. $rowsAffected = $this->_db->insert('zfproducts', $row);
  83. $this->assertEquals(1, $rowsAffected);
  84. $product_id = $this->_db->quoteIdentifier('product_id', true);
  85. $select = $this->_db->select()
  86. ->from('zfproducts')
  87. ->where("$product_id = 4");
  88. $result = $this->_db->fetchAll($select);
  89. $this->assertTrue(is_array($result));
  90. $this->assertEquals('SOLARIS', $result[0]['product_name']);
  91. }
  92. /**
  93. * Used by _testAdapterOptionCaseFoldingNatural()
  94. * DB2 and Oracle return identifiers in uppercase naturally,
  95. * so those test suites will override this method.
  96. */
  97. protected function _testAdapterOptionCaseFoldingNaturalIdentifier()
  98. {
  99. return 'CASE_FOLDED_IDENTIFIER';
  100. }
  101. /**
  102. * Test that quote() takes an array and returns
  103. * an imploded string of comma-separated, quoted elements.
  104. */
  105. public function testAdapterQuoteArray()
  106. {
  107. $array = array("it's", 'all', 'right!');
  108. $value = $this->_db->quote($array);
  109. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  110. }
  111. /**
  112. * test that quote() escapes a double-quote
  113. * character in a string.
  114. */
  115. public function testAdapterQuoteDoubleQuote()
  116. {
  117. $string = 'St John"s Wort';
  118. $value = $this->_db->quote($string);
  119. $this->assertEquals("'St John\"s Wort'", $value);
  120. }
  121. /**
  122. * test that quote() escapes a single-quote
  123. * character in a string.
  124. */
  125. public function testAdapterQuoteSingleQuote()
  126. {
  127. $string = "St John's Wort";
  128. $value = $this->_db->quote($string);
  129. $this->assertEquals("'St John''s Wort'", $value);
  130. }
  131. /**
  132. * test that quoteInto() escapes a double-quote
  133. * character in a string.
  134. */
  135. public function testAdapterQuoteIntoDoubleQuote()
  136. {
  137. $string = 'id=?';
  138. $param = 'St John"s Wort';
  139. $value = $this->_db->quoteInto($string, $param);
  140. $this->assertEquals("id='St John\"s Wort'", $value);
  141. }
  142. /**
  143. * test that quoteInto() escapes a single-quote
  144. * character in a string.
  145. */
  146. public function testAdapterQuoteIntoSingleQuote()
  147. {
  148. $string = 'id = ?';
  149. $param = 'St John\'s Wort';
  150. $value = $this->_db->quoteInto($string, $param);
  151. $this->assertEquals("id = 'St John''s Wort'", $value);
  152. }
  153. /**
  154. * test that quoteTableAs() accepts a string and an alias,
  155. * and returns each as delimited identifiers.
  156. * Oracle does not want the 'AS' in between.
  157. */
  158. public function testAdapterQuoteTableAs()
  159. {
  160. $string = "foo";
  161. $alias = "bar";
  162. $value = $this->_db->quoteTableAs($string, $alias);
  163. $this->assertEquals('"foo" "bar"', $value);
  164. }
  165. /**
  166. * @group ZF-5146
  167. */
  168. public function testAdapterReadClobFetchAll()
  169. {
  170. $documents = $this->_db->quoteIdentifier('zfdocuments');
  171. $document_id = $this->_db->quoteIdentifier('doc_id');
  172. $value = $this->_db->fetchAll("SELECT * FROM $documents WHERE $document_id = 1");
  173. $expected = 'this is the clob that never ends...'.
  174. 'this is the clob that never ends...'.
  175. 'this is the clob that never ends...';
  176. $this->assertEquals($expected, stream_get_contents($value[0]['doc_clob']));
  177. }
  178. /**
  179. * @group ZF-5146
  180. */
  181. public function testAdapterReadClobFetchRow()
  182. {
  183. $documents = $this->_db->quoteIdentifier('zfdocuments');
  184. $document_id = $this->_db->quoteIdentifier('doc_id');
  185. $value = $this->_db->fetchRow("SELECT * FROM $documents WHERE $document_id = 1");
  186. $expected = 'this is the clob that never ends...'.
  187. 'this is the clob that never ends...'.
  188. 'this is the clob that never ends...';
  189. $this->assertEquals($expected, stream_get_contents($value['doc_clob']));
  190. }
  191. /**
  192. * @group ZF-5146
  193. */
  194. public function testAdapterReadClobFetchAssoc()
  195. {
  196. $documents = $this->_db->quoteIdentifier('zfdocuments');
  197. $document_id = $this->_db->quoteIdentifier('doc_id');
  198. $value = $this->_db->fetchAssoc("SELECT * FROM $documents WHERE $document_id = 1");
  199. $expected = 'this is the clob that never ends...'.
  200. 'this is the clob that never ends...'.
  201. 'this is the clob that never ends...';
  202. $this->assertEquals($expected, stream_get_contents($value[1]['doc_clob']));
  203. }
  204. /**
  205. * @group ZF-5146
  206. */
  207. public function testAdapterReadClobFetchCol()
  208. {
  209. $documents = $this->_db->quoteIdentifier('zfdocuments');
  210. $document_id = $this->_db->quoteIdentifier('doc_id');
  211. $document_clob = $this->_db->quoteIdentifier('doc_clob');
  212. $value = $this->_db->fetchCol("SELECT $document_clob FROM $documents WHERE $document_id = 1");
  213. $expected = 'this is the clob that never ends...'.
  214. 'this is the clob that never ends...'.
  215. 'this is the clob that never ends...';
  216. $this->assertEquals($expected, stream_get_contents($value[0]));
  217. }
  218. /**
  219. * @group ZF-5146
  220. */
  221. public function testAdapterReadClobFetchOne()
  222. {
  223. $documents = $this->_db->quoteIdentifier('zfdocuments');
  224. $document_id = $this->_db->quoteIdentifier('doc_id');
  225. $document_clob = $this->_db->quoteIdentifier('doc_clob');
  226. $value = $this->_db->fetchOne("SELECT $document_clob FROM $documents WHERE $document_id = 1");
  227. $expected = 'this is the clob that never ends...'.
  228. 'this is the clob that never ends...'.
  229. 'this is the clob that never ends...';
  230. $this->assertEquals($expected, stream_get_contents($value));
  231. }
  232. /**
  233. * @group ZF-8399
  234. */
  235. public function testLongQueryWithTextField()
  236. {
  237. $this->markTestSkipped($this->getDriver() . ' does not have TEXT field type');
  238. }
  239. public function getDriver()
  240. {
  241. return 'Pdo_Oci';
  242. }
  243. }