Skip to content
This repository
Browse code

Issue 211: Workaround for MemoryError when selecting NULL values with…

… FreeTDS.
  • Loading branch information...
commit d30bcd39e9d751950513b8e29395f3b0b717a9b2 1 parent 9a32afd
Michael Kleehammer authored

Showing 1 changed file with 14 additions and 3 deletions. Show diff stats Hide diff stats

  1. +14 3 src/getdata.cpp
17 src/getdata.cpp
@@ -215,7 +215,7 @@ class DataBuffer
215 215 return PyBytes_FromStringAndSize(buffer, bytesUsed);
216 216 #endif
217 217 }
218   -
  218 +
219 219 if (sizeof(SQLWCHAR) == Py_UNICODE_SIZE)
220 220 return PyUnicode_FromUnicode((const Py_UNICODE*)buffer, bytesUsed / element_size);
221 221
@@ -253,7 +253,7 @@ class DataBuffer
253 253 return tmp;
254 254 }
255 255 #endif
256   -
  256 +
257 257 // We have allocated our own SQLWCHAR buffer and must now copy it to a Unicode object.
258 258 I(bufferOwner == 0);
259 259 PyObject* result = PyUnicode_FromSQLWCHAR((const SQLWCHAR*)buffer, bytesUsed / element_size);
@@ -340,8 +340,19 @@ static PyObject* GetDataString(Cursor* cur, Py_ssize_t iCol)
340 340 ret = SQLGetData(cur->hstmt, (SQLUSMALLINT)(iCol+1), nTargetType, buffer.GetBuffer(), buffer.GetRemaining(), &cbData);
341 341 Py_END_ALLOW_THREADS;
342 342
343   - if (cbData == SQL_NULL_DATA)
  343 + if (cbData == SQL_NULL_DATA || (ret == SQL_SUCCESS && cbData < 0))
  344 + {
  345 + // HACK: FreeTDS 0.91 on OS/X returns -4 for NULL data instead of SQL_NULL_DATA (-1). I've traced into the
  346 + // code and it appears to be the result of assigning -1 to a SQLLEN:
  347 + //
  348 + // if (colinfo->column_cur_size < 0) {
  349 + // /* TODO check what should happen if pcbValue was NULL */
  350 + // *pcbValue = SQL_NULL_DATA;
  351 + //
  352 + // I believe it will be fine to treat all negative values as NULL for now.
  353 +
344 354 Py_RETURN_NONE;
  355 + }
345 356
346 357 if (!SQL_SUCCEEDED(ret) && ret != SQL_NO_DATA)
347 358 return RaiseErrorFromHandle("SQLGetData", cur->cnxn->hdbc, cur->hstmt);

0 comments on commit d30bcd3

Please sign in to comment.
Something went wrong with that request. Please try again.