Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
--
-- Test cases for encoding with COPY commands
--
-- skip test if not UTF8 server encoding
SELECT getdatabaseencoding() <> 'UTF8'
       AS skip_test \gset
\if :skip_test
\quit
\endif
-- directory paths are passed to us in environment variables
\getenv abs_builddir PG_ABS_BUILDDIR
\set utf8_csv :abs_builddir '/results/copyencoding_utf8.csv'
CREATE TABLE copy_encoding_tab (t text);
-- Valid cases
-- Use ENCODING option
-- U+3042 HIRAGANA LETTER A
COPY (SELECT E'\u3042') TO :'utf8_csv' WITH (FORMAT csv, ENCODING 'UTF8');
-- Read UTF8 data as LATIN1: no error
COPY copy_encoding_tab FROM :'utf8_csv' WITH (FORMAT csv, ENCODING 'LATIN1');
-- Non-server encodings have distinct code paths.
\set fname :abs_builddir '/results/copyencoding_gb18030.csv'
COPY (SELECT E'\u3042,') TO :'fname' WITH (FORMAT csv, ENCODING 'GB18030');
COPY copy_encoding_tab FROM :'fname' WITH (FORMAT csv, ENCODING 'GB18030');
\set fname :abs_builddir '/results/copyencoding_gb18030.data'
COPY (SELECT E'\u3042,') TO :'fname' WITH (FORMAT text, ENCODING 'GB18030');
COPY copy_encoding_tab FROM :'fname' WITH (FORMAT text, ENCODING 'GB18030');
-- Use client_encoding
SET client_encoding TO UTF8;
-- U+3042 HIRAGANA LETTER A
COPY (SELECT E'\u3042') TO :'utf8_csv' WITH (FORMAT csv);
-- Read UTF8 data as LATIN1: no error
SET client_encoding TO LATIN1;
COPY copy_encoding_tab FROM :'utf8_csv' WITH (FORMAT csv);
RESET client_encoding;
-- Invalid cases
-- Use ENCODING explicitly
-- U+3042 HIRAGANA LETTER A
COPY (SELECT E'\u3042') TO :'utf8_csv' WITH (FORMAT csv, ENCODING 'UTF8');
-- Read UTF8 data as EUC_JP: no error
COPY copy_encoding_tab FROM :'utf8_csv' WITH (FORMAT csv, ENCODING 'EUC_JP');
ERROR:  invalid byte sequence for encoding "EUC_JP": 0xe3 0x81
CONTEXT:  COPY copy_encoding_tab, line 1
-- Use client_encoding
SET client_encoding TO UTF8;
-- U+3042 HIRAGANA LETTER A
COPY (SELECT E'\u3042') TO :'utf8_csv' WITH (FORMAT csv);
-- Read UTF8 data as EUC_JP: no error
SET client_encoding TO EUC_JP;
COPY copy_encoding_tab FROM :'utf8_csv' WITH (FORMAT csv);
ERROR:  invalid byte sequence for encoding "EUC_JP": 0xe3 0x81
CONTEXT:  COPY copy_encoding_tab, line 1
RESET client_encoding;
-- JSON format encoding conversion
\set json_latin1 :abs_builddir '/results/copyencoding_json_latin1.json'
COPY (SELECT E'\u00e9' AS c) TO :'json_latin1' WITH (FORMAT json, ENCODING 'LATIN1');
-- Verify the file contains LATIN1 'é' (single byte 0xe9) and not UTF-8 (0xc3 0xa9).
-- Done as separate position checks to stay independent of the platform's
-- end-of-line convention.
SELECT position('\xe9'::bytea  IN pg_read_binary_file(:'json_latin1')) > 0 AS has_latin1_e9,
       position('\xc3a9'::bytea IN pg_read_binary_file(:'json_latin1')) > 0 AS has_utf8_e9;
 has_latin1_e9 | has_utf8_e9 
---------------+-------------
 t             | f
(1 row)
 
-- Same with implicit encoding inherited from client_encoding (no ENCODING
-- option).  Covers the case where a client with a non-UTF8 client_encoding
-- runs COPY ... FORMAT json and would otherwise receive unconverted bytes.
\set json_implicit :abs_builddir '/results/copyencoding_json_implicit_latin1.json'
SET client_encoding TO LATIN1;
COPY (SELECT E'\u00e9' AS c) TO :'json_implicit' WITH (FORMAT json);
RESET client_encoding;
SELECT position('\xe9'::bytea  IN pg_read_binary_file(:'json_implicit')) > 0 AS has_latin1_e9,
       position('\xc3a9'::bytea IN pg_read_binary_file(:'json_implicit')) > 0 AS has_utf8_e9;
 has_latin1_e9 | has_utf8_e9 
---------------+-------------
 t             | f
(1 row)
 
DROP TABLE copy_encoding_tab;