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