2
0

OciTest.php 9.2 KB

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