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