[pywin32-checkins] pywin32/win32/src odbc.cpp,1.20.2.5,1.20.2.6
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Roger U. <ru...@us...> - 2008-09-16 15:13:56
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4812 Modified Files: Tag: py3k odbc.cpp Log Message: Accept and return raw binary fields as buffers Remove last dependencies on dbi module Index: odbc.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/odbc.cpp,v retrieving revision 1.20.2.5 retrieving revision 1.20.2.6 diff -C2 -d -r1.20.2.5 -r1.20.2.6 *** odbc.cpp 16 Sep 2008 01:01:17 -0000 1.20.2.5 --- odbc.cpp 16 Sep 2008 15:14:05 -0000 1.20.2.6 *************** *** 36,44 **** // #endif ! #include "dbi.h" /*$ This is a hack */ ! static PyObject *odbcError; static PyObject *datetime_module, *datetime_class; ! #define MAX_STR 45 static HENV Env; --- 36,50 ---- // #endif ! static PyObject *datetime_module, *datetime_class; ! // Type names ! static PyObject *DbiString, *DbiRaw, *DbiNumber, *DbiDate; ! // Exceptions ! static PyObject *odbcError; ! static PyObject *DbiNoError, *DbiOpError, *DbiProgError; ! static PyObject *DbiIntegrityError, *DbiDataError, *DbiInternalError; ! ! #define MAX_STR 256 static HENV Env; *************** *** 621,625 **** static PyObject *rawCopy(const void *v, int sz) { ! return dbiMakeRaw(PyString_FromStringAndSize((char *)v, sz)); } --- 627,642 ---- static PyObject *rawCopy(const void *v, int sz) { ! PyObject *ret = PyBuffer_New(sz); ! if (!ret) ! return NULL; ! void *buf; ! DWORD buflen; ! // Should not fail, but check anyway ! if (!PyWinObject_AsWriteBuffer(ret, &buf, &buflen)){ ! Py_DECREF(ret); ! return NULL; ! } ! memcpy(buf, v, sz); ! return ret; } *************** *** 753,762 **** } else{ ! long long longlongval = PyLong_AsLongLong(item); if (longlongval == -1 && PyErr_Occurred()) return 0; CType = SQL_C_SBIGINT; SqlType = SQL_BIGINT; ! len = sizeof(long long); ib = initInputBinding(cur, len); if (!ib) --- 770,779 ---- } else{ ! __int64 longlongval = PyLong_AsLongLong(item); if (longlongval == -1 && PyErr_Occurred()) return 0; CType = SQL_C_SBIGINT; SqlType = SQL_BIGINT; ! len = sizeof(longlongval); ib = initInputBinding(cur, len); if (!ib) *************** *** 881,887 **** static int ibindRaw(cursorObject *cur, int column, PyObject *item) { ! const char *val = PyString_AsString(item); ! Py_ssize_t len = PyObject_Length(item); ! InputBinding *ib = initInputBinding(cur, len); if (!ib) --- 898,905 ---- static int ibindRaw(cursorObject *cur, int column, PyObject *item) { ! void *val; ! DWORD len; ! if (!PyWinObject_AsReadBuffer(item, &val, &len)) ! return 0; InputBinding *ib = initInputBinding(cur, len); if (!ib) *************** *** 1065,1073 **** item = PySequence_GetItem(vars, i); iCol = i + 1; ! if (dbiIsRaw(item)) ! { ! rv = ibindRaw(cur, iCol, dbiValue(item)); ! } ! else if (PyLong_Check(item)) { rv = ibindLong(cur, iCol, item); --- 1083,1087 ---- item = PySequence_GetItem(vars, i); iCol = i + 1; ! if (PyLong_Check(item)) { rv = ibindLong(cur, iCol, item); *************** *** 1097,1102 **** --- 1111,1127 ---- rv = ibindDate(cur, iCol, item); } + #if (PY_VERSION_HEX < 0x03000000) + else if (PyBuffer_Check(item)) + #else + else if (PyObject_CheckReadBuffer(item)) + #endif + { + rv = ibindRaw(cur, iCol, item); + } else { + OutputDebugString(_T("bindInput - using repr conversion for type: '")); + OutputDebugStringA(item->ob_type->tp_name); + OutputDebugString(_T("'\n")); PyObject *sitem = PyObject_Str(item); if (sitem==NULL) *************** *** 1956,1961 **** if (!dict) RETURN_ERROR; - if (!PyImport_ImportModule("dbi")) - RETURN_ERROR; // Sql dates are now returned as python's datetime object. --- 1981,1984 ---- *************** *** 1974,1981 **** } odbcError = PyErr_NewException("odbc.odbcError", NULL, NULL); if (odbcError == NULL || PyDict_SetItemString(dict, "error", odbcError) == -1) RETURN_ERROR; ! /* The indices go to indices in the ODBC error table */ dbiErrors[0] = DbiNoError; --- 1997,2050 ---- } + /* Names of various sql datatypes. + 's' format of Py_BuildValue creates unicode on py3k, and char string on 2.x + */ + char *szDbiString = "STRING"; + char *szDbiRaw = "RAW"; + char *szDbiNumber = "NUMBER"; + char *szDbiDate = "DATE"; + PyObject *obtypes=Py_BuildValue("(ssss)", + szDbiString, + szDbiRaw, + szDbiNumber, + szDbiDate); + // Steals a ref to obtypes, so it doesn't need to be DECREF'ed. + if (obtypes==NULL || PyModule_AddObject(module, "TYPES", obtypes) == -1) + RETURN_ERROR; + DbiString = PyTuple_GET_ITEM(obtypes, 0); + DbiRaw = PyTuple_GET_ITEM(obtypes, 1); + DbiNumber = PyTuple_GET_ITEM(obtypes, 2); + DbiDate = PyTuple_GET_ITEM(obtypes, 3); + /* ??? These are also added to the module with attribute name same as value, + not sure what the point of this is ??? + */ + if (PyDict_SetItem(dict, DbiString, DbiString) == -1 + ||PyDict_SetItem(dict, DbiRaw, DbiRaw) == -1 + ||PyDict_SetItem(dict, DbiNumber, DbiNumber) == -1 + ||PyDict_SetItem(dict, DbiDate, DbiDate) == -1) + RETURN_ERROR; + + // Initialize various exception types odbcError = PyErr_NewException("odbc.odbcError", NULL, NULL); if (odbcError == NULL || PyDict_SetItemString(dict, "error", odbcError) == -1) RETURN_ERROR; ! DbiNoError = PyErr_NewException("dbi.noError", NULL, NULL); ! if (DbiNoError == NULL || PyDict_SetItemString(dict, "noError", DbiNoError) == -1) ! RETURN_ERROR; ! DbiOpError = PyErr_NewException("dbi.opError", NULL, NULL); ! if (DbiOpError == NULL || PyDict_SetItemString(dict, "opError", DbiOpError) == -1) ! RETURN_ERROR; ! DbiProgError = PyErr_NewException("dbi.progError", NULL, NULL); ! if (DbiProgError == NULL || PyDict_SetItemString(dict, "progError", DbiProgError) == -1) ! RETURN_ERROR; ! DbiIntegrityError = PyErr_NewException("dbi.integrityError", NULL, NULL); ! if (DbiIntegrityError == NULL || PyDict_SetItemString(dict, "integrityError", DbiIntegrityError) == -1) ! RETURN_ERROR; ! DbiDataError = PyErr_NewException("dbi.dataError", NULL, NULL); ! if (DbiDataError == NULL || PyDict_SetItemString(dict, "dataError", DbiDataError) == -1) ! RETURN_ERROR; ! DbiInternalError = PyErr_NewException("dbi.internalError", NULL, NULL); ! if (DbiInternalError == NULL || PyDict_SetItemString(dict, "internalError", DbiInternalError) == -1) ! RETURN_ERROR; /* The indices go to indices in the ODBC error table */ dbiErrors[0] = DbiNoError; |