diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | psycopg/typecast_array.c | 23 |
2 files changed, 30 insertions, 0 deletions
@@ -1,3 +1,10 @@ +2005-12-11 Federico Di Gregorio <fog@initd.org> + + * psycopg/typecast_array.c (typecast_array_cleanup): added functio + to cleanup the "[...]=" part of an array result. This probably will + need some more work for nested arrays but it fixed every test I was + able to write. (Closes: #80) + 2005-12-11 Federico Di Gregorio <fog@initd.org> * setup.py: half-applied patch from Tavis to specify mx_include_dir diff --git a/psycopg/typecast_array.c b/psycopg/typecast_array.c index 2e34a3e..8fe7437 100644 --- a/psycopg/typecast_array.c +++ b/psycopg/typecast_array.c @@ -21,6 +21,27 @@ #define MAX_DIMENSIONS 16 +/** typecast_array_cleanup - remove the horrible [...]= stuff **/ + +static int +typecast_array_cleanup(char **str, int *len) +{ + int i, depth = 1; + + if ((*str)[0] != '[') return -1; + + for (i=1 ; depth > 0 && i < *len ; i++) { + if ((*str)[i] == '[') + depth += 1; + else if ((*str)[i] == ']') + depth -= 1; + } + if ((*str)[i] != '=') return -1; + + *str = &((*str)[i+1]); + *len = *len - i - 2; + return 0; +} /** typecast_array_scan - scan a string looking for array items **/ @@ -198,6 +219,8 @@ typecast_GENERIC_ARRAY_cast(char *str, int len, PyObject *curs) PyObject *base = ((typecastObject*)((cursorObject*)curs)->caster)->bcast; if (str == NULL) {Py_INCREF(Py_None); return Py_None;} + if (str[0] == '[') + typecast_array_cleanup(&str, &len); if (str[0] != '{') { PyErr_SetString(Error, "array does not start with '{'"); return NULL; |