Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:2giosangmitom Sqmeow nvim Adapter Selection

From Leeroopedia
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:

  1. Scheme detection: Dialect::from_url(url) extracts the scheme and maps it to a Dialect variant (case-insensitive).
  2. Type erasure: The Backend enum wraps each concrete adapter, hiding the specific type.
  3. Dispatch: A dispatch! macro matches the variant and forwards calls to the inner adapter, which implements Adapter.
# 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

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment