forked from paetzke/format-sql
-
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 c73d2fd
Showing
28 changed files
with
639 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,4 @@ | ||
[bumpversion] | ||
current_version = 0.0.0 | ||
files = setup.py format_sql/__init__.py | ||
commit = True |
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,6 @@ | ||
*.pyc | ||
.coverage | ||
|
||
build/ | ||
dist/ | ||
format_sql.egg-info/ |
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 @@ | ||
language: python | ||
python: | ||
- "2.7" | ||
- "3.3" | ||
- "pypy" | ||
install: | ||
- pip install -r requirements/testing.txt | ||
script: py.test --cov=format_sql --cov-report=term-missing | ||
after_success: | ||
- coveralls |
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,23 @@ | ||
Copyright (c) 2014, Friedrich Paetzke | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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,3 @@ | ||
include LICENSE | ||
include README.rst | ||
include requirements/package.txt |
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,27 @@ | ||
format-sql | ||
========== | ||
|
||
.. image:: https://travis-ci.org/paetzke/format-sql.png?branch=master | ||
:target: https://travis-ci.org/paetzke/format-sql | ||
.. image:: https://coveralls.io/repos/paetzke/format-sql/badge.png?branch=master | ||
:target: https://coveralls.io/r/paetzke/format-sql?branch=master | ||
.. image:: https://pypip.in/v/format-sql/badge.png | ||
:target: https://pypi.python.org/pypi/format-sql/ | ||
|
||
Copyright (c) 2014, Friedrich Paetzke (f.paetzke@gmail.com) | ||
All rights reserved. | ||
|
||
format-sql is a tool to format SQL in your Python strings! | ||
|
||
Install ``format-sql`` via ``pip``: | ||
|
||
.. code:: bash | ||
$ pip install format-sql | ||
You can then just call ``format-sql`` with files and directories: | ||
|
||
.. code:: bash | ||
format-sql my_python_file.py my/python/dir/ | ||
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,15 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
format-sql | ||
Copyright (c) 2014, Friedrich Paetzke (f.paetzke@gmail.com) | ||
All rights reserved. | ||
""" | ||
from .format_sql import format_sql | ||
from .formatter import pretty_format | ||
|
||
__version__ = '0.0.0' | ||
__author__ = 'Friedrich Paetzke' | ||
__license__ = 'BSD' | ||
__copyright__ = 'Copyright 2014 Friedrich Paetzke' |
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,95 @@ | ||
#! /usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
format-sql | ||
Copyright (c) 2014, Friedrich Paetzke (f.paetzke@gmail.com) | ||
All rights reserved. | ||
""" | ||
from __future__ import print_function | ||
|
||
import fnmatch | ||
import os | ||
import re | ||
import sys | ||
|
||
import sqlparse | ||
|
||
from .formatter import pretty_format | ||
|
||
|
||
def load_from_file(filename): | ||
with open(filename, 'r') as f: | ||
content = f.read() | ||
try: | ||
content = unicode(content, 'utf-8') | ||
except NameError: | ||
pass | ||
return content.splitlines() | ||
|
||
|
||
def _read_dir(dirname): | ||
for root, unused_dir, files in os.walk(dirname): | ||
for item in fnmatch.filter(files, "*.py"): | ||
fname = os.path.join(root, item) | ||
yield fname | ||
|
||
|
||
def _handle_file(filename): | ||
content = load_from_file(filename) | ||
content = format_sql(content) | ||
|
||
if content: | ||
content = '\n'.join(content) | ||
content = content.encode('utf-8') | ||
with open(filename, 'wb') as f: | ||
f.write(content) | ||
f.write('\n') | ||
|
||
|
||
def format_sql(lines): | ||
content = '\n'.join(lines) | ||
|
||
queries = re.findall(r'([ ]*)[_\w\d]*\s*=*\s*"{3}(\s*.*?;*\s*)"{3}', | ||
content, re.DOTALL) | ||
|
||
for indent, query in queries: | ||
indent += ' ' * 4 | ||
old_query = query | ||
query = ' '.join(query.split()) | ||
|
||
first = query.split() | ||
if not first: | ||
continue | ||
|
||
first = first[0].lower() | ||
if not first in ['select']: | ||
continue | ||
|
||
fs = [] | ||
fmt = pretty_format(query).splitlines() | ||
for line in fmt: | ||
fs.append(indent + line.rstrip()) | ||
|
||
content = content.replace(old_query, '\n%s ' % '\n'.join(fs)) | ||
|
||
return content.split('\n') | ||
|
||
|
||
def main(): | ||
for arg in sys.argv[1:]: | ||
pathname = os.path.abspath(arg) | ||
if os.path.isfile(pathname): | ||
print(pathname) | ||
_handle_file(pathname) | ||
elif os.path.isdir(pathname): | ||
for filename in _read_dir(pathname): | ||
print(filename) | ||
_handle_file(filename) | ||
else: | ||
print('Unknown path %s' % pathname) | ||
|
||
|
||
if __name__ == '__main__': | ||
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,86 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
format-sql | ||
Copyright (c) 2014, Friedrich Paetzke (f.paetzke@gmail.com) | ||
All rights reserved. | ||
""" | ||
import sqlparse | ||
from sqlparse.sql import Comparison, Identifier, IdentifierList, Where | ||
|
||
|
||
def _val(token, indent=0): | ||
if token.is_group() and len(token.tokens) > 1: | ||
if isinstance(token, Identifier): | ||
return ''.join([tk.value for tk in token.tokens]) | ||
|
||
if isinstance(token, Comparison): | ||
return ''.join([tk.value for tk in token.tokens]) | ||
|
||
if isinstance(token, IdentifierList): | ||
s = ''.join([_val(tk) for tk in token.tokens]) | ||
return s.replace(',', ',\n' + ' ' * indent) | ||
|
||
return token.value | ||
|
||
|
||
class _Formatter: | ||
|
||
TOKENS = ['SELECT', 'FROM', 'WHERE', ] | ||
TOKENS_BREAK = ['LEFT JOIN', 'AND', 'OR', 'JOIN', ] | ||
|
||
def __init__(self): | ||
self._lines = [] | ||
self._line = [] | ||
|
||
def _add_to_lines(self, indent): | ||
if self._line: | ||
row = '%s%s' % (indent * ' ', ' '.join(self._line)) | ||
self._line = [] | ||
self._lines.append(row) | ||
|
||
def _remove_white_before_semicolon(self): | ||
last_line = self._lines.pop() | ||
if last_line.endswith(' ;'): | ||
last_line = last_line.replace(' ;', ';') | ||
self._lines.append(last_line) | ||
|
||
def _tokens(self, sql): | ||
s = sqlparse.format(sql, keyword_case='upper') | ||
parsed = sqlparse.parse(s)[0] | ||
return self._filter(parsed.tokens) | ||
|
||
def _filter(self, tokens): | ||
return (token for token in tokens if not token.is_whitespace()) | ||
|
||
def k(self, sql): | ||
tokens = self._tokens(sql) | ||
return self._format(tokens) | ||
|
||
def _format(self, tokens): | ||
indent = 4 | ||
for token in tokens: | ||
if token.value in self.TOKENS: | ||
self._add_to_lines(indent) | ||
self._lines.append(token.value) | ||
continue | ||
|
||
if isinstance(token, Where): | ||
self._add_to_lines(indent) | ||
self._format(self._filter(token.tokens)) | ||
continue | ||
|
||
if token.value in self.TOKENS_BREAK: | ||
self._add_to_lines(indent) | ||
|
||
self._line.append(_val(token, indent)) | ||
|
||
self._add_to_lines(indent) | ||
self._remove_white_before_semicolon() | ||
return '\n'.join(self._lines) | ||
|
||
|
||
def pretty_format(sql): | ||
formatter = _Formatter() | ||
return formatter.k(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,7 @@ | ||
-r testing.txt | ||
|
||
bumpversion | ||
ipdb | ||
orgco | ||
tox | ||
wheel |
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 @@ | ||
sqlparse |
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,5 @@ | ||
-r package.txt | ||
|
||
coveralls | ||
pytest | ||
pytest-cov |
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 @@ | ||
[wheel] | ||
universal = 1 |
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,37 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
format-sql | ||
Copyright (c) 2014, Friedrich Paetzke (f.paetzke@gmail.com) | ||
All rights reserved. | ||
""" | ||
from setuptools import find_packages, setup | ||
|
||
setup(name='format-sql', | ||
py_modules=['format_sql'], | ||
description='format-sql is a tool to format SQL in your Python strings!', | ||
long_description=(open('README.rst').read()), | ||
version='0.0.0', | ||
license='BSD', | ||
author='Friedrich Paetzke', | ||
author_email='f.paetzke@gmail.com', | ||
url='https://github.com/paetzke/format-sql', | ||
packages=find_packages(exclude=['tests*']), | ||
install_requires=open('requirements/package.txt').read().splitlines(), | ||
entry_points={ | ||
'console_scripts': ['format-sql = format_sql.format_sql:main'] | ||
}, | ||
classifiers=[ | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: BSD License', | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 2', | ||
'Programming Language :: Python :: 2.7', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: 3.3', | ||
'Programming Language :: Python :: Implementation :: CPython', | ||
'Programming Language :: Python :: Implementation :: PyPy', | ||
'Topic :: Software Development :: Libraries', | ||
'Topic :: Utilities', | ||
]) |
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,8 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
format-sql | ||
Copyright (c) 2014, Friedrich Paetzke (f.paetzke@gmail.com) | ||
All rights reserved. | ||
""" |
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,7 @@ | ||
def func(): | ||
sql = """ | ||
select * | ||
from my_table; | ||
""" | ||
|
||
return None |
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,8 @@ | ||
def func(): | ||
sql = """ | ||
SELECT | ||
* | ||
FROM | ||
my_table; """ | ||
|
||
return None |
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 @@ | ||
def func(): | ||
sql = """ | ||
select * | ||
from my_table; | ||
""" | ||
|
||
sql2 = """ | ||
No SQL! | ||
""" | ||
return None |
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 @@ | ||
def func(): | ||
sql = """ | ||
SELECT | ||
* | ||
FROM | ||
my_table; """ | ||
|
||
sql2 = """ | ||
No SQL! | ||
""" | ||
return None |
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,8 @@ | ||
def func(): | ||
sql = """ | ||
select * | ||
from my_table as mt join ma_table as ta on ma.id = k.id | ||
where idt=4; | ||
""" | ||
|
||
return None |
Oops, something went wrong.