Skip to content

Commit 85fc8b3

Browse files
committed
Resolving commits
1 parent a80a391 commit 85fc8b3

2 files changed

Lines changed: 103 additions & 103 deletions

File tree

mssql_python/cursor.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -950,14 +950,12 @@ def _create_parameter_types_list( # pylint: disable=too-many-arguments,too-many
950950
c_type = self._get_c_type_for_sql_type(sql_type)
951951

952952
# Convert Decimal to string for SQL_C_CHAR binding (GH-503)
953-
if (
954-
isinstance(parameter, decimal.Decimal)
955-
and sql_type in (
956-
ddbc_sql_const.SQL_DECIMAL.value,
957-
ddbc_sql_const.SQL_NUMERIC.value,
958-
)
953+
if isinstance(parameter, decimal.Decimal) and sql_type in (
954+
ddbc_sql_const.SQL_DECIMAL.value,
955+
ddbc_sql_const.SQL_NUMERIC.value,
959956
):
960957
parameters_list[i] = format(parameter, "f")
958+
parameter = parameters_list[i]
961959

962960
# Check if this should be a DAE (data at execution) parameter
963961
# For string types with large column sizes
@@ -2316,26 +2314,20 @@ def executemany( # pylint: disable=too-many-locals,too-many-branches,too-many-s
23162314
and parameters_type[i].paramSQLType == ddbc_sql_const.SQL_VARCHAR.value
23172315
):
23182316
processed_row[i] = format(val, "f")
2319-
# Convert Decimal to string for SQL_C_CHAR binding (GH-503)
2320-
elif (
2321-
isinstance(val, decimal.Decimal)
2322-
and parameters_type[i].paramSQLType in (
2323-
ddbc_sql_const.SQL_DECIMAL.value,
2324-
ddbc_sql_const.SQL_NUMERIC.value,
2325-
)
2326-
):
2327-
processed_row[i] = format(val, "f")
2328-
# Existing numeric conversion
2317+
# Convert all values to string for DECIMAL/NUMERIC columns (GH-503)
23292318
elif parameters_type[i].paramSQLType in (
23302319
ddbc_sql_const.SQL_DECIMAL.value,
23312320
ddbc_sql_const.SQL_NUMERIC.value,
2332-
) and not isinstance(val, decimal.Decimal):
2333-
try:
2334-
processed_row[i] = decimal.Decimal(str(val))
2335-
except Exception as e: # pylint: disable=broad-exception-caught
2336-
raise ValueError(
2337-
f"Failed to convert parameter at row {row}, column {i} to Decimal: {e}"
2338-
) from e
2321+
):
2322+
if isinstance(val, decimal.Decimal):
2323+
processed_row[i] = format(val, "f")
2324+
else:
2325+
try:
2326+
processed_row[i] = format(decimal.Decimal(str(val)), "f")
2327+
except Exception as e: # pylint: disable=broad-exception-caught
2328+
raise ValueError(
2329+
f"Failed to convert parameter at row {row}, column {i} to Decimal: {e}"
2330+
) from e
23392331
processed_parameters.append(processed_row)
23402332

23412333
# Now transpose the processed parameters

tests/test_004_cursor.py

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9984,119 +9984,127 @@ def test_setinputsizes_sql_decimal_with_executemany(db_connection):
99849984
cursor = db_connection.cursor()
99859985

99869986
cursor.execute("DROP TABLE IF EXISTS #test_sis_decimal")
9987-
cursor.execute("""
9988-
CREATE TABLE #test_sis_decimal (
9989-
Name NVARCHAR(100),
9990-
CategoryID INT,
9991-
Price DECIMAL(18,2)
9992-
)
9993-
""")
9994-
9995-
cursor.setinputsizes([
9996-
(mssql_python.SQL_WVARCHAR, 100, 0),
9997-
(mssql_python.SQL_INTEGER, 0, 0),
9998-
(mssql_python.SQL_DECIMAL, 18, 2),
9999-
])
9987+
try:
9988+
cursor.execute("""
9989+
CREATE TABLE #test_sis_decimal (
9990+
Name NVARCHAR(100),
9991+
CategoryID INT,
9992+
Price DECIMAL(18,2)
9993+
)
9994+
""")
100009995

10001-
cursor.executemany(
10002-
"INSERT INTO #test_sis_decimal (Name, CategoryID, Price) VALUES (?, ?, ?)",
10003-
[
10004-
("Widget", 1, decimal.Decimal("19.99")),
10005-
("Gadget", 2, decimal.Decimal("29.99")),
10006-
("Gizmo", 3, decimal.Decimal("0.01")),
10007-
],
10008-
)
9996+
cursor.setinputsizes(
9997+
[
9998+
(mssql_python.SQL_WVARCHAR, 100, 0),
9999+
(mssql_python.SQL_INTEGER, 0, 0),
10000+
(mssql_python.SQL_DECIMAL, 18, 2),
10001+
]
10002+
)
1000910003

10010-
cursor.execute("SELECT Name, CategoryID, Price FROM #test_sis_decimal ORDER BY CategoryID")
10011-
rows = cursor.fetchall()
10004+
cursor.executemany(
10005+
"INSERT INTO #test_sis_decimal (Name, CategoryID, Price) VALUES (?, ?, ?)",
10006+
[
10007+
("Widget", 1, decimal.Decimal("19.99")),
10008+
("Gadget", 2, decimal.Decimal("29.99")),
10009+
("Gizmo", 3, decimal.Decimal("0.01")),
10010+
],
10011+
)
1001210012

10013-
assert len(rows) == 3
10014-
assert rows[0][0] == "Widget"
10015-
assert rows[0][1] == 1
10016-
assert rows[0][2] == decimal.Decimal("19.99")
10017-
assert rows[1][0] == "Gadget"
10018-
assert rows[1][1] == 2
10019-
assert rows[1][2] == decimal.Decimal("29.99")
10020-
assert rows[2][0] == "Gizmo"
10021-
assert rows[2][1] == 3
10022-
assert rows[2][2] == decimal.Decimal("0.01")
10013+
cursor.execute("SELECT Name, CategoryID, Price FROM #test_sis_decimal ORDER BY CategoryID")
10014+
rows = cursor.fetchall()
1002310015

10024-
cursor.execute("DROP TABLE IF EXISTS #test_sis_decimal")
10016+
assert len(rows) == 3
10017+
assert rows[0][0] == "Widget"
10018+
assert rows[0][1] == 1
10019+
assert rows[0][2] == decimal.Decimal("19.99")
10020+
assert rows[1][0] == "Gadget"
10021+
assert rows[1][1] == 2
10022+
assert rows[1][2] == decimal.Decimal("29.99")
10023+
assert rows[2][0] == "Gizmo"
10024+
assert rows[2][1] == 3
10025+
assert rows[2][2] == decimal.Decimal("0.01")
10026+
finally:
10027+
cursor.execute("DROP TABLE IF EXISTS #test_sis_decimal")
1002510028

1002610029

1002710030
def test_setinputsizes_sql_numeric_with_executemany(db_connection):
1002810031
"""Test setinputsizes with SQL_NUMERIC accepts Python Decimal values (GH-503)."""
1002910032
cursor = db_connection.cursor()
1003010033

1003110034
cursor.execute("DROP TABLE IF EXISTS #test_sis_numeric")
10032-
cursor.execute("""
10033-
CREATE TABLE #test_sis_numeric (
10034-
Value NUMERIC(10,4)
10035-
)
10036-
""")
10037-
10038-
cursor.setinputsizes([
10039-
(mssql_python.SQL_NUMERIC, 10, 4),
10040-
])
10035+
try:
10036+
cursor.execute("""
10037+
CREATE TABLE #test_sis_numeric (
10038+
Value NUMERIC(10,4)
10039+
)
10040+
""")
1004110041

10042-
cursor.executemany(
10043-
"INSERT INTO #test_sis_numeric (Value) VALUES (?)",
10044-
[
10045-
(decimal.Decimal("123.4567"),),
10046-
(decimal.Decimal("-99.0001"),),
10047-
(decimal.Decimal("0.0000"),),
10048-
],
10049-
)
10042+
cursor.setinputsizes(
10043+
[
10044+
(mssql_python.SQL_NUMERIC, 10, 4),
10045+
]
10046+
)
1005010047

10051-
cursor.execute("SELECT Value FROM #test_sis_numeric ORDER BY Value")
10052-
rows = cursor.fetchall()
10048+
cursor.executemany(
10049+
"INSERT INTO #test_sis_numeric (Value) VALUES (?)",
10050+
[
10051+
(decimal.Decimal("123.4567"),),
10052+
(decimal.Decimal("-99.0001"),),
10053+
(decimal.Decimal("0.0000"),),
10054+
],
10055+
)
1005310056

10054-
assert len(rows) == 3
10055-
assert rows[0][0] == decimal.Decimal("-99.0001")
10056-
assert rows[1][0] == decimal.Decimal("0.0000")
10057-
assert rows[2][0] == decimal.Decimal("123.4567")
10057+
cursor.execute("SELECT Value FROM #test_sis_numeric ORDER BY Value")
10058+
rows = cursor.fetchall()
1005810059

10059-
cursor.execute("DROP TABLE IF EXISTS #test_sis_numeric")
10060+
assert len(rows) == 3
10061+
assert rows[0][0] == decimal.Decimal("-99.0001")
10062+
assert rows[1][0] == decimal.Decimal("0.0000")
10063+
assert rows[2][0] == decimal.Decimal("123.4567")
10064+
finally:
10065+
cursor.execute("DROP TABLE IF EXISTS #test_sis_numeric")
1006010066

1006110067

1006210068
def test_setinputsizes_sql_decimal_with_execute(db_connection):
1006310069
"""Test setinputsizes with SQL_DECIMAL works with single execute() too (GH-503)."""
1006410070
cursor = db_connection.cursor()
1006510071

1006610072
cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_exec")
10067-
cursor.execute("CREATE TABLE #test_sis_dec_exec (Price DECIMAL(18,2))")
10068-
10069-
cursor.setinputsizes([(mssql_python.SQL_DECIMAL, 18, 2)])
10070-
cursor.execute(
10071-
"INSERT INTO #test_sis_dec_exec (Price) VALUES (?)",
10072-
decimal.Decimal("99.95"),
10073-
)
10073+
try:
10074+
cursor.execute("CREATE TABLE #test_sis_dec_exec (Price DECIMAL(18,2))")
1007410075

10075-
cursor.execute("SELECT Price FROM #test_sis_dec_exec")
10076-
row = cursor.fetchone()
10077-
assert row[0] == decimal.Decimal("99.95")
10076+
cursor.setinputsizes([(mssql_python.SQL_DECIMAL, 18, 2)])
10077+
cursor.execute(
10078+
"INSERT INTO #test_sis_dec_exec (Price) VALUES (?)",
10079+
decimal.Decimal("99.95"),
10080+
)
1007810081

10079-
cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_exec")
10082+
cursor.execute("SELECT Price FROM #test_sis_dec_exec")
10083+
row = cursor.fetchone()
10084+
assert row[0] == decimal.Decimal("99.95")
10085+
finally:
10086+
cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_exec")
1008010087

1008110088

1008210089
def test_setinputsizes_sql_decimal_null(db_connection):
1008310090
"""Test setinputsizes with SQL_DECIMAL handles NULL values correctly (GH-503)."""
1008410091
cursor = db_connection.cursor()
1008510092

1008610093
cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_null")
10087-
cursor.execute("CREATE TABLE #test_sis_dec_null (Price DECIMAL(18,2))")
10088-
10089-
cursor.setinputsizes([(mssql_python.SQL_DECIMAL, 18, 2)])
10090-
cursor.execute(
10091-
"INSERT INTO #test_sis_dec_null (Price) VALUES (?)",
10092-
None,
10093-
)
10094+
try:
10095+
cursor.execute("CREATE TABLE #test_sis_dec_null (Price DECIMAL(18,2))")
1009410096

10095-
cursor.execute("SELECT Price FROM #test_sis_dec_null")
10096-
row = cursor.fetchone()
10097-
assert row[0] is None
10097+
cursor.setinputsizes([(mssql_python.SQL_DECIMAL, 18, 2)])
10098+
cursor.execute(
10099+
"INSERT INTO #test_sis_dec_null (Price) VALUES (?)",
10100+
None,
10101+
)
1009810102

10099-
cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_null")
10103+
cursor.execute("SELECT Price FROM #test_sis_dec_null")
10104+
row = cursor.fetchone()
10105+
assert row[0] is None
10106+
finally:
10107+
cursor.execute("DROP TABLE IF EXISTS #test_sis_dec_null")
1010010108

1010110109

1010210110
def test_cursor_setinputsizes_reset(db_connection):

0 commit comments

Comments
 (0)