From 07a38c31cd881f786930c2268210fe1c6ddc5950 Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Tue, 22 Mar 2005 14:20:20 +0000 Subject: [PATCH] Starting array work. --- ChangeLog | 7 +++++ psycopg/typecast.c | 2 ++ psycopg/typecast.h | 3 ++ psycopg/typecast_array.c | 55 +++++++++++++++++++++++++++++++++++++ psycopg/typecast_builtins.c | 36 ++++++++++++++---------- scripts/buildtypes.py | 20 ++++++++++++-- setup.cfg | 2 +- 7 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 psycopg/typecast_array.c diff --git a/ChangeLog b/ChangeLog index 164dab43..efeb5345 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2005-03-22 Federico Di Gregorio + + * psycopg/typecast_array.c: added some more structure to implement + array typecasting. + + * scripts/buildtypes.py: new version to include array data. + 2005-03-12 Federico Di Gregorio * psycopg/cursor_type.c (psyco_curs_executemany): implemented as a diff --git a/psycopg/typecast.c b/psycopg/typecast.c index 82367b3b..78e873c8 100644 --- a/psycopg/typecast.c +++ b/psycopg/typecast.c @@ -50,6 +50,8 @@ skip_until_space(char *s) #include "psycopg/typecast_datetime.c" #endif +#include "psycopg/typecast_array.c" + #include "psycopg/typecast_builtins.c" /* a list of initializers, used to make the typecasters accessible anyway */ diff --git a/psycopg/typecast.h b/psycopg/typecast.h index 3cf50181..3e5a4c4f 100644 --- a/psycopg/typecast.h +++ b/psycopg/typecast.h @@ -51,6 +51,9 @@ typedef struct { char *name; long int *values; typecast_function cast; + + /* base is the base typecaster for arrays */ + char *base; } typecastObject_initlist; /* the type dictionary, much faster to access it globally */ diff --git a/psycopg/typecast_array.c b/psycopg/typecast_array.c new file mode 100644 index 00000000..d4a982a9 --- /dev/null +++ b/psycopg/typecast_array.c @@ -0,0 +1,55 @@ +/* typecast_array.c - array typecasters + * + * Copyright (C) 2005 Federico Di Gregorio + * + * This file is part of the psycopg module. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2, + * or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +/* the pointer to the datetime module API is initialized by the module init + code, we just need to grab it */ +extern PyObject* pyDateTimeModuleP; +extern PyObject *pyDateTypeP; +extern PyObject *pyTimeTypeP; +extern PyObject *pyDateTimeTypeP; +extern PyObject *pyDeltaTypeP; + +/** LONGINTEGERARRAY and INTEGERARRAY - cast integers arrays **/ + +static PyObject * +typecast_INTEGERARRAY_cast(unsigned char *str, int len, PyObject *curs) +{ + PyObject* obj = NULL; + + if (str == NULL) {Py_INCREF(Py_None); return Py_None;} + + return obj; +} + +#define typecast_LONGINTEGERARRAY_cast typecast_INTEGERARRAY_cast + +/** STRINGARRAY - cast integers arrays **/ + +static PyObject * +typecast_STRINGARRAY_cast(unsigned char *str, int len, PyObject *curs) +{ + PyObject* obj = NULL; + + if (str == NULL) {Py_INCREF(Py_None); return Py_None;} + + return obj; +} diff --git a/psycopg/typecast_builtins.c b/psycopg/typecast_builtins.c index c554a37d..0cea3851 100644 --- a/psycopg/typecast_builtins.c +++ b/psycopg/typecast_builtins.c @@ -12,23 +12,29 @@ static long int typecast_DATE_types[] = {1082, 0}; static long int typecast_INTERVAL_types[] = {704, 1186, 0}; static long int typecast_BINARY_types[] = {17, 0}; static long int typecast_ROWID_types[] = {26, 0}; +static long int typecast_LONGINTEGERARRAY_types[] = {1016, 0}; +static long int typecast_INTEGERARRAY_types[] = {1005, 1006, 1007, 0}; +static long int typecast_STRINGARRAY_types[] = {1002, 1003, 1009, 1014, 1015, 0}; typecastObject_initlist typecast_builtins[] = { - {"NUMBER", typecast_NUMBER_types, typecast_NUMBER_cast}, - {"LONGINTEGER", typecast_LONGINTEGER_types, typecast_LONGINTEGER_cast}, - {"INTEGER", typecast_INTEGER_types, typecast_INTEGER_cast}, - {"FLOAT", typecast_FLOAT_types, typecast_FLOAT_cast}, - {"DECIMAL", typecast_DECIMAL_types, typecast_DECIMAL_cast}, - {"UNICODE", typecast_UNICODE_types, typecast_UNICODE_cast}, - {"STRING", typecast_STRING_types, typecast_STRING_cast}, - {"BOOLEAN", typecast_BOOLEAN_types, typecast_BOOLEAN_cast}, - {"DATETIME", typecast_DATETIME_types, typecast_DATETIME_cast}, - {"TIME", typecast_TIME_types, typecast_TIME_cast}, - {"DATE", typecast_DATE_types, typecast_DATE_cast}, - {"INTERVAL", typecast_INTERVAL_types, typecast_INTERVAL_cast}, - {"BINARY", typecast_BINARY_types, typecast_BINARY_cast}, - {"ROWID", typecast_ROWID_types, typecast_ROWID_cast}, - {NULL, NULL, NULL} + {"NUMBER", typecast_NUMBER_types, typecast_NUMBER_cast, NULL}, + {"LONGINTEGER", typecast_LONGINTEGER_types, typecast_LONGINTEGER_cast, NULL}, + {"INTEGER", typecast_INTEGER_types, typecast_INTEGER_cast, NULL}, + {"FLOAT", typecast_FLOAT_types, typecast_FLOAT_cast, NULL}, + {"DECIMAL", typecast_DECIMAL_types, typecast_DECIMAL_cast, NULL}, + {"UNICODE", typecast_UNICODE_types, typecast_UNICODE_cast, NULL}, + {"STRING", typecast_STRING_types, typecast_STRING_cast, NULL}, + {"BOOLEAN", typecast_BOOLEAN_types, typecast_BOOLEAN_cast, NULL}, + {"DATETIME", typecast_DATETIME_types, typecast_DATETIME_cast, NULL}, + {"TIME", typecast_TIME_types, typecast_TIME_cast, NULL}, + {"DATE", typecast_DATE_types, typecast_DATE_cast, NULL}, + {"INTERVAL", typecast_INTERVAL_types, typecast_INTERVAL_cast, NULL}, + {"BINARY", typecast_BINARY_types, typecast_BINARY_cast, NULL}, + {"ROWID", typecast_ROWID_types, typecast_ROWID_cast, NULL}, + {"LONGINTEGERARRAY", typecast_LONGINTEGERARRAY_types, typecast_LONGINTEGERARRAY_cast, "LONGINTEGER"}, + {"INTEGERARRAY", typecast_INTEGERARRAY_types, typecast_INTEGERARRAY_cast, "INTEGER"}, + {"STRINGARRAY", typecast_STRINGARRAY_types, typecast_STRINGARRAY_cast, "STRING"}, + {NULL, NULL, NULL, NULL} }; diff --git a/scripts/buildtypes.py b/scripts/buildtypes.py index a09a0cca..5d414986 100644 --- a/scripts/buildtypes.py +++ b/scripts/buildtypes.py @@ -49,13 +49,19 @@ basic_types = (['NUMBER', ['INT8', 'INT4', 'INT2', 'FLOAT8', 'FLOAT4', ['BINARY', ['BYTEA']], ['ROWID', ['OID']]) +# unfortunately we don't have a nice way to extract array information +# from postgresql headers; we'll have to do it hard-coding :/ +array_types = (['LONGINTEGER', [1016]], + ['INTEGER', [1005, 1006, 1007]], + ['STRING', [1002, 1003, 1009, 1014, 1015]]) + # this is the header used to compile the data in the C module HEADER = """ typecastObject_initlist typecast_builtins[] = { """ # then comes the footer -FOOTER = """ {NULL, NULL, NULL}\n};\n""" +FOOTER = """ {NULL, NULL, NULL, NULL}\n};\n""" # usefull error reporting function @@ -92,8 +98,16 @@ for t in basic_types: s = str(found_types[k]) s = '{' + s[1:-1] + ', 0}' stypes = stypes + ('static long int typecast_%s_types[] = %s;\n' % (k, s)) - sstruct = sstruct + (' {"%s", typecast_%s_types, typecast_%s_cast},\n' - % (k, k, k)) + sstruct += (' {"%s", typecast_%s_types, typecast_%s_cast, NULL},\n' + % (k, k, k)) +for t in array_types: + kt = t[0] + ka = t[0]+'ARRAY' + s = str(t[1]) + s = '{' + s[1:-1] + ', 0}' + stypes = stypes + ('static long int typecast_%s_types[] = %s;\n' % (ka, s)) + sstruct += (' {"%s", typecast_%s_types, typecast_%s_cast, "%s"},\n' + % (ka, ka, ka, kt)) sstruct = HEADER + sstruct + FOOTER print stypes diff --git a/setup.cfg b/setup.cfg index 7409c618..46bcbc0a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [build_ext] -define=PSYCOPG_EXTENSIONS,PSYCOPG_DISPLAY_SIZE,HAVE_PQFREEMEM,HAVE_PQPROTOCOL3,PSYCOPG_DEBUG +define=PSYCOPG_EXTENSIONS,PSYCOPG_DISPLAY_SIZE,HAVE_PQFREEMEM,HAVE_PQPROTOCOL3 # PSYCOPG_DEBUG can be added to enable verbose debug information # PSYCOPG_OWN_QUOTING can be added above but it is deprecated