Created
August 10, 2015 17:41
-
-
Save atsaki/e606fcb40744627b27ee to your computer and use it in GitHub Desktop.
sqlparseによるSQLのフォーマット ref: http://qiita.com/atsaki/items/eb36a6a7935d525a43e7
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
def format(sql, **options): | |
"""Format *sql* according to *options*. | |
Available options are documented in :ref:`formatting`. | |
In addition to the formatting options this function accepts the | |
keyword "encoding" which determines the encoding of the statement. | |
:returns: The formatted SQL statement as string. | |
""" | |
encoding = options.pop('encoding', None) | |
stack = engine.FilterStack() | |
options = formatter.validate_options(options) | |
stack = formatter.build_filter_stack(stack, options) | |
stack.postprocess.append(filters.SerializerUnicode()) | |
return ''.join(stack.run(sql, encoding)) |
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
$ pip install 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
$ SQL='select t1.c1 A, t2.c2 B from t1 join t2 on t1.id = t2.id where t1.c1 = "HOGE";' | |
$ echo "$SQL" | sqlformat -r -k upper - | |
SELECT t1.c1 A, | |
t2.c2 B | |
FROM t1 | |
JOIN t2 ON t1.id = t2.id | |
WHERE t1.c1 = "HOGE"; |
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
>>> stack = engine.FilterStack() | |
>>> stack.enable_grouping() | |
>>> stack.stmtprocess.append(MyReindentFilter()) | |
>>> stack.postprocess.append(filters.SerializerUnicode()) | |
>>> print stack.run(sql).next() | |
select | |
t1.c1 A | |
,t2.c2 B | |
from t1 | |
join t2 on t1.id = t2.id | |
where t1.c1 = "HOGE" | |
and t2.c2 = 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
>>> print stack.run('select a, b, c FROM (select a, b, c FROM t1) t2;').next() | |
select | |
a | |
,b | |
,c | |
FROM | |
(select | |
a | |
,b | |
,c | |
FROM t1) t2; |
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
>>> import sqlparse | |
>>> sql = 'select t1.c1 A, t2.c2 B from t1 join t2 on t1.id = t2.id where t1.c1 = "HOGE";' | |
>>> print sqlparse.format(sql, reindent=True, keyword_case='upper') | |
SELECT t1.c1 A, | |
t2.c2 B | |
FROM t1 | |
JOIN t2 ON t1.id = t2.id | |
WHERE t1.c1 = "HOGE"; | |
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
>>> parsed = sqlparse.parse(sql) | |
>>> parsed | |
(<Statement 'select...' at 0x1077c6160>,) |
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
>>> sqlparse.parse("select 1; select 2;") | |
(<Statement 'select...' at 0x1077c6958>, | |
<Statement 'select...' at 0x1077c6848>) |
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
>>> stmt = parsed[0] | |
>>> for t in stmt.tokens: | |
... print type(t), t | |
... | |
<class 'sqlparse.sql.Token'> select | |
<class 'sqlparse.sql.Token'> | |
<class 'sqlparse.sql.IdentifierList'> t1.c1 A, t2.c2 B | |
<class 'sqlparse.sql.Token'> | |
<class 'sqlparse.sql.Token'> from | |
<class 'sqlparse.sql.Token'> | |
<class 'sqlparse.sql.Identifier'> t1 | |
<class 'sqlparse.sql.Token'> | |
<class 'sqlparse.sql.Token'> join | |
<class 'sqlparse.sql.Token'> | |
<class 'sqlparse.sql.Identifier'> t2 | |
<class 'sqlparse.sql.Token'> | |
<class 'sqlparse.sql.Token'> on | |
<class 'sqlparse.sql.Token'> | |
<class 'sqlparse.sql.Comparison'> t1.id = t2.id | |
<class 'sqlparse.sql.Token'> | |
<class 'sqlparse.sql.Where'> where t1.c1 = "HOGE"; |
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
>>> from sqlparse import engine | |
>>> stack = engine.FilterStack() | |
>>> stack.postprocess.append(filters.SerializerUnicode()) | |
>>> print stack.run(sql).next() | |
select t1.c1 A, t2.c2 B from t1 join t2 on t1.id = t2.id where t1.c1 = "HOGE"; |
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
>>> from sqlparse.filters import ReindentFilter | |
>>> stack = engine.FilterStack() | |
>>> stack.enable_grouping() | |
>>> stack.stmtprocess.append(ReindentFilter()) | |
>>> stack.postprocess.append(filters.SerializerUnicode()) | |
>>> print stack.run(sql).next() | |
select t1.c1 A, | |
t2.c2 B | |
from t1 | |
join t2 on t1.id = t2.id | |
where t1.c1 = "HOGE"; |
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
from sqlparse.sql import Function | |
class MyReindentFilter(ReindentFilter): | |
def _process_identifierlist(self, tlist): | |
identifiers = list(tlist.get_identifiers()) | |
if len(identifiers) > 1 and not tlist.within(Function): | |
first = identifiers[0] | |
self.indent += 1 | |
tlist.insert_before(first, self.nl()) | |
self.offset -= 1 | |
tlist.insert_after(first, self.nl()) | |
for token in identifiers[1:len(identifiers)-1]: | |
prev = tlist.token_prev(tlist.token_index(token), False) | |
if prev and prev.is_whitespace(): | |
prev.value = '' | |
tlist.insert_after(token, self.nl()) | |
last = identifiers[-1] | |
prev = tlist.token_prev(tlist.token_index(last), False) | |
if prev and prev.is_whitespace(): | |
prev.value = '' | |
self.offset += 1 | |
self.indent -= 1 | |
self._process_default(tlist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment