Skip to content

Commit e31f58d

Browse files
authored
Merge pull request #22 from shigeru0215/release/v0.2
Release v0.2.0
2 parents 0442812 + 14be362 commit e31f58d

26 files changed

+135
-77
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ COPY requirements.txt /tmp
1313
RUN pip install --upgrade pip
1414
RUN pip3 install -r /tmp/requirements.txt
1515

16-
COPY . /work
16+
COPY src /work
17+
COPY tests /work/tests
1718
WORKDIR /work

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ REPL
3838
```bash
3939
$ python
4040
>>> from sqlint import parse, check
41-
>>> stmt = 'SELECT id From user_table where user_table.age >10'
41+
>>> sql = 'SELECT id From user_table where user_table.age >10'
4242
>>>
43-
>>> parse(stmt)
43+
>>> parse(sql)
4444
[[<Keyword: 'SELECT'>, <Whitespace: ' '>, <Identifier: 'id'>, <Whitespace: ' '>, <Keyword: 'From'>, <Whitespace: ' '>, <Identifier: 'user_table'>, <Whitespace: ' '>, <Keyword: 'where'>, <Whitespace: ' '>, <Identifier: 'user_table.age'>, <Whitespace: ' '>, <Operator: '>'>, <Identifier: '10'>]]
4545
>>>
46-
>>> check(stmt)
46+
>>> check(sql)
4747
['(L1, 1): reserved keywords must be lower case: SELECT -> select', '(L1, 11): reserved keywords must be lower case: From -> from', '(L1, 26): too many spaces', '(L1, 49): whitespace must be after binary operator: >10']
4848
```
4949

README.rst

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,68 @@
1+
=============
12
sqlint
23
=============
34

4-
https://github.com/shigeru0215/sqlint'
5+
This is a SQL parser and linter for Standard SQL(BigQuery).
6+
7+
https://github.com/shigeru0215/sqlint
58

69
Install
7-
-------
10+
=======
811

912
.. code::
1013
1114
$ pip install sqlint
1215
1316
Usage
14-
-----
17+
========
1518

16-
Command line
19+
linting
20+
--------
1721

1822
.. code::
1923
20-
$ sqlint query/*sql
24+
$ cat example.sql
25+
>> select
26+
>> a + b as x
27+
>> , b+c as y
28+
>> from
29+
>> test_table as t1
30+
$ sqlint example.sql
31+
>> example.sql (L3, 8): whitespace must be before binary operator: b+
32+
>> example.sql (L3, 8): whitespace must be after binary operator: +c
33+
34+
formatting
35+
-----------
2136

22-
Python module
37+
With -f option, this linter show formatted SQL.
38+
39+
.. code::
40+
41+
$ sqlint -f query/example.sql
42+
>> select
43+
>> a + b as x
44+
>> , b + c as y
45+
>> from
46+
>> test_table as t1
47+
48+
REPL
2349

2450
.. code::
2551
2652
$ python
27-
>>> from sqlint import parse, check
28-
>>> stmt = 'SELECT id From user_table where user_table.age >10'
53+
>>> from sqlint import parse, check, format
54+
>>> sql = 'SELECT id From user_table where user_table.age >10'
2955
>>>
30-
>>> parse(stmt)
56+
>>> parse(sql)
3157
[[<Keyword: 'SELECT'>, <Whitespace: ' '>, <Identifier: 'id'>, <Whitespace: ' '>, <Keyword: 'From'>, <Whitespace: ' '>, <Identifier: 'user_table'>, <Whitespace: ' '>, <Keyword: 'where'>, <Whitespace: ' '>, <Identifier: 'user_table.age'>, <Whitespace: ' '>, <Operator: '>'>, <Identifier: '10'>]]
3258
>>>
33-
>>> check(stmt)
59+
>>> check(sql)
3460
['(L1, 1): reserved keywords must be lower case: SELECT -> select', '(L1, 11): reserved keywords must be lower case: From -> from', '(L1, 26): too many spaces', '(L1, 49): whitespace must be after binary operator: >10']
61+
>>>
62+
>>> select
63+
>>> id
64+
>>> from
65+
>>> user_table
66+
>>> where
67+
>>> user_table.age > 10
68+

requirements.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
click==7.0
2-
configparser>=4.0
3-
# requirements for test
4-
pytest>=5.2.0
5-
pytest-cov>=2.8.0
6-
flake8>=3.7.8
2+
configparser==4.0.2

setup.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,40 @@ def _requirements():
2020
long_description = ''
2121

2222
# read __init__
23-
with open(path.join(root_dir, 'src', '__init__.py')) as f:
23+
with open(path.join(root_dir, 'src/sqlint', '__init__.py')) as f:
2424
init_text = f.read()
2525
version = re.search(r'__version__\s*=\s*[\'\"](.+?)[\'\"]', init_text).group(1)
2626
assert version
2727

2828
setup(
29-
name='src',
29+
name='sqlint',
3030
version=version,
31-
license="MIT",
32-
author='shigeru0215',
33-
author_email='[email protected]',
34-
url='https://github.com/shigeru0215/sqlint',
3531
description='Simple Sql Linter',
3632
long_description=long_description,
37-
packages=find_packages(exclude=("tests",)),
38-
test_suite='tests',
39-
install_requires=_requirements(),
33+
url='https://github.com/shigeru0215/sqlint',
34+
author='shigeru0215',
35+
author_email='[email protected]',
36+
license="MIT",
4037
classifiers=[
41-
# 'Development Status :: 5 - Production/Stable',
42-
# 'License :: OSI Approved :: MIT License',
38+
'Development Status :: 3 - Alpha',
39+
'Environment :: Console',
40+
'Intended Audience :: Developers',
41+
'License :: OSI Approved :: MIT License',
4342
'Programming Language :: Python',
4443
'Programming Language :: Python :: 3',
45-
'Programming Language :: Python :: 3.4',
46-
'Programming Language :: Python :: 3.5',
4744
'Programming Language :: Python :: 3.6',
48-
'Topic :: Database',
49-
'Topic :: Software Development :: Libraries :: Python Modules',
45+
'Programming Language :: Python :: 3.7',
46+
'Topic :: Software Development :: Quality Assurance',
47+
'Topic :: Text Processing :: General',
5048
],
49+
packages=find_packages('src', exclude=['tests']),
50+
package_dir={'': 'src'},
51+
package_data={'': ['config/default.ini']},
52+
test_suite='tests',
53+
install_requires=_requirements(),
5154
entry_points={
5255
"console_scripts": [
53-
'sqlint=src.__main__:main',
56+
'sqlint=sqlint.__main__:main',
5457
]
5558
}
5659
)

src/__init__.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/sqlint/__init__.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import logging
2+
3+
from .syntax_tree import SyntaxTree
4+
from .config import Config
5+
from .parser import parse as parse_sql
6+
from .checker import check as check_sql
7+
from .formatter import format as format_sql
8+
9+
__version__ = '0.2.0'
10+
11+
__all__ = [
12+
'parse',
13+
'check',
14+
'format'
15+
]
16+
17+
# setting logger
18+
logger = logging.getLogger(__name__)
19+
logger.setLevel(logging.INFO)
20+
21+
22+
def parse(sql: str):
23+
tokens_list = parse_sql(sql)
24+
logger.info(tokens_list)
25+
26+
27+
def check(sql: str):
28+
tree = SyntaxTree.sqlptree(sql)
29+
for v in check_sql(tree, Config()):
30+
logger.info(v)
31+
32+
33+
def format(sql: str):
34+
tree = SyntaxTree.sqlptree(sql, is_abstract=True)
35+
formatted_tree = format_sql(tree, Config())
36+
logger.info(formatted_tree.sqlftree())
File renamed without changes.
File renamed without changes.

src/checker/base.py renamed to src/sqlint/checker/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from . import checker as chk
44
from .violation import Violation
5-
from src.syntax_tree import SyntaxTree
6-
from src.config import Config
5+
from sqlint.syntax_tree import SyntaxTree
6+
from sqlint.config import Config
77

88

99
def check(tree: SyntaxTree, config: Config) -> List[Violation]:

src/checker/checker.py renamed to src/sqlint/checker/checker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
from . import violation
55
from .violation import Violation
6-
from src.syntax_tree import SyntaxTree, Node
7-
from src.parser import Token
8-
from src.parser.keywords import format as format_keyword
9-
from src.config import Config
6+
from sqlint.config import Config
7+
from sqlint.syntax_tree import SyntaxTree, Node
8+
from sqlint.parser import Token
9+
from sqlint.parser.keywords import format as format_keyword
1010

1111

1212
class Checker(metaclass=ABCMeta):

src/checker/violation.py renamed to src/sqlint/checker/violation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from typing import Dict
22
from enum import Enum
33

4-
from src.parser import Token
5-
from src.syntax_tree import SyntaxTree
4+
from sqlint.parser import Token
5+
from sqlint.syntax_tree import SyntaxTree
66

77

88
class Code(Enum):
@@ -65,7 +65,7 @@ def __init__(self, tree: SyntaxTree, index: int, code: Code, **kwargs):
6565
self.code: Code = code
6666
self.params: Dict = kwargs
6767

68-
def get_message(self):
68+
def __str__(self):
6969
_template = '(L{line}, {pos}): ' + self.code.template
7070

7171
return _template.format(

src/cli.py renamed to src/sqlint/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def main(files, config_file, is_format):
6565
else:
6666
tree.sqlftree()
6767
for v in check_tree(tree, config):
68-
logger.info('{} {}'.format(file, v.get_message()))
68+
logger.info('{} {}'.format(file, v))
6969

7070

7171
if __name__ == '__main__':
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/formatter/base.py renamed to src/sqlint/formatter/base.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import logging
12
from typing import List, Tuple
23

34
from . import splitter as spt
45
from . import formatter as fmt
5-
from src.syntax_tree import SyntaxTree
6-
from src.parser import Token
7-
from src.config import Config
6+
from sqlint.config import Config
7+
from sqlint.parser import Token
8+
from sqlint.syntax_tree import SyntaxTree
9+
10+
11+
logger = logging.getLogger(__name__)
812

913

1014
def format(tree: SyntaxTree, config: Config) -> SyntaxTree:
@@ -66,11 +70,9 @@ def _reshape_tree(tree: SyntaxTree, config: Config):
6670

6771
"""DEBUG
6872
if own and own[0].word.lower() == 'date_diff':
69-
print('-'*10)
70-
print(f'\033[32mown\033[0m = {own}')
71-
print(f'\033[32mchildren\033[0m = {children}')
72-
print(f'\033[32msibling\033[0m = {sibling}')
73-
print('-'*10)
73+
logger.debug(f'\033[32mown\033[0m = {own}')
74+
logger.debug(f'\033[32mchildren\033[0m = {children}')
75+
logger.debug(f'\033[32msibling\033[0m = {sibling}')
7476
"""
7577
tree.tokens = own
7678

@@ -83,11 +85,9 @@ def _reshape_tree(tree: SyntaxTree, config: Config):
8385
_o, _c, _s = spt.LongLineSplitter.split(own, tree)
8486
"""DEBUG
8587
if own and own[0].word.lower() == 'date_diff':
86-
print('+' * 10)
87-
print(f' \033[92m_o\033[0m = {_o}')
88-
print(f' \033[92m_c\033[0m = {_c}')
89-
print(f' \033[92m_s\033[0m = {_s}')
90-
print('+' * 10)
88+
logger.debug(f' \033[92m_o\033[0m = {_o}')
89+
logger.debug(f' \033[92m_c\033[0m = {_c}')
90+
logger.debug(f' \033[92m_s\033[0m = {_s}')
9191
"""
9292
tree.tokens = _o
9393
children = _c + children

src/formatter/formatter.py renamed to src/sqlint/formatter/formatter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from abc import ABCMeta, abstractmethod
22
from typing import List
33

4-
from src.config import Config
5-
from src.parser import Token
6-
from src.parser.keywords import format as format_keyword
7-
from src.syntax_tree import SyntaxTree
4+
from sqlint.config import Config
5+
from sqlint.parser import Token
6+
from sqlint.parser.keywords import format as format_keyword
7+
from sqlint.syntax_tree import SyntaxTree
88

99

1010
class Formatter(metaclass=ABCMeta):

src/formatter/splitter.py renamed to src/sqlint/formatter/splitter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from abc import ABCMeta, abstractmethod
22
from typing import List, TypeVar, Tuple
33

4-
from src.parser import Token
5-
from src.syntax_tree import SyntaxTree
4+
from sqlint.parser import Token
5+
from sqlint.syntax_tree import SyntaxTree
66

77
T = TypeVar('T')
88

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/syntax_tree.py renamed to src/sqlint/syntax_tree.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from .parser import Token
55
from .parser import parse as parse_sql
66

7-
logger = logging.getLogger(__name__)
8-
97

108
class Node:
119
def __init__(self, line_num: int, tokens: List[Token] = None):
@@ -290,6 +288,7 @@ def sqlftree(self) -> str:
290288
Returns:
291289
sql stetement
292290
"""
291+
293292
return SyntaxTree._sqlftree(self)
294293

295294
@staticmethod

0 commit comments

Comments
 (0)