Skip to content

Commit c14cc42

Browse files
authored
FEAT: Adding setinputsizes (#192)
### Work Item / Issue Reference <!-- IMPORTANT: Please follow the PR template guidelines below. For mssql-python maintainers: Insert your ADO Work Item ID below (e.g. AB#37452) For external contributors: Insert Github Issue number below (e.g. #149) Only one reference is required - either GitHub issue OR ADO Work Item. --> <!-- mssql-python maintainers: ADO Work Item --> > [AB#32890](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/32890) ------------------------------------------------------------------- ### Summary This pull request adds comprehensive support for advanced SQL metadata and catalog functions to the MSSQL Python bindings, making it easier to interact with database schema information and supported data types from Python. The changes include new wrapper functions and Python bindings for several ODBC catalog APIs, the addition of SQL type constants, and enhancements to the constants module for easier type validation and categorization. ### SQL Catalog & Metadata API Support * Added wrapper functions and Python bindings for ODBC catalog APIs: `SQLGetTypeInfo`, `SQLProcedures`, `SQLForeignKeys`, `SQLPrimaryKeys`, `SQLSpecialColumns`, `SQLStatistics`, and `SQLColumns` in `ddbc_bindings.cpp`. These allow Python code to directly query metadata about tables, columns, keys, procedures, and supported types. ### Constants and Type Handling * Exported SQL type constants (e.g., `SQL_CHAR`, `SQL_INTEGER`, etc.) at the module level in `__init__.py` for easier use in Python code. * Added new constants to `ConstantsDDBC` for catalog API parameters such as `SQL_SCOPE_CURROW`, `SQL_BEST_ROWID`, `SQL_ROWVER`, and others. * Introduced the `SQLTypes` class in `constants.py`, providing methods to retrieve sets of valid SQL types and categorize them (e.g., string types, numeric types), which can help with input validation and type checking in Python. These changes significantly improve the ability of Python applications to introspect and work with database schema and type information, making your code more robust and flexible when dealing with SQL Server databases. --------- Co-authored-by: Jahnvi Thakkar <jathakkar@microsoft.com>
1 parent d521601 commit c14cc42

File tree

8 files changed

+4403
-194
lines changed

8 files changed

+4403
-194
lines changed

mssql_python/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,28 @@ def _custom_setattr(name, value):
180180

181181
# Replace the module's __setattr__ with our custom version
182182
sys.modules[__name__].__setattr__ = _custom_setattr
183+
184+
185+
# Export SQL constants at module level
186+
SQL_CHAR = ConstantsDDBC.SQL_CHAR.value
187+
SQL_VARCHAR = ConstantsDDBC.SQL_VARCHAR.value
188+
SQL_LONGVARCHAR = ConstantsDDBC.SQL_LONGVARCHAR.value
189+
SQL_WCHAR = ConstantsDDBC.SQL_WCHAR.value
190+
SQL_WVARCHAR = ConstantsDDBC.SQL_WVARCHAR.value
191+
SQL_WLONGVARCHAR = ConstantsDDBC.SQL_WLONGVARCHAR.value
192+
SQL_DECIMAL = ConstantsDDBC.SQL_DECIMAL.value
193+
SQL_NUMERIC = ConstantsDDBC.SQL_NUMERIC.value
194+
SQL_BIT = ConstantsDDBC.SQL_BIT.value
195+
SQL_TINYINT = ConstantsDDBC.SQL_TINYINT.value
196+
SQL_SMALLINT = ConstantsDDBC.SQL_SMALLINT.value
197+
SQL_INTEGER = ConstantsDDBC.SQL_INTEGER.value
198+
SQL_BIGINT = ConstantsDDBC.SQL_BIGINT.value
199+
SQL_REAL = ConstantsDDBC.SQL_REAL.value
200+
SQL_FLOAT = ConstantsDDBC.SQL_FLOAT.value
201+
SQL_DOUBLE = ConstantsDDBC.SQL_DOUBLE.value
202+
SQL_BINARY = ConstantsDDBC.SQL_BINARY.value
203+
SQL_VARBINARY = ConstantsDDBC.SQL_VARBINARY.value
204+
SQL_LONGVARBINARY = ConstantsDDBC.SQL_LONGVARBINARY.value
205+
SQL_DATE = ConstantsDDBC.SQL_DATE.value
206+
SQL_TIME = ConstantsDDBC.SQL_TIME.value
207+
SQL_TIMESTAMP = ConstantsDDBC.SQL_TIMESTAMP.value

mssql_python/constants.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,63 @@ class ConstantsDDBC(Enum):
124124
SQL_FETCH_ABSOLUTE = 5
125125
SQL_FETCH_RELATIVE = 6
126126
SQL_FETCH_BOOKMARK = 8
127+
SQL_SCOPE_CURROW = 0
128+
SQL_BEST_ROWID = 1
129+
SQL_ROWVER = 2
130+
SQL_NO_NULLS = 0
131+
SQL_NULLABLE_UNKNOWN = 2
132+
SQL_INDEX_UNIQUE = 0
133+
SQL_INDEX_ALL = 1
134+
SQL_QUICK = 0
135+
SQL_ENSURE = 1
127136

128137
class AuthType(Enum):
129138
"""Constants for authentication types"""
130139
INTERACTIVE = "activedirectoryinteractive"
131140
DEVICE_CODE = "activedirectorydevicecode"
132-
DEFAULT = "activedirectorydefault"
141+
DEFAULT = "activedirectorydefault"
142+
143+
class SQLTypes:
144+
"""Constants for valid SQL data types to use with setinputsizes"""
145+
146+
@classmethod
147+
def get_valid_types(cls) -> set:
148+
"""Returns a set of all valid SQL type constants"""
149+
150+
return {
151+
ConstantsDDBC.SQL_CHAR.value, ConstantsDDBC.SQL_VARCHAR.value,
152+
ConstantsDDBC.SQL_LONGVARCHAR.value, ConstantsDDBC.SQL_WCHAR.value,
153+
ConstantsDDBC.SQL_WVARCHAR.value, ConstantsDDBC.SQL_WLONGVARCHAR.value,
154+
ConstantsDDBC.SQL_DECIMAL.value, ConstantsDDBC.SQL_NUMERIC.value,
155+
ConstantsDDBC.SQL_BIT.value, ConstantsDDBC.SQL_TINYINT.value,
156+
ConstantsDDBC.SQL_SMALLINT.value, ConstantsDDBC.SQL_INTEGER.value,
157+
ConstantsDDBC.SQL_BIGINT.value, ConstantsDDBC.SQL_REAL.value,
158+
ConstantsDDBC.SQL_FLOAT.value, ConstantsDDBC.SQL_DOUBLE.value,
159+
ConstantsDDBC.SQL_BINARY.value, ConstantsDDBC.SQL_VARBINARY.value,
160+
ConstantsDDBC.SQL_LONGVARBINARY.value, ConstantsDDBC.SQL_DATE.value,
161+
ConstantsDDBC.SQL_TIME.value, ConstantsDDBC.SQL_TIMESTAMP.value,
162+
ConstantsDDBC.SQL_GUID.value
163+
}
164+
165+
# Could also add category methods for convenience
166+
@classmethod
167+
def get_string_types(cls) -> set:
168+
"""Returns a set of string SQL type constants"""
169+
170+
return {
171+
ConstantsDDBC.SQL_CHAR.value, ConstantsDDBC.SQL_VARCHAR.value,
172+
ConstantsDDBC.SQL_LONGVARCHAR.value, ConstantsDDBC.SQL_WCHAR.value,
173+
ConstantsDDBC.SQL_WVARCHAR.value, ConstantsDDBC.SQL_WLONGVARCHAR.value
174+
}
175+
176+
@classmethod
177+
def get_numeric_types(cls) -> set:
178+
"""Returns a set of numeric SQL type constants"""
179+
180+
return {
181+
ConstantsDDBC.SQL_DECIMAL.value, ConstantsDDBC.SQL_NUMERIC.value,
182+
ConstantsDDBC.SQL_BIT.value, ConstantsDDBC.SQL_TINYINT.value,
183+
ConstantsDDBC.SQL_SMALLINT.value, ConstantsDDBC.SQL_INTEGER.value,
184+
ConstantsDDBC.SQL_BIGINT.value, ConstantsDDBC.SQL_REAL.value,
185+
ConstantsDDBC.SQL_FLOAT.value, ConstantsDDBC.SQL_DOUBLE.value
186+
}

0 commit comments

Comments
 (0)