3 # Copyright (C) 2001-2003 Federico Di Gregorio <fog@debian.org>
5 # This file is part of the psycopg module.
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.
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.
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.
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
26 import sys, os, string, copy
27 from string import split, join, strip
30 # here is the list of the foundamental types we want to import from
31 # postgresql header files
33 basic_types = (['NUMBER', ['INT8', 'INT4', 'INT2', 'FLOAT8', 'FLOAT4',
35 ['LONGINTEGER', ['INT8']],
36 ['INTEGER', ['INT4', 'INT2']],
37 ['FLOAT', ['FLOAT8', 'FLOAT4']],
38 ['DECIMAL', ['NUMERIC']],
39 ['UNICODE', ['NAME', 'CHAR', 'TEXT', 'BPCHAR',
41 ['STRING', ['NAME', 'CHAR', 'TEXT', 'BPCHAR',
43 ['BOOLEAN', ['BOOL']],
44 ['DATETIME', ['TIMESTAMP', 'TIMESTAMPTZ',
45 'TINTERVAL', 'INTERVAL']],
46 ['TIME', ['TIME', 'TIMETZ']],
48 ['INTERVAL', ['TINTERVAL', 'INTERVAL']],
49 ['BINARY', ['BYTEA']],
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]],
58 ['UNICODE', [1002, 1003, 1009, 1014, 1015]],
59 ['STRING', [1002, 1003, 1009, 1014, 1015]],
61 ['DATETIME', [1115, 1185]],
62 ['TIME', [1183, 1270]],
66 ['ROWID', [1028, 1013]])
68 # this is the header used to compile the data in the C module
70 typecastObject_initlist typecast_builtins[] = {
73 # then comes the footer
74 FOOTER = """ {NULL, NULL, NULL, NULL}\n};\n"""
77 # usefull error reporting function
79 """Report an error on stderr."""
80 sys.stderr.write(msg+'\n')
83 # read couples from stdin and build list
85 for l in sys.stdin.readlines():
87 read_types.append((strip(oid)[:-3], strip(val)))
89 # look for the wanted types in the read touples
96 found = filter(lambda x, y=v: x[0] == y, read_types)
98 error(v+': value not found')
100 error(v+': too many values')
102 found_types[k].append(int(found[0][1]))
104 # now outputs to stdout the right C-style definitions
105 stypes = "" ; sstruct = ""
106 for t in basic_types:
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'
113 for t in array_types:
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'
121 sstruct = HEADER + sstruct + FOOTER