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