xref: /PHP-8.2/ext/oci8/tests/bug42841.phpt (revision b5a14e6c)
1--TEST--
2Bug #42841 (REF CURSOR and oci_new_cursor PHP crash)
3--EXTENSIONS--
4oci8
5--SKIPIF--
6<?php
7$target_dbs = array('oracledb' => true, 'timesten' => false);  // test runs on these DBs
8require(__DIR__.'/skipif.inc');
9?>
10--INI--
11oci8.statement_cache_size=20
12--FILE--
13<?php
14
15require __DIR__.'/details.inc';
16
17// note a oci_new_connect() occurs lower in the script
18$c = oci_connect($user, $password, $dbase);
19
20// Initialization
21
22$stmtarray = array(
23    "create or replace procedure bug42841_proc(out_1 out sys_refcursor) is
24   begin
25      open out_1 for select 11 from dual union all select 12 from dual union all select 13 from dual;
26   end bug42841_proc;",
27
28    "create or replace package bug43449_pkg is
29        type cursortype is ref Cursor;
30        function testcursor return cursortype;
31    end bug43449_pkg;",
32
33    "create or replace package body bug43449_pkg is
34    function testcursor return cursortype is
35    retCursor cursorType;
36    begin
37        Open retCursor For 'select * from dual';
38        return retCursor;
39    end;
40    end bug43449_pkg;"
41);
42
43oci8_test_sql_execute($c, $stmtarray);
44
45// Main code
46
47function do_bug42841($c)
48{
49    echo "First attempt\n";
50
51    $sql = "BEGIN bug42841_proc(:cursor); END;";
52    $stmt = oci_parse($c, $sql);
53    $cursor = oci_new_cursor($c);
54    oci_bind_by_name($stmt, ":cursor", $cursor, -1, OCI_B_CURSOR);
55
56    oci_execute($stmt, OCI_DEFAULT);
57    oci_execute($cursor);
58
59    while($row = oci_fetch_array($cursor, OCI_ASSOC + OCI_RETURN_LOBS)) {
60        $data1[] = $row;
61    }
62
63    oci_free_statement($stmt);
64    oci_free_statement($cursor);
65    var_dump($data1);
66
67    echo "Second attempt\n";
68
69    $sql = "BEGIN bug42841_proc(:cursor); END;";
70    $stmt = oci_parse($c, $sql);
71    $cursor = oci_new_cursor($c);
72    oci_bind_by_name($stmt, ":cursor", $cursor, -1, OCI_B_CURSOR);
73
74    oci_execute($stmt, OCI_DEFAULT);
75    oci_execute($cursor);
76
77    while($row = oci_fetch_array($cursor, OCI_ASSOC + OCI_RETURN_LOBS)) {
78        $data2[] = $row;
79    }
80
81    oci_free_statement($stmt);
82    oci_free_statement($cursor);
83    var_dump($data2);
84}
85
86function do_bug43449($c)
87{
88
89    for ($i = 0; $i < 2; $i++) {
90        var_dump(bug43449_getCur($c));
91    }
92}
93
94function bug43449_getCur($c)
95{
96    $cur = oci_new_cursor($c);
97    $stmt = oci_parse($c, 'begin :cur := bug43449_pkg.testcursor; end;');
98    oci_bind_by_name($stmt, ':cur', $cur, -1, OCI_B_CURSOR);
99    oci_execute($stmt, OCI_DEFAULT);
100    oci_execute($cur, OCI_DEFAULT);
101
102    $ret = array();
103
104    while ($row = oci_fetch_assoc($cur)) {
105        $ret[] = $row;
106    }
107
108    oci_free_statement($cur);
109    oci_free_statement($stmt);
110    return $ret;
111}
112
113echo "Test bug 42841: Procedure with OUT cursor parameter\n";
114do_bug42841($c);
115
116$c = oci_new_connect($user, $password, $dbase);
117
118echo "Test bug 43449: Cursor as function result\n";
119do_bug43449($c);
120
121// Cleanup
122
123$stmtarray = array(
124    "drop procedure bug42841_proc",
125    "drop package bug43449_pkg"
126);
127
128oci8_test_sql_execute($c, $stmtarray);
129
130echo "Done\n";
131
132?>
133--EXPECT--
134Test bug 42841: Procedure with OUT cursor parameter
135First attempt
136array(3) {
137  [0]=>
138  array(1) {
139    [11]=>
140    string(2) "11"
141  }
142  [1]=>
143  array(1) {
144    [11]=>
145    string(2) "12"
146  }
147  [2]=>
148  array(1) {
149    [11]=>
150    string(2) "13"
151  }
152}
153Second attempt
154array(3) {
155  [0]=>
156  array(1) {
157    [11]=>
158    string(2) "11"
159  }
160  [1]=>
161  array(1) {
162    [11]=>
163    string(2) "12"
164  }
165  [2]=>
166  array(1) {
167    [11]=>
168    string(2) "13"
169  }
170}
171Test bug 43449: Cursor as function result
172array(1) {
173  [0]=>
174  array(1) {
175    ["DUMMY"]=>
176    string(1) "X"
177  }
178}
179array(1) {
180  [0]=>
181  array(1) {
182    ["DUMMY"]=>
183    string(1) "X"
184  }
185}
186Done
187