Skip to content

Commit 0f672b9

Browse files
committed
working uuid
1 parent fba0e63 commit 0f672b9

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

mssql_python/cursor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@ def _map_sql_type(self, param, parameters_list, i, min_val=None, max_val=None):
465465
0,
466466
False
467467
)
468+
469+
if isinstance(param, uuid.UUID):
470+
return (
471+
ddbc_sql_const.SQL_GUID.value,
472+
ddbc_sql_const.SQL_C_GUID.value,
473+
16,
474+
0,
475+
False,
476+
)
468477

469478
if isinstance(param, datetime.datetime):
470479
if param.tzinfo is not None:
@@ -984,6 +993,8 @@ def execute(
984993

985994
if parameters:
986995
for i, param in enumerate(parameters):
996+
if isinstance(param, uuid.UUID):
997+
parameters[i] = param.bytes
987998
paraminfo = self._create_parameter_types_list(
988999
param, param_info, parameters, i
9891000
)

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ SQLRETURN SQLGetData_wrap(SqlHandlePtr StatementHandle, SQLUSMALLINT colCount, p
27132713
}
27142714
break;
27152715
}
2716-
#endif
2716+
#endif
27172717
default:
27182718
std::ostringstream errorString;
27192719
errorString << "Unsupported data type for column - " << columnName << ", Type - "

tests/test_004_cursor.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7146,6 +7146,62 @@ def test_extreme_uuids(cursor, db_connection):
71467146
cursor.execute(f"DROP TABLE IF EXISTS {table_name}")
71477147
db_connection.commit()
71487148

7149+
def test_executemany_uuid_insert_and_select(cursor, db_connection):
7150+
"""Test inserting multiple UUIDs using executemany and verifying retrieval."""
7151+
table_name = "#pytest_uuid_executemany"
7152+
7153+
try:
7154+
# Drop and create a temporary table for the test
7155+
cursor.execute(f"DROP TABLE IF EXISTS {table_name}")
7156+
cursor.execute(f"""
7157+
CREATE TABLE {table_name} (
7158+
id UNIQUEIDENTIFIER PRIMARY KEY,
7159+
description NVARCHAR(50)
7160+
)
7161+
""")
7162+
db_connection.commit()
7163+
7164+
# Generate data for insertion
7165+
data_to_insert = []
7166+
uuids_to_check = {}
7167+
for i in range(5):
7168+
new_uuid = uuid.uuid4()
7169+
description = f"Item {i}"
7170+
data_to_insert.append((new_uuid, description))
7171+
uuids_to_check[description] = new_uuid
7172+
7173+
# Insert all data with a single call to executemany
7174+
sql = f"INSERT INTO {table_name} (id, description) VALUES (?, ?)"
7175+
cursor.executemany(sql, data_to_insert)
7176+
db_connection.commit()
7177+
7178+
# Verify the number of rows inserted
7179+
assert cursor.rowcount == 5, f"Expected 5 rows inserted, but got {cursor.rowcount}"
7180+
7181+
# Fetch all data from the table
7182+
cursor.execute(f"SELECT id, description FROM {table_name}")
7183+
rows = cursor.fetchall()
7184+
7185+
# Verify the number of fetched rows
7186+
assert len(rows) == len(data_to_insert), "Number of fetched rows does not match."
7187+
7188+
# Verify each fetched row's data and type
7189+
for row in rows:
7190+
retrieved_uuid, retrieved_desc = row
7191+
7192+
# Assert the type is correct
7193+
assert isinstance(retrieved_uuid, uuid.UUID), f"Expected uuid.UUID, got {type(retrieved_uuid)}"
7194+
7195+
# Assert the value matches the original data
7196+
expected_uuid = uuids_to_check.get(retrieved_desc)
7197+
assert expected_uuid is not None, f"Retrieved description '{retrieved_desc}' was not in the original data."
7198+
assert retrieved_uuid == expected_uuid, f"UUID mismatch for '{retrieved_desc}': expected {expected_uuid}, got {retrieved_uuid}"
7199+
7200+
finally:
7201+
# Clean up the temporary table
7202+
cursor.execute(f"DROP TABLE IF EXISTS {table_name}")
7203+
db_connection.commit()
7204+
71497205
def test_decimal_separator_with_multiple_values(cursor, db_connection):
71507206
"""Test decimal separator with multiple different decimal values"""
71517207
original_separator = mssql_python.getDecimalSeparator()
@@ -10560,6 +10616,7 @@ def test_decimal_separator_calculations(cursor, db_connection):
1056010616

1056110617
# Cleanup
1056210618
cursor.execute("DROP TABLE IF EXISTS #pytest_decimal_calc_test")
10619+
db_connection.commit()
1056310620

1056410621
def test_close(db_connection):
1056510622
"""Test closing the cursor"""

0 commit comments

Comments
 (0)