Skip to content

Commit a16c087

Browse files
author
quest
committed
various optimizations in sql.py
1 parent 1f8dfd8 commit a16c087

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

sqlparse/engine/grouping.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def _group_matching(tlist, start_ttype, start_value, end_ttype, end_value,
5555
cls, include_semicolon=False, recurse=False):
5656
def _find_matching(i, tl, stt, sva, ett, eva):
5757
depth = 1
58-
for t in tl.tokens[i:]:
58+
for n in xrange(i, len(tl.tokens)):
59+
t = tl.tokens[n]
5960
if t.match(stt, sva):
6061
depth += 1
6162
elif t.match(ett, eva):

sqlparse/sql.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ class Token(object):
1515
the type of the token.
1616
"""
1717

18-
__slots__ = ('value', 'ttype', 'parent')
18+
__slots__ = ('value', 'ttype', 'parent', 'normalized', 'is_keyword')
1919

2020
def __init__(self, ttype, value):
2121
self.value = value
22+
self.normalized = value.upper() if ttype in T.Keyword else value
2223
self.ttype = ttype
24+
self.is_keyword = ttype in T.Keyword
2325
self.parent = None
2426

2527
def __str__(self):
@@ -71,9 +73,9 @@ def match(self, ttype, values, regex=False):
7173
type_matched = self.ttype is ttype
7274
if not type_matched or values is None:
7375
return type_matched
74-
if isinstance(values, basestring):
75-
values = set([values])
7676
if regex:
77+
if isinstance(values, basestring):
78+
values = set([values])
7779
if self.ttype is T.Keyword:
7880
values = set([re.compile(v, re.IGNORECASE) for v in values])
7981
else:
@@ -83,10 +85,18 @@ def match(self, ttype, values, regex=False):
8385
return True
8486
return False
8587
else:
86-
if self.ttype in T.Keyword:
87-
values = set([v.upper() for v in values])
88-
return self.value.upper() in values
88+
if isinstance(values, basestring):
89+
if self.is_keyword:
90+
return values.upper() == self.normalized
91+
else:
92+
return values == self.value
93+
if self.is_keyword:
94+
for v in values:
95+
if v.upper() == self.normalized:
96+
return True
97+
return False
8998
else:
99+
print len(values)
90100
return self.value in values
91101

92102
def is_group(self):
@@ -227,7 +237,8 @@ def token_next_match(self, idx, ttype, value, regex=False):
227237
if not isinstance(idx, int):
228238
idx = self.token_index(idx)
229239

230-
for token in self.tokens[idx:]:
240+
for n in xrange(idx, len(self.tokens)):
241+
token = self.tokens[n]
231242
if token.match(ttype, value, regex):
232243
return token
233244

@@ -395,7 +406,7 @@ def get_type(self):
395406
return 'UNKNOWN'
396407

397408
elif first_token.ttype in (T.Keyword.DML, T.Keyword.DDL):
398-
return first_token.value.upper()
409+
return first_token.normalized
399410

400411
return 'UNKNOWN'
401412

0 commit comments

Comments
 (0)