Skip to content

Commit 53fdf2f

Browse files
Preserve s_bin_mode behavior in BLOB retrieval (#1047)
Signed-off-by: Balram Choudhary <bchoudhary@rocketsoftware.com>
1 parent 62ce7b2 commit 53fdf2f

File tree

1 file changed

+55
-9
lines changed

1 file changed

+55
-9
lines changed

ibm_db.c

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12885,9 +12885,26 @@ static PyObject *ibm_db_result(PyObject *self, PyObject *args)
1288512885
case SQL_VARBINARY:
1288612886
{
1288712887
LogMsg(DEBUG, "Processing SQL_BLOB/SQL_BINARY/SQL_LONGVARBINARY/SQL_VARBINARY");
12888-
targetCType = SQL_C_BINARY;
12889-
len_terChar = 0;
12890-
/* Initial buffer allocation */
12888+
/* Respect binary mode */
12889+
switch (stmt_res->s_bin_mode)
12890+
{
12891+
case PASSTHRU:
12892+
LogMsg(DEBUG, "BLOB mode: PASSTHRU");
12893+
targetCType = SQL_C_BINARY;
12894+
break;
12895+
case CONVERT:
12896+
LogMsg(DEBUG, "BLOB mode: CONVERT");
12897+
targetCType = SQL_C_CHAR;
12898+
break;
12899+
case BINARY:
12900+
LogMsg(DEBUG, "BLOB mode: BINARY");
12901+
targetCType = SQL_C_BINARY;
12902+
break;
12903+
default:
12904+
LogMsg(ERROR, "Unknown BLOB mode");
12905+
Py_RETURN_FALSE;
12906+
}
12907+
/* Initial allocation */
1289112908
out_ptr = ALLOC_N(char, INIT_BUFSIZ);
1289212909
if (out_ptr == NULL)
1289312910
{
@@ -12910,7 +12927,8 @@ static PyObject *ibm_db_result(PyObject *self, PyObject *args)
1291012927
{
1291112928
LogMsg(DEBUG, "BLOB larger than INIT_BUFSIZ, reallocating buffer");
1291212929
void *tmp_out_ptr = NULL;
12913-
tmp_out_ptr = ALLOC_N(char, INIT_BUFSIZ + out_length);
12930+
SQLINTEGER total_length = INIT_BUFSIZ + out_length;
12931+
tmp_out_ptr = ALLOC_N(char, total_length);
1291412932
if (tmp_out_ptr == NULL)
1291512933
{
1291612934
LogMsg(ERROR, "Failed to reallocate memory for large BLOB");
@@ -12926,7 +12944,7 @@ static PyObject *ibm_db_result(PyObject *self, PyObject *args)
1292612944
col_num + 1,
1292712945
targetCType,
1292812946
(char *)out_ptr + INIT_BUFSIZ,
12929-
INIT_BUFSIZ + out_length,
12947+
total_length,
1293012948
&out_length);
1293112949
snprintf(messageStr, sizeof(messageStr),
1293212950
"SQL_Get_Data (BLOB continuation) rc: %d, out_length: %d",
@@ -12938,8 +12956,22 @@ static PyObject *ibm_db_result(PyObject *self, PyObject *args)
1293812956
PyMem_Del(out_ptr);
1293912957
return NULL;
1294012958
}
12941-
retVal = PyBytes_FromStringAndSize((char *)out_ptr,
12942-
INIT_BUFSIZ + out_length);
12959+
if (stmt_res->s_bin_mode == CONVERT)
12960+
{
12961+
retVal = PyUnicode_FromStringAndSize((char *)out_ptr,
12962+
total_length);
12963+
}
12964+
else
12965+
{
12966+
retVal = PyBytes_FromStringAndSize((char *)out_ptr,
12967+
total_length);
12968+
}
12969+
if (retVal == NULL)
12970+
{
12971+
LogMsg(ERROR, "Failed to create Python object for large BLOB");
12972+
PyMem_Del(out_ptr);
12973+
return NULL;
12974+
}
1294312975
}
1294412976
else if (rc == SQL_ERROR)
1294512977
{
@@ -12956,8 +12988,22 @@ static PyObject *ibm_db_result(PyObject *self, PyObject *args)
1295612988
PyMem_Del(out_ptr);
1295712989
Py_RETURN_NONE;
1295812990
}
12959-
retVal = PyBytes_FromStringAndSize((char *)out_ptr,
12960-
out_length);
12991+
if (stmt_res->s_bin_mode == CONVERT)
12992+
{
12993+
retVal = PyUnicode_FromStringAndSize((char *)out_ptr,
12994+
out_length);
12995+
}
12996+
else
12997+
{
12998+
retVal = PyBytes_FromStringAndSize((char *)out_ptr,
12999+
out_length);
13000+
}
13001+
if (retVal == NULL)
13002+
{
13003+
LogMsg(ERROR, "Failed to create Python object for BLOB");
13004+
PyMem_Del(out_ptr);
13005+
return NULL;
13006+
}
1296113007
snprintf(messageStr, sizeof(messageStr),
1296213008
"Returning BLOB data of length: %d",
1296313009
out_length);

0 commit comments

Comments
 (0)