Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions mssql_python/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def _map_sql_type(self, param, parameters_list, i, min_val=None, max_val=None):
0,
False,
)

try:
val = uuid.UUID(param)
parameters_list[i] = val.bytes_le
Expand All @@ -376,38 +376,6 @@ def _map_sql_type(self, param, parameters_list, i, min_val=None, max_val=None):
except ValueError:
pass


# Attempt to parse as date, datetime, datetime2, timestamp, smalldatetime or time
if self._parse_date(param):
parameters_list[i] = self._parse_date(
param
) # Replace the parameter with the date object
return (
ddbc_sql_const.SQL_DATE.value,
ddbc_sql_const.SQL_C_TYPE_DATE.value,
10,
0,
False,
)
if self._parse_datetime(param):
parameters_list[i] = self._parse_datetime(param)
return (
ddbc_sql_const.SQL_TIMESTAMP.value,
ddbc_sql_const.SQL_C_TYPE_TIMESTAMP.value,
26,
6,
False,
)
if self._parse_time(param):
parameters_list[i] = self._parse_time(param)
return (
ddbc_sql_const.SQL_TIME.value,
ddbc_sql_const.SQL_C_TYPE_TIME.value,
8,
0,
False,
)

# String mapping logic here
is_unicode = self._is_unicode_string(param)

Expand Down
83 changes: 83 additions & 0 deletions tests/test_004_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10661,6 +10661,89 @@ def test_decimal_separator_calculations(cursor, db_connection):
# Cleanup
cursor.execute("DROP TABLE IF EXISTS #pytest_decimal_calc_test")

def test_date_string_parameter_binding(cursor, db_connection):
"""Verify that date-like strings are treated as strings in parameter binding"""
table_name = "#pytest_date_string"
try:
drop_table_if_exists(cursor, table_name)
cursor.execute(f"""
CREATE TABLE {table_name} (
a_column VARCHAR(20)
)
""")
cursor.execute(f"INSERT INTO {table_name} (a_column) VALUES ('string1'), ('string2')")
db_connection.commit()

date_str = "2025-08-12"

# Should fail to match anything, since binding may treat it as DATE not VARCHAR
cursor.execute(f"SELECT a_column FROM {table_name} WHERE RIGHT(a_column, 10) = ?", (date_str,))
rows = cursor.fetchall()

assert rows == [], f"Expected no match for date-like string, got {rows}"

except Exception as e:
pytest.fail(f"Date string parameter binding test failed: {e}")
finally:
drop_table_if_exists(cursor, table_name)
db_connection.commit()

def test_time_string_parameter_binding(cursor, db_connection):
"""Verify that time-like strings are treated as strings in parameter binding"""
table_name = "#pytest_time_string"
try:
drop_table_if_exists(cursor, table_name)
cursor.execute(f"""
CREATE TABLE {table_name} (
time_col VARCHAR(22)
)
""")
cursor.execute(f"INSERT INTO {table_name} (time_col) VALUES ('prefix_14:30:45_suffix')")
db_connection.commit()

time_str = "14:30:45"

# This should fail because '14:30:45' gets converted to TIME type
# and SQL Server can't compare TIME against VARCHAR with prefix/suffix
cursor.execute(f"SELECT time_col FROM {table_name} WHERE time_col = ?", (time_str,))
rows = cursor.fetchall()

assert rows == [], f"Expected no match for time-like string, got {rows}"

except Exception as e:
pytest.fail(f"Time string parameter binding test failed: {e}")
finally:
drop_table_if_exists(cursor, table_name)
db_connection.commit()

def test_datetime_string_parameter_binding(cursor, db_connection):
"""Verify that datetime-like strings are treated as strings in parameter binding"""
table_name = "#pytest_datetime_string"
try:
drop_table_if_exists(cursor, table_name)
cursor.execute(f"""
CREATE TABLE {table_name} (
datetime_col VARCHAR(33)
)
""")
cursor.execute(f"INSERT INTO {table_name} (datetime_col) VALUES ('prefix_2025-08-12T14:30:45_suffix')")
db_connection.commit()

datetime_str = "2025-08-12T14:30:45"

# This should fail because '2025-08-12T14:30:45' gets converted to TIMESTAMP type
# and SQL Server can't compare TIMESTAMP against VARCHAR with prefix/suffix
cursor.execute(f"SELECT datetime_col FROM {table_name} WHERE datetime_col = ?", (datetime_str,))
rows = cursor.fetchall()

assert rows == [], f"Expected no match for datetime-like string, got {rows}"

except Exception as e:
pytest.fail(f"Datetime string parameter binding test failed: {e}")
finally:
drop_table_if_exists(cursor, table_name)
db_connection.commit()

def test_close(db_connection):
"""Test closing the cursor"""
try:
Expand Down
Loading