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