]> git.8kb.co.uk Git - postgresql/encoding_woes/blob - codepage_850.plpythonu
Added sql functions to check if we really need to do any conversion.
[postgresql/encoding_woes] / codepage_850.plpythonu
1 --
2 -- Glyn Astill 09/02/2014
3 -- Plpython untrusted functions to do conversion from DOS cp850 native through Latin1 to UTF8 and vice versa
4 --
5
6 CREATE OR REPLACE FUNCTION cp850_to_utf8(in_text text) RETURNS text AS 
7 $BODY$
8     rv = plpy.execute("SHOW client_encoding", 1)
9     v_encoding = rv[0]["client_encoding"]
10     if v_encoding.upper() != 'UTF8':
11         raise plpy.Error('Sorry client_encoding should be UTF8')
12     else:
13         return unicode(in_text, 'cp850').encode('iso8859_1')
14 $BODY$ 
15 LANGUAGE plpythonu;
16
17 --
18
19 CREATE OR REPLACE FUNCTION utf8_to_cp850(in_text text) RETURNS text AS 
20 $BODY$
21     rv = plpy.execute("SHOW client_encoding", 1)
22     v_encoding = rv[0]["client_encoding"]
23     if v_encoding.upper() != 'UTF8':
24         raise plpy.Error('Sorry client_encoding should be UTF8')
25     else:
26         return unicode(in_text, 'iso8859_1').encode('cp850')
27 $BODY$ 
28 LANGUAGE plpythonu;
29
30 --
31
32 CREATE OR REPLACE FUNCTION cp850_to_latin1(in_text text) RETURNS text AS 
33 $BODY$
34     rv = plpy.execute("SHOW client_encoding", 1)
35     v_encoding = rv[0]["client_encoding"]
36     if v_encoding.upper() != 'LATIN1':
37         raise plpy.Error('Sorry client_encoding should be LATIN1')
38     else:
39         return unicode(in_text, 'cp850').encode('utf-8')
40 $BODY$ 
41 LANGUAGE plpythonu;
42
43 --
44
45 CREATE OR REPLACE FUNCTION latin1_to_cp850(in_text text) RETURNS text AS 
46 $BODY$
47     rv = plpy.execute("SHOW client_encoding", 1)
48     v_encoding = rv[0]["client_encoding"]
49     if v_encoding.upper() != 'LATIN1':
50         raise plpy.Error('Sorry client_encoding should be LATIN1')
51     else:
52         return unicode(in_text, 'utf-8').encode('cp850')
53 $BODY$ 
54 LANGUAGE plpythonu;