Skip to main content
Workspace Backends own storage routing; hb.Database owns direct SQL connection and inspection workflows.

1. Why the Database Boundary Exists

Provider-specific connection code quickly leaks dialect names, engine lifecycle, readonly policy, and credential-bearing URLs into application logic. HeavenBase keeps those concerns under heavenbase.database and exposes the common direct surface as hb.Database, hb.DBEngine, and hb.DBSpec. Workspace SQL Backends resolve through the Registry and reuse the same dialect and engine owners. The optional Database extension adds schema-ingestion entities and controlled MCP tools; it is not a second SQL implementation.

2. Choose a Supported SQL Route

DBEngine resolves provider configuration from active scoped config through Preset -> Normalize -> Validate -> Apply(profile). It captures the selected DatabaseDialectSpec, then Database connects through the shared engine registry. Current provider presets include SQLite, DuckDB, PostgreSQL, MySQL-compatible engines, SQL Server, Oracle, Trino, and StarRocks-style MySQL wire connections. The concrete workspace Backend selectors are sqlite, duckdb, postgres, supabase, pgvector, mysql, seekdb, oceanbase, dolt, mssql, oracle, starrocks, trino, and clickhouse. Protocol-compatible selectors remain distinct identities because construction, lifecycle, placement, and execution evidence differ. sqlalchemy>=2.0 is a core HeavenBase dependency. Non-SQLite providers still require their driver packages, such as psycopg2, pymysql, pymssql, oracledb, trino, or clickhouse-connect; install the sql or full extra for the maintained driver set.

3. Understand Execution Behavior

  • Database resolution is independent of machine bootstrap. Context constructs the system backend; ordinary SQL backends read active heavenbase.db config and do not inherit hidden bootstrap overrides.
  • Engine pooling is shared by resolved database spec and tracks disposed engines explicitly. Two Database instances with the same provider/database/pool args use the same SQLAlchemy engine and pool. Dispose/create races are guarded so stale facades cannot silently recreate an engine after drop.
  • Autocreate is best effort on first engine access. If superuser/database-creation credentials are missing or wrong, HeavenBase still attempts a direct connection so existing databases remain accessible.
  • Explicit drop uses db.drop_database(force=True). SQLite/DuckDB remove files, PostgreSQL/MySQL/MSSQL/StarRocks drop databases, and Oracle drops the target user/schema while leaving the PDB service intact. Trino catalogs are connector-managed, so catalog create/drop is intentionally not attempted.
  • readonly execution is explicit. The default is readonly=False; pass readonly=True only as a conservative guard for statements expected to be read-only. Pass readonly=None when you explicitly want conservative auto-detection for commit/rollback behavior.
  • Raw SQL placeholders are normalized before execution. Supported styles are :name, ?, %s, %(name)s, $name, and $1, with dictionaries, positional tuples/lists, list-of-dicts batches, and list-of-tuples batches where the driver supports batching. Bare ? tokens are rewritten only for positional parameter payloads, so PostgreSQL JSON operators such as payload ? 'key', ?|, and ?& remain valid. PostgreSQL casts such as ?::int are preserved after bind rewriting.
  • safe=True turns execution errors into SQLResponse(ok=False). In an active transaction, a safe failure marks the transaction failed and the context rolls back unless the caller explicitly handles it with rollback(). In execute_many(..., safe=True), the first failed statement stops the batch and rolls back by default; autocommit=True opts into best-effort per-statement commits.
  • SQL healing is intentionally future work and tracked separately. The current API returns or raises the original database error instead of calling an LLM repair path.

4. Use the Direct Database Surface

Database exposes schema listing, table/view/column inspection, table creation and mutation helpers, comments where supported by the dialect, exact percentiles, string-length summaries, deterministic sampling without assuming an id column, and SQLResponse export helpers for dictionaries, lists, pandas, NumPy, PyArrow, and compact table display.
Use readonly=None only when you want HeavenBase to infer whether a statement should commit or roll back. Unknown statements are treated as mutating.
Database lifecycle helpers are explicit:
The canonical naming model is the only supported Database API. Use tables(), columns(), pks(), fks(), n_rows(), sample(), create_table(), add_table_col(), clear_table(), and drop_table(); legacy compatibility aliases such as db_tabs, tab_cols, row_sample, and create_tab are not kept. When the wrapper is intentionally smaller than SQLAlchemy, use the public engine or ORM execution path directly:
SQLResponse validates projected columns by default. Pass check=False only when missing columns should be rendered as None for permissive export/display code.

Summary

  • Workspace SQL Backends own routed Entity storage; hb.Database owns direct SQL workflows.
  • Fourteen concrete SQL-family selectors cover embedded, self-hosted, and managed protocols.
  • Provider identity remains distinct even when two routes share a wire protocol.

Further Exploration

Related resources:
  • Backends - Compare every built-in storage provider
  • Workspace - Configure named Backend instances
  • Routing - Place Entity fields on those instances
  • Database Workshop - Ingest schema and expose controlled SQL tools