Skip to main content

SQLite SQL tools — parser, formatter, validator, and MCP server

Project description

syntaqlite

Parse, format, and validate SQLite SQL from Python using SQLite's own grammar and tokenizer. No approximations: if SQLite accepts it, syntaqlite parses it.

Docs · Playground · GitHub

pip install syntaqlite

Requires Python 3.10+. Wheels for Linux (x86_64, aarch64), macOS (x86_64, arm64), and Windows (x86_64).

Library

Everything goes through a Syntaqlite instance. Create one and reuse it across many calls:

import syntaqlite

with syntaqlite.Syntaqlite() as sq:
    print(sq.format_sql("select 1"))

Formatting

sql = "select u.id, u.name, p.title from users u join posts p on u.id = p.user_id where u.active = 1 and p.published = true order by p.created_at desc limit 10"
print(sq.format_sql(sql))
SELECT u.id, u.name, p.title
FROM users AS u
JOIN posts AS p ON u.id = p.user_id
WHERE
  u.active = 1
  AND p.published = true
ORDER BY
  p.created_at DESC
LIMIT 10;

Raises syntaqlite.FormatError on invalid input.

Parsing

sq.parse() returns a full AST as typed Python objects, one per statement:

stmts = sq.parse("SELECT 1 + 2 FROM foo")
stmt = stmts[0]  # SelectStmt

print(type(stmt).__name__)       # SelectStmt
print(stmt.columns[0].expr)      # BinaryExpr(...)
print(stmt.from_clause)          # TableRef(...)
print(stmt.where_clause)         # None

Every node type is a __slots__ class with typed attributes, so you get IDE autocomplete and isinstance checks:

from syntaqlite.nodes import SelectStmt, BinaryExpr

assert isinstance(stmt, SelectStmt)
assert isinstance(stmt.columns[0].expr, BinaryExpr)

Enum and flag fields are IntEnum/IntFlag from syntaqlite.enums:

from syntaqlite.enums import BinaryOp

expr = stmt.columns[0].expr
print(expr.op)  # BinaryOp.PLUS

For performance-sensitive code, use sq.parse_raw() to get plain dicts instead of typed objects.

Tokenizing

for tok in sq.tokenize("SELECT 1 + 2"):
    print(tok["text"], tok["category"])
SELECT keyword
  other
1 number
  other
+ operator
  other
2 number

Each token is a dict with text, offset, length, type, and category fields.

Analysis

Check SQL against a schema without touching a database. Catches unknown tables, columns, functions, CTE column mismatches, and more.

schema = syntaqlite.Schema(
    tables=[syntaqlite.Table("users", columns=["id", "name", "email"])],
)
result = sq.analyze("SELECT nme FROM users", schema)
for d in result.diagnostics:
    print(f"{d.severity}: {d.message}")
error: unknown column 'nme'

Switch output to get formatted diagnostics with source locations and suggestions:

print(sq.analyze(
    "SELECT nme FROM users", schema,
    output=syntaqlite.AnalysisOutput.TEXT,
))
error: unknown column 'nme'
 --> <input>:1:8
  |
1 | SELECT nme FROM users
  |        ^~~
  = help: did you mean 'name'?

Schema also accepts raw DDL:

schema = syntaqlite.Schema(ddl="CREATE TABLE orders (id INTEGER, total REAL);")
result = sq.analyze("SELECT * FROM orders", schema)

Column lineage

For query-bearing statements, the result includes column lineage:

schema = syntaqlite.Schema(
    tables=[syntaqlite.Table("users", columns=["id", "name", "email"])],
)
result = sq.analyze("SELECT id, name FROM users", schema)
for col in result.lineage.columns:
    print(f"{col.name} <- {col.origin}")
id <- users.id
name <- users.name

CLI

The pip package also bundles the syntaqlite binary:

syntaqlite fmt -e "select 1, 2, 3"
syntaqlite analyze query.sql
syntaqlite parse -e "SELECT * FROM users"

The CLI supports pinning to a specific SQLite version or enabling compile-time flags to match your target environment:

syntaqlite --sqlite-version 3.32.0 validate query.sql
syntaqlite --sqlite-cflag SQLITE_ENABLE_MATH_FUNCTIONS validate query.sql

See the CLI reference for all commands and flags.

License

Apache 2.0. SQLite components are public domain under the SQLite blessing.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

syntaqlite-0.5.5-py3-none-win_amd64.whl (2.2 MB view details)

Uploaded Python 3Windows x86-64

syntaqlite-0.5.5-py3-none-manylinux_2_28_x86_64.whl (2.6 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

syntaqlite-0.5.5-py3-none-manylinux_2_28_aarch64.whl (2.5 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

syntaqlite-0.5.5-py3-none-macosx_11_0_universal2.whl (2.4 MB view details)

Uploaded Python 3macOS 11.0+ universal2 (ARM64, x86-64)

syntaqlite-0.5.5-py3-none-macosx_10_13_universal2.whl (2.5 MB view details)

Uploaded Python 3macOS 10.13+ universal2 (ARM64, x86-64)

File details

Details for the file syntaqlite-0.5.5-py3-none-win_amd64.whl.

File metadata

  • Download URL: syntaqlite-0.5.5-py3-none-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for syntaqlite-0.5.5-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 12b6be074e7eb986d6d6843c227a56b24d21435c0246974ba44fffe0c8d1a744
MD5 cd943288e565ae2b53823e0faa2a10cc
BLAKE2b-256 10d53b908c40a8369967527a3076c42921fbd68efdfe9340e9a07f0ad4eda67e

See more details on using hashes here.

Provenance

The following attestation bundles were made for syntaqlite-0.5.5-py3-none-win_amd64.whl:

Publisher: release.yml on LalitMaganti/syntaqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file syntaqlite-0.5.5-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for syntaqlite-0.5.5-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1c6d53d9652edceb5a94d261216a5d40c6a02be24fa7818f839a1c4ec97fb391
MD5 50890f73b4ba6455d9c78140e6ccf2a9
BLAKE2b-256 d331fb307182ad70ee28db7d3aa7204515b155cada739b66b82a8834d67727c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for syntaqlite-0.5.5-py3-none-manylinux_2_28_x86_64.whl:

Publisher: release.yml on LalitMaganti/syntaqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file syntaqlite-0.5.5-py3-none-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for syntaqlite-0.5.5-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8c146fe993f4537d1e951ac6c052aaacbba45159015d41d247f9b78873638a33
MD5 ef9b56536ee077e34c0f3073318ef662
BLAKE2b-256 ae75f0f954ca316ff8bf140b9914700058c47471de11419eb64cd71005ada3ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for syntaqlite-0.5.5-py3-none-manylinux_2_28_aarch64.whl:

Publisher: release.yml on LalitMaganti/syntaqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file syntaqlite-0.5.5-py3-none-macosx_11_0_universal2.whl.

File metadata

File hashes

Hashes for syntaqlite-0.5.5-py3-none-macosx_11_0_universal2.whl
Algorithm Hash digest
SHA256 3a8beaef4dbe467bc3ee547bef0e2d1c8167625d3046dbc7bb896427bfd1c2d5
MD5 1ae4bf4538844c7734f39dfef8de37d1
BLAKE2b-256 24f46766b21add8f1e107680094d563f7fc920317aa2f1e527cbeaa0d8c086ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for syntaqlite-0.5.5-py3-none-macosx_11_0_universal2.whl:

Publisher: release.yml on LalitMaganti/syntaqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file syntaqlite-0.5.5-py3-none-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for syntaqlite-0.5.5-py3-none-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 05c790e808d1bd3d44d606dd5e18d316d8aa59f989319dd815096c762a1b717e
MD5 20c43e78594b2134b86ca1f46f31a5bc
BLAKE2b-256 69aff53e0f91b4129e18375eb068a8bdfcb1186a4853bf2b580b3e7ad3572375

See more details on using hashes here.

Provenance

The following attestation bundles were made for syntaqlite-0.5.5-py3-none-macosx_10_13_universal2.whl:

Publisher: release.yml on LalitMaganti/syntaqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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