refcur_prefetch_2.phpt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. --TEST--
  2. Prefetch with REF cursor. Test No 2
  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. // Steps to Fetch from PHP . For every sub-test,the cursor is bound and then executed.
  59. $sql1 = "begin refcurpkg.open_ref_cur(:cur1); end;";
  60. $s1 = oci_parse($c,$sql1);
  61. $cur1 = oci_new_cursor($c);
  62. if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
  63. die("oci_bind_by_name(sql1) failed!\n");
  64. }
  65. // Steps to Fetch from PL/SQL . For every sub-test,the cursor is bound and then executed.
  66. $sql2 = "begin refcurpkg.fetch_ref_cur(:curs1,:c1,:c2); end;";
  67. $s2 = oci_parse($c,$sql2);
  68. if (!oci_bind_by_name($s2, ":curs1", $cur1, -1, SQLT_RSET)) {
  69. die("oci_bind_by_name(sql2) failed!\n");
  70. }
  71. if (!oci_bind_by_name($s2, ":c1", $c1, -1, SQLT_INT)) {
  72. die("oci_bind_by_name(sql2) failed!\n");
  73. }
  74. if (!oci_bind_by_name($s2, ":c2", $c2, 20, SQLT_CHR)) {
  75. die("oci_bind_by_name(sql2) failed!\n");
  76. }
  77. echo "------Test 1- Check Roundtrips with prefetch 0 and 5 -----------\n";
  78. oci_execute($s1);
  79. oci_execute($cur1);
  80. $initial_rt = print_roundtrips($c);
  81. oci_set_prefetch($cur1,0);
  82. for ($i = 0;$i<5;$i++) {
  83. var_dump(oci_fetch_row($cur1));
  84. }
  85. $cnt = (print_roundtrips($c) - $initial_rt);
  86. echo "Number of roundtrips made with prefetch count 0 for 5 rows is $cnt\n";
  87. $initial_rt = print_roundtrips($c);
  88. oci_set_prefetch($cur1,5);
  89. for ($i = 0;$i<5;$i++) {
  90. var_dump(oci_fetch_row($cur1));
  91. }
  92. $cnt = (print_roundtrips($c) - $initial_rt );
  93. echo "Number of roundtrips made with prefetch count 5 for 5 rows is $cnt\n";
  94. echo "------Test 2 - Set Prefetch before PL/SQL fetch ----------\n";
  95. // Fetch from PHP
  96. $cur1 = oci_new_cursor($c);
  97. if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
  98. die("oci_bind_by_name(sql1) failed!\n");
  99. }
  100. echo "Fetch Row from PHP\n";
  101. oci_execute($s1);
  102. oci_execute($cur1);
  103. var_dump(oci_fetch_row($cur1));
  104. oci_set_prefetch($cur1,5);
  105. // Fetch from PL/SQL
  106. if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
  107. die("oci_bind_by_name(sql2) failed!\n");
  108. }
  109. oci_execute($s2);
  110. echo "Fetch Row from PL/SQL\n";
  111. var_dump($c1);
  112. var_dump($c2);
  113. function print_roundtrips($c) {
  114. $sql_stmt = "select value from v\$mystat a,v\$statname c where
  115. a.statistic#=c.statistic# and c.name='SQL*Net roundtrips to/from client'";
  116. $s = oci_parse($c,$sql_stmt);
  117. oci_define_by_name($s,"VALUE",$value);
  118. oci_execute($s);
  119. oci_fetch($s);
  120. return $value;
  121. }
  122. // Clean up here
  123. $stmtarray = array(
  124. "drop package refcurpkg",
  125. "drop table refcurtest"
  126. );
  127. oci8_test_sql_execute($c, $stmtarray);
  128. echo "Done\n";
  129. ?>
  130. --EXPECTF--
  131. ------Test 1- Check Roundtrips with prefetch 0 and 5 -----------
  132. array(2) {
  133. [0]=>
  134. string(1) "0"
  135. [1]=>
  136. string(5) "test0"
  137. }
  138. array(2) {
  139. [0]=>
  140. string(1) "1"
  141. [1]=>
  142. string(5) "test1"
  143. }
  144. array(2) {
  145. [0]=>
  146. string(1) "2"
  147. [1]=>
  148. string(5) "test2"
  149. }
  150. array(2) {
  151. [0]=>
  152. string(1) "3"
  153. [1]=>
  154. string(5) "test3"
  155. }
  156. array(2) {
  157. [0]=>
  158. string(1) "4"
  159. [1]=>
  160. string(5) "test4"
  161. }
  162. Number of roundtrips made with prefetch count 0 for 5 rows is 6
  163. array(2) {
  164. [0]=>
  165. string(1) "5"
  166. [1]=>
  167. string(5) "test5"
  168. }
  169. array(2) {
  170. [0]=>
  171. string(1) "6"
  172. [1]=>
  173. string(5) "test6"
  174. }
  175. array(2) {
  176. [0]=>
  177. string(1) "7"
  178. [1]=>
  179. string(5) "test7"
  180. }
  181. array(2) {
  182. [0]=>
  183. string(1) "8"
  184. [1]=>
  185. string(5) "test8"
  186. }
  187. array(2) {
  188. [0]=>
  189. string(1) "9"
  190. [1]=>
  191. string(5) "test9"
  192. }
  193. Number of roundtrips made with prefetch count 5 for 5 rows is 2
  194. ------Test 2 - Set Prefetch before PL/SQL fetch ----------
  195. Fetch Row from PHP
  196. array(2) {
  197. [0]=>
  198. string(1) "0"
  199. [1]=>
  200. string(5) "test0"
  201. }
  202. Fetch Row from PL/SQL
  203. int(101)
  204. string(%d) "test101"
  205. Done