xref: /PHP-8.1/ext/oci8/tests/bug70700.phpt (revision b5a14e6c)
1--TEST--
2Tests for LOBs with multibyte strings, reading them out in chunks
3(Doc Bug #70700)
4--CREDITS--
5Chuck Burgess
6ashnazg@php.net
7--EXTENSIONS--
8mbstring
9oci8
10--SKIPIF--
11<?php
12$target_dbs = array('oracledb' => true, 'timesten' => false);  // test runs on these DBs
13require(__DIR__.'/skipif.inc');
14?>
15--ENV--
16NLS_LANG=.AL32UTF8
17--FILE--
18<?php
19require(__DIR__.'/connect.inc');
20
21
22$stmt = oci_parse($c, 'DROP TABLE oci8_bug70700');
23@oci_execute($stmt);
24oci_free_statement($stmt);
25$stmt = oci_parse($c, 'CREATE TABLE oci8_bug70700 (id NUMBER, data CLOB)');
26oci_execute($stmt);
27oci_free_statement($stmt);
28
29
30$id = null;
31$insert = oci_parse($c, 'INSERT INTO oci8_bug70700 (id, data) VALUES (:id, :data)');
32oci_bind_by_name($insert, ':id', $id);
33$select = oci_parse($c, "SELECT data FROM oci8_bug70700 WHERE id = :id");
34oci_bind_by_name($select, ':id', $id);
35
36
37echo PHP_EOL, 'Test 1: j', PHP_EOL;
38$string1 = 'abc' . str_repeat('j', 1000000) . 'xyz';
39$id = 1;
40$desc = oci_new_descriptor($c, OCI_D_LOB);
41oci_bind_by_name($insert, ':data', $desc, -1, OCI_B_CLOB);
42$desc->writeTemporary($string1, OCI_TEMP_CLOB);
43oci_execute($insert);
44$desc->save($string1);
45oci_commit($c);
46$desc->close();
47oci_bind_by_name($select, ':id', $id);
48oci_execute($select);
49$row = oci_fetch_array($select, OCI_ASSOC);
50$lob = $row['DATA'];
51$fh = fopen('php://temp', 'rw');
52while (! $lob->eof()) {
53    $data = $lob->read(8192);             // read($characters), not read($bytes)
54    fwrite($fh, $data, strlen($data));    // fwrite(a, b, $bytes)
55}
56$lob->free();
57rewind($fh);
58$stream1a = stream_get_contents($fh);
59fclose($fh);
60$start1a = mb_substr($stream1a, 0, 10);
61$ending1a = mb_substr($stream1a, -10);
62echo 'size of string1 is ', strlen($string1), ' bytes, ', mb_strlen($string1), ' chars.', PHP_EOL;
63echo 'size of stream1a is ', strlen($stream1a), ' bytes, ', mb_strlen($stream1a), ' chars.', PHP_EOL;
64echo 'beg of stream1a is ', $start1a, PHP_EOL;
65echo 'end of stream1a is ', $ending1a, PHP_EOL;
66
67
68echo PHP_EOL, 'Test 2: £', PHP_EOL;
69$string2 = 'abc' . str_repeat('£', 4094) . 'xyz';
70$id = 2;
71$desc = oci_new_descriptor($c, OCI_D_LOB);
72oci_bind_by_name($insert, ':data', $desc, -1, OCI_B_CLOB);
73$desc->writeTemporary($string2, OCI_TEMP_CLOB);
74oci_execute($insert);
75$desc->save($string2);
76oci_commit($c);
77$desc->close();
78oci_bind_by_name($select, ':id', $id);
79oci_execute($select);
80$row = oci_fetch_array($select, OCI_ASSOC);
81$lob = $row['DATA'];
82$fh = fopen('php://temp', 'rw');
83while (! $lob->eof()) {
84    $data = $lob->read(8192);             // read($characters), not read($bytes)
85    fwrite($fh, $data, strlen($data));    // fwrite(a, b, $bytes)
86}
87$lob->free();
88rewind($fh);
89$stream2a = stream_get_contents($fh);
90fclose($fh);
91$start2a = mb_substr($stream2a, 0, 10);
92$ending2a = mb_substr($stream2a, -10);
93echo 'size of string2 is ', strlen($string2), ' bytes, ', mb_strlen($string2), ' chars.', PHP_EOL;
94echo 'size of stream2a is ', strlen($stream2a), ' bytes, ', mb_strlen($stream2a), ' chars.', PHP_EOL;
95echo 'beg of stream2a is ', $start2a, PHP_EOL;
96echo 'end of stream2a is ', $ending2a, PHP_EOL;
97
98
99echo PHP_EOL, 'Test 3: Җ', PHP_EOL;
100$string3 = 'abc' . str_repeat('Җ', 4094) . 'xyz';
101$id = 3;
102$desc = oci_new_descriptor($c, OCI_D_LOB);
103oci_bind_by_name($insert, ':data', $desc, -1, OCI_B_CLOB);
104$desc->writeTemporary($string3, OCI_TEMP_CLOB);
105oci_execute($insert);
106$desc->save($string3);
107oci_commit($c);
108$desc->close();
109oci_bind_by_name($select, ':id', $id);
110oci_execute($select);
111$row = oci_fetch_array($select, OCI_ASSOC);
112$lob = $row['DATA'];
113$fh = fopen('php://temp', 'rw');
114while (! $lob->eof()) {
115    $data = $lob->read(8192);             // read($characters), not read($bytes)
116    fwrite($fh, $data, strlen($data));    // fwrite(a, b, $bytes)
117}
118$lob->free();
119rewind($fh);
120$stream3a = stream_get_contents($fh);
121fclose($fh);
122$start3a = mb_substr($stream3a, 0, 10);
123$ending3a = mb_substr($stream3a, -10);
124echo 'size of string3 is ', strlen($string3), ' bytes, ', mb_strlen($string3), ' chars.', PHP_EOL;
125echo 'size of stream3a is ', strlen($stream3a), ' bytes, ', mb_strlen($stream3a), ' chars.', PHP_EOL;
126echo 'beg of stream3a is ', $start3a, PHP_EOL;
127echo 'end of stream3a is ', $ending3a, PHP_EOL;
128
129
130echo PHP_EOL, 'Test 4: の', PHP_EOL;
131$string4 = 'abc' . str_repeat('の', 2729) . 'xyz';
132$id = 4;
133$desc = oci_new_descriptor($c, OCI_D_LOB);
134oci_bind_by_name($insert, ':data', $desc, -1, OCI_B_CLOB);
135$desc->writeTemporary($string4, OCI_TEMP_CLOB);
136oci_execute($insert);
137$desc->save($string4);
138oci_commit($c);
139$desc->close();
140oci_bind_by_name($select, ':id', $id);
141oci_execute($select);
142$row = oci_fetch_array($select, OCI_ASSOC);
143$lob = $row['DATA'];
144$fh = fopen('php://temp', 'rw');
145while (! $lob->eof()) {
146    $data = $lob->read(8192);             // read($characters), not read($bytes)
147    fwrite($fh, $data, strlen($data));    // fwrite(a, b, $bytes)
148}
149$lob->free();
150rewind($fh);
151$stream4a = stream_get_contents($fh);
152fclose($fh);
153$start4a = mb_substr($stream4a, 0, 10);
154$ending4a = mb_substr($stream4a, -10);
155echo 'size of string4 is ', strlen($string4), ' bytes, ', mb_strlen($string4), ' chars.', PHP_EOL;
156echo 'size of stream4a is ', strlen($stream4a), ' bytes, ', mb_strlen($stream4a), ' chars.', PHP_EOL;
157echo 'beg of stream4a is ', $start4a, PHP_EOL;
158echo 'end of stream4a is ', $ending4a, PHP_EOL;
159?>
160--EXPECT--
161Test 1: j
162size of string1 is 1000006 bytes, 1000006 chars.
163size of stream1a is 1000006 bytes, 1000006 chars.
164beg of stream1a is abcjjjjjjj
165end of stream1a is jjjjjjjxyz
166
167Test 2: £
168size of string2 is 8194 bytes, 4100 chars.
169size of stream2a is 8194 bytes, 4100 chars.
170beg of stream2a is abc£££££££
171end of stream2a is £££££££xyz
172
173Test 3: Җ
174size of string3 is 8194 bytes, 4100 chars.
175size of stream3a is 8194 bytes, 4100 chars.
176beg of stream3a is abcҖҖҖҖҖҖҖ
177end of stream3a is ҖҖҖҖҖҖҖxyz
178
179Test 4: の
180size of string4 is 8193 bytes, 2735 chars.
181size of stream4a is 8193 bytes, 2735 chars.
182beg of stream4a is abcののののののの
183end of stream4a is のののののののxyz
184