Skip to content

Commit 42824f1

Browse files
committed
cleaning up
1 parent b47655b commit 42824f1

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

mssql_python/cursor.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@
1818
from mssql_python.logging_config import get_logger, ENABLE_LOGGING
1919
from mssql_python import ddbc_bindings
2020
from .row import Row
21-
from typing import Sequence, Any
22-
2321

2422
logger = get_logger()
2523

26-
2724
class Cursor:
2825
"""
2926
Represents a database cursor, which is used to manage the context of a fetch operation.
@@ -541,7 +538,7 @@ def _map_data_type(self, sql_type):
541538
# Add more mappings as needed
542539
}
543540
return sql_to_python_type.get(sql_type, str)
544-
541+
545542
def execute(
546543
self,
547544
operation: str,
@@ -656,13 +653,10 @@ def executemany(self, operation: str, seq_of_parameters: list) -> None:
656653
for col_index in range(param_count):
657654
column = [row[col_index] for row in seq_of_parameters]
658655
sample_value = column[0]
659-
660656
if isinstance(sample_value, str):
661657
sample_value = max(column, key=lambda s: len(str(s)) if s is not None else 0)
662-
663658
elif isinstance(sample_value, decimal.Decimal):
664659
sample_value = max(column, key=lambda d: len(d.as_tuple().digits) if d is not None else 0)
665-
666660
param = sample_value
667661
dummy_row = list(seq_of_parameters[0])
668662
parameters_type.append(self._create_parameter_types_list(param, param_info, dummy_row, col_index))
@@ -679,7 +673,7 @@ def executemany(self, operation: str, seq_of_parameters: list) -> None:
679673
)
680674
check_error(ddbc_sql_const.SQL_HANDLE_STMT.value, self.hstmt, ret)
681675

682-
self.rowcount = len(seq_of_parameters)
676+
self.rowcount = ddbc_bindings.DDBCSQLRowCount(self.hstmt)
683677
self.last_executed_stmt = operation
684678
self._initialize_description()
685679

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)