Skip to content

Commit e6bd5b5

Browse files
committed
Fix NULL parameter type mapping for VARBINARY columns (#458)
1 parent 95eef16 commit e6bd5b5

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

mssql_python/cursor.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def _map_sql_type( # pylint: disable=too-many-arguments,too-many-positional-arg
392392
if param is None:
393393
logger.debug("_map_sql_type: NULL parameter - index=%d", i)
394394
return (
395-
ddbc_sql_const.SQL_VARCHAR.value,
395+
ddbc_sql_const.SQL_UNKNOWN_TYPE.value,
396396
ddbc_sql_const.SQL_C_DEFAULT.value,
397397
1,
398398
0,
@@ -2185,6 +2185,16 @@ def executemany( # pylint: disable=too-many-locals,too-many-branches,too-many-s
21852185
min_val=min_val,
21862186
max_val=max_val,
21872187
)
2188+
2189+
# For executemany with all-NULL columns, SQL_UNKNOWN_TYPE doesn't work
2190+
# with array binding. Fall back to SQL_VARCHAR as a safe default.
2191+
if (
2192+
sample_value is None
2193+
and paraminfo.paramSQLType == ddbc_sql_const.SQL_UNKNOWN_TYPE.value
2194+
):
2195+
paraminfo.paramSQLType = ddbc_sql_const.SQL_VARCHAR.value
2196+
paraminfo.columnSize = 1
2197+
21882198
# Special handling for binary data in auto-detected types
21892199
if paraminfo.paramSQLType in (
21902200
ddbc_sql_const.SQL_BINARY.value,

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,19 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
471471
hStmt, static_cast<SQLUSMALLINT>(paramIndex + 1), &describedType,
472472
&describedSize, &describedDigits, &nullable);
473473
if (!SQL_SUCCEEDED(rc)) {
474+
// SQLDescribeParam can fail for generic SELECT statements where
475+
// no table column is referenced. Fall back to SQL_VARCHAR as a safe default.
474476
LOG("BindParameters: SQLDescribeParam failed for "
475-
"param[%d] (NULL parameter) - SQLRETURN=%d",
477+
"param[%d] (NULL parameter) - SQLRETURN=%d, falling back to SQL_VARCHAR",
476478
paramIndex, rc);
477-
return rc;
479+
sqlType = SQL_VARCHAR;
480+
columnSize = 1;
481+
decimalDigits = 0;
482+
} else {
483+
sqlType = describedType;
484+
columnSize = describedSize;
485+
decimalDigits = describedDigits;
478486
}
479-
sqlType = describedType;
480-
columnSize = describedSize;
481-
decimalDigits = describedDigits;
482487
}
483488
dataPtr = nullptr;
484489
strLenOrIndPtr = AllocateParamBuffer<SQLLEN>(paramBuffers);

0 commit comments

Comments
 (0)