xref: /PHP-5.4/ext/pdo_oci/tests/bug57702.phpt (revision 4fcf4e08)
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 mytable'; 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}
84print "done\n";
85
86////////////////////
87
88echo "\nFourth Query\n";
89
90$a = array();
91$i = 0;
92foreach($db->query("select data1 as d4_1, data2 as d4_2 from bug57702 order by id") as $row) {
93	$a[$i][0] = $row['d4_1'];
94	$a[$i][1] = $row['d4_2'];
95    $i++;
96}
97
98for ($i = 0; $i < count($a); $i++) {
99    var_dump($a[$i][0]);
100    var_dump($a[$i][1]);
101}
102
103////////////////////
104
105echo "\nFifth Query\n";
106
107$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);  // Let's use streams
108
109// Since each column only has one lob descriptor, the last row is
110// shown twice because the lob descriptor for each column is reused in
111// the stream
112
113$a = array();
114$i = 0;
115foreach($db->query("select data1 as d4_1, data2 as d4_2 from bug57702 order by id") as $row) {
116	$a[$i][0] = $row['d4_1'];
117	$a[$i][1] = $row['d4_2'];
118    $i++;
119}
120
121for ($i = 0; $i < count($a); $i++) {
122    var_dump(stream_get_contents($a[$i][0]));
123    var_dump(stream_get_contents($a[$i][1]));
124}
125
126// Cleanup
127$query = "drop table bug57702";
128$stmt = $db->prepare($query);
129$stmt->execute();
130
131print "done\n";
132
133?>
134--EXPECTF--
135First Query
136string(11) "row 1 col 1"
137string(11) "row 1 col 2"
138string(11) "row 2 col 1"
139string(11) "row 2 col 2"
140
141Second Query
142string(11) "row 1 col 1"
143string(11) "row 1 col 2"
144string(11) "row 2 col 1"
145string(11) "row 2 col 2"
146
147Third Query
148string(11) "row 1 col 1"
149string(11) "row 1 col 2"
150string(11) "row 2 col 1"
151string(11) "row 2 col 2"
152done
153
154Fourth Query
155string(11) "row 1 col 1"
156string(11) "row 1 col 2"
157string(11) "row 2 col 1"
158string(11) "row 2 col 2"
159
160Fifth Query
161string(11) "row 2 col 1"
162string(11) "row 2 col 2"
163string(11) "row 2 col 1"
164string(11) "row 2 col 2"
165done
166