Skip to main content

Python library for parsing SQLite SELECT queries into an AST

Project description

sqlite-ast

PyPI Tests Changelog License

Python library for parsing SQLite SELECT queries into an AST

Installation

Install this library using pip:

pip install sqlite-ast

Usage

The main entry point is parse(sql), which returns a nested Python dictionary:

from sqlite_ast import parse

ast = parse("select 1")
print(ast)
{'type': 'select', 'distinct': False, 'all': False, 'columns': [{'expr': {'type': 'integer', 'value': 1}, 'alias': None}], 'from': None, 'where': None, 'group_by': None, 'having': None, 'order_by': None, 'limit': None}

You can pretty-print that dictionary as JSON:

import json
from sqlite_ast import parse

ast = parse("select 1")
print(json.dumps(ast, indent=2))
{
  "type": "select",
  "distinct": false,
  "all": false,
  "columns": [
    {
      "expr": {
        "type": "integer",
        "value": 1
      },
      "alias": null
    }
  ],
  "from": null,
  "where": null,
  "group_by": null,
  "having": null,
  "order_by": null,
  "limit": null
}

If you want structured dataclass nodes instead of dictionaries, use parse_ast(sql):

from pprint import pprint
from sqlite_ast import parse_ast

node = parse_ast("select 1")
pprint(node)
Select(distinct=False,
       all=False,
       with_ctes=None,
       columns=[ResultColumn(expr=IntegerLiteral(value=1), alias=None)],
       from_clause=None,
       where=None,
       group_by=None,
       having=None,
       window_definitions=None,
       order_by=None,
       limit=None,
       offset=None,
       _has_limit=False,
       _compound_member=False)

Parse failures raise ParseError:

from pprint import pprint
from sqlite_ast import parse, ParseError

try:
    parse("select 1 union select")
except ParseError as e:
    print(e)
    print("\nPartial AST:")
    pprint(e.partial_ast)
Parse error at position 21: Unexpected token in expression: EOF ('')

Partial AST:
Select(distinct=False,
       all=False,
       with_ctes=None,
       columns=[ResultColumn(expr=IntegerLiteral(value=1), alias=None)],
       from_clause=None,
       where=None,
       group_by=None,
       having=None,
       window_definitions=None,
       order_by=None,
       limit=None,
       offset=None,
       _has_limit=False,
       _compound_member=True)

Development

To contribute to this library, first checkout the code. Then run the tests with uv:

cd sqlite-ast
uv run pytest

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

sqlite_ast-0.1a0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

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

sqlite_ast-0.1a0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file sqlite_ast-0.1a0.tar.gz.

File metadata

  • Download URL: sqlite_ast-0.1a0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlite_ast-0.1a0.tar.gz
Algorithm Hash digest
SHA256 d7de663c956b7beec8381d1d896a7a4270235df233740c6584c4a65e35bc409b
MD5 93385bcc6905877ce081ee76e7a53139
BLAKE2b-256 1a95c8b0eb07b2124e9f2589ae4519034a379fc001a4d175ae12757a2bb1172c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlite_ast-0.1a0.tar.gz:

Publisher: publish.yml on simonw/sqlite-ast

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

File details

Details for the file sqlite_ast-0.1a0-py3-none-any.whl.

File metadata

  • Download URL: sqlite_ast-0.1a0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sqlite_ast-0.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 32aefdec5e019125607a67364bf80f69c57b789ccbaba266d025fdfb45309325
MD5 d2d66c175ac8309bbc6ae877f3b30f09
BLAKE2b-256 603d11dcaa1a02e02856612b01e29af3097d2fbf6d7eb91d5a214c22361d688b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sqlite_ast-0.1a0-py3-none-any.whl:

Publisher: publish.yml on simonw/sqlite-ast

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