1--TEST--
2Prefetch with REF cursor. Test No 4
3--EXTENSIONS--
4oci8
5--SKIPIF--
6<?php require(__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
28       "CREATE TABLE refcurtest (c1 NUMBER, c2 VARCHAR(20))",
29
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
36       "CREATE or REPLACE PACKAGE body refcurpkg is
37        procedure open_ref_cur(cur1 out refcursortype) is
38          begin
39            open cur1 for select * from refcurtest order by c1;
40          end open_ref_cur;
41         procedure fetch_ref_cur(cur1 in refcursortype, c1 out number, 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 - Set Prefetch after PL/SQL fetch ----------\n";
91$cur1 = oci_new_cursor($c);
92// Fetch from PL/SQL
93if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
94    die("oci_bind_by_name(sql2) failed!\n");
95}
96oci_execute($s2);
97echo "Fetch Row from PL/SQL\n";
98var_dump($c1);
99var_dump($c2);
100
101// Fetch from PHP
102echo "Fetch Row from PHP\n";
103if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
104    die("oci_bind_by_name(sql1) failed!\n");
105}
106oci_set_prefetch($cur1,5);
107oci_execute($s1);
108oci_execute($cur1);
109var_dump(oci_fetch_row($cur1));
110
111echo "------Test 2- Overwrite prefetch-----------\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}
117echo "Fetch Row from PHP\n";
118oci_execute($s1);
119oci_execute($cur1);
120var_dump(oci_fetch_row($cur1));
121oci_set_prefetch($cur1,5);
122oci_set_prefetch($cur1,0);
123oci_set_prefetch($cur1,100);
124
125// Fetch from PL/SQL
126if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
127    die("oci_bind_by_name(sql2) failed!\n");
128}
129oci_execute($s2);
130echo "Fetch Row from PL/SQL\n";
131var_dump($c1);
132var_dump($c2);
133
134
135function  print_roundtrips($c) {
136    $sql_stmt = "select value from v\$mystat a,v\$statname c where
137     a.statistic#=c.statistic# and c.name='SQL*Net roundtrips to/from client'";
138    $s = oci_parse($c,$sql_stmt);
139    oci_define_by_name($s,"VALUE",$value);
140    oci_execute($s);
141    oci_fetch($s);
142    return $value;
143}
144
145// Clean up  here
146
147$stmtarray = array(
148    "drop package refcurpkg",
149    "drop table refcurtest"
150);
151
152oci8_test_sql_execute($c, $stmtarray);
153
154echo "Done\n";
155?>
156--EXPECTF--
157------Test 1 - Set Prefetch after PL/SQL fetch ----------
158
159Warning: oci_execute(): ORA-01001: %s
160ORA-06512: at "%s.REFCURPKG", line %d
161ORA-06512: at line %d in %s on line %d
162Fetch Row from PL/SQL
163int(0)
164NULL
165Fetch Row from PHP
166array(2) {
167  [0]=>
168  string(1) "0"
169  [1]=>
170  string(5) "test0"
171}
172------Test 2- Overwrite prefetch-----------
173Fetch Row from PHP
174array(2) {
175  [0]=>
176  string(1) "0"
177  [1]=>
178  string(5) "test0"
179}
180Fetch Row from PL/SQL
181int(101)
182string(%d) "test101"
183Done
184