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