pymssql Adapter¶
from sqlspec.adapters.pymssql import (
PymssqlConfig,
PymssqlConnectionParams,
PymssqlConnectionPool,
PymssqlDriver,
PymssqlDriverFeatures,
PymssqlExceptionHandler,
PymssqlPoolParams,
PymssqlSessionContextStrategy,
)
Synchronous Microsoft SQL Server adapter built on pymssql (FreeTDS). Inherited execution methods are documented on SyncDriverAdapterBase. See the SQL Server Setup Guide for usage examples and the SQL Server Data Dictionary & SQL Catalog.
Configuration & Parameters¶
PymssqlConfig
¶
Bases: SyncDatabaseConfig[PymssqlConnection, PymssqlConnectionPool, PymssqlDriver]
Configuration for pymssql synchronous connections.
PymssqlConnectionParams
¶
Bases: TypedDict
pymssql connection parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
server |
NotRequired[str]
|
SQL Server hostname, |
host |
NotRequired[str]
|
Alias for |
user |
NotRequired[str]
|
SQL Server or Windows domain login ( |
password |
NotRequired[str]
|
Password for |
db |
NotRequired[str]
|
Alias for the target database. |
username |
NotRequired[str]
|
Alias for the authentication user. |
database |
NotRequired[str]
|
Initial SQL Server database selected on connect. |
port |
NotRequired[int | str]
|
SQL Server TCP port (defaults to |
timeout |
NotRequired[PymssqlTimeout]
|
Query response timeout in seconds ( |
login_timeout |
NotRequired[PymssqlTimeout]
|
Connection and authentication timeout in seconds (defaults to |
charset |
NotRequired[str]
|
Character encoding communicated over FreeTDS (defaults to |
as_dict |
NotRequired[bool]
|
Return rows from raw |
appname |
NotRequired[str]
|
Application name visible in |
conn_properties |
NotRequired[str]
|
Semicolon-separated |
autocommit |
NotRequired[bool]
|
Enable autocommit mode on new |
tds_version |
NotRequired[str]
|
Explicit FreeTDS protocol version (such as |
encryption |
NotRequired[Literal['off', 'request', 'require']]
|
FreeTDS TLS transport encryption requirement ( |
use_datetime2 |
NotRequired[bool]
|
Bind Python |
arraysize |
NotRequired[int]
|
Default row batch count returned by |
conv |
NotRequired[Mapping[int | type[Any], Callable[..., Any]]]
|
Custom FreeTDS type serialization/deserialization mapping. |
read_only |
NotRequired[bool]
|
Set Always On |
pool_recycle_seconds |
NotRequired[int]
|
Maximum connection age in seconds before |
health_check_interval |
NotRequired[float]
|
Minimum interval in seconds between connection health checks in |
extra |
NotRequired[dict[str, Any]]
|
Additional keyword arguments forwarded to |
PymssqlPoolParams
¶
PymssqlDriverFeatures
¶
Bases: TypedDict
Optional pymssql driver feature flags for JSON serialization, connection hooks, and events.
Attributes:
| Name | Type | Description |
|---|---|---|
tenant_strategy |
NotRequired[SyncTenantStrategy | TenantStrategyShorthand | None]
|
Opt-in tenant strategy, or the |
json_serializer |
NotRequired[Callable[[Any], str]]
|
Custom JSON serializer applied to bound JSON parameters (defaults to |
json_deserializer |
NotRequired[Callable[[str], Any]]
|
Custom JSON deserializer applied to JSON result columns (defaults to |
on_connection_create |
NotRequired[Callable[[PymssqlConnection], None]]
|
Synchronous hook invoked with each raw |
enable_events |
NotRequired[bool]
|
Enable durable table-backed SQLSpec event queue support. |
events_backend |
NotRequired[Literal['poll_queue']]
|
Event channel backend ( |
enable_pooling |
NotRequired[bool]
|
Enable driver-level connection pooling (defaults to |
enable_uuid_conversion |
NotRequired[bool]
|
Enable automatic UUID string-to-object conversions. |
Tenant Strategies¶
Set driver_features={"tenant_strategy": "rls"}; "session_context", "default", and True select the same strategy. Both providers use one standalone connection per lease rather than the normal thread-local connection. Cleanup or setup failure physically closes that connection. Standalone create_connection() is rejected in tenant mode.
Each boundary uses one bound SQL batch: capture/apply/verify at checkout, restore/verify at checkin. Owned keys remain mutable (@read_only=0). The strategy neither commits nor rolls back nor changes autocommit. The lease rolls back unfinished work before cleanup and closes its standalone connection after release.
Missing tenant identity raises TenantContextError before acquisition. Failed cleanup raises TenantCleanupError when no body exception is pending; otherwise the body error keeps its identity and receives a cleanup note. See Multi-Tenancy and Tenant Operations.
PymssqlSessionContextStrategy
¶
Bases: MssqlTenantSettings
Apply and restore owned mutable keys with one verified SQL batch per boundary.
Driver, Pool & Data Dictionary¶
PymssqlDriver
¶
PymssqlExceptionHandler
¶
PymssqlConnectionPool
¶
Thread-local connection manager for pymssql.
PymssqlSyncDataDictionary
¶
Extension Stores & Configs¶
PymssqlStore
¶
Bases: BaseSQLSpecStore['PymssqlConfig']
SQL Server-backed session store using pymssql sync sessions.
PymssqlADKConfig
¶
Bases: ADKConfig
pymssql ADK extension settings.
Attributes:
| Name | Type | Description |
|---|---|---|
native_json |
NotRequired[bool]
|
Force native SQL Server JSON columns when True, or NVARCHAR(MAX) when False. |