Skip to main content

Lightweight, source-first SQL AST + compiler + runner.

Project description

SQLStratum

SQLStratum logo

SQLStratum is a modern, typed, deterministic SQL query builder and compiler for Python with a SQLite runner and a hydration pipeline. It exists to give applications and ORMs a reliable foundation layer with composable SQL, predictable parameter binding, and explicit execution boundaries.

Key Features

  • Deterministic compilation: identical AST inputs produce identical SQL + params
  • Typed, composable DSL for SELECT/INSERT/UPDATE/DELETE
  • Safe parameter binding (no raw interpolation)
  • Hydration targets for structured results
  • SQLite-first execution via a small Runner API
  • Testable compiled output and runtime behavior

Non-Goals

  • Not an ORM (no identity map, relationships, lazy loading)
  • Not a migrations/DDL system
  • Not a full database abstraction layer for every backend yet (SQLite first)
  • Not a SQL string templating engine

SQLStratum focuses on queries. DDL statements such as CREATE TABLE or ALTER TABLE are intended to live in a complementary library with similar design goals that is currently in the works.

Quickstart

import sqlite3

from sqlstratum import SELECT, INSERT, Table, col, Runner

users = Table(
    "users",
    col("id", int),
    col("email", str),
    col("active", int),
)

conn = sqlite3.connect(":memory:")
runner = Runner(conn)
runner.exec_ddl("CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT, active INTEGER)")

runner.execute(INSERT(users).VALUES(email="a@b.com", active=1))
runner.execute(INSERT(users).VALUES(email="c@d.com", active=0))

q = (
    SELECT(users.c.id, users.c.email)
    .FROM(users)
    .WHERE(users.c.active.is_true())
    .HYDRATE(dict)
)

rows = runner.fetch_all(q)
print(rows)

Why Table objects?

SQLStratum’s Table objects are the schema anchor for the typed, deterministic query builder. They provides column metadata and a stable namespace for column access, which enables predictable SQL generation and safe parameter binding. They also support explicit aliasing to avoid ambiguous column names in joins.

Project Structure

  • AST: immutable query nodes in sqlstratum/ast.py
  • Compiler: SQL + params generation in sqlstratum/compile.py
  • Runner: SQLite execution and transactions in sqlstratum/runner.py
  • Hydration: projection rules and targets in sqlstratum/hydrate.py

SQL Debugging

SQLStratum can log executed SQL statements (compiled SQL + parameters + duration), but logging is intentionally gated to avoid noisy output in production. Debug output requires two conditions:

  • Environment variable gate: SQLSTRATUM_DEBUG must be truthy ("1", "true", "yes", case-insensitive).
  • Logger gate: the sqlstratum logger must be DEBUG-enabled.

Why it does not work by default: Python logging defaults to WARNING level, so even if SQLSTRATUM_DEBUG=1 is set, DEBUG logs will not appear unless logging is configured.

To enable debugging in a development app:

Step 1 - set the environment variable:

SQLSTRATUM_DEBUG=1

Step 2 - configure logging early in the app:

import logging

logging.basicConfig(level=logging.DEBUG)
# or
logging.getLogger("sqlstratum").setLevel(logging.DEBUG)

Output looks like:

SQL: <compiled sql> | params={<sorted params>} | duration_ms=<...>

Architectural intent: logging happens at the Runner boundary (after execution). AST building and compilation remain deterministic and side-effect free, preserving separation of concerns.

Logo Inspiration

Vinicunca (Rainbow Mountain) in Peru’s Cusco Region — a high-altitude day hike from Cusco at roughly 5,036 m (16,500 ft). See Vinicunca for background.

Versioning / Roadmap

Current version: 0.1.0. Design notes and current limitations are tracked in NOTES.md. Roadmap planning is intentionally minimal at this stage and will evolve with real usage.

Authorship

Antonio Ognio is the maintainer and author of SQLStratum. ChatGPT is used for brainstorming, architectural thinking, documentation drafting, and project management advisory. Codex (CLI/agentic coding) is used to implement many code changes under Antonio's direction and review. The maintainer reviews and curates changes; AI tools are assistants, not owners, and accountability remains with the maintainer.

License

MIT License.

Contributing

PRs are welcome. Please read CONTRIBUTING.md for the workflow and expectations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlstratum-0.1.0.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlstratum-0.1.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file sqlstratum-0.1.0.tar.gz.

File metadata

  • Download URL: sqlstratum-0.1.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for sqlstratum-0.1.0.tar.gz
Algorithm Hash digest
SHA256 60c981b067212449d9f175fd2a8dbb0ec7222e6b20fe4488ba9d375878e244cb
MD5 61f6dc9a2d5b76eb484d444f210f701c
BLAKE2b-256 87f3c5f201ddc99addbfe13b5c7741cd1faa4e22257c3c153eb2350955c44896

See more details on using hashes here.

File details

Details for the file sqlstratum-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sqlstratum-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for sqlstratum-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fcd3e9514220c2625bc920d071536a7aaca00d6787784b908d63e7a5586daa5d
MD5 98e6d9bb63ae9837270d6f9c634a6d70
BLAKE2b-256 f12e93eab6b8db995e0dcd91a210905c4d4d1cff8f7e52b147f285c4463d0230

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page