refcur_prefetch_4.phpt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. --TEST--
  2. Prefetch with REF cursor. Test No 4
  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, c2 out varchar2) is
  38. begin
  39. fetch cur1 into c1,c2;
  40. end fetch_ref_cur;
  41. end refcurpkg;"
  42. );
  43. oci8_test_sql_execute($c, $stmtarray);
  44. // Insert 500 rows into the table.
  45. $insert_sql = "INSERT INTO refcurtest (c1, c2) VALUES (:c1,:c2)";
  46. if (!($s = oci_parse($c, $insert_sql))) {
  47. die("oci_parse(insert) failed!\n");
  48. }
  49. for ($i = 0; $i <= 500; $i++) {
  50. $val2 = 'test'.$i;
  51. oci_bind_by_name($s,':c1',$i);
  52. oci_bind_by_name($s,':c2',$val2);
  53. if (!oci_execute($s)) {
  54. die("oci_execute(insert) failed!\n");
  55. }
  56. }
  57. // Steps to Fetch from PHP . For every sub-test,the cursor is bound and then executed.
  58. $sql1 = "begin refcurpkg.open_ref_cur(:cur1); end;";
  59. $s1 = oci_parse($c,$sql1);
  60. $cur1 = oci_new_cursor($c);
  61. if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
  62. die("oci_bind_by_name(sql1) failed!\n");
  63. }
  64. // Steps to Fetch from PL/SQL . For every sub-test,the cursor is bound and then executed.
  65. $sql2 = "begin refcurpkg.fetch_ref_cur(:curs1,:c1,:c2); end;";
  66. $s2 = oci_parse($c,$sql2);
  67. if (!oci_bind_by_name($s2, ":curs1", $cur1, -1, SQLT_RSET)) {
  68. die("oci_bind_by_name(sql2) failed!\n");
  69. }
  70. if (!oci_bind_by_name($s2, ":c1", $c1, -1, SQLT_INT)) {
  71. die("oci_bind_by_name(sql2) failed!\n");
  72. }
  73. if (!oci_bind_by_name($s2, ":c2", $c2, 20, SQLT_CHR)) {
  74. die("oci_bind_by_name(sql2) failed!\n");
  75. }
  76. echo "------Test 1 - Set Prefetch after PL/SQL fetch ----------\n";
  77. $cur1 = oci_new_cursor($c);
  78. // Fetch from PL/SQL
  79. if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
  80. die("oci_bind_by_name(sql2) failed!\n");
  81. }
  82. oci_execute($s2);
  83. echo "Fetch Row from PL/SQL\n";
  84. var_dump($c1);
  85. var_dump($c2);
  86. // Fetch from PHP
  87. echo "Fetch Row from PHP\n";
  88. if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
  89. die("oci_bind_by_name(sql1) failed!\n");
  90. }
  91. oci_set_prefetch($cur1,5);
  92. oci_execute($s1);
  93. oci_execute($cur1);
  94. var_dump(oci_fetch_row($cur1));
  95. echo "------Test 2- Overwrite prefetch-----------\n";
  96. // Fetch from PHP
  97. $cur1 = oci_new_cursor($c);
  98. if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
  99. die("oci_bind_by_name(sql1) failed!\n");
  100. }
  101. echo "Fetch Row from PHP\n";
  102. oci_execute($s1);
  103. oci_execute($cur1);
  104. var_dump(oci_fetch_row($cur1));
  105. oci_set_prefetch($cur1,5);
  106. oci_set_prefetch($cur1,0);
  107. oci_set_prefetch($cur1,100);
  108. // Fetch from PL/SQL
  109. if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
  110. die("oci_bind_by_name(sql2) failed!\n");
  111. }
  112. oci_execute($s2);
  113. echo "Fetch Row from PL/SQL\n";
  114. var_dump($c1);
  115. var_dump($c2);
  116. function print_roundtrips($c) {
  117. $sql_stmt = "select value from v\$mystat a,v\$statname c where
  118. a.statistic#=c.statistic# and c.name='SQL*Net roundtrips to/from client'";
  119. $s = oci_parse($c,$sql_stmt);
  120. oci_define_by_name($s,"VALUE",$value);
  121. oci_execute($s);
  122. oci_fetch($s);
  123. return $value;
  124. }
  125. // Clean up here
  126. $stmtarray = array(
  127. "drop package refcurpkg",
  128. "drop table refcurtest"
  129. );
  130. oci8_test_sql_execute($c, $stmtarray);
  131. echo "Done\n";
  132. ?>
  133. --EXPECTF--
  134. ------Test 1 - Set Prefetch after PL/SQL fetch ----------
  135. Warning: oci_execute(): ORA-01001: %s
  136. ORA-06512: at "%s.REFCURPKG", line %d
  137. ORA-06512: at line %d in %s on line %d
  138. Fetch Row from PL/SQL
  139. int(0)
  140. NULL
  141. Fetch Row from PHP
  142. array(2) {
  143. [0]=>
  144. string(1) "0"
  145. [1]=>
  146. string(5) "test0"
  147. }
  148. ------Test 2- Overwrite prefetch-----------
  149. Fetch Row from PHP
  150. array(2) {
  151. [0]=>
  152. string(1) "0"
  153. [1]=>
  154. string(5) "test0"
  155. }
  156. Fetch Row from PL/SQL
  157. int(101)
  158. string(%d) "test101"
  159. Done