1--TEST--
2Prefetch with REF cursor. Test No 4
3--SKIPIF--
4<?php if (!extension_loaded('oci8')) die("skip no oci8 extension");
5if (!extension_loaded('oci8')) die("skip no oci8 extension");
6require(dirname(__FILE__)."/connect.inc");
7if (preg_match('/Release 1[012]\./', oci_server_version($c), $matches) !== 1) {
8	die("skip expected output only valid when using Oracle 10g or greater databases");
9} else if (preg_match('/^(11\.2|12)\./', oci_client_version()) != 1) {
10    die("skip test expected to work only with Oracle 11gR2 or greater version of client");
11}
12?>
13--FILE--
14<?php
15require dirname(__FILE__)."/connect.inc";
16
17// Creates the necessary package and tables.
18$stmtarray = array(
19	   "DROP TABLE refcurtest",
20
21	   "CREATE TABLE refcurtest (c1 NUMBER, c2 VARCHAR(20))",
22
23       "CREATE or REPLACE PACKAGE refcurpkg is
24           type refcursortype is ref cursor;
25           procedure open_ref_cur(cur1 out refcursortype);
26           procedure fetch_ref_cur(cur1 in refcursortype, c1 out number, c2 out varchar2);
27        end refcurpkg;",
28
29       "CREATE or REPLACE PACKAGE body refcurpkg is
30  	    procedure open_ref_cur(cur1 out refcursortype) is
31          begin
32	        open cur1 for select * from refcurtest order by c1;
33	      end open_ref_cur;
34  	     procedure fetch_ref_cur(cur1 in refcursortype, c1 out number, c2 out varchar2) is
35  	      begin
36	    	fetch cur1 into c1,c2;
37  	      end fetch_ref_cur;
38         end refcurpkg;"
39	);
40
41oci8_test_sql_execute($c, $stmtarray);
42
43// Insert 500 rows into the table.
44$insert_sql = "INSERT INTO refcurtest (c1, c2) VALUES (:c1,:c2)";
45if (!($s = oci_parse($c, $insert_sql))) {
46    die("oci_parse(insert) failed!\n");
47}
48
49for ($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
58// Steps to Fetch from PHP . For every sub-test,the cursor is bound and then executed.
59
60$sql1 = "begin refcurpkg.open_ref_cur(:cur1); end;";
61$s1 = oci_parse($c,$sql1);
62$cur1 = oci_new_cursor($c);
63if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
64    die("oci_bind_by_name(sql1) failed!\n");
65}
66
67
68// Steps to Fetch from PL/SQL . For every sub-test,the cursor is bound and then executed.
69
70$sql2 = "begin refcurpkg.fetch_ref_cur(:curs1,:c1,:c2); end;";
71$s2 = oci_parse($c,$sql2);
72if (!oci_bind_by_name($s2, ":curs1", $cur1, -1, SQLT_RSET)) {
73    die("oci_bind_by_name(sql2) failed!\n");
74}
75if (!oci_bind_by_name($s2, ":c1", $c1, -1, SQLT_INT)) {
76    die("oci_bind_by_name(sql2) failed!\n");
77}
78if (!oci_bind_by_name($s2, ":c2", $c2, 20, SQLT_CHR)) {
79    die("oci_bind_by_name(sql2) failed!\n");
80}
81
82
83echo "------Test 1 - Set Prefetch after PL/SQL fetch ----------\n";
84$cur1 = oci_new_cursor($c);
85// Fetch from PL/SQL
86if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
87    die("oci_bind_by_name(sql2) failed!\n");
88}
89oci_execute($s2);
90echo "Fetch Row from PL/SQL\n";
91var_dump($c1);
92var_dump($c2);
93
94// Fetch from PHP
95echo "Fetch Row from PHP\n";
96if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
97    die("oci_bind_by_name(sql1) failed!\n");
98}
99oci_set_prefetch($cur1,5);
100oci_execute($s1);
101oci_execute($cur1);
102var_dump(oci_fetch_row($cur1));
103
104echo "------Test 2- Overwrite prefetch-----------\n";
105// Fetch from PHP
106$cur1 = oci_new_cursor($c);
107if (!oci_bind_by_name($s1,":cur1",$cur1,-1,SQLT_RSET)) {
108    die("oci_bind_by_name(sql1) failed!\n");
109}
110echo "Fetch Row from PHP\n";
111oci_execute($s1);
112oci_execute($cur1);
113var_dump(oci_fetch_row($cur1));
114oci_set_prefetch($cur1,5);
115oci_set_prefetch($cur1,0);
116oci_set_prefetch($cur1,100);
117
118// Fetch from PL/SQL
119if (!oci_bind_by_name($s2,":curs1",$cur1,-1,SQLT_RSET)) {
120    die("oci_bind_by_name(sql2) failed!\n");
121}
122oci_execute($s2);
123echo "Fetch Row from PL/SQL\n";
124var_dump($c1);
125var_dump($c2);
126
127
128function  print_roundtrips($c) {
129    $sql_stmt = "select value from v\$mystat a,v\$statname c where
130	 a.statistic#=c.statistic# and c.name='SQL*Net roundtrips to/from client'";
131    $s = oci_parse($c,$sql_stmt);
132    oci_define_by_name($s,"VALUE",$value);
133    oci_execute($s);
134    oci_fetch($s);
135    return $value;
136}
137
138// Clean up  here
139
140$stmtarray = array(
141    "drop package refcurpkg",
142    "drop table refcurtest"
143);
144
145oci8_test_sql_execute($c, $stmtarray);
146
147echo "Done\n";
148?>
149--EXPECTF--
150------Test 1 - Set Prefetch after PL/SQL fetch ----------
151
152Warning: oci_execute(): ORA-01001: %s
153ORA-06512: at "SYSTEM.REFCURPKG", line %d
154ORA-06512: at line %d in %s on line %d
155Fetch Row from PL/SQL
156int(0)
157NULL
158Fetch Row from PHP
159array(2) {
160  [0]=>
161  string(1) "0"
162  [1]=>
163  string(5) "test0"
164}
165------Test 2- Overwrite prefetch-----------
166Fetch Row from PHP
167array(2) {
168  [0]=>
169  string(1) "0"
170  [1]=>
171  string(5) "test0"
172}
173Fetch Row from PL/SQL
174int(101)
175string(%d) "test101"
176Done
177