1--TEST--
2Oracle Database 12c Implicit Result Sets: interleaved with DBMS_OUTPUT
3--SKIPIF--
4<?php
5if (!extension_loaded('oci8')) die ("skip no oci8 extension");
6$target_dbs = array('oracledb' => true, 'timesten' => false);  // test runs on these DBs
7require(dirname(__FILE__).'/skipif.inc');
8preg_match('/.*Release ([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)*/', oci_server_version($c), $matches);
9if (!(isset($matches[0]) && $matches[1] >= 12)) {
10    die("skip expected output only valid when using Oracle Database 12c or greater");
11}
12preg_match('/^[[:digit:]]+/', oci_client_version(), $matches);
13if (!(isset($matches[0]) && $matches[0] >= 12)) {
14    die("skip works only with Oracle 12c or greater version of Oracle client libraries");
15}
16?>
17--FILE--
18<?php
19
20require(dirname(__FILE__).'/connect.inc');
21
22// Initialization
23
24$stmtarray = array(
25    "drop table imp_res_dbmsoutput_tab_1",
26    "create table imp_res_dbmsoutput_tab_1 (c1 number, c2 varchar2(10))",
27    "insert into imp_res_dbmsoutput_tab_1 values (1, 'abcde')",
28    "insert into imp_res_dbmsoutput_tab_1 values (2, 'fghij')",
29    "insert into imp_res_dbmsoutput_tab_1 values (3, 'klmno')",
30
31    "drop table imp_res_dbmsoutput_tab_2",
32    "create table imp_res_dbmsoutput_tab_2 (c3 varchar2(1))",
33    "insert into imp_res_dbmsoutput_tab_2 values ('t')",
34    "insert into imp_res_dbmsoutput_tab_2 values ('u')",
35    "insert into imp_res_dbmsoutput_tab_2 values ('v')",
36
37    "create or replace procedure imp_res_dbmsoutput_proc as
38      c1 sys_refcursor;
39    begin
40      dbms_output.put_line('dbms_output Line 1');
41      open c1 for select * from imp_res_dbmsoutput_tab_1 order by 1;
42      dbms_sql.return_result(c1);
43      dbms_output.put_line('dbms_output Line 2');
44      open c1 for select * from imp_res_dbmsoutput_tab_2 order by 1;
45      dbms_sql.return_result(c1);
46    end;"
47);
48
49oci8_test_sql_execute($c, $stmtarray);
50
51function setserveroutputon($c)
52{
53    $s = oci_parse($c, "begin dbms_output.enable(null); end;");
54    oci_execute($s);
55}
56
57function getdbmsoutput_do($c)
58{
59    $s = oci_parse($c, "begin dbms_output.get_line(:ln, :st); end;");
60    oci_bind_by_name($s, ":ln", $ln, 100);
61    oci_bind_by_name($s, ":st", $st, -1, SQLT_INT);
62    $res = false;
63    while (($succ = oci_execute($s)) && !$st) {
64        $res[] = $ln;  // append each line to the array
65    }
66    return $res;
67}
68
69setserveroutputon($c);
70
71// Run Test
72
73echo "Test 1\n";
74$s = oci_parse($c, "begin imp_res_dbmsoutput_proc(); end;");
75oci_execute($s);
76var_dump(getdbmsoutput_do($c));
77while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
78    foreach ($row as $item) {
79        echo "  ".$item;
80    }
81    echo "\n";
82}
83
84echo "\nTest 2\n";
85$s = oci_parse($c, "begin imp_res_dbmsoutput_proc(); end;");
86oci_execute($s);
87while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
88    foreach ($row as $item) {
89        echo "  ".$item;
90    }
91    echo "\n";
92}
93var_dump(getdbmsoutput_do($c));
94
95// Clean up
96
97$stmtarray = array(
98    "drop procedure imp_res_dbmsoutput_proc",
99    "drop table imp_res_dbmsoutput_tab_1",
100    "drop table imp_res_dbmsoutput_tab_2"
101);
102
103oci8_test_sql_execute($c, $stmtarray);
104
105?>
106===DONE===
107<?php exit(0); ?>
108--EXPECT--
109Test 1
110array(2) {
111  [0]=>
112  string(18) "dbms_output Line 1"
113  [1]=>
114  string(18) "dbms_output Line 2"
115}
116  1  abcde
117  2  fghij
118  3  klmno
119  t
120  u
121  v
122
123Test 2
124  1  abcde
125  2  fghij
126  3  klmno
127  t
128  u
129  v
130array(2) {
131  [0]=>
132  string(18) "dbms_output Line 1"
133  [1]=>
134  string(18) "dbms_output Line 2"
135}
136===DONE===
137