This commit was manufactured by cvs2svn to create branch
[plcapi.git] / psycopg2 / scripts / buildtypes.py
1 # -*- python -*-
2 #
3 # Copyright (C) 2001-2003 Federico Di Gregorio <fog@debian.org>
4 #
5 # This file is part of the psycopg module.
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2,
10 # or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21 # this a little script that analyze a file with (TYPE, NUMBER) tuples
22 # and write out C code ready for inclusion in psycopg. the generated
23 # code defines the DBAPITypeObject fundamental types and warns for
24 # undefined types.
25
26 import sys, os, string, copy
27 from string import split, join, strip
28
29
30 # here is the list of the foundamental types we want to import from
31 # postgresql header files
32
33 basic_types = (['NUMBER', ['INT8', 'INT4', 'INT2', 'FLOAT8', 'FLOAT4',
34                            'NUMERIC']],
35                ['LONGINTEGER', ['INT8']],
36                ['INTEGER', ['INT4', 'INT2']],
37                ['FLOAT', ['FLOAT8', 'FLOAT4']],
38                ['DECIMAL', ['NUMERIC']],
39                ['UNICODE', ['NAME', 'CHAR', 'TEXT', 'BPCHAR',
40                             'VARCHAR']],
41                ['STRING', ['NAME', 'CHAR', 'TEXT', 'BPCHAR',
42                            'VARCHAR']],
43                ['BOOLEAN', ['BOOL']],
44                ['DATETIME', ['TIMESTAMP', 'TIMESTAMPTZ', 
45                              'TINTERVAL', 'INTERVAL']],
46                ['TIME', ['TIME', 'TIMETZ']],
47                ['DATE', ['DATE']],
48                ['INTERVAL', ['TINTERVAL', 'INTERVAL']],
49                ['BINARY', ['BYTEA']],
50                ['ROWID', ['OID']])
51
52 # unfortunately we don't have a nice way to extract array information
53 # from postgresql headers; we'll have to do it hard-coding :/
54 array_types = (['LONGINTEGER', [1016]],
55                ['INTEGER', [1005, 1006, 1007]],
56                ['FLOAT', [1017, 1021, 1022]],
57                ['DECIMAL', [1231]],
58                ['UNICODE', [1002, 1003, 1009, 1014, 1015]],
59                ['STRING', [1002, 1003, 1009, 1014, 1015]],
60                ['BOOLEAN', [1000]],
61                ['DATETIME', [1115, 1185]],
62                ['TIME', [1183, 1270]],
63                ['DATE', [1182]],
64                ['INTERVAL', [1187]],
65                ['BINARY', [1001]],
66                ['ROWID', [1028, 1013]])
67
68 # this is the header used to compile the data in the C module
69 HEADER = """
70 typecastObject_initlist typecast_builtins[] = {
71 """
72
73 # then comes the footer
74 FOOTER = """    {NULL, NULL, NULL, NULL}\n};\n"""
75
76
77 # usefull error reporting function
78 def error(msg):
79     """Report an error on stderr."""
80     sys.stderr.write(msg+'\n')
81     
82
83 # read couples from stdin and build list
84 read_types = []
85 for l in sys.stdin.readlines():
86     oid, val = split(l)
87     read_types.append((strip(oid)[:-3], strip(val)))
88
89 # look for the wanted types in the read touples
90 found_types = {}
91
92 for t in basic_types:
93     k = t[0]
94     found_types[k] = []
95     for v in t[1]:
96         found = filter(lambda x, y=v: x[0] == y, read_types)
97         if len(found) == 0:
98             error(v+': value not found')
99         elif len(found) > 1:
100             error(v+': too many values')
101         else:
102             found_types[k].append(int(found[0][1]))
103
104 # now outputs to stdout the right C-style definitions
105 stypes = "" ; sstruct = ""
106 for t in basic_types:
107     k = t[0]
108     s = str(found_types[k])
109     s = '{' + s[1:-1] + ', 0}'
110     stypes = stypes + ('static long int typecast_%s_types[] = %s;\n' % (k, s))
111     sstruct += ('  {"%s", typecast_%s_types, typecast_%s_cast, NULL},\n'
112                 % (k, k, k))
113 for t in array_types:
114     kt = t[0]
115     ka = t[0]+'ARRAY'
116     s = str(t[1])
117     s = '{' + s[1:-1] + ', 0}'
118     stypes = stypes + ('static long int typecast_%s_types[] = %s;\n' % (ka, s))
119     sstruct += ('  {"%s", typecast_%s_types, typecast_%s_cast, "%s"},\n'
120                 % (ka, ka, ka, kt))
121 sstruct = HEADER + sstruct + FOOTER
122
123 print stypes
124 print sstruct