xref: /PHP-8.3/ext/oci8/tests/imp_res_field.phpt (revision a53e5617)
1--TEST--
2Oracle Database 12c Implicit Result Sets: field tests
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_field_tab_1",
28    "create table imp_res_field_tab_1 (c1_number number, c2_varchar210 varchar2(10))",
29    "insert into imp_res_field_tab_1 values (1111, 'abcde')",
30
31    "drop table imp_res_field_tab_2",
32    "create table imp_res_field_tab_2 (c3_varchar21 varchar2(4))",
33    "insert into imp_res_field_tab_2 values ('tttt')",
34
35    "drop table imp_res_field_tab_3",
36    "create table imp_res_field_tab_3 (c4_number52 number(5,2))",
37    "insert into imp_res_field_tab_3 values (33)",
38    "insert into imp_res_field_tab_3 values (NULL)",
39
40    "create or replace procedure imp_res_field_proc as
41      c1 sys_refcursor;
42    begin
43      open c1 for select * from imp_res_field_tab_1 order by 1;
44      dbms_sql.return_result(c1);
45
46      open c1 for select * from imp_res_field_tab_2 order by 1;
47      dbms_sql.return_result(c1);
48
49      open c1 for select * from imp_res_field_tab_3 order by 1;
50      dbms_sql.return_result(c1);
51    end;"
52);
53
54oci8_test_sql_execute($c, $stmtarray);
55
56function print_fields($s)
57{
58    echo "num fields : " . oci_num_fields($s) . "\n";
59    for ($i = 1; $i <= oci_num_fields($s); $i++) {
60        $is_null = oci_field_is_null($s, $i) ? "T" : "F";
61        $name = oci_field_name($s, $i);
62        $precision = oci_field_precision($s, $i);
63        $scale = oci_field_scale($s, $i);
64        $size = oci_field_size($s, $i);
65        $typeraw = oci_field_type_raw($s, $i);
66        $type = oci_field_type($s, $i);
67        echo "$name\t: is_null $is_null, precision $precision, scale $scale, size $size, typeraw $typeraw, type $type\n";
68    }
69}
70
71// Run Test
72
73echo "Test 1 - can't get IRS fields from parent\n";
74$s = oci_parse($c, "begin imp_res_field_proc(); end;");
75oci_execute($s);
76print_fields($s);
77
78echo "\nTest 2 - can't get IRS fields from parent when fetching\n";
79$s = oci_parse($c, "begin imp_res_field_proc(); end;");
80oci_execute($s);
81while (($r = oci_fetch_row($s))) {
82    var_dump($r);
83    print_fields($s);
84}
85
86echo "\nTest 3 - get IRS fields\n";
87$s = oci_parse($c, "begin imp_res_field_proc(); end;");
88oci_execute($s);
89while (($s1 = oci_get_implicit_resultset($s))) {
90    print_fields($s1);
91}
92
93echo "\nTest 4 - get IRS fields before fetching rows\n";
94$s = oci_parse($c, "begin imp_res_field_proc(); end;");
95oci_execute($s);
96$i = 0;
97while (($s1 = oci_get_implicit_resultset($s))) {
98    echo "===> Result set ".++$i."\n";
99    print_fields($s1);
100    while (($r = oci_fetch_row($s1)) !== false) {
101        var_dump($r);
102    }
103}
104
105echo "\nTest 5 - get IRS fields when fetching rows\n";
106$s = oci_parse($c, "begin imp_res_field_proc(); end;");
107oci_execute($s);
108$i = 0;
109while (($s1 = oci_get_implicit_resultset($s))) {
110    echo "===> Result set ".++$i."\n";
111    while (($r = oci_fetch_row($s1)) !== false) {
112        var_dump($r);
113        print_fields($s1);
114    }
115}
116
117// Clean up
118
119$stmtarray = array(
120    "drop procedure imp_res_field_proc",
121    "drop table imp_res_field_tab_1",
122    "drop table imp_res_field_tab_2",
123    "drop table imp_res_field_tab_3"
124);
125
126oci8_test_sql_execute($c, $stmtarray);
127
128?>
129--EXPECT--
130Test 1 - can't get IRS fields from parent
131num fields : 0
132
133Test 2 - can't get IRS fields from parent when fetching
134array(2) {
135  [0]=>
136  string(4) "1111"
137  [1]=>
138  string(5) "abcde"
139}
140num fields : 0
141array(1) {
142  [0]=>
143  string(4) "tttt"
144}
145num fields : 0
146array(1) {
147  [0]=>
148  string(2) "33"
149}
150num fields : 0
151array(1) {
152  [0]=>
153  NULL
154}
155num fields : 0
156
157Test 3 - get IRS fields
158num fields : 2
159C1_NUMBER	: is_null F, precision 0, scale -127, size 22, typeraw 2, type NUMBER
160C2_VARCHAR210	: is_null F, precision 0, scale 0, size 10, typeraw 1, type VARCHAR2
161num fields : 1
162C3_VARCHAR21	: is_null F, precision 0, scale 0, size 4, typeraw 1, type VARCHAR2
163num fields : 1
164C4_NUMBER52	: is_null F, precision 5, scale 2, size 22, typeraw 2, type NUMBER
165
166Test 4 - get IRS fields before fetching rows
167===> Result set 1
168num fields : 2
169C1_NUMBER	: is_null F, precision 0, scale -127, size 22, typeraw 2, type NUMBER
170C2_VARCHAR210	: is_null F, precision 0, scale 0, size 10, typeraw 1, type VARCHAR2
171array(2) {
172  [0]=>
173  string(4) "1111"
174  [1]=>
175  string(5) "abcde"
176}
177===> Result set 2
178num fields : 1
179C3_VARCHAR21	: is_null F, precision 0, scale 0, size 4, typeraw 1, type VARCHAR2
180array(1) {
181  [0]=>
182  string(4) "tttt"
183}
184===> Result set 3
185num fields : 1
186C4_NUMBER52	: is_null F, precision 5, scale 2, size 22, typeraw 2, type NUMBER
187array(1) {
188  [0]=>
189  string(2) "33"
190}
191array(1) {
192  [0]=>
193  NULL
194}
195
196Test 5 - get IRS fields when fetching rows
197===> Result set 1
198array(2) {
199  [0]=>
200  string(4) "1111"
201  [1]=>
202  string(5) "abcde"
203}
204num fields : 2
205C1_NUMBER	: is_null F, precision 0, scale -127, size 22, typeraw 2, type NUMBER
206C2_VARCHAR210	: is_null F, precision 0, scale 0, size 10, typeraw 1, type VARCHAR2
207===> Result set 2
208array(1) {
209  [0]=>
210  string(4) "tttt"
211}
212num fields : 1
213C3_VARCHAR21	: is_null F, precision 0, scale 0, size 4, typeraw 1, type VARCHAR2
214===> Result set 3
215array(1) {
216  [0]=>
217  string(2) "33"
218}
219num fields : 1
220C4_NUMBER52	: is_null F, precision 5, scale 2, size 22, typeraw 2, type NUMBER
221array(1) {
222  [0]=>
223  NULL
224}
225num fields : 1
226C4_NUMBER52	: is_null T, precision 5, scale 2, size 22, typeraw 2, type NUMBER
227