Skip to content

Commit ce3a27d

Browse files
committed
reverting python changes and fixing it in ddbc
1 parent 87fd98c commit ce3a27d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

mssql_python/cursor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def _map_sql_type( # pylint: disable=too-many-arguments,too-many-positional-arg
307307
logger.debug('_map_sql_type: NULL parameter - index=%d', i)
308308
return (
309309
ddbc_sql_const.SQL_VARCHAR.value,
310-
ddbc_sql_const.SQL_C_CHAR.value,
310+
ddbc_sql_const.SQL_C_DEFAULT.value,
311311
1,
312312
0,
313313
False,

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,33 @@ SQLRETURN BindParameterArray(SQLHANDLE hStmt,
21832183
bufferLength = sizeof(SQLGUID);
21842184
break;
21852185
}
2186+
case SQL_C_DEFAULT: {
2187+
// Handle NULL parameters - all values in this column should be NULL
2188+
LOG("BindParameterArray: Binding SQL_C_DEFAULT (NULL) array - param_index=%d, count=%zu", paramIndex, paramSetSize);
2189+
2190+
// Verify all values are indeed NULL
2191+
for (size_t i = 0; i < paramSetSize; ++i) {
2192+
if (!columnValues[i].is_none()) {
2193+
LOG("BindParameterArray: SQL_C_DEFAULT non-NULL value detected - param_index=%d, row=%zu", paramIndex, i);
2194+
ThrowStdException("SQL_C_DEFAULT (99) should only be used for NULL parameters at index " + std::to_string(paramIndex));
2195+
}
2196+
}
2197+
2198+
// For NULL parameters, we need to allocate a minimal buffer and set all indicators to SQL_NULL_DATA
2199+
// Use SQL_C_CHAR as a safe default C type for NULL values
2200+
char* nullBuffer = AllocateParamBufferArray<char>(tempBuffers, paramSetSize);
2201+
strLenOrIndArray = AllocateParamBufferArray<SQLLEN>(tempBuffers, paramSetSize);
2202+
2203+
for (size_t i = 0; i < paramSetSize; ++i) {
2204+
nullBuffer[i] = 0;
2205+
strLenOrIndArray[i] = SQL_NULL_DATA;
2206+
}
2207+
2208+
dataPtr = nullBuffer;
2209+
bufferLength = 1;
2210+
LOG("BindParameterArray: SQL_C_DEFAULT bound - param_index=%d, all_null=true", paramIndex);
2211+
break;
2212+
}
21862213
default: {
21872214
LOG("BindParameterArray: Unsupported C type - param_index=%d, C_type=%d", paramIndex, info.paramCType);
21882215
ThrowStdException("BindParameterArray: Unsupported C type: " + std::to_string(info.paramCType));

0 commit comments

Comments
 (0)