Python library for parsing SQLite SELECT queries into an AST
Project description
sqlite-ast
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7de663c956b7beec8381d1d896a7a4270235df233740c6584c4a65e35bc409b
|
|
| MD5 |
93385bcc6905877ce081ee76e7a53139
|
|
| BLAKE2b-256 |
1a95c8b0eb07b2124e9f2589ae4519034a379fc001a4d175ae12757a2bb1172c
|
Provenance
The following attestation bundles were made for sqlite_ast-0.1a0.tar.gz:
Publisher:
publish.yml on simonw/sqlite-ast
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlite_ast-0.1a0.tar.gz -
Subject digest:
d7de663c956b7beec8381d1d896a7a4270235df233740c6584c4a65e35bc409b - Sigstore transparency entry: 871700257
- Sigstore integration time:
-
Permalink:
simonw/sqlite-ast@7a83f213328891a1128382a155c568989c7ff51e -
Branch / Tag:
refs/tags/0.1a0 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7a83f213328891a1128382a155c568989c7ff51e -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32aefdec5e019125607a67364bf80f69c57b789ccbaba266d025fdfb45309325
|
|
| MD5 |
d2d66c175ac8309bbc6ae877f3b30f09
|
|
| BLAKE2b-256 |
603d11dcaa1a02e02856612b01e29af3097d2fbf6d7eb91d5a214c22361d688b
|
Provenance
The following attestation bundles were made for sqlite_ast-0.1a0-py3-none-any.whl:
Publisher:
publish.yml on simonw/sqlite-ast
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlite_ast-0.1a0-py3-none-any.whl -
Subject digest:
32aefdec5e019125607a67364bf80f69c57b789ccbaba266d025fdfb45309325 - Sigstore transparency entry: 871700262
- Sigstore integration time:
-
Permalink:
simonw/sqlite-ast@7a83f213328891a1128382a155c568989c7ff51e -
Branch / Tag:
refs/tags/0.1a0 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@7a83f213328891a1128382a155c568989c7ff51e -
Trigger Event:
release
-
Statement type: