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