22
33import itertools
44
5+ from sqlparse import sql
56from sqlparse import tokens as T
6- from sqlparse .sql import *
77
88
99def _group_left_right (tlist , ttype , value , cls ,
@@ -83,34 +83,35 @@ def _find_matching(i, tl, stt, sva, ett, eva):
8383 token = tlist .token_next_match (idx , start_ttype , start_value )
8484
8585def group_if (tlist ):
86- _group_matching (tlist , T .Keyword , 'IF' , T .Keyword , 'END IF' , If , True )
86+ _group_matching (tlist , T .Keyword , 'IF' , T .Keyword , 'END IF' , sql . If , True )
8787
8888def group_for (tlist ):
89- _group_matching (tlist , T .Keyword , 'FOR' , T .Keyword , 'END LOOP' , For , True )
89+ _group_matching (tlist , T .Keyword , 'FOR' , T .Keyword , 'END LOOP' ,
90+ sql .For , True )
9091
9192def group_as (tlist ):
9293 def _right_valid (token ):
9394 # Currently limited to DML/DDL. Maybe additional more non SQL reserved
9495 # keywords should appear here (see issue8).
9596 return not token .ttype in (T .DML , T .DDL )
96- _group_left_right (tlist , T .Keyword , 'AS' , Identifier ,
97+ _group_left_right (tlist , T .Keyword , 'AS' , sql . Identifier ,
9798 check_right = _right_valid )
9899
99100def group_assignment (tlist ):
100- _group_left_right (tlist , T .Assignment , ':=' , Assignment ,
101+ _group_left_right (tlist , T .Assignment , ':=' , sql . Assignment ,
101102 include_semicolon = True )
102103
103104def group_comparsion (tlist ):
104105 def _parts_valid (token ):
105106 return (token .ttype in (T .String .Symbol , T .Name , T .Number ,
106107 T .Number .Integer , T .Literal )
107- or isinstance (token , (Identifier ,)))
108- _group_left_right (tlist , T .Operator .Comparsion , None , Comparsion ,
108+ or isinstance (token , (sql . Identifier ,)))
109+ _group_left_right (tlist , T .Operator .Comparsion , None , sql . Comparsion ,
109110 check_left = _parts_valid , check_right = _parts_valid )
110111
111112
112113def group_case (tlist ):
113- _group_matching (tlist , T .Keyword , 'CASE' , T .Keyword , 'END' , Case ,
114+ _group_matching (tlist , T .Keyword , 'CASE' , T .Keyword , 'END' , sql . Case ,
114115 include_semicolon = True , recurse = True )
115116
116117
@@ -131,40 +132,40 @@ def _consume_cycle(tl, i):
131132
132133 # bottom up approach: group subgroups first
133134 [group_identifier (sgroup ) for sgroup in tlist .get_sublists ()
134- if not isinstance (sgroup , Identifier )]
135+ if not isinstance (sgroup , sql . Identifier )]
135136
136137 # real processing
137138 idx = 0
138- token = tlist .token_next_by_instance (idx , Function )
139+ token = tlist .token_next_by_instance (idx , sql . Function )
139140 if token is None :
140141 token = tlist .token_next_by_type (idx , (T .String .Symbol , T .Name ))
141142 while token :
142143 identifier_tokens = [token ]+ list (
143144 _consume_cycle (tlist ,
144145 tlist .token_index (token )+ 1 ))
145146 if not (len (identifier_tokens ) == 1
146- and isinstance (identifier_tokens [0 ], Function )):
147- group = tlist .group_tokens (Identifier , identifier_tokens )
147+ and isinstance (identifier_tokens [0 ], sql . Function )):
148+ group = tlist .group_tokens (sql . Identifier , identifier_tokens )
148149 idx = tlist .token_index (group )+ 1
149150 else :
150151 idx += 1
151- token = tlist .token_next_by_instance (idx , Function )
152+ token = tlist .token_next_by_instance (idx , sql . Function )
152153 if token is None :
153154 token = tlist .token_next_by_type (idx , (T .String .Symbol , T .Name ))
154155
155156
156157def group_identifier_list (tlist ):
157158 [group_identifier_list (sgroup ) for sgroup in tlist .get_sublists ()
158- if not isinstance (sgroup , (Identifier , IdentifierList ))]
159+ if not isinstance (sgroup , (sql . Identifier , sql . IdentifierList ))]
159160 idx = 0
160161 # Allowed list items
161- fend1_funcs = [lambda t : isinstance (t , Identifier ),
162+ fend1_funcs = [lambda t : isinstance (t , sql . Identifier ),
162163 lambda t : t .is_whitespace (),
163164 lambda t : t .ttype == T .Wildcard ,
164165 lambda t : t .match (T .Keyword , 'null' ),
165166 lambda t : t .ttype == T .Number .Integer ,
166167 lambda t : t .ttype == T .String .Single ,
167- lambda t : isinstance (t , Comparsion ),
168+ lambda t : isinstance (t , sql . Comparsion ),
168169 ]
169170 tcomma = tlist .token_next_match (idx , T .Punctuation , ',' )
170171 start = None
@@ -190,7 +191,7 @@ def group_identifier_list(tlist):
190191 if next_ is None or not next_ .match (T .Punctuation , ',' ):
191192 # Reached the end of the list
192193 tokens = tlist .tokens_between (start , after )
193- group = tlist .group_tokens (IdentifierList , tokens )
194+ group = tlist .group_tokens (sql . IdentifierList , tokens )
194195 start = None
195196 tcomma = tlist .token_next_match (tlist .token_index (group )+ 1 ,
196197 T .Punctuation , ',' )
@@ -199,11 +200,12 @@ def group_identifier_list(tlist):
199200
200201
201202def group_parenthesis (tlist ):
202- _group_matching (tlist , T .Punctuation , '(' , T .Punctuation , ')' , Parenthesis )
203+ _group_matching (tlist , T .Punctuation , '(' , T .Punctuation , ')' ,
204+ sql .Parenthesis )
203205
204206def group_comments (tlist ):
205207 [group_comments (sgroup ) for sgroup in tlist .get_sublists ()
206- if not isinstance (sgroup , Comment )]
208+ if not isinstance (sgroup , sql . Comment )]
207209 idx = 0
208210 token = tlist .token_next_by_type (idx , T .Comment )
209211 while token :
@@ -217,13 +219,13 @@ def group_comments(tlist):
217219 eidx = tlist .token_index (end )
218220 grp_tokens = tlist .tokens_between (token ,
219221 tlist .token_prev (eidx , False ))
220- group = tlist .group_tokens (Comment , grp_tokens )
222+ group = tlist .group_tokens (sql . Comment , grp_tokens )
221223 idx = tlist .token_index (group )
222224 token = tlist .token_next_by_type (idx , T .Comment )
223225
224226def group_where (tlist ):
225227 [group_where (sgroup ) for sgroup in tlist .get_sublists ()
226- if not isinstance (sgroup , Where )]
228+ if not isinstance (sgroup , sql . Where )]
227229 idx = 0
228230 token = tlist .token_next_match (idx , T .Keyword , 'WHERE' )
229231 stopwords = ('ORDER' , 'GROUP' , 'LIMIT' , 'UNION' )
@@ -234,41 +236,42 @@ def group_where(tlist):
234236 end = tlist ._groupable_tokens [- 1 ]
235237 else :
236238 end = tlist .tokens [tlist .token_index (end )- 1 ]
237- group = tlist .group_tokens (Where , tlist .tokens_between (token , end ))
239+ group = tlist .group_tokens (sql .Where ,
240+ tlist .tokens_between (token , end ))
238241 idx = tlist .token_index (group )
239242 token = tlist .token_next_match (idx , T .Keyword , 'WHERE' )
240243
241244def group_aliased (tlist ):
242245 [group_aliased (sgroup ) for sgroup in tlist .get_sublists ()
243- if not isinstance (sgroup , Identifier )]
246+ if not isinstance (sgroup , sql . Identifier )]
244247 idx = 0
245- token = tlist .token_next_by_instance (idx , Identifier )
248+ token = tlist .token_next_by_instance (idx , sql . Identifier )
246249 while token :
247250 next_ = tlist .token_next (tlist .token_index (token ))
248- if next_ is not None and isinstance (next_ , Identifier ):
251+ if next_ is not None and isinstance (next_ , sql . Identifier ):
249252 grp = tlist .tokens_between (token , next_ )[1 :]
250253 token .tokens .extend (grp )
251254 for t in grp :
252255 tlist .tokens .remove (t )
253256 idx = tlist .token_index (token )+ 1
254- token = tlist .token_next_by_instance (idx , Identifier )
257+ token = tlist .token_next_by_instance (idx , sql . Identifier )
255258
256259
257260def group_typecasts (tlist ):
258- _group_left_right (tlist , T .Punctuation , '::' , Identifier )
261+ _group_left_right (tlist , T .Punctuation , '::' , sql . Identifier )
259262
260263
261264def group_functions (tlist ):
262265 [group_functions (sgroup ) for sgroup in tlist .get_sublists ()
263- if not isinstance (sgroup , Function )]
266+ if not isinstance (sgroup , sql . Function )]
264267 idx = 0
265268 token = tlist .token_next_by_type (idx , T .Name )
266269 while token :
267270 next_ = tlist .token_next (token )
268- if not isinstance (next_ , Parenthesis ):
271+ if not isinstance (next_ , sql . Parenthesis ):
269272 idx = tlist .token_index (token )+ 1
270273 else :
271- func = tlist .group_tokens (Function ,
274+ func = tlist .group_tokens (sql . Function ,
272275 tlist .tokens_between (token , next_ ))
273276 idx = tlist .token_index (func )+ 1
274277 token = tlist .token_next_by_type (idx , T .Name )
0 commit comments