refcur_prefetch_1.phpt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. --TEST--
  2. Prefetch with REF cursor. Test different values for prefetch with oci_set_prefetch().
  3. --SKIPIF--
  4. <?php if (!extension_loaded('oci8')) die("skip no oci8 extension");
  5. if (!extension_loaded('oci8')) die("skip no oci8 extension");
  6. require(dirname(__FILE__)."/connect.inc");
  7. preg_match('/.*Release ([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)*/', oci_server_version($c), $matches);
  8. if (!(isset($matches[0]) &&
  9. ($matches[1] >= 10))) {
  10. die("skip expected output only valid when using Oracle 10g or greater database server");
  11. }
  12. preg_match('/^([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)/', oci_client_version(), $matches);
  13. if (!(isset($matches[0]) &&
  14. (($matches[1] == 11 && $matches[2] >= 2) ||
  15. ($matches[1] >= 12)
  16. ))) {
  17. die("skip test expected to work only with Oracle 11gR2 or greater version of client");
  18. }
  19. ?>
  20. --FILE--
  21. <?php
  22. require(dirname(__FILE__)."/connect.inc");
  23. // Creates the necessary package and tables.
  24. $stmtarray = array(
  25. "DROP TABLE refcurtest",
  26. "CREATE TABLE refcurtest (c1 NUMBER, c2 VARCHAR(20))",
  27. "CREATE or REPLACE PACKAGE refcurpkg is
  28. type refcursortype is ref cursor;
  29. procedure open_ref_cur(cur1 out refcursortype);
  30. procedure fetch_ref_cur(cur1 in refcursortype, c1 out number,c2 out varchar2);
  31. end refcurpkg;",
  32. "CREATE or REPLACE PACKAGE body refcurpkg is
  33. procedure open_ref_cur(cur1 out refcursortype) is
  34. begin
  35. open cur1 for select * from refcurtest order by c1;
  36. end open_ref_cur;
  37. procedure fetch_ref_cur(cur1 in refcursortype, c1 out number,
  38. c2 out varchar2) is
  39. begin
  40. fetch cur1 into c1,c2;
  41. end fetch_ref_cur;
  42. end refcurpkg;"
  43. );
  44. oci8_test_sql_execute($c, $stmtarray);
  45. // Insert 500 rows into the table.
  46. $insert_sql = "INSERT INTO refcurtest (c1, c2) VALUES (:c1,:c2)";
  47. if (!($s = oci_parse($c, $insert_sql))) {
  48. die("oci_parse(insert) failed!\n");
  49. }
  50. for ($i = 0; $i<=500; $i++) {
  51. $val2 = 'test'.$i;
  52. oci_bind_by_name($s,':c1',$i);
  53. oci_bind_by_name($s,':c2',$val2);
  54. if (!oci_execute($s)) {
  55. die("oci_execute(insert) failed!\n");
  56. }
  57. }
  58. // Various values for prefetch
  59. $pref = array(0,1,501,499,250,12345,-12345,-1);
  60. foreach($pref as $value) {
  61. echo"-----------------------------------------------\n";
  62. echo "Test with Prefetch value set to $value \n";
  63. echo"-----------------------------------------------\n";
  64. $cur1 = oci_new_cursor($c);
  65. fetch_frm_php($c,$cur1,$value);
  66. fetch_frm_plsql($c,$cur1);
  67. }
  68. // This function sets the prefetch count to the given $value and fetches one row .
  69. function fetch_frm_php($c,$cur1,$value) {
  70. $sql1 = "begin refcurpkg.open_ref_cur(:cur1); end;";
  71. $s1 = oci_parse($c,$sql1);
  72. if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
  73. die("oci_bind_by_name(sql1) failed!\n");
  74. }
  75. oci_execute($s1);
  76. oci_set_prefetch($cur1,$value);
  77. oci_execute($cur1);
  78. echo "Fetch Row from PHP\n";
  79. var_dump(oci_fetch_row($cur1));
  80. }
  81. // This function calls the fetch_ref_cur procedure to get the values from the REF cur.
  82. function fetch_frm_plsql($c,$cur1) {
  83. $sql2 = "begin refcurpkg.fetch_ref_cur(:curs1,:c1,:c2); end;";
  84. $s2 = oci_parse($c,$sql2);
  85. if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
  86. die("oci_bind_by_name(sql2) failed!\n");
  87. }
  88. if (!oci_bind_by_name($s2,":c1",$c1,-1,SQLT_INT)) {
  89. die("oci_bind_by_name(sql2) failed!\n");
  90. }
  91. if (!oci_bind_by_name($s2,":c2",$c2,20,SQLT_CHR)) {
  92. die("oci_bind_by_name(sql2) failed!\n");
  93. }
  94. oci_execute($s2);
  95. echo "Fetch Row from PL/SQL\n";
  96. var_dump($c1);
  97. var_dump($c2);
  98. }
  99. // Clean up here
  100. $stmtarray = array(
  101. "drop package refcurpkg",
  102. "drop table refcurtest"
  103. );
  104. oci8_test_sql_execute($c, $stmtarray);
  105. echo "Done\n";
  106. ?>
  107. --EXPECTF--
  108. -----------------------------------------------
  109. Test with Prefetch value set to 0
  110. -----------------------------------------------
  111. Fetch Row from PHP
  112. array(2) {
  113. [0]=>
  114. string(%d) "0"
  115. [1]=>
  116. string(%d) "test0"
  117. }
  118. Fetch Row from PL/SQL
  119. int(1)
  120. string(%d) "test1"
  121. -----------------------------------------------
  122. Test with Prefetch value set to 1
  123. -----------------------------------------------
  124. Fetch Row from PHP
  125. array(2) {
  126. [0]=>
  127. string(%d) "0"
  128. [1]=>
  129. string(%d) "test0"
  130. }
  131. Fetch Row from PL/SQL
  132. int(2)
  133. string(%d) "test2"
  134. -----------------------------------------------
  135. Test with Prefetch value set to 501
  136. -----------------------------------------------
  137. Fetch Row from PHP
  138. array(2) {
  139. [0]=>
  140. string(%d) "0"
  141. [1]=>
  142. string(%d) "test0"
  143. }
  144. Warning: oci_execute(): ORA-01002: %s
  145. ORA-06512: at "%s.REFCURPKG", line %d
  146. ORA-06512: at line %d in %s on line %d
  147. Fetch Row from PL/SQL
  148. int(0)
  149. NULL
  150. -----------------------------------------------
  151. Test with Prefetch value set to 499
  152. -----------------------------------------------
  153. Fetch Row from PHP
  154. array(2) {
  155. [0]=>
  156. string(%d) "0"
  157. [1]=>
  158. string(%d) "test0"
  159. }
  160. Fetch Row from PL/SQL
  161. int(500)
  162. string(%d) "test500"
  163. -----------------------------------------------
  164. Test with Prefetch value set to 250
  165. -----------------------------------------------
  166. Fetch Row from PHP
  167. array(2) {
  168. [0]=>
  169. string(%d) "0"
  170. [1]=>
  171. string(%d) "test0"
  172. }
  173. Fetch Row from PL/SQL
  174. int(251)
  175. string(%d) "test251"
  176. -----------------------------------------------
  177. Test with Prefetch value set to 12345
  178. -----------------------------------------------
  179. Fetch Row from PHP
  180. array(2) {
  181. [0]=>
  182. string(%d) "0"
  183. [1]=>
  184. string(%d) "test0"
  185. }
  186. Warning: oci_execute(): ORA-01002: %s
  187. ORA-06512: at "%s.REFCURPKG", line %d
  188. ORA-06512: at line %d in %s on line %d
  189. Fetch Row from PL/SQL
  190. int(0)
  191. NULL
  192. -----------------------------------------------
  193. Test with Prefetch value set to -12345
  194. -----------------------------------------------
  195. Warning: oci_set_prefetch(): Number of rows to be prefetched has to be greater than or equal to 0 in %s on line %d
  196. Fetch Row from PHP
  197. array(2) {
  198. [0]=>
  199. string(%d) "0"
  200. [1]=>
  201. string(%d) "test0"
  202. }
  203. Fetch Row from PL/SQL
  204. int(101)
  205. string(%d) "test101"
  206. -----------------------------------------------
  207. Test with Prefetch value set to -1
  208. -----------------------------------------------
  209. Warning: oci_set_prefetch(): Number of rows to be prefetched has to be greater than or equal to 0 in %s on line %d
  210. Fetch Row from PHP
  211. array(2) {
  212. [0]=>
  213. string(%d) "0"
  214. [1]=>
  215. string(%d) "test0"
  216. }
  217. Fetch Row from PL/SQL
  218. int(101)
  219. string(%d) "test101"
  220. Done