1--TEST--
2Oracle Database 12c Implicit Result Sets: 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_dbmsoutput_tab_1",
28    "create table imp_res_dbmsoutput_tab_1 (c1 number, c2 varchar2(10))",
29    "insert into imp_res_dbmsoutput_tab_1 values (1, 'abcde')",
30    "insert into imp_res_dbmsoutput_tab_1 values (2, 'fghij')",
31    "insert into imp_res_dbmsoutput_tab_1 values (3, 'klmno')",
32
33    "drop table imp_res_dbmsoutput_tab_2",
34    "create table imp_res_dbmsoutput_tab_2 (c3 varchar2(1))",
35    "insert into imp_res_dbmsoutput_tab_2 values ('t')",
36    "insert into imp_res_dbmsoutput_tab_2 values ('u')",
37    "insert into imp_res_dbmsoutput_tab_2 values ('v')",
38
39    "create or replace procedure imp_res_dbmsoutput_proc as
40      c1 sys_refcursor;
41    begin
42      dbms_output.put_line('dbms_output Line 1');
43      open c1 for select * from imp_res_dbmsoutput_tab_1 order by 1;
44      dbms_sql.return_result(c1);
45      dbms_output.put_line('dbms_output Line 2');
46      open c1 for select * from imp_res_dbmsoutput_tab_2 order by 1;
47      dbms_sql.return_result(c1);
48    end;"
49);
50
51oci8_test_sql_execute($c, $stmtarray);
52
53function setserveroutputon($c)
54{
55    $s = oci_parse($c, "begin dbms_output.enable(null); end;");
56    oci_execute($s);
57}
58
59function getdbmsoutput_do($c)
60{
61    $s = oci_parse($c, "begin dbms_output.get_line(:ln, :st); end;");
62    oci_bind_by_name($s, ":ln", $ln, 100);
63    oci_bind_by_name($s, ":st", $st, -1, SQLT_INT);
64    $res = [];
65    while (($succ = oci_execute($s)) && !$st) {
66        $res[] = $ln;  // append each line to the array
67    }
68    return $res;
69}
70
71setserveroutputon($c);
72
73// Run Test
74
75echo "Test 1\n";
76$s = oci_parse($c, "begin imp_res_dbmsoutput_proc(); end;");
77oci_execute($s);
78var_dump(getdbmsoutput_do($c));
79while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
80    foreach ($row as $item) {
81        echo "  ".$item;
82    }
83    echo "\n";
84}
85
86echo "\nTest 2\n";
87$s = oci_parse($c, "begin imp_res_dbmsoutput_proc(); end;");
88oci_execute($s);
89while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
90    foreach ($row as $item) {
91        echo "  ".$item;
92    }
93    echo "\n";
94}
95var_dump(getdbmsoutput_do($c));
96
97// Clean up
98
99$stmtarray = array(
100    "drop procedure imp_res_dbmsoutput_proc",
101    "drop table imp_res_dbmsoutput_tab_1",
102    "drop table imp_res_dbmsoutput_tab_2"
103);
104
105oci8_test_sql_execute($c, $stmtarray);
106
107?>
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