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