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