xref: /PHP-8.2/ext/pdo_oci/tests/bug57702.phpt (revision 39131219)
1--TEST--
2PDO OCI Bug #57702 (Multi-row BLOB fetches)
3--EXTENSIONS--
4pdo
5pdo_oci
6--SKIPIF--
7<?php
8require(__DIR__.'/../../pdo/tests/pdo_test.inc');
9PDOTest::skip();
10?>
11--FILE--
12<?php
13
14require('ext/pdo/tests/pdo_test.inc');
15$db = PDOTest::test_factory('ext/pdo_oci/tests/common.phpt');
16
17// Note the PDO test setup sets PDO::ATTR_STRINGIFY_FETCHES to true
18// (and sets PDO::ATTR_CASE to PDO::CASE_LOWER)
19
20$query = "begin execute immediate 'drop table bug57702'; exception when others then if sqlcode <> -942 then raise; end if; end;";
21$stmt = $db->prepare($query);
22$stmt->execute();
23
24$query = "create table bug57702 (id number, data1 blob, data2 blob)";
25$stmt = $db->prepare($query);
26$stmt->execute();
27
28function do_insert($db, $id, $data1, $data2)
29{
30    $db->beginTransaction();
31    $stmt = $db->prepare("insert into bug57702 (id, data1, data2) values (:id, empty_blob(), empty_blob()) returning data1, data2 into :blob1, :blob2");
32    $stmt->bindParam(':id', $id);
33    $stmt->bindParam(':blob1', $blob1, PDO::PARAM_LOB);
34    $stmt->bindParam(':blob2', $blob2, PDO::PARAM_LOB);
35    $blob1 = null;
36    $blob2 = null;
37    $stmt->execute();
38
39    fwrite($blob1, $data1);
40    fclose($blob1);
41    fwrite($blob2, $data2);
42    fclose($blob2);
43    $db->commit();
44}
45
46do_insert($db, 1, "row 1 col 1", "row 1 col 2");
47do_insert($db, 2, "row 2 col 1", "row 2 col 2");
48
49////////////////////
50
51echo "First Query\n";
52
53// Fetch it back
54$stmt = $db->prepare('select data1, data2 from bug57702 order by id');
55$stmt->execute();
56$row = $stmt->fetch(PDO::FETCH_ASSOC);
57var_dump($row['data1']);
58var_dump($row['data2']);
59$row = $stmt->fetch(PDO::FETCH_ASSOC);
60var_dump($row['data1']);
61var_dump($row['data2']);
62
63////////////////////
64
65echo "\nSecond Query\n";
66
67foreach($db->query("select data1 as d1, data2 as d2 from bug57702 order by id") as $row) {
68    var_dump($row['d1']);
69    var_dump($row['d2']);
70}
71
72////////////////////
73
74echo "\nThird Query\n";
75
76$stmt = $db->prepare('select data1 as d3_1, data2 as d3_2 from bug57702 order by id');
77
78$rs = $stmt->execute();
79$stmt->bindColumn('d3_1' , $clob1, PDO::PARAM_LOB);
80$stmt->bindColumn('d3_2' , $clob2, PDO::PARAM_LOB);
81
82while ($stmt->fetch(PDO::FETCH_BOUND)) {
83    var_dump($clob1);
84    var_dump($clob2);
85}
86
87////////////////////
88
89echo "\nFourth Query\n";
90
91$a = array();
92$i = 0;
93foreach($db->query("select data1 as d4_1, data2 as d4_2 from bug57702 order by id") as $row) {
94    $a[$i][0] = $row['d4_1'];
95    $a[$i][1] = $row['d4_2'];
96    $i++;
97}
98
99for ($i = 0; $i < count($a); $i++) {
100    var_dump($a[$i][0]);
101    var_dump($a[$i][1]);
102}
103
104////////////////////
105
106echo "\nFifth Query\n";
107
108$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);  // Let's use streams
109
110// Since each column only has one lob descriptor, the last row is
111// shown twice because the lob descriptor for each column is reused in
112// the stream
113
114$a = array();
115$i = 0;
116foreach($db->query("select data1 as d4_1, data2 as d4_2 from bug57702 order by id") as $row) {
117    $a[$i][0] = $row['d4_1'];
118    $a[$i][1] = $row['d4_2'];
119    $i++;
120}
121
122for ($i = 0; $i < count($a); $i++) {
123    var_dump(stream_get_contents($a[$i][0]));
124    var_dump(stream_get_contents($a[$i][1]));
125}
126
127////////////////////
128
129echo "\nSixth Query\n";
130
131$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);  // Let's use streams
132
133$a = array();
134$i = 0;
135foreach($db->query("select data1 as d4_1, data2 as d4_2 from bug57702 order by id") as $row) {
136    $a[$i][0] = $row['d4_1'];
137    $a[$i][1] = $row['d4_2'];
138    var_dump(stream_get_contents($a[$i][0]));
139    var_dump(stream_get_contents($a[$i][1]));
140    $i++;
141}
142
143// Cleanup
144$query = "drop table bug57702";
145$stmt = $db->prepare($query);
146$stmt->execute();
147
148print "done\n";
149
150?>
151--EXPECT--
152First Query
153string(11) "row 1 col 1"
154string(11) "row 1 col 2"
155string(11) "row 2 col 1"
156string(11) "row 2 col 2"
157
158Second Query
159string(11) "row 1 col 1"
160string(11) "row 1 col 2"
161string(11) "row 2 col 1"
162string(11) "row 2 col 2"
163
164Third Query
165string(11) "row 1 col 1"
166string(11) "row 1 col 2"
167string(11) "row 2 col 1"
168string(11) "row 2 col 2"
169
170Fourth Query
171string(11) "row 1 col 1"
172string(11) "row 1 col 2"
173string(11) "row 2 col 1"
174string(11) "row 2 col 2"
175
176Fifth Query
177string(11) "row 2 col 1"
178string(11) "row 2 col 2"
179string(11) "row 2 col 1"
180string(11) "row 2 col 2"
181
182Sixth Query
183string(11) "row 1 col 1"
184string(11) "row 1 col 2"
185string(11) "row 2 col 1"
186string(11) "row 2 col 2"
187done
188