[Informixdb-cvs] informixdb/ext _informixdb.ec,1.75,1.76
Brought to you by:
chaese,
f-apolloner
From: Carsten H. <ch...@us...> - 2006-10-08 04:10:42
|
Update of /cvsroot/informixdb/informixdb/ext In directory sc8-pr-cvs2.sourceforge.net:/tmp/cvs-serv17741/ext Modified Files: _informixdb.ec Log Message: allow reading and writing of opaque types in binary format Index: _informixdb.ec =================================================================== RCS file: /cvsroot/informixdb/informixdb/ext/_informixdb.ec,v retrieving revision 1.75 retrieving revision 1.76 diff -C2 -d -r1.75 -r1.76 *** _informixdb.ec 8 Oct 2006 01:16:22 -0000 1.75 --- _informixdb.ec 8 Oct 2006 04:10:39 -0000 1.76 *************** *** 447,450 **** --- 447,451 ---- PyObject *messages; PyObject *errorhandler; + PyObject *binary_types; } Cursor; *************** *** 619,622 **** --- 620,627 ---- ); + PyDoc_STRVAR(Cursor_binary_types_doc, + "Dictionary that controls if opaque types are read in binary format.\n\n\ + See also: Connection.binary_types"); + static PyMemberDef Cursor_members[] = { { "description", T_OBJECT_EX, offsetof(Cursor, description), READONLY, *************** *** 634,637 **** --- 639,644 ---- { "command", T_OBJECT_EX, offsetof(Cursor, op), READONLY, "Last prepared or executed command." }, + { "binary_types", T_OBJECT_EX, offsetof(Cursor, binary_types), READONLY, + Cursor_binary_types_doc }, { NULL } }; *************** *** 714,717 **** --- 721,725 ---- PyObject *errorhandler; int autocommit; + PyObject *binary_types; } Connection; *************** *** 845,848 **** --- 853,866 ---- a Warning."); + PyDoc_STRVAR(Connection_binary_types_doc, + "Dictionary that controls if opaque types are read in binary format.\n\n\ + When a query returns an opaque type, its type name is looked up in\n\ + this dictionary. If an entry is present and has a true value, the\n\ + values are fetched in binary (internal) format. Otherwise, values\n\ + are fetched in text (external) format.\n\n\ + By default, this dictionary is empty. Cursors make a copy of this\n\ + dictionary when they are created, which allows this setting to be\n\ + overridden on a per-cursor basis."); + static PyMemberDef Connection_members[] = { { "messages", T_OBJECT_EX, offsetof(Connection, messages), READONLY, *************** *** 850,853 **** --- 868,873 ---- { "errorhandler", T_OBJECT_EX, offsetof(Connection, errorhandler), 0, Connection_errorhandler_doc }, + { "binary_types", T_OBJECT_EX, offsetof(Connection, binary_types), READONLY, + Connection_binary_types_doc }, { NULL } }; *************** *** 1197,1201 **** char *buf; Py_ssize_t n; - loc_t *loc; if (PyObject_AsReadBuffer(item, (const void**)&buf, &n) == -1) { --- 1217,1220 ---- *************** *** 1203,1220 **** } ! loc = (loc_t*) malloc(sizeof(loc_t)); ! loc->loc_loctype = LOCMEMORY; ! loc->loc_buffer = malloc((int)n); ! loc->loc_bufsize = (int)n; ! loc->loc_size = (int)n; ! loc->loc_oflags = 0; ! loc->loc_mflags = 0; ! loc->loc_indicator = 0; ! memcpy(loc->loc_buffer, buf, (int)n); ! var->sqldata = (char *) loc; ! var->sqllen = sizeof(loc_t); ! var->sqltype = CLOCATORTYPE; ! *var->sqlind = 0; return 1; } --- 1222,1266 ---- } ! $ifdef HAVE_ESQL9; ! /* If the target column is an opaque type, bind the buffer contents ! as a var binary. This, in combination with the fact that the binary ! contents of opaque types are returned as a buffer, allows for ! seamless select-insert/update roundtrips of opaque types in binary ! format. */ ! if (ISUDTTYPE(var->sqltype)) { ! EXEC SQL BEGIN DECLARE SECTION; ! var binary *data; ! EXEC SQL END DECLARE SECTION; ! data = malloc(sizeof(void*)); ! *data = 0; ! ifx_var_flag(data, 0); ! ifx_var_alloc(data, n+1); ! ifx_var_setlen(data, n); ! ifx_var_setdata(data, (char *)buf, n); ! var->sqltype = CVARBINTYPE; ! var->sqldata = *data; ! var->sqllen = sizeof(void*); ! *var->sqlind = 0; ! free( data ); ! } ! else ! $endif; ! { ! loc_t *loc; ! loc = (loc_t*) malloc(sizeof(loc_t)); ! loc->loc_loctype = LOCMEMORY; ! loc->loc_buffer = malloc((int)n); ! loc->loc_bufsize = (int)n; ! loc->loc_size = (int)n; ! loc->loc_oflags = 0; ! loc->loc_mflags = 0; ! loc->loc_indicator = 0; ! memcpy(loc->loc_buffer, buf, (int)n); ! var->sqldata = (char *) loc; ! var->sqllen = sizeof(loc_t); ! var->sqltype = CLOCATORTYPE; ! *var->sqlind = 0; ! } return 1; } *************** *** 1639,1648 **** exec sql end declare section; ! var->sqltype = CLVCHARPTRTYPE; currentlvarcharptr = malloc(sizeof(void *)); *currentlvarcharptr = 0; ifx_var_flag(currentlvarcharptr,1); - var->sqlxid = 1; var->sqldata = *currentlvarcharptr; var->sqllen = sizeof(void *); --- 1685,1708 ---- exec sql end declare section; ! if (ISUDTTYPE(var->sqltype)) { ! /* For opaque types, check if the user wants them in binary ! format instead. */ ! PyObject *binflag = PyDict_GetItemString(cur->binary_types, ! var->sqltypename); ! if (binflag && (PyObject_IsTrue(binflag)==1)) { ! var->sqltype = CVARBINTYPE; ! } ! else { ! var->sqltype = CLVCHARPTRTYPE; ! } ! } ! else { ! /* Complex types are always read in text format.*/ ! var->sqltype = CLVCHARPTRTYPE; ! } currentlvarcharptr = malloc(sizeof(void *)); *currentlvarcharptr = 0; ifx_var_flag(currentlvarcharptr,1); var->sqldata = *currentlvarcharptr; var->sqllen = sizeof(void *); *************** *** 1993,1999 **** } ! static PyObject *doCopy(/* const */ void *data, int type, int4 xid, int4 sqllen, struct Cursor_t *cur) { switch(type & SQLTYPE){ case SQLDATE: --- 2053,2060 ---- } ! static PyObject *doCopy(struct sqlvar_struct *var, int type, int4 xid, int4 sqllen, struct Cursor_t *cur) { + void *data = var->sqldata; switch(type & SQLTYPE){ case SQLDATE: *************** *** 2205,2209 **** Py_ssize_t len = ifx_var_getlen(&data); char *lvcharbuf = ifx_var_getdata(&data); ! result = PyString_FromStringAndSize(lvcharbuf,len-1); ifx_var_dealloc(&data); return result; --- 2266,2287 ---- Py_ssize_t len = ifx_var_getlen(&data); char *lvcharbuf = ifx_var_getdata(&data); ! if ((var->sqltype&SQLTYPE)==CVARBINTYPE) { ! /* If the output was bound as var binary, build a buffer object. */ ! char *b_mem; ! Py_ssize_t b_len; ! result = PyBuffer_New(len); ! ! if (PyObject_AsWriteBuffer(result, (void**)&b_mem, &b_len) == -1) { ! Py_DECREF(result); ! result = NULL; ! } ! else { ! memcpy(b_mem, lvcharbuf, (int)b_len); ! } ! } ! else { ! /* Otherwise build a string. */ ! result = PyString_FromStringAndSize(lvcharbuf,len-1); ! } ifx_var_dealloc(&data); return result; *************** *** 2242,2246 **** Py_INCREF(v); } else { ! v = doCopy(var->sqldata, cur->originalType[pos], cur->originalXid[pos], cur->originalLen[pos], cur); } --- 2320,2324 ---- Py_INCREF(v); } else { ! v = doCopy(var, cur->originalType[pos], cur->originalXid[pos], cur->originalLen[pos], cur); } *************** *** 2534,2537 **** --- 2612,2616 ---- self->arraysize = 1; self->messages = PyList_New(0); + self->binary_types = PyDict_Copy(conn->binary_types); self->errorhandler = conn->errorhandler; Py_INCREF(self->errorhandler); *************** *** 2827,2830 **** --- 2906,2910 ---- self->messages = PyList_New(0); self->errorhandler = Py_None; + self->binary_types = PyDict_New(); Py_INCREF(self->errorhandler); |