Array support for all basic types.

This commit is contained in:
Federico Di Gregorio 2005-03-23 11:02:13 +00:00
parent 19cb161d27
commit cb9cec57c0
6 changed files with 59 additions and 38 deletions

View File

@ -116,7 +116,7 @@ typecast_init(PyObject *dict)
Dprintf("typecast_init: initializing %s", typecast_builtins[i].name);
t = (typecastObject *)typecast_from_c(&(typecast_builtins[i]));
t = (typecastObject *)typecast_from_c(&(typecast_builtins[i]), dict);
if (t == NULL) return -1;
if (typecast_add((PyObject *)t, 0) != 0) return -1;
@ -129,14 +129,14 @@ typecast_init(PyObject *dict)
}
/* create and save a default cast object (but does not register it) */
psyco_default_cast = typecast_from_c(&typecast_default);
psyco_default_cast = typecast_from_c(&typecast_default, dict);
/* register the date/time typecasters with their original names */
#ifdef HAVE_MXDATETIME
for (i = 0; typecast_mxdatetime[i].name != NULL; i++) {
typecastObject *t;
Dprintf("typecast_init: initializing %s", typecast_mxdatetime[i].name);
t = (typecastObject *)typecast_from_c(&(typecast_mxdatetime[i]));
t = (typecastObject *)typecast_from_c(&(typecast_mxdatetime[i]), dict);
if (t == NULL) return -1;
PyDict_SetItem(dict, t->name, (PyObject *)t);
}
@ -145,7 +145,7 @@ typecast_init(PyObject *dict)
for (i = 0; typecast_pydatetime[i].name != NULL; i++) {
typecastObject *t;
Dprintf("typecast_init: initializing %s", typecast_pydatetime[i].name);
t = (typecastObject *)typecast_from_c(&(typecast_pydatetime[i]));
t = (typecastObject *)typecast_from_c(&(typecast_pydatetime[i]), dict);
if (t == NULL) return -1;
PyDict_SetItem(dict, t->name, (PyObject *)t);
}
@ -154,26 +154,6 @@ typecast_init(PyObject *dict)
return 0;
}
/* typecast_get_by_name - get a type object by name (slow!) */
static PyObject*
typecast_get_by_name(unsigned char *name)
{
PyObject *value, *res = NULL;
int ppos = 0;
while (PyDict_Next(psyco_types, &ppos, NULL, &value)) {
if (strcmp(PyString_AsString(((typecastObject*)value)->name),
name) == 0) {
res = value;
break;
}
}
/* borrowed reference */
return res;
}
/* typecast_add - add a type object to the dictionary */
int
typecast_add(PyObject *obj, int binary)
@ -422,7 +402,7 @@ typecast_from_python(PyObject *self, PyObject *args, PyObject *keywds)
}
PyObject *
typecast_from_c(typecastObject_initlist *type)
typecast_from_c(typecastObject_initlist *type, PyObject *dict)
{
PyObject *tuple, *base = NULL;
typecastObject *obj;
@ -430,7 +410,8 @@ typecast_from_c(typecastObject_initlist *type)
/* before doing anything else we look for the base */
if (type->base) {
base = typecast_get_by_name(type->base);
/* NOTE: base is a borrowed reference! */
base = PyDict_GetItemString(dict, type->base);
if (!base) {
PyErr_Format(Error, "typecast base not found: %s", type->base);
return NULL;

View File

@ -72,7 +72,7 @@ extern int typecast_init(PyObject *dict);
extern int typecast_add(PyObject *obj, int binary);
/* the C callable typecastObject creator function */
extern PyObject *typecast_from_c(typecastObject_initlist *type);
extern PyObject *typecast_from_c(typecastObject_initlist *type, PyObject *d);
/* the python callable typecast creator function */
extern PyObject *typecast_from_python(

View File

@ -203,12 +203,18 @@ typecast_GENERIC_ARRAY_cast(unsigned char *str, int len, PyObject *curs)
return obj;
}
/** LONGINTEGERARRAY and INTEGERARRAY - cast integers arrays **/
/** almost all the basic array typecasters are derived from GENERIC **/
#define typecast_LONGINTEGERARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_INTEGERARRAY_cast typecast_GENERIC_ARRAY_cast
/** STRINGARRAY - cast integers arrays **/
#define typecast_FLOATARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_DECIMALARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_STRINGARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_UNICODEARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_BOOLEANARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_DATETIMEARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_DATEARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_TIMEARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_INTERVALARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_BINARYARRAY_cast typecast_GENERIC_ARRAY_cast
#define typecast_ROWIDARRAY_cast typecast_GENERIC_ARRAY_cast

View File

@ -14,7 +14,17 @@ 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_FLOATARRAY_types[] = {1017, 1021, 1022, 0};
static long int typecast_DECIMALARRAY_types[] = {1231, 0};
static long int typecast_UNICODEARRAY_types[] = {1002, 1003, 1009, 1014, 1015, 0};
static long int typecast_STRINGARRAY_types[] = {1002, 1003, 1009, 1014, 1015, 0};
static long int typecast_BOOLEANARRAY_types[] = {1000, 0};
static long int typecast_DATETIMEARRAY_types[] = {1115, 1185, 0};
static long int typecast_TIMEARRAY_types[] = {1183, 1270, 0};
static long int typecast_DATEARRAY_types[] = {1182, 0};
static long int typecast_INTERVALARRAY_types[] = {1187, 0};
static long int typecast_BINARYARRAY_types[] = {1001, 0};
static long int typecast_ROWIDARRAY_types[] = {1028, 1013, 0};
typecastObject_initlist typecast_builtins[] = {
@ -34,7 +44,17 @@ typecastObject_initlist typecast_builtins[] = {
{"ROWID", typecast_ROWID_types, typecast_ROWID_cast, NULL},
{"LONGINTEGERARRAY", typecast_LONGINTEGERARRAY_types, typecast_LONGINTEGERARRAY_cast, "LONGINTEGER"},
{"INTEGERARRAY", typecast_INTEGERARRAY_types, typecast_INTEGERARRAY_cast, "INTEGER"},
{"FLOATARRAY", typecast_FLOATARRAY_types, typecast_FLOATARRAY_cast, "FLOAT"},
{"DECIMALARRAY", typecast_DECIMALARRAY_types, typecast_DECIMALARRAY_cast, "DECIMAL"},
{"UNICODEARRAY", typecast_UNICODEARRAY_types, typecast_UNICODEARRAY_cast, "UNICODE"},
{"STRINGARRAY", typecast_STRINGARRAY_types, typecast_STRINGARRAY_cast, "STRING"},
{"BOOLEANARRAY", typecast_BOOLEANARRAY_types, typecast_BOOLEANARRAY_cast, "BOOLEAN"},
{"DATETIMEARRAY", typecast_DATETIMEARRAY_types, typecast_DATETIMEARRAY_cast, "DATETIME"},
{"TIMEARRAY", typecast_TIMEARRAY_types, typecast_TIMEARRAY_cast, "TIME"},
{"DATEARRAY", typecast_DATEARRAY_types, typecast_DATEARRAY_cast, "DATE"},
{"INTERVALARRAY", typecast_INTERVALARRAY_types, typecast_INTERVALARRAY_cast, "INTERVAL"},
{"BINARYARRAY", typecast_BINARYARRAY_types, typecast_BINARYARRAY_cast, "BINARY"},
{"ROWIDARRAY", typecast_ROWIDARRAY_types, typecast_ROWIDARRAY_cast, "ROWID"},
{NULL, NULL, NULL, NULL}
};

View File

@ -4,14 +4,18 @@ conn = psycopg.connect("dbname=test")
curs = conn.cursor()
curs.execute("SELECT ARRAY[1,2,3] AS foo")
print curs.fetchone()
print curs.fetchone()[0]
curs.execute("SELECT ARRAY['1','2','3'] AS foo")
print curs.fetchone()
print curs.fetchone()[0]
curs.execute("""SELECT ARRAY[',','"','\\\\'] AS foo""")
d = curs.fetchone()
print d, '->', d[0][0], d[0][1], d[0][2]
d = curs.fetchone()[0]
print d, '->', d[0], d[1], d[2]
curs.execute("SELECT ARRAY[ARRAY[1,2],ARRAY[3,4]] AS foo")
print curs.fetchone()
print curs.fetchone()[0]
curs.execute("SELECT ARRAY['20:00:01'::time] AS foo")
print curs.description
print curs.fetchone()[0]

View File

@ -53,7 +53,17 @@ basic_types = (['NUMBER', ['INT8', 'INT4', 'INT2', 'FLOAT8', 'FLOAT4',
# 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]])
['FLOAT', [1017, 1021, 1022]],
['DECIMAL', [1231]],
['UNICODE', [1002, 1003, 1009, 1014, 1015]],
['STRING', [1002, 1003, 1009, 1014, 1015]],
['BOOLEAN', [1000]],
['DATETIME', [1115, 1185]],
['TIME', [1183, 1270]],
['DATE', [1182]],
['INTERVAL', [1187]],
['BINARY', [1001]],
['ROWID', [1028, 1013]])
# this is the header used to compile the data in the C module
HEADER = """