Principle:2giosangmitom Sqmeow nvim Adapter Selection
| Knowledge Sources | |
|---|---|
| Domains | Database, Connection_Management |
| Last Updated | 2026-10-16 14:00 GMT |
Overview
A polymorphic dispatch principle that selects a database adapter from a connection URL's scheme and type-erases it behind a uniform interface, enabling the engine to hold heterogeneous connections.
Description
Adapter Selection is the principle of inferring the database dialect from a connection URL's scheme and dispatching to the appropriate adapter implementation. The Dialect::from_url function maps URL schemes to one of 11 dialects, recognizing protocol-compatible aliases (PostgreSQL/CockroachDB, MySQL/MariaDB, Redis/Valkey, Scylla/Cassandra). The Backend enum wraps the concrete adapter behind a type-erased interface using a dispatch! macro that matches the variant and forwards method calls.
This principle solves the problem of supporting many database systems with different drivers, connection pools, and query languages behind a single engine interface. Each adapter implements the Adapter trait for execution, editing, and metadata discovery. The engine never needs to know which concrete adapter it holds; the Backend enum and dispatch macro handle the routing.
Usage
Use this principle when designing a system that supports multiple database backends: identify the backend by URL scheme, wrap it in a type-erasing enum, and dispatch method calls through a macro. Add new backends by adding an enum variant and adapter implementation.
Theoretical Basis
The principle combines scheme-based dispatch with type erasure:
- Scheme detection:
Dialect::from_url(url)extracts the scheme and maps it to aDialectvariant (case-insensitive). - Type erasure: The
Backendenum wraps each concrete adapter, hiding the specific type. - Dispatch: A
dispatch!macro matches the variant and forwards calls to the inner adapter, which implementsAdapter.
# Abstract pattern (NOT real implementation):
def from_url(url: str) -> Dialect | None:
scheme = url.split("://")[0].lower()
return SCHEME_MAP.get(scheme) # "postgres" → Postgres, "mariadb" → MySql, ...
def connect_to(url, database, read_only) -> Backend:
dialect = from_url(url) or raise UnsupportedUrl
match dialect:
case Dialect.Postgres: return Backend.Postgres(PostgresAdapter.connect(url, database, read_only))
case Dialect.MySql: return Backend.MySql(MySqlAdapter.connect(url, read_only))
# ... 9 more variants