Skip to content

Commit

Permalink
chore/squel: first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nomorepanic committed Jun 2, 2024
0 parents commit 9df3ba9
Show file tree
Hide file tree
Showing 29 changed files with 738 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .coveragerc
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/
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
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
106 changes: 106 additions & 0 deletions .gitignore
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
21 changes: 21 additions & 0 deletions LICENSE
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.
22 changes: 22 additions & 0 deletions README.md
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
```
75 changes: 75 additions & 0 deletions grammar/postgres.ebnf
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: "\""
32 changes: 32 additions & 0 deletions pyproject.toml
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"
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
python_files=tests/*/*.py
Empty file added squel/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions squel/app.py
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)
17 changes: 17 additions & 0 deletions squel/cli.py
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))
10 changes: 10 additions & 0 deletions squel/grammar.py
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()
11 changes: 11 additions & 0 deletions squel/indenter.py
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
Loading

0 comments on commit 9df3ba9

Please sign in to comment.