refcur_prefetch_1.phpt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. --TEST--
  2. Prefetch with REF cursor. Test different values for prefetch with oci_set_prefetch().
  3. --EXTENSIONS--
  4. oci8
  5. --SKIPIF--
  6. <?php require(__DIR__."/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(__DIR__."/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);
  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. // Various invalid values for prefetch
  69. $pref = array(-12345,-1);
  70. foreach($pref as $value) {
  71. try {
  72. echo "-----------------------------------------------\n";
  73. echo "Test with Prefetch (invalid) value set to $value \n";
  74. echo "-----------------------------------------------\n";
  75. $cur1 = oci_new_cursor($c);
  76. fetch_frm_php($c,$cur1,$value);
  77. fetch_frm_plsql($c,$cur1);
  78. } catch(ValueError $e) {
  79. echo $e->getMessage(), "\n";
  80. }
  81. }
  82. // This function sets the prefetch count to the given $value and fetches one row .
  83. function fetch_frm_php($c,$cur1,$value) {
  84. $sql1 = "begin refcurpkg.open_ref_cur(:cur1); end;";
  85. $s1 = oci_parse($c,$sql1);
  86. if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
  87. die("oci_bind_by_name(sql1) failed!\n");
  88. }
  89. oci_execute($s1);
  90. oci_set_prefetch($cur1,$value);
  91. oci_execute($cur1);
  92. echo "Fetch Row from PHP\n";
  93. var_dump(oci_fetch_row($cur1));
  94. }
  95. // This function calls the fetch_ref_cur procedure to get the values from the REF cur.
  96. function fetch_frm_plsql($c,$cur1) {
  97. $sql2 = "begin refcurpkg.fetch_ref_cur(:curs1,:c1,:c2); end;";
  98. $s2 = oci_parse($c,$sql2);
  99. if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
  100. die("oci_bind_by_name(sql2) failed!\n");
  101. }
  102. if (!oci_bind_by_name($s2,":c1",$c1,-1,SQLT_INT)) {
  103. die("oci_bind_by_name(sql2) failed!\n");
  104. }
  105. if (!oci_bind_by_name($s2,":c2",$c2,20,SQLT_CHR)) {
  106. die("oci_bind_by_name(sql2) failed!\n");
  107. }
  108. oci_execute($s2);
  109. echo "Fetch Row from PL/SQL\n";
  110. var_dump($c1);
  111. var_dump($c2);
  112. }
  113. // Clean up here
  114. $stmtarray = array(
  115. "drop package refcurpkg",
  116. "drop table refcurtest"
  117. );
  118. oci8_test_sql_execute($c, $stmtarray);
  119. echo "Done\n";
  120. ?>
  121. --EXPECTF--
  122. -----------------------------------------------
  123. Test with Prefetch value set to 0
  124. -----------------------------------------------
  125. Fetch Row from PHP
  126. array(2) {
  127. [0]=>
  128. string(%d) "0"
  129. [1]=>
  130. string(%d) "test0"
  131. }
  132. Fetch Row from PL/SQL
  133. int(1)
  134. string(%d) "test1"
  135. -----------------------------------------------
  136. Test with Prefetch value set to 1
  137. -----------------------------------------------
  138. Fetch Row from PHP
  139. array(2) {
  140. [0]=>
  141. string(%d) "0"
  142. [1]=>
  143. string(%d) "test0"
  144. }
  145. Fetch Row from PL/SQL
  146. int(2)
  147. string(%d) "test2"
  148. -----------------------------------------------
  149. Test with Prefetch value set to 501
  150. -----------------------------------------------
  151. Fetch Row from PHP
  152. array(2) {
  153. [0]=>
  154. string(%d) "0"
  155. [1]=>
  156. string(%d) "test0"
  157. }
  158. Warning: oci_execute(): ORA-01002: %s
  159. ORA-06512: at "%s.REFCURPKG", line %d
  160. ORA-06512: at line %d in %s on line %d
  161. Fetch Row from PL/SQL
  162. int(0)
  163. NULL
  164. -----------------------------------------------
  165. Test with Prefetch value set to 499
  166. -----------------------------------------------
  167. Fetch Row from PHP
  168. array(2) {
  169. [0]=>
  170. string(%d) "0"
  171. [1]=>
  172. string(%d) "test0"
  173. }
  174. Fetch Row from PL/SQL
  175. int(500)
  176. string(%d) "test500"
  177. -----------------------------------------------
  178. Test with Prefetch value set to 250
  179. -----------------------------------------------
  180. Fetch Row from PHP
  181. array(2) {
  182. [0]=>
  183. string(%d) "0"
  184. [1]=>
  185. string(%d) "test0"
  186. }
  187. Fetch Row from PL/SQL
  188. int(251)
  189. string(%d) "test251"
  190. -----------------------------------------------
  191. Test with Prefetch value set to 12345
  192. -----------------------------------------------
  193. Fetch Row from PHP
  194. array(2) {
  195. [0]=>
  196. string(%d) "0"
  197. [1]=>
  198. string(%d) "test0"
  199. }
  200. Warning: oci_execute(): ORA-01002: %s
  201. ORA-06512: at "%s.REFCURPKG", line %d
  202. ORA-06512: at line %d in %s on line %d
  203. Fetch Row from PL/SQL
  204. int(0)
  205. NULL
  206. -----------------------------------------------
  207. Test with Prefetch (invalid) value set to -12345
  208. -----------------------------------------------
  209. oci_set_prefetch(): Argument #2 ($rows) must be greater than or equal to 0
  210. -----------------------------------------------
  211. Test with Prefetch (invalid) value set to -1
  212. -----------------------------------------------
  213. oci_set_prefetch(): Argument #2 ($rows) must be greater than or equal to 0
  214. Done