Skip to content

sqlanywhere Adapter

from sqlspec.adapters.sqlanywhere import (
    SqlanywhereConfig,
    SqlanywhereConnectionParams,
    SqlanywhereConnectionPool,
    SqlanywhereDriver,
    SqlanywhereDriverFeatures,
    SqlanywhereExceptionHandler,
    SqlanywherePoolParams,
    SqlanywhereSyncDataDictionary,
    SqlanywhereSyncMigrationTracker,
)

Synchronous SAP SQL Anywhere 12+ adapter built on sqlanydb (dbcapi C library). Inherited execution methods are documented on SyncDriverAdapterBase. See the SAP SQL Anywhere Setup Guide for engine connection parameters and pooling, and the SQL Anywhere Data Dictionary & SQL Catalog for system catalog introspection queries.

Configuration & Parameters

SqlanywhereConfig

SqlanywhereConfig(
    *,
    connection_config: SqlanywherePoolParams | dict[str, Any] | None = None,
    connection_instance: SqlanywhereConnectionPool | None = None,
    migration_config: dict[str, Any] | None = None,
    statement_config: StatementConfig | None = None,
    driver_features: SqlanywhereDriverFeatures | dict[str, Any] | None = None,
    bind_key: str | None = None,
    extension_config: ExtensionConfigs | None = None,
    observability_config: ObservabilityConfig | None = None,
    **kwargs: Any,
)

Bases: SyncDatabaseConfig[SqlanywhereConnection, SqlanywhereConnectionPool, SqlanywhereDriver]

Configuration for SAP SQL Anywhere synchronous connections over sqlanydb.

Methods:

Name Description
create_connection

Open a standalone SAP SQL Anywhere connection owned by the caller.

get_signature_namespace
get_event_runtime_hints

Attributes:

Name Type Description
driver_type type[SqlanywhereDriver]
connection_type type[SqlanywhereConnection]
migration_tracker_type type[SqlanywhereSyncMigrationTracker]
supports_transactional_ddl bool
supports_migration_schemas bool
supports_native_arrow_export bool
supports_native_arrow_import bool
supports_native_parquet_export bool
supports_native_parquet_import bool
supports_native_row_streaming bool
type_coercion_capabilities TypeCoercionCapabilities

create_connection

create_connection() -> SqlanywhereConnection

Open a standalone SAP SQL Anywhere connection owned by the caller.

get_signature_namespace

get_signature_namespace() -> dict[str, Any]

get_event_runtime_hints

get_event_runtime_hints() -> EventRuntimeHints

SqlanywhereConnectionParams

Bases: TypedDict

SAP SQL Anywhere connection parameters.

Attributes:

Name Type Description
uid NotRequired[str]

User ID / login name for authentication.

user NotRequired[str]

Alias for uid.

username NotRequired[str]

Alias for uid.

pwd NotRequired[str]

Password for authentication.

password NotRequired[str]

Alias for pwd.

dbn NotRequired[str]

Database name on the SQL Anywhere server.

database NotRequired[str]

Alias for dbn.

db NotRequired[str]

Alias for dbn.

eng NotRequired[str]

SQL Anywhere database engine name.

server NotRequired[str]

Alias for eng.

engine NotRequired[str]

Alias for eng.

server_name NotRequired[str]

Alias for eng.

dbf NotRequired[str]

Database file path for local or embedded engines.

database_file NotRequired[str]

Alias for dbf.

host NotRequired[str]

TCP hostname or IP address of the server.

port NotRequired[int | str]

TCP port number of the server.

dsn NotRequired[str]

ODBC data source name.

links NotRequired[str]

Network communications links protocol configuration.

cset NotRequired[str]

Character set used for character data conversion.

extra NotRequired[dict[str, Any]]

Additional connection parameters passed to the driver.

SqlanywherePoolParams

Bases: SqlanywhereConnectionParams

SAP SQL Anywhere pool parameters.

Attributes:

Name Type Description
pool_min_size NotRequired[int]

Minimum number of connections maintained in the pool.

pool_max_size NotRequired[int]

Maximum number of connections allowed in the pool.

pool_timeout NotRequired[float]

Seconds to wait for a connection before timing out.

pool_recycle_seconds NotRequired[int]

Maximum age in seconds before a connection is recycled.

health_check_interval NotRequired[float]

Interval in seconds between background health checks.

SqlanywhereDriverFeatures

Bases: TypedDict

SAP SQL Anywhere driver feature flags.

Attributes:

Name Type Description
json_serializer NotRequired[Callable[[Any], str]]

Custom callable for serializing objects to JSON strings.

json_deserializer NotRequired[Callable[[str], Any]]

Custom callable for deserializing JSON strings.

on_connection_create NotRequired[Callable[[SqlanywhereConnection], None]]

Callback invoked immediately when a new connection is established.

enable_events NotRequired[bool]

Enable database event listening.

events_backend NotRequired[Literal['poll_queue']]

Backend mechanism used for dispatching events.

Driver, Pool & Data Dictionary

SqlanywhereDriver

SqlanywhereDriver(
    connection: SqlanywhereConnection,
    statement_config: StatementConfig | None = None,
    driver_features: dict[str, Any] | None = None,
)

Bases: SyncDriverAdapterBase

SAP SQL Anywhere database driver using sqlanydb.

Attributes:

Name Type Description
dialect
data_dictionary SqlanywhereSyncDataDictionary

SqlanywhereExceptionHandler

SqlanywhereExceptionHandler()

Bases: BaseSyncExceptionHandler

Context manager for handling SAP SQL Anywhere sqlanydb exceptions.

SqlanywhereConnectionPool

SqlanywhereConnectionPool(
    connection_parameters: dict[str, Any],
    min_size: int = 1,
    max_size: int = 10,
    timeout: float = 30.0,
    recycle_seconds: int = 86400,
    health_check_interval: float = 30.0,
    on_connection_create: Callable[[SqlanywhereConnection], None] | None = None,
)

Thread-local connection manager for SAP SQL Anywhere over sqlanydb.

Methods:

Name Description
new_connection

Open a standalone SAP SQL Anywhere connection via sqlanydb.connect.

get_connection

Yield a thread-local SQL Anywhere connection.

close

Close every connection opened by this pool.

acquire

Acquire a healthy thread-local connection.

release

Release a checked-out connection back to the thread-local slot.

size

Return the number of active connections registered with this pool.

checked_out

Return the number of currently checked-out connections.

new_connection

new_connection() -> SqlanywhereConnection

Open a standalone SAP SQL Anywhere connection via sqlanydb.connect.

get_connection

get_connection() -> Generator[SqlanywhereConnection, None, None]

Yield a thread-local SQL Anywhere connection.

close

close() -> None

Close every connection opened by this pool.

acquire

acquire() -> SqlanywhereConnection

Acquire a healthy thread-local connection.

release

release(connection: SqlanywhereConnection) -> None

Release a checked-out connection back to the thread-local slot.

size

size() -> int

Return the number of active connections registered with this pool.

checked_out

checked_out() -> int

Return the number of currently checked-out connections.

SqlanywhereSyncDataDictionary

SqlanywhereSyncDataDictionary()

Bases: SyncDataDictionaryBase

Synchronous SAP SQL Anywhere data dictionary.

Attributes:

Name Type Description
dialect str

SqlanywhereSyncMigrationTracker

SqlanywhereSyncMigrationTracker(
    version_table_name: str = "ddl_migrations",
    version_table_schema: str | None = None,
)

Bases: SqlanywhereMigrationTrackerMixin, SyncMigrationTracker

Sync migration tracker using SAP SQL Anywhere DDL.

Extension Stores & Configs

SqlanywhereStore

SqlanywhereStore(config: SqlanywhereConfig)

Bases: BaseSQLSpecStore['SqlanywhereConfig']

SAP SQL Anywhere-backed session store using sqlanydb sync sessions.

Methods:

Name Description
create_table

Create the session table if it doesn't exist.

get

Get a session value by key.

set

Store a session value.

delete

Delete a session by key.

delete_all

Delete all sessions from the store.

exists

Check if a session key exists and is not expired.

expires_in

Get the time in seconds until the session expires.

delete_expired

Delete all expired sessions.

create_table async
create_table() -> None

Create the session table if it doesn't exist.

get async
get(key: str, renew_for: int | timedelta | None = None) -> bytes | None

Get a session value by key.

set async
set(
    key: str, value: str | bytes, expires_in: int | timedelta | None = None
) -> None

Store a session value.

delete async
delete(key: str) -> None

Delete a session by key.

delete_all async
delete_all() -> None

Delete all sessions from the store.

exists async
exists(key: str) -> bool

Check if a session key exists and is not expired.

expires_in async
expires_in(key: str) -> int | None

Get the time in seconds until the session expires.

delete_expired async
delete_expired() -> int

Delete all expired sessions.

SqlanywhereADKConfig

Bases: ADKConfig

SAP SQL Anywhere ADK extension settings.

SqlanywhereADKStore

SqlanywhereADKStore(config: SqlanywhereConfig)

Bases: BaseSyncADKStore['SqlanywhereConfig']

Synchronous SAP SQL Anywhere ADK session/event store.

Methods:

Name Description
create_tables

Create ADK tables and indexes using SQL Anywhere IF NOT EXISTS DDL.

create_session

Create a new ADK session.

get_session

Return a scoped session or None if absent.

update_session_state

Replace a session's durable state.

list_sessions

List ADK sessions for an application, optionally scoped to a user.

delete_session

Delete a session and its associated events.

append_event

Append an event to a session.

append_event_and_update_state

Atomically append an event and update durable session/scoped state.

get_events

Return events for a scoped session ordered by event timestamp.

delete_expired_events

Delete events older than before.

delete_idle_sessions

Delete sessions whose update_time is older than updated_before.

delete_idle_user_states

Delete user state rows whose update_time is older than updated_before.

get_app_state

Return app-scoped state.

get_user_state

Return user-scoped state.

upsert_app_state

Insert or replace app-scoped state.

upsert_user_state

Insert or replace user-scoped state.

get_metadata

Return an ADK metadata value.

set_metadata

Set an ADK metadata value.

Attributes:

Name Type Description
connector_name str
create_tables
create_tables() -> None

Create ADK tables and indexes using SQL Anywhere IF NOT EXISTS DDL.

create_session
create_session(
    session_id: str,
    app_name: str,
    user_id: str,
    state: dict[str, Any],
    owner_id: Any | None = None,
) -> StoredSession

Create a new ADK session.

get_session
get_session(
    app_name: str,
    user_id: str,
    session_id: str,
    *,
    renew_for: int | timedelta | None = None,
) -> StoredSession | None

Return a scoped session or None if absent.

update_session_state
update_session_state(
    app_name: str, user_id: str, session_id: str, state: dict[str, Any]
) -> None

Replace a session's durable state.

list_sessions
list_sessions(
    app_name: str,
    user_id: str | None = None,
    *,
    order_by: SessionOrderBy = "update_time",
    descending: bool = True,
    limit: int | None = None,
    offset: int | None = None,
) -> list[StoredSession]

List ADK sessions for an application, optionally scoped to a user.

delete_session
delete_session(app_name: str, user_id: str, session_id: str) -> None

Delete a session and its associated events.

append_event
append_event(event_record: StoredEvent) -> None

Append an event to a session.

append_event_and_update_state
append_event_and_update_state(
    event_record: StoredEvent,
    app_name: str,
    user_id: str,
    session_id: str,
    state: dict[str, Any],
    *,
    app_state: dict[str, Any] | None = None,
    user_state: dict[str, Any] | None = None,
) -> StoredSession

Atomically append an event and update durable session/scoped state.

get_events
get_events(
    app_name: str,
    user_id: str,
    session_id: str,
    after_timestamp: datetime | None = None,
    limit: int | None = None,
) -> list[StoredEvent]

Return events for a scoped session ordered by event timestamp.

delete_expired_events
delete_expired_events(before: datetime, app_name: str | None = None) -> int

Delete events older than before.

delete_idle_sessions
delete_idle_sessions(
    updated_before: datetime, app_name: str | None = None
) -> int

Delete sessions whose update_time is older than updated_before.

delete_idle_user_states
delete_idle_user_states(
    updated_before: datetime, app_name: str | None = None
) -> int

Delete user state rows whose update_time is older than updated_before.

get_app_state
get_app_state(app_name: str) -> dict[str, Any] | None

Return app-scoped state.

get_user_state
get_user_state(app_name: str, user_id: str) -> dict[str, Any] | None

Return user-scoped state.

upsert_app_state
upsert_app_state(app_name: str, state: dict[str, Any]) -> None

Insert or replace app-scoped state.

upsert_user_state
upsert_user_state(app_name: str, user_id: str, state: dict[str, Any]) -> None

Insert or replace user-scoped state.

get_metadata
get_metadata(key: str) -> str | None

Return an ADK metadata value.

set_metadata
set_metadata(key: str, value: str) -> None

Set an ADK metadata value.

SqlanywhereADKMemoryStore

SqlanywhereADKMemoryStore(config: SqlanywhereConfig)

Bases: BaseSyncADKMemoryStore['SqlanywhereConfig']

SAP SQL Anywhere ADK memory store using sqlanydb.

Methods:

Name Description
create_tables

Create the memory table and indexes using SQL Anywhere IF NOT EXISTS DDL.

insert_memory_entries

Bulk insert memory entries with event-id deduplication.

search_entries

Search memory entries by text query.

delete_entries_by_session

Delete all memory entries for a specific session.

delete_entries_older_than

Delete memory entries older than the retention window.

create_tables
create_tables() -> None

Create the memory table and indexes using SQL Anywhere IF NOT EXISTS DDL.

insert_memory_entries
insert_memory_entries(
    entries: list[StoredMemory], owner_id: object | None = None
) -> int

Bulk insert memory entries with event-id deduplication.

search_entries
search_entries(
    query: str,
    app_name: str,
    user_id: str,
    limit: int | None = None,
    scope_filter: Literal["all", "user", "app"] = "all",
    embedding: Sequence[float] | None = None,
) -> list[StoredMemory]

Search memory entries by text query.

delete_entries_by_session
delete_entries_by_session(session_id: str) -> int

Delete all memory entries for a specific session.

delete_entries_older_than
delete_entries_older_than(
    days: int, app_name: str | None = None, scope: str | None = None
) -> int

Delete memory entries older than the retention window.

SqlanywhereSyncEventQueueStore

SqlanywhereSyncEventQueueStore(config: ConfigT)

Bases: BaseEventQueueStore[SqlanywhereConfig]

SAP SQL Anywhere DDL hooks for the event queue store.