]> git.8kb.co.uk Git - postgresql/encoding_woes/blob - codepage_850.plperlu
Added sql functions to check if we really need to do any conversion.
[postgresql/encoding_woes] / codepage_850.plperlu
1 --
2 -- Glyn Astill 09/02/2014
3 -- Plperl untrusted functions to do conversion from DOS cp850 native through Latin1 to UTF8 and vice versa
4 --
5
6 -- The following functions require the user to know what their client_encoiding is and call the appropriate function
7
8 DROP FUNCTION IF EXISTS cp850_to_latin1(text);
9 CREATE OR REPLACE FUNCTION cp850_to_latin1(text)
10 RETURNS text AS
11 $BODY$
12         use Encode;
13         return encode( 'utf8', decode('cp850', $_[0] ));
14 $BODY$
15 LANGUAGE 'plperlu' IMMUTABLE;
16
17 COMMENT ON FUNCTION public.cp850_to_latin1(text) IS 'Converts text from cp850 to current latin1';
18
19 --
20
21 DROP FUNCTION IF EXISTS cp850_to_utf8(text);
22 CREATE OR REPLACE FUNCTION cp850_to_utf8(text)
23 RETURNS text AS
24 $BODY$
25         use Encode;
26         return encode( 'iso-8859-1', decode('cp850', $_[0] ));
27 $BODY$
28 LANGUAGE 'plperlu' IMMUTABLE;
29
30 COMMENT ON FUNCTION public.cp850_to_utf8(text) IS 'Converts text from cp850 to current utf8';
31
32 --
33
34 DROP FUNCTION IF EXISTS latin1_to_cp850(text);
35 CREATE OR REPLACE FUNCTION latin1_to_cp850(text)
36 RETURNS text AS
37 $BODY$
38         use Encode;
39         return encode( 'cp850', decode('utf8', $_[0] ));
40 $BODY$
41 LANGUAGE 'plperlu' IMMUTABLE;
42
43 COMMENT ON FUNCTION public.latin1_to_cp850(text) IS 'Converts text from current latin1 to cp850';
44
45 --
46
47 DROP FUNCTION IF EXISTS utf8_to_cp850(text);
48 CREATE OR REPLACE FUNCTION utf8_to_cp850(text)
49 RETURNS text AS
50 $BODY$
51         use Encode;
52         return encode( 'cp850', decode('iso-8859-1', $_[0] ));
53 $BODY$
54 LANGUAGE 'plperlu' IMMUTABLE;
55
56 COMMENT ON FUNCTION public.utf8_to_cp850(text) IS 'Converts text from current utf8 to cp850';
57
58 -- The following functions do not require the user to know what their client_encoiding is
59
60 DROP FUNCTION IF EXISTS public.to_cp850(text);
61 CREATE OR REPLACE FUNCTION public.to_cp850(text)
62 RETURNS text AS
63 $BODY$
64         use Encode;
65         my $string;
66         my $rv = spi_exec_query('SELECT pg_client_encoding();', 1);
67         my $encoding = $rv->{rows}[0]->{pg_client_encoding};
68         
69         eval {
70                 if ($encoding eq 'UTF8') {
71                         $string = encode( 'cp850', decode('iso-8859-1', $_[0] ));
72                 }
73                 elsif (($encoding eq 'LATIN1') || ($encoding eq 'SQL_ASCII')){
74                         $string = encode( 'cp850', decode('utf8', $_[0] ));
75                 }
76                 else {
77                         elog(ERROR, "to_cp850 currently does not support client_encoding $encoding");
78                 }
79         };
80         if ($@) {
81                 elog(WARNING, "Re-encoding failed, stripping non alphanumerics: $@");
82                 $string = $_[0] =~ s/[\x01-\x7f]/?/g;
83         }
84         return $string;
85 $BODY$
86 LANGUAGE 'plperlu' IMMUTABLE;
87
88 COMMENT ON FUNCTION public.to_cp850(text) IS 'Converts text from current client_encoding to cp850';
89
90 --
91
92 DROP FUNCTION IF EXISTS public.from_cp850(text);
93 CREATE OR REPLACE FUNCTION public.from_cp850(text)
94 RETURNS text AS
95 $BODY$
96         use Encode;
97         my $string;
98         my $rv = spi_exec_query('SELECT pg_client_encoding();', 1);
99         my $encoding = $rv->{rows}[0]->{pg_client_encoding};
100         
101         eval {
102                 if ($encoding eq 'UTF8') {
103                         $string = encode( 'iso-8859-1', decode('cp850', $_[0] ));
104                 }
105                 elsif (($encoding eq 'LATIN1') || ($encoding eq 'SQL_ASCII')){
106                         $string = encode( 'utf8', decode('cp850', $_[0] ));
107                 }
108                 else {
109                         elog(ERROR, "from_cp850 currently does not support client_encoding $encoding");
110                 }
111         };
112         if ($@) {
113                 elog(WARNING, "Re-encoding failed: $@");
114                 $string = $_[0];
115         }
116         return $string;
117
118         
119 $BODY$
120 LANGUAGE 'plperlu' IMMUTABLE;
121
122 COMMENT ON FUNCTION public.from_cp850(text) IS 'Converts text from cp850 to current client_encoding';
123
124