Skip to content

Commit bf67d3a

Browse files
committed
cleaning up
1 parent b47655b commit bf67d3a

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

mssql_python/cursor.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,13 +656,10 @@ def executemany(self, operation: str, seq_of_parameters: list) -> None:
656656
for col_index in range(param_count):
657657
column = [row[col_index] for row in seq_of_parameters]
658658
sample_value = column[0]
659-
660659
if isinstance(sample_value, str):
661660
sample_value = max(column, key=lambda s: len(str(s)) if s is not None else 0)
662-
663661
elif isinstance(sample_value, decimal.Decimal):
664662
sample_value = max(column, key=lambda d: len(d.as_tuple().digits) if d is not None else 0)
665-
666663
param = sample_value
667664
dummy_row = list(seq_of_parameters[0])
668665
parameters_type.append(self._create_parameter_types_list(param, param_info, dummy_row, col_index))
@@ -679,7 +676,7 @@ def executemany(self, operation: str, seq_of_parameters: list) -> None:
679676
)
680677
check_error(ddbc_sql_const.SQL_HANDLE_STMT.value, self.hstmt, ret)
681678

682-
self.rowcount = len(seq_of_parameters)
679+
self.rowcount = ddbc_bindings.DDBCSQLRowCount(self.hstmt)
683680
self.last_executed_stmt = operation
684681
self._initialize_description()
685682

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -948,15 +948,12 @@ SQLRETURN BindParameterArray(SQLHANDLE hStmt,
948948
size_t paramSetSize,
949949
std::vector<std::shared_ptr<void>>& paramBuffers) {
950950
LOG("Starting column-wise parameter array binding. paramSetSize: {}, paramCount: {}", paramSetSize, columnwise_params.size());
951-
952951
for (int paramIndex = 0; paramIndex < columnwise_params.size(); ++paramIndex) {
953952
const py::list& columnValues = columnwise_params[paramIndex].cast<py::list>();
954953
const ParamInfo& info = paramInfos[paramIndex];
955-
956954
if (columnValues.size() != paramSetSize) {
957955
ThrowStdException("Column " + std::to_string(paramIndex) + " has mismatched size.");
958956
}
959-
960957
void* dataPtr = nullptr;
961958
SQLLEN* strLenOrIndArray = nullptr;
962959
SQLLEN bufferLength = 0;
@@ -1003,7 +1000,6 @@ SQLRETURN BindParameterArray(SQLHANDLE hStmt,
10031000
std::memset(wcharArray + i * (info.columnSize + 1), 0, (info.columnSize + 1) * sizeof(SQLWCHAR));
10041001
continue;
10051002
}
1006-
10071003
std::wstring wstr = columnValues[i].cast<std::wstring>();
10081004
if (wstr.length() > info.columnSize) {
10091005
std::string offending = WideToUTF8(wstr);
@@ -1023,15 +1019,19 @@ SQLRETURN BindParameterArray(SQLHANDLE hStmt,
10231019
case SQL_C_UTINYINT: {
10241020
unsigned char* dataArray = AllocateParamBufferArray<unsigned char>(paramBuffers, paramSetSize);
10251021
for (size_t i = 0; i < paramSetSize; ++i) {
1026-
const py::handle& value = columnValues[i];
1027-
if (!py::isinstance<py::int_>(value)) {
1028-
ThrowStdException(MakeParamMismatchErrorStr(info.paramCType, paramIndex));
1029-
}
1030-
int intVal = value.cast<int>();
1031-
if (intVal < 0 || intVal > 255) {
1032-
ThrowStdException("UTINYINT value out of range at rowIndex " + std::to_string(i));
1022+
if (columnValues[i].is_none()) {
1023+
if (!strLenOrIndArray)
1024+
strLenOrIndArray = AllocateParamBufferArray<SQLLEN>(paramBuffers, paramSetSize);
1025+
dataArray[i] = 0;
1026+
strLenOrIndArray[i] = SQL_NULL_DATA;
1027+
} else {
1028+
int intVal = columnValues[i].cast<int>();
1029+
if (intVal < 0 || intVal > 255) {
1030+
ThrowStdException("UTINYINT value out of range at rowIndex " + std::to_string(i));
1031+
}
1032+
dataArray[i] = static_cast<unsigned char>(intVal);
1033+
if (strLenOrIndArray) strLenOrIndArray[i] = 0;
10331034
}
1034-
dataArray[i] = static_cast<unsigned char>(intVal);
10351035
}
10361036
dataPtr = dataArray;
10371037
bufferLength = sizeof(unsigned char);
@@ -1040,22 +1040,25 @@ SQLRETURN BindParameterArray(SQLHANDLE hStmt,
10401040
case SQL_C_SHORT: {
10411041
short* dataArray = AllocateParamBufferArray<short>(paramBuffers, paramSetSize);
10421042
for (size_t i = 0; i < paramSetSize; ++i) {
1043-
const py::handle& value = columnValues[i];
1044-
if (!py::isinstance<py::int_>(value)) {
1045-
ThrowStdException(MakeParamMismatchErrorStr(info.paramCType, paramIndex));
1046-
}
1047-
int intVal = value.cast<int>();
1048-
if (intVal < std::numeric_limits<short>::min() ||
1049-
intVal > std::numeric_limits<short>::max()) {
1050-
ThrowStdException("SHORT value out of range at rowIndex " + std::to_string(i));
1043+
if (columnValues[i].is_none()) {
1044+
if (!strLenOrIndArray)
1045+
strLenOrIndArray = AllocateParamBufferArray<SQLLEN>(paramBuffers, paramSetSize);
1046+
dataArray[i] = 0;
1047+
strLenOrIndArray[i] = SQL_NULL_DATA;
1048+
} else {
1049+
int intVal = columnValues[i].cast<int>();
1050+
if (intVal < std::numeric_limits<short>::min() ||
1051+
intVal > std::numeric_limits<short>::max()) {
1052+
ThrowStdException("SHORT value out of range at rowIndex " + std::to_string(i));
1053+
}
1054+
dataArray[i] = static_cast<short>(intVal);
1055+
if (strLenOrIndArray) strLenOrIndArray[i] = 0;
10511056
}
1052-
dataArray[i] = static_cast<short>(intVal);
10531057
}
10541058
dataPtr = dataArray;
10551059
bufferLength = sizeof(short);
10561060
break;
10571061
}
1058-
10591062
default: {
10601063
ThrowStdException("BindParameterArray: Unsupported C type: " + std::to_string(info.paramCType));
10611064
}

0 commit comments

Comments
 (0)