Skip to content
This repository was archived by the owner on Mar 12, 2020. It is now read-only.

Commit 10efe2d

Browse files
committed
Update sqlparse library to version 0.2.3
1 parent 952df06 commit 10efe2d

22 files changed

+165
-174
lines changed

SQLToolsAPI/lib/sqlparse/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2016 Andi Albrecht, [email protected]
44
#
55
# This module is part of python-sqlparse and is released under
6-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

88
"""Parse SQL statements."""
99

@@ -17,7 +17,7 @@
1717

1818
from sqlparse.compat import text_type
1919

20-
__version__ = '0.2.1'
20+
__version__ = '0.2.3'
2121
__all__ = ['engine', 'filters', 'formatter', 'sql', 'tokens', 'cli']
2222

2323

@@ -57,7 +57,7 @@ def format(sql, encoding=None, **options):
5757
options = formatter.validate_options(options)
5858
stack = formatter.build_filter_stack(stack, options)
5959
stack.postprocess.append(filters.SerializerUnicode())
60-
return ''.join(stack.run(sql, encoding))
60+
return u''.join(stack.run(sql, encoding))
6161

6262

6363
def split(sql, encoding=None):

SQLToolsAPI/lib/sqlparse/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Copyright (C) 2016 Andi Albrecht, [email protected]
55
#
66
# This module is part of python-sqlparse and is released under
7-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
7+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
88

99
"""Entrypoint module for `python -m sqlparse`.
1010

SQLToolsAPI/lib/sqlparse/cli.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Copyright (C) 2016 Andi Albrecht, [email protected]
55
#
66
# This module is part of python-sqlparse and is released under
7-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
7+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
88

99
"""Module that contains the command line app.
1010
@@ -21,6 +21,8 @@
2121

2222
import argparse
2323
import sys
24+
from io import TextIOWrapper
25+
from codecs import open, getreader
2426

2527
import sqlparse
2628
from sqlparse.compat import PY2
@@ -118,12 +120,25 @@ def create_parser():
118120
type=int,
119121
help='Column after which lists should be wrapped')
120122

123+
group.add_argument(
124+
'--comma_first',
125+
dest='comma_first',
126+
default=False,
127+
type=bool,
128+
help='Insert linebreak before comma (default False)')
129+
130+
group.add_argument(
131+
'--encoding',
132+
dest='encoding',
133+
default='utf-8',
134+
help='Specify the input encoding (default utf-8)')
135+
121136
return parser
122137

123138

124139
def _error(msg):
125140
"""Print msg and optionally exit with return code exit_."""
126-
sys.stderr.write('[ERROR] {0}\n'.format(msg))
141+
sys.stderr.write(u'[ERROR] {0}\n'.format(msg))
127142
return 1
128143

129144

@@ -132,31 +147,33 @@ def main(args=None):
132147
args = parser.parse_args(args)
133148

134149
if args.filename == '-': # read from stdin
135-
data = sys.stdin.read()
150+
if PY2:
151+
data = getreader(args.encoding)(sys.stdin).read()
152+
else:
153+
data = TextIOWrapper(
154+
sys.stdin.buffer, encoding=args.encoding).read()
136155
else:
137156
try:
138-
# TODO: Needs to deal with encoding
139-
data = ''.join(open(args.filename).readlines())
157+
data = ''.join(open(args.filename, 'r', args.encoding).readlines())
140158
except IOError as e:
141-
return _error('Failed to read {0}: {1}'.format(args.filename, e))
159+
return _error(
160+
u'Failed to read {0}: {1}'.format(args.filename, e))
142161

143162
if args.outfile:
144163
try:
145-
stream = open(args.outfile, 'w')
164+
stream = open(args.outfile, 'w', args.encoding)
146165
except IOError as e:
147-
return _error('Failed to open {0}: {1}'.format(args.outfile, e))
166+
return _error(u'Failed to open {0}: {1}'.format(args.outfile, e))
148167
else:
149168
stream = sys.stdout
150169

151170
formatter_opts = vars(args)
152171
try:
153172
formatter_opts = sqlparse.formatter.validate_options(formatter_opts)
154173
except SQLParseError as e:
155-
return _error('Invalid options: {0}'.format(e))
174+
return _error(u'Invalid options: {0}'.format(e))
156175

157176
s = sqlparse.format(data, **formatter_opts)
158-
if PY2:
159-
s = s.encode('utf-8', 'replace')
160177
stream.write(s)
161178
stream.flush()
162179
return 0

SQLToolsAPI/lib/sqlparse/compat.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2016 Andi Albrecht, [email protected]
44
#
55
# This module is part of python-sqlparse and is released under
6-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

88
"""Python 2/3 compatibility.
99
@@ -23,35 +23,23 @@
2323

2424

2525
if PY3:
26-
def u(s, encoding=None):
27-
return str(s)
28-
29-
3026
def unicode_compatible(cls):
3127
return cls
3228

33-
29+
bytes_type = bytes
3430
text_type = str
3531
string_types = (str,)
3632
from io import StringIO
3733
file_types = (StringIO, TextIOBase)
3834

3935

4036
elif PY2:
41-
def u(s, encoding=None):
42-
encoding = encoding or 'unicode-escape'
43-
try:
44-
return unicode(s)
45-
except UnicodeDecodeError:
46-
return unicode(s, encoding)
47-
48-
4937
def unicode_compatible(cls):
5038
cls.__unicode__ = cls.__str__
5139
cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
5240
return cls
5341

54-
42+
bytes_type = str
5543
text_type = unicode
5644
string_types = (str, unicode,)
5745
from StringIO import StringIO

SQLToolsAPI/lib/sqlparse/engine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2016 Andi Albrecht, [email protected]
44
#
55
# This module is part of python-sqlparse and is released under
6-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

88
from sqlparse.engine import grouping
99
from sqlparse.engine.filter_stack import FilterStack

SQLToolsAPI/lib/sqlparse/engine/filter_stack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2016 Andi Albrecht, [email protected]
44
#
55
# This module is part of python-sqlparse and is released under
6-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

88
"""filter"""
99

SQLToolsAPI/lib/sqlparse/engine/grouping.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2016 Andi Albrecht, [email protected]
44
#
55
# This module is part of python-sqlparse and is released under
6-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

88
from sqlparse import sql
99
from sqlparse import tokens as T
@@ -21,13 +21,13 @@ def _group_matching(tlist, cls):
2121
for idx, token in enumerate(list(tlist)):
2222
tidx = idx - tidx_offset
2323

24-
if token.is_whitespace():
24+
if token.is_whitespace:
2525
# ~50% of tokens will be whitespace. Will checking early
2626
# for them avoid 3 comparisons, but then add 1 more comparison
2727
# for the other ~50% of tokens...
2828
continue
2929

30-
if token.is_group() and not isinstance(token, cls):
30+
if token.is_group and not isinstance(token, cls):
3131
# Check inside previously grouped (ie. parenthesis) if group
3232
# of differnt type is inside (ie, case). though ideally should
3333
# should check for all open/close tokens at once to avoid recursion
@@ -121,7 +121,7 @@ def valid_prev(token):
121121

122122
def valid_next(token):
123123
ttypes = T.DML, T.DDL
124-
return not imt(token, t=ttypes)
124+
return not imt(token, t=ttypes) and token is not None
125125

126126
def post(tlist, pidx, tidx, nidx):
127127
return pidx, nidx
@@ -246,7 +246,7 @@ def group_comments(tlist):
246246
tidx, token = tlist.token_next_by(t=T.Comment)
247247
while token:
248248
eidx, end = tlist.token_not_matching(
249-
lambda tk: imt(tk, t=T.Comment) or tk.is_whitespace(), idx=tidx)
249+
lambda tk: imt(tk, t=T.Comment) or tk.is_whitespace, idx=tidx)
250250
if end is not None:
251251
eidx, end = tlist.token_prev(eidx, skip_ws=False)
252252
tlist.group_tokens(sql.Comment, tidx, eidx)
@@ -343,9 +343,9 @@ def group(stmt):
343343
group_period,
344344
group_arrays,
345345
group_identifier,
346-
group_operator,
347346
group_order,
348347
group_typecasts,
348+
group_operator,
349349
group_as,
350350
group_aliased,
351351
group_assignment,
@@ -372,15 +372,15 @@ def _group(tlist, cls, match,
372372
for idx, token in enumerate(list(tlist)):
373373
tidx = idx - tidx_offset
374374

375-
if token.is_whitespace():
375+
if token.is_whitespace:
376376
continue
377377

378-
if recurse and token.is_group() and not isinstance(token, cls):
378+
if recurse and token.is_group and not isinstance(token, cls):
379379
_group(token, cls, match, valid_prev, valid_next, post, extend)
380380

381381
if match(token):
382382
nidx, next_ = tlist.token_next(tidx)
383-
if valid_prev(prev_) and valid_next(next_):
383+
if prev_ and valid_prev(prev_) and valid_next(next_):
384384
from_idx, to_idx = post(tlist, pidx, tidx, nidx)
385385
grp = tlist.group_tokens(cls, from_idx, to_idx, extend=extend)
386386

SQLToolsAPI/lib/sqlparse/engine/statement_splitter.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2016 Andi Albrecht, [email protected]
44
#
55
# This module is part of python-sqlparse and is released under
6-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

88
from sqlparse import sql, tokens as T
99

@@ -17,7 +17,6 @@ def __init__(self):
1717
def _reset(self):
1818
"""Set the filter attributes to its default values"""
1919
self._in_declare = False
20-
self._in_dbldollar = False
2120
self._is_create = False
2221
self._begin_depth = 0
2322

@@ -27,23 +26,6 @@ def _reset(self):
2726

2827
def _change_splitlevel(self, ttype, value):
2928
"""Get the new split level (increase, decrease or remain equal)"""
30-
# PostgreSQL
31-
if ttype == T.Name.Builtin and value[0] == '$' and value[-1] == '$':
32-
33-
# 2nd dbldollar found. $quote$ completed
34-
# decrease level
35-
if self._in_dbldollar:
36-
self._in_dbldollar = False
37-
return -1
38-
else:
39-
self._in_dbldollar = True
40-
return 1
41-
42-
# if inside $$ everything inside is defining function character.
43-
# Nothing inside can create a new statement
44-
elif self._in_dbldollar:
45-
return 0
46-
4729
# ANSI
4830
# if normal token return
4931
# wouldn't parenthesis increase/decrease a level?

SQLToolsAPI/lib/sqlparse/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2016 Andi Albrecht, [email protected]
44
#
55
# This module is part of python-sqlparse and is released under
6-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

88
"""Exceptions used in this package."""
99

SQLToolsAPI/lib/sqlparse/filters/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (C) 2016 Andi Albrecht, [email protected]
44
#
55
# This module is part of python-sqlparse and is released under
6-
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6+
# the BSD License: https://opensource.org/licenses/BSD-3-Clause
77

88
from sqlparse.filters.others import SerializerUnicode
99
from sqlparse.filters.others import StripCommentsFilter

0 commit comments

Comments
 (0)