Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 53
FIX: Align date/time type code mappings with ODBC 18 driver source (#352)#355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -844,7 +844,7 @@ def _reset_inputsizes(self) -> None: | ||
| self._inputsizes = None | ||
| def _get_c_type_for_sql_type(self, sql_type: int) -> int: | ||
| """Map SQL type to appropriate C type for parameter binding""" | ||
| """Map SQL type to appropriate C type for parameter binding.""" | ||
| sql_to_c_type = { | ||
| ddbc_sql_const.SQL_CHAR.value: ddbc_sql_const.SQL_C_CHAR.value, | ||
| ddbc_sql_const.SQL_VARCHAR.value: ddbc_sql_const.SQL_C_CHAR.value, | ||
| @@ -865,9 +865,19 @@ def _get_c_type_for_sql_type(self, sql_type: int) -> int: | ||
| ddbc_sql_const.SQL_BINARY.value: ddbc_sql_const.SQL_C_BINARY.value, | ||
| ddbc_sql_const.SQL_VARBINARY.value: ddbc_sql_const.SQL_C_BINARY.value, | ||
| ddbc_sql_const.SQL_LONGVARBINARY.value: ddbc_sql_const.SQL_C_BINARY.value, | ||
| # ODBC 3.x date/time types (reported by ODBC 18 driver) | ||
| ddbc_sql_const.SQL_TYPE_DATE.value: ddbc_sql_const.SQL_C_TYPE_DATE.value, | ||
| ddbc_sql_const.SQL_TYPE_TIME.value: ddbc_sql_const.SQL_C_TYPE_TIME.value, | ||
| ddbc_sql_const.SQL_TYPE_TIMESTAMP.value: ddbc_sql_const.SQL_C_TYPE_TIMESTAMP.value, | ||
dlevy-msft-sql marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ddbc_sql_const.SQL_SS_TIME2.value: ddbc_sql_const.SQL_C_TYPE_TIME.value, | ||
| ddbc_sql_const.SQL_DATETIMEOFFSET.value: ddbc_sql_const.SQL_C_SS_TIMESTAMPOFFSET.value, | ||
dlevy-msft-sql marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # ODBC 2.x aliases (accepted by setinputsizes via SQLTypes) | ||
| ddbc_sql_const.SQL_DATE.value: ddbc_sql_const.SQL_C_TYPE_DATE.value, | ||
| ddbc_sql_const.SQL_TIME.value: ddbc_sql_const.SQL_C_TYPE_TIME.value, | ||
| ddbc_sql_const.SQL_TIMESTAMP.value: ddbc_sql_const.SQL_C_TYPE_TIMESTAMP.value, | ||
| # Other types | ||
| ddbc_sql_const.SQL_GUID.value: ddbc_sql_const.SQL_C_GUID.value, | ||
| ddbc_sql_const.SQL_SS_XML.value: ddbc_sql_const.SQL_C_WCHAR.value, | ||
| } | ||
| return sql_to_c_type.get(sql_type, ddbc_sql_const.SQL_C_DEFAULT.value) | ||
| @@ -1026,34 +1036,71 @@ def _map_data_type(self, sql_type): | ||
| """ | ||
| Map SQL data type to Python data type. | ||
| Maps the ODBC SQL type code returned by SQLDescribeCol to the | ||
| corresponding Python type for cursor.description[i][1]. | ||
| The ODBC 18 driver for SQL Server reports these type codes: | ||
| Standard ODBC 3.x types: | ||
| SQL_CHAR(1), SQL_VARCHAR(12), SQL_LONGVARCHAR(-1), | ||
| SQL_WCHAR(-8), SQL_WVARCHAR(-9), SQL_WLONGVARCHAR(-10), | ||
| SQL_INTEGER(4), SQL_SMALLINT(5), SQL_TINYINT(-6), SQL_BIGINT(-5), | ||
| SQL_BIT(-7), SQL_FLOAT(6), SQL_REAL(7), SQL_DOUBLE(8), | ||
| SQL_DECIMAL(3), SQL_NUMERIC(2), | ||
| SQL_BINARY(-2), SQL_VARBINARY(-3), SQL_LONGVARBINARY(-4), | ||
| SQL_TYPE_DATE(91), SQL_TYPE_TIME(92), SQL_TYPE_TIMESTAMP(93), SQL_GUID(-11) | ||
| SQL Server-specific types (from msodbcsql.h): | ||
| SQL_SS_TIME2(-154) for time columns | ||
| SQL_DATETIMEOFFSET(-155) for datetimeoffset columns | ||
| SQL_SS_XML(-152) for xml columns | ||
| ODBC 2.x aliases (9, 10, 11) are also accepted defensively. | ||
| Args: | ||
| sql_type: SQL data type. | ||
| sql_type: SQL data type code from SQLDescribeCol. | ||
| Returns: | ||
| Corresponding Python data type. | ||
| """ | ||
| sql_to_python_type = { | ||
| ddbc_sql_const.SQL_INTEGER.value: int, | ||
| ddbc_sql_const.SQL_VARCHAR.value: str, | ||
| ddbc_sql_const.SQL_WVARCHAR.value: str, | ||
| # String types | ||
| ddbc_sql_const.SQL_CHAR.value: str, | ||
| ddbc_sql_const.SQL_VARCHAR.value: str, | ||
| ddbc_sql_const.SQL_LONGVARCHAR.value: str, | ||
| ddbc_sql_const.SQL_WCHAR.value: str, | ||
| ddbc_sql_const.SQL_WVARCHAR.value: str, | ||
| ddbc_sql_const.SQL_WLONGVARCHAR.value: str, | ||
| # Integer types | ||
| ddbc_sql_const.SQL_INTEGER.value: int, | ||
| ddbc_sql_const.SQL_SMALLINT.value: int, | ||
| ddbc_sql_const.SQL_TINYINT.value: int, | ||
| ddbc_sql_const.SQL_BIGINT.value: int, | ||
| # Floating-point types | ||
| ddbc_sql_const.SQL_FLOAT.value: float, | ||
| ddbc_sql_const.SQL_DOUBLE.value: float, | ||
| ddbc_sql_const.SQL_REAL.value: float, | ||
| # Exact numeric types | ||
| ddbc_sql_const.SQL_DECIMAL.value: decimal.Decimal, | ||
dlevy-msft-sql marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ddbc_sql_const.SQL_NUMERIC.value: decimal.Decimal, | ||
| ddbc_sql_const.SQL_DATE.value: datetime.date, | ||
| ddbc_sql_const.SQL_TIMESTAMP.value: datetime.datetime, | ||
| ddbc_sql_const.SQL_TIME.value: datetime.time, | ||
| # Date/time types — values the ODBC 18 driver actually reports | ||
| ddbc_sql_const.SQL_TYPE_DATE.value: datetime.date, # 91 — date | ||
| ddbc_sql_const.SQL_TYPE_TIME.value: datetime.time, # 92 — time (ODBC 3.x) | ||
| ddbc_sql_const.SQL_TYPE_TIMESTAMP.value: datetime.datetime, # 93 — datetime/datetime2/smalldatetime | ||
| ddbc_sql_const.SQL_SS_TIME2.value: datetime.time, # -154 — time | ||
| ddbc_sql_const.SQL_DATETIMEOFFSET.value: datetime.datetime, # -155 — datetimeoffset | ||
| # ODBC 2.x date/time aliases (defensive, in case any driver reports these) | ||
| ddbc_sql_const.SQL_DATE.value: datetime.date, # 9 | ||
| ddbc_sql_const.SQL_TIME.value: datetime.time, # 10 | ||
| ddbc_sql_const.SQL_TIMESTAMP.value: datetime.datetime, # 11 | ||
| # Boolean | ||
| ddbc_sql_const.SQL_BIT.value: bool, | ||
| ddbc_sql_const.SQL_TINYINT.value: int, | ||
| ddbc_sql_const.SQL_SMALLINT.value: int, | ||
| ddbc_sql_const.SQL_BIGINT.value: int, | ||
| # Binary types | ||
| ddbc_sql_const.SQL_BINARY.value: bytes, | ||
| ddbc_sql_const.SQL_VARBINARY.value: bytes, | ||
| ddbc_sql_const.SQL_LONGVARBINARY.value: bytes, | ||
| # UUID | ||
| ddbc_sql_const.SQL_GUID.value: uuid.UUID, | ||
| # Add more mappings as needed | ||
| # XML — driver reports SQL_SS_XML (-152), fetched as str | ||
| ddbc_sql_const.SQL_SS_XML.value: str, | ||
| } | ||
| return sql_to_python_type.get(sql_type, str) | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.