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(__DIR__.'/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(__DIR__.'/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--EXPECT--
107Test 1
108array(2) {
109  [0]=>
110  string(18) "dbms_output Line 1"
111  [1]=>
112  string(18) "dbms_output Line 2"
113}
114  1  abcde
115  2  fghij
116  3  klmno
117  t
118  u
119  v
120
121Test 2
122  1  abcde
123  2  fghij
124  3  klmno
125  t
126  u
127  v
128array(2) {
129  [0]=>
130  string(18) "dbms_output Line 1"
131  [1]=>
132  string(18) "dbms_output Line 2"
133}
134