pymssql¶
Sync SQL Server adapter using pymssql
and FreeTDS. It uses pyformat parameters (%s and %(name)s) and exposes
sync SQLSpec config, driver, pooling, data dictionary, migration, and extension
store integrations.
Configuration¶
- class sqlspec.adapters.pymssql.PymssqlConfig[source]¶
Bases:
SyncDatabaseConfig[Connection,PymssqlConnectionPool,PymssqlDriver]Configuration for pymssql synchronous connections.
- driver_type¶
alias of
PymssqlDriver
- connection_type¶
alias of
Connection
- migration_tracker_type¶
alias of
PymssqlSyncMigrationTracker
- __init__(*, connection_config=None, connection_instance=None, migration_config=None, statement_config=None, driver_features=None, bind_key=None, extension_config=None, observability_config=None, **kwargs)[source]¶
- get_signature_namespace()[source]¶
Get the signature namespace for this database configuration.
Returns a dictionary of type names to objects (classes, functions, or other callables) that should be registered with Litestar's signature namespace to prevent serialization attempts on database-specific structures.
Connection Parameters¶
Driver Features¶
- class sqlspec.adapters.pymssql.config.PymssqlDriverFeatures[source]¶
Bases:
TypedDictpymssql driver feature flags.
- json_serializer: Custom JSON serializer function.
Defaults to sqlspec.utils.serializers.to_json.
- json_deserializer: Custom JSON deserializer function.
Defaults to sqlspec.utils.serializers.from_json.
- on_connection_create: Callback executed when a connection is created.
Receives the raw pymssql connection for low-level driver configuration. Runs after connection creation.
enable_events: Enable database event channel support. events_backend: Event channel backend selection.
Driver¶
- class sqlspec.adapters.pymssql.PymssqlDriver[source]¶
Bases:
SyncDriverAdapterBaseSQL Server database driver using pymssql.
- __init__(connection, statement_config=None, driver_features=None)[source]¶
Initialize driver adapter with connection and configuration.
- Parameters:
connection¶ (
Connection) -- Database connection instancestatement_config¶ (
StatementConfig|None) -- Statement configuration for the driverdriver_features¶ (
dict[str, typing.Any] |None) -- Driver-specific features like extensions, secrets, and connection callbacksobservability¶ -- Optional runtime handling lifecycle hooks, observers, and spans
- dispatch_execute(cursor, statement)[source]¶
Execute a single SQL statement.
Must be implemented by each driver for database-specific execution logic.
- dispatch_execute_many(cursor, statement)[source]¶
Execute SQL with multiple parameter sets (executemany).
Must be implemented by each driver for database-specific executemany logic.
- dispatch_execute_script(cursor, statement)[source]¶
Execute a SQL script containing multiple statements.
Default implementation splits the script and executes statements individually. Drivers can override for database-specific script execution methods.
- with_cursor(connection)[source]¶
Create and return a context manager for cursor acquisition and cleanup.
Returns a context manager that yields a cursor for database operations. Concrete implementations handle database-specific cursor creation and cleanup.
- Return type:
PymssqlCursor
- handle_database_exceptions()[source]¶
Handle database-specific exceptions and wrap them appropriately.
- Return type:
PymssqlExceptionHandler- Returns:
Exception handler with deferred exception pattern for mypyc compatibility. The handler stores mapped exceptions in pending_exception rather than raising from __exit__ to avoid ABI boundary violations.
- property data_dictionary: PymssqlSyncDataDictionary¶
Get the data dictionary for this driver.
- Returns:
Data dictionary instance for metadata queries
- collect_rows(cursor, fetched)[source]¶
Collect rows from cursor after fetchall for the direct execution path.
Adapters should override this method to provide optimized row collection that bypasses full dispatch_execute overhead.
- Parameters:
- Return type:
- Returns:
Tuple of (data, column_names, row_count).
- Raises:
NotImplementedError -- If the adapter does not implement this method.
- resolve_rowcount(cursor)[source]¶
Resolve the number of affected rows from cursor for the direct execution path.
Adapters should override this method to provide optimized rowcount resolution that bypasses full dispatch_execute overhead.
- Parameters:
cursor¶ (
Cursor) -- Database cursor with rowcount metadata.- Return type:
- Returns:
Number of affected rows, or 0 when unknown.
- Raises:
NotImplementedError -- If the adapter does not implement this method.
Connection Pool¶
Data Dictionary¶
- class sqlspec.adapters.pymssql.data_dictionary.PymssqlSyncDataDictionary[source]¶
Bases:
_MssqlDataDictionaryMixin,SyncDataDictionaryBaseMSSQL sync data dictionary.
- dialect: ClassVar[str] = 'mssql'¶
Dialect identifier. Must be defined by subclasses as a class attribute.
- get_version(driver)[source]¶
Get SQL Server version information.
- Return type:
MssqlVersionInfo|None
- get_feature_flag(driver, feature)[source]¶
Check whether SQL Server supports a feature.
- Return type:
- get_optimal_type(driver, type_category)[source]¶
Get optimal SQL Server type for a category.
- Return type:
- get_tables(driver, schema=None)[source]¶
Get tables sorted by dependency order with catalog fallback.
- Return type:
list[TableMetadata]
- get_columns(driver, table=None, schema=None)[source]¶
Get column information for a table or schema.
- Return type:
list[ColumnMetadata]
Migrations¶
- class sqlspec.adapters.pymssql.migrations.PymssqlSyncMigrationTracker[source]¶
Bases:
PymssqlMigrationTrackerMixin,SyncMigrationTrackerT-SQL sync migration tracker.