1--TEST--
2Prefetch with REF cursor. Test No 2
3--SKIPIF--
4<?php if (!extension_loaded('oci8')) die("skip no oci8 extension");
5if (!extension_loaded('oci8')) die("skip no oci8 extension");
6require(__DIR__."/connect.inc");
7preg_match('/.*Release ([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)*/', oci_server_version($c), $matches);
8if (!(isset($matches[0]) &&
9      ($matches[1] >= 10))) {
10       	die("skip expected output only valid when using Oracle 10g or greater database server");
11}
12preg_match('/^([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)/', oci_client_version(), $matches);
13if (!(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
22require __DIR__."/connect.inc";
23
24// Creates the necessary package and tables.
25$stmtarray = array(
26	   "DROP TABLE refcurtest",
27	   "CREATE TABLE refcurtest (c1 NUMBER, c2 VARCHAR(20))",
28           "CREATE or REPLACE PACKAGE refcurpkg is
29           type refcursortype is ref cursor;
30           procedure open_ref_cur(cur1 out refcursortype);
31           procedure fetch_ref_cur(cur1 in refcursortype, c1 out number,c2 out varchar2);
32           end refcurpkg;",
33          "CREATE or REPLACE PACKAGE body refcurpkg is
34  	    procedure open_ref_cur(cur1 out refcursortype) is
35              begin
36	        open cur1 for select * from refcurtest order by c1;
37	      end open_ref_cur;
38  	     procedure fetch_ref_cur(cur1 in refcursortype, c1 out number,
39		c2 out varchar2) is
40  	      begin
41	    	fetch cur1 into c1,c2;
42	    end fetch_ref_cur;
43         end refcurpkg;"
44	);
45
46oci8_test_sql_execute($c, $stmtarray);
47
48// Insert 500 rows into the table.
49$insert_sql = "INSERT INTO refcurtest (c1, c2) VALUES (:c1,:c2)";
50if (!($s = oci_parse($c, $insert_sql))) {
51    die("oci_parse(insert) failed!\n");
52}
53
54for ($i = 0; $i <= 500; $i++) {
55    $val2 = 'test'.$i;
56    oci_bind_by_name($s,':c1',$i);
57    oci_bind_by_name($s,':c2',$val2);
58    if (!oci_execute($s)) {
59        die("oci_execute(insert) failed!\n");
60    }
61}
62
63// Steps to Fetch from PHP . For every sub-test,the cursor is bound and then executed.
64
65$sql1 = "begin refcurpkg.open_ref_cur(:cur1); end;";
66$s1 = oci_parse($c,$sql1);
67$cur1 = oci_new_cursor($c);
68if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
69    die("oci_bind_by_name(sql1) failed!\n");
70}
71
72
73// Steps to Fetch from PL/SQL . For every sub-test,the cursor is bound and then executed.
74
75$sql2 = "begin refcurpkg.fetch_ref_cur(:curs1,:c1,:c2); end;";
76$s2 = oci_parse($c,$sql2);
77if (!oci_bind_by_name($s2, ":curs1", $cur1, -1, SQLT_RSET)) {
78    die("oci_bind_by_name(sql2) failed!\n");
79}
80if (!oci_bind_by_name($s2, ":c1", $c1, -1, SQLT_INT)) {
81    die("oci_bind_by_name(sql2) failed!\n");
82}
83if (!oci_bind_by_name($s2, ":c2", $c2, 20, SQLT_CHR)) {
84    die("oci_bind_by_name(sql2) failed!\n");
85}
86
87
88echo "------Test 1- Check Roundtrips with  prefetch 0 and 5 -----------\n";
89oci_execute($s1);
90oci_execute($cur1);
91$initial_rt = print_roundtrips($c);
92oci_set_prefetch($cur1,0);
93for ($i = 0;$i<5;$i++) {
94    var_dump(oci_fetch_row($cur1));
95}
96
97$cnt = (print_roundtrips($c) - $initial_rt);
98echo "Number of roundtrips made with prefetch count 0 for 5 rows is  $cnt\n";
99
100$initial_rt = print_roundtrips($c);
101oci_set_prefetch($cur1,5);
102for ($i = 0;$i<5;$i++) {
103    var_dump(oci_fetch_row($cur1));
104}
105
106$cnt = (print_roundtrips($c) - $initial_rt );
107echo "Number of roundtrips made with prefetch count 5 for 5 rows is  $cnt\n";
108
109echo "------Test 2 - Set Prefetch before PL/SQL fetch ----------\n";
110// Fetch from PHP
111$cur1 = oci_new_cursor($c);
112if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
113    die("oci_bind_by_name(sql1) failed!\n");
114}
115
116echo "Fetch Row from PHP\n";
117oci_execute($s1);
118oci_execute($cur1);
119var_dump(oci_fetch_row($cur1));
120oci_set_prefetch($cur1,5);
121
122// Fetch from PL/SQL
123if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
124    die("oci_bind_by_name(sql2) failed!\n");
125}
126oci_execute($s2);
127echo "Fetch Row from PL/SQL\n";
128var_dump($c1);
129var_dump($c2);
130
131function  print_roundtrips($c) {
132    $sql_stmt = "select value from v\$mystat a,v\$statname c where
133	 a.statistic#=c.statistic# and c.name='SQL*Net roundtrips to/from client'";
134    $s = oci_parse($c,$sql_stmt);
135    oci_define_by_name($s,"VALUE",$value);
136    oci_execute($s);
137    oci_fetch($s);
138    return $value;
139}
140
141// Clean up  here
142
143$stmtarray = array(
144    "drop package refcurpkg",
145    "drop table refcurtest"
146);
147
148oci8_test_sql_execute($c, $stmtarray);
149
150echo "Done\n";
151?>
152--EXPECTF--
153------Test 1- Check Roundtrips with  prefetch 0 and 5 -----------
154array(2) {
155  [0]=>
156  string(1) "0"
157  [1]=>
158  string(5) "test0"
159}
160array(2) {
161  [0]=>
162  string(1) "1"
163  [1]=>
164  string(5) "test1"
165}
166array(2) {
167  [0]=>
168  string(1) "2"
169  [1]=>
170  string(5) "test2"
171}
172array(2) {
173  [0]=>
174  string(1) "3"
175  [1]=>
176  string(5) "test3"
177}
178array(2) {
179  [0]=>
180  string(1) "4"
181  [1]=>
182  string(5) "test4"
183}
184Number of roundtrips made with prefetch count 0 for 5 rows is  6
185array(2) {
186  [0]=>
187  string(1) "5"
188  [1]=>
189  string(5) "test5"
190}
191array(2) {
192  [0]=>
193  string(1) "6"
194  [1]=>
195  string(5) "test6"
196}
197array(2) {
198  [0]=>
199  string(1) "7"
200  [1]=>
201  string(5) "test7"
202}
203array(2) {
204  [0]=>
205  string(1) "8"
206  [1]=>
207  string(5) "test8"
208}
209array(2) {
210  [0]=>
211  string(1) "9"
212  [1]=>
213  string(5) "test9"
214}
215Number of roundtrips made with prefetch count 5 for 5 rows is  2
216------Test 2 - Set Prefetch before PL/SQL fetch ----------
217Fetch Row from PHP
218array(2) {
219  [0]=>
220  string(1) "0"
221  [1]=>
222  string(5) "test0"
223}
224Fetch Row from PL/SQL
225int(101)
226string(%d) "test101"
227Done
228