-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9df3ba9
Showing
29 changed files
with
738 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[run] | ||
source = squel | ||
branch = True | ||
|
||
[report] | ||
ignore_errors = True | ||
omit = | ||
.venv/* | ||
.pytest_cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: test | ||
on: [push] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-24.04 | ||
name: Python ${{matrix.python-version}} | ||
strategy: | ||
matrix: | ||
python-version: ["3.11", "3.12"] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install poetry | ||
run: sudo apt-get install python3-poetry | ||
|
||
- name: Load cached venv | ||
id: cached-poetry-dependencies | ||
uses: actions/cache@v4 | ||
with: | ||
path: ~/.cache/pypoetry | ||
key: cache-${{ matrix.python-version }}-${{ hashFiles('**/pyproject.toml') }} | ||
|
||
- name: Install dependencies | ||
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true' | ||
run: poetry install --no-interaction --no-root | ||
|
||
- name: Run linting | ||
run: poetry run flake8 --max-complexity=15 --exclude=./build,.venv,.tox,.eggs,dist,docs | ||
|
||
- name: Run tests | ||
run: poetry run coverage run -m pytest | ||
|
||
- name: Collect coverage | ||
run: poetry run coverage report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# celery beat schedule file | ||
celerybeat-schedule | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
poetry.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 nomorepanic <nomorepanic@nomorepanic.me> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Squel | ||
|
||
SQL parsing, done right. | ||
|
||
WIP, but you can you use it as base. Just add the missing definitions to | ||
the grammar. | ||
|
||
## Example | ||
|
||
```python | ||
from squel.app import Squel | ||
|
||
|
||
tree = Squel.parse('hello.sql') | ||
print(tree.pretty()) | ||
``` | ||
|
||
### CLI | ||
|
||
```sh | ||
squel parse hello.sql | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
start: _NL? block? | ||
argument_value: NAME | INT | ||
argument: _OP (argument_value (_COMMA _WS? argument_value)*) _CP | ||
primary: _PRIMARY _WS _KEY | ||
unique: _UNIQUE | ||
references: _REFS _WS NAME _WS (_ON _WS _DELETE _WS _CASCADE)? | ||
function_name: NAME _OP _CP | ||
timestamp: TIME_TYPE _WS "without time zone" | ||
char: CHAR_TYPE _WS _VARYING argument | ||
type: BOOLEAN_TYPE | INT_TYPE | char | TEXT_TYPE | timestamp | SERIAL_TYPE | UUID_TYPE | ||
value: INT | FALSE | TRUE | ||
not_null: _NOT _WS _NULL | ||
default: _DEFAULT _WS (value|function_name) | ||
property: not_null | default | ||
constraint: (primary|unique|references) _COMMA? | ||
bound_constraint: constraint _WS? argument _COMMA? | ||
column_name: _QUOTE? NAME _QUOTE? | ||
column: column_name _WS type (_WS property)* (_WS constraint)? _COMMA? | ||
table_name: NAME (_DOT NAME)* | ||
table_head: _CREATE _WS _TABLE _WS table_name _WS? _OP | ||
table_body: (column _NL)+ (bound_constraint _NL)* | ||
table_block: table_head _NL _INDENT table_body _DEDENT _CP _SCOLON | ||
index: _CREATE _WS _INDEX _WS NAME _WS _ON _WS NAME _WS? argument _SCOLON | ||
comment: _COMMENT | ||
line: comment | index | block | ||
block: line _NL | table_block | ||
_CREATE: "CREATE" | ||
_TABLE: "TABLE"i | ||
_NOT: "not"i | ||
_NULL: "null"i | ||
_DEFAULT: "default"i | ||
_VARYING: "varying"i | ||
_PRIMARY: "primary"i | ||
_KEY: "KEY"i | ||
_UNIQUE: "UNIQUE"i | ||
_REFS: "references"i | ||
_ON: "on"i | ||
_DELETE: "delete"i | ||
_CASCADE: "cascade"i | ||
_INDEX: "index"i | ||
_COMMENT: /--(.*)/ | ||
BOOLEAN_TYPE: "boolean" | ||
INT_TYPE: "integer" | ||
CHAR_TYPE: "character" | ||
TEXT_TYPE: "text" | ||
TIME_TYPE: "timestamp" | ||
SERIAL_TYPE: "serial" | ||
UUID_TYPE: "uuid" | ||
NAME.1: /[a-zA-Z_0-9]+/ | ||
INT.2: "0".."9"+ | ||
TRUE: "true" | ||
FALSE: "false" | ||
_WS: (" ")+ | ||
_NL: /(\r?\n[\t ]*)+/ | ||
_INDENT: "<INDENT>" | ||
_DEDENT: "<DEDENT>" | ||
_OP: "(" | ||
_CP: ")" | ||
_COMMA: "," | ||
_SCOLON: ";" | ||
_DOT: "." | ||
_QUOTE: "\"" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[tool.poetry] | ||
name = "squel" | ||
version = "1.0.0" | ||
description = "SQL parsing, done right" | ||
authors = ["nomorepanic <nomorepanic@nomorepanic.me>"] | ||
readme = "README.md" | ||
repository = "https://github.com/nomorepanic/squel" | ||
license="MIT" | ||
include = ["LICENSE", "README.md", "grammar/postgres.ebnf"] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.11" | ||
click = ">=6.7" | ||
lark-parser = ">=0.6.4" | ||
|
||
|
||
[tool.poetry.group.dev.dependencies] | ||
coverage = "^7.0" | ||
flake8 = "^7.0" | ||
flake8-quotes = "^3.4" | ||
flake8-import-order = "^0.18" | ||
pep8-naming = "^0.14" | ||
pytest = "^8" | ||
pytest-mock = "^3.14" | ||
pytest-sugar = "^1.0" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry.scripts] | ||
sqlast = "squel.Cli:Cli.main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
python_files=tests/*/*.py |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
import io | ||
|
||
from .parser import Parser | ||
|
||
|
||
class Squel: | ||
|
||
@staticmethod | ||
def parse(path): | ||
with io.open(path, 'r') as file: | ||
source = file.read() | ||
return Parser().parse(source) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
import click | ||
|
||
from .app import Squel | ||
|
||
|
||
class Cli: | ||
|
||
@click.group() | ||
def main(): | ||
pass | ||
|
||
@staticmethod | ||
@main.command() | ||
@click.argument('path') | ||
def parse(path): | ||
click.echo(Squel.parse(path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
import io | ||
|
||
|
||
class Grammar: | ||
|
||
@staticmethod | ||
def grammar(ebnf_file): | ||
with io.open(ebnf_file) as file: | ||
return file.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
from lark.indenter import Indenter | ||
|
||
|
||
class CustomIndenter(Indenter): | ||
NL_type = '_NL' | ||
OPEN_PAREN_types = [] | ||
CLOSE_PAREN_types = [] | ||
INDENT_type = '_INDENT' | ||
DEDENT_type = '_DEDENT' | ||
tab_len = 8 |
Oops, something went wrong.