77
88import re
99
10- from os .path import abspath , join
11-
1210from sqlparse import sql , tokens as T
1311from sqlparse .compat import u , text_type
14- from sqlparse .engine import FilterStack
15- from sqlparse .pipeline import Pipeline
16- from sqlparse .tokens import (Comment , Comparison , Keyword , Name , Punctuation ,
17- String , Whitespace )
18- from sqlparse .utils import memoize_generator
1912from sqlparse .utils import split_unquoted_newlines
2013
2114
@@ -74,130 +67,6 @@ def process(self, stack, stream):
7467 yield ttype , value
7568
7669
77- class GetComments (object ):
78- """Get the comments from a stack"""
79- def process (self , stack , stream ):
80- for token_type , value in stream :
81- if token_type in Comment :
82- yield token_type , value
83-
84-
85- class StripComments (object ):
86- """Strip the comments from a stack"""
87- def process (self , stack , stream ):
88- for token_type , value in stream :
89- if token_type not in Comment :
90- yield token_type , value
91-
92-
93- def StripWhitespace (stream ):
94- "Strip the useless whitespaces from a stream leaving only the minimal ones"
95- last_type = None
96- has_space = False
97- ignore_group = frozenset ((Comparison , Punctuation ))
98-
99- for token_type , value in stream :
100- # We got a previous token (not empty first ones)
101- if last_type :
102- if token_type in Whitespace :
103- has_space = True
104- continue
105-
106- # Ignore first empty spaces and dot-commas
107- elif token_type in (Whitespace , Whitespace .Newline , ignore_group ):
108- continue
109-
110- # Yield a whitespace if it can't be ignored
111- if has_space :
112- if not ignore_group .intersection ((last_type , token_type )):
113- yield Whitespace , ' '
114- has_space = False
115-
116- # Yield the token and set its type for checking with the next one
117- yield token_type , value
118- last_type = token_type
119-
120-
121- class IncludeStatement (object ):
122- """Filter that enable a INCLUDE statement"""
123-
124- def __init__ (self , dirpath = "." , maxrecursive = 10 , raiseexceptions = False ):
125- if maxrecursive <= 0 :
126- raise ValueError ('Max recursion limit reached' )
127-
128- self .dirpath = abspath (dirpath )
129- self .maxRecursive = maxrecursive
130- self .raiseexceptions = raiseexceptions
131-
132- self .detected = False
133-
134- @memoize_generator
135- def process (self , stack , stream ):
136- # Run over all tokens in the stream
137- for token_type , value in stream :
138- # INCLUDE statement found, set detected mode
139- if token_type in Name and value .upper () == 'INCLUDE' :
140- self .detected = True
141- continue
142-
143- # INCLUDE statement was found, parse it
144- elif self .detected :
145- # Omit whitespaces
146- if token_type in Whitespace :
147- continue
148-
149- # Found file path to include
150- if token_type in String .Symbol :
151- # Get path of file to include
152- path = join (self .dirpath , value [1 :- 1 ])
153-
154- try :
155- f = open (path )
156- raw_sql = f .read ()
157- f .close ()
158-
159- # There was a problem loading the include file
160- except IOError as err :
161- # Raise the exception to the interpreter
162- if self .raiseexceptions :
163- raise
164-
165- # Put the exception as a comment on the SQL code
166- yield Comment , u'-- IOError: %s\n ' % err
167-
168- else :
169- # Create new FilterStack to parse readed file
170- # and add all its tokens to the main stack recursively
171- try :
172- filtr = IncludeStatement (self .dirpath ,
173- self .maxRecursive - 1 ,
174- self .raiseexceptions )
175-
176- # Max recursion limit reached
177- except ValueError as err :
178- # Raise the exception to the interpreter
179- if self .raiseexceptions :
180- raise
181-
182- # Put the exception as a comment on the SQL code
183- yield Comment , u'-- ValueError: %s\n ' % err
184-
185- stack = FilterStack ()
186- stack .preprocess .append (filtr )
187-
188- for tv in stack .run (raw_sql ):
189- yield tv
190-
191- # Set normal mode
192- self .detected = False
193-
194- # Don't include any token while in detected mode
195- continue
196-
197- # Normal token
198- yield token_type , value
199-
200-
20170# ----------------------
20271# statement process
20372
@@ -520,57 +389,6 @@ def process(self, stack, group):
520389 group .tokens = self ._process (stack , group , group .tokens )
521390
522391
523- class ColumnsSelect (object ):
524- """Get the columns names of a SELECT query"""
525- def process (self , stack , stream ):
526- mode = 0
527- oldValue = ""
528- parenthesis = 0
529-
530- for token_type , value in stream :
531- # Ignore comments
532- if token_type in Comment :
533- continue
534-
535- # We have not detected a SELECT statement
536- if mode == 0 :
537- if token_type in Keyword and value == 'SELECT' :
538- mode = 1
539-
540- # We have detected a SELECT statement
541- elif mode == 1 :
542- if value == 'FROM' :
543- if oldValue :
544- yield oldValue
545-
546- mode = 3 # Columns have been checked
547-
548- elif value == 'AS' :
549- oldValue = ""
550- mode = 2
551-
552- elif (token_type == Punctuation
553- and value == ',' and not parenthesis ):
554- if oldValue :
555- yield oldValue
556- oldValue = ""
557-
558- elif token_type not in Whitespace :
559- if value == '(' :
560- parenthesis += 1
561- elif value == ')' :
562- parenthesis -= 1
563-
564- oldValue += value
565-
566- # We are processing an AS keyword
567- elif mode == 2 :
568- # We check also for Keywords because a bug in SQLParse
569- if token_type == Name or token_type == Keyword :
570- yield value
571- mode = 1
572-
573-
574392# ---------------------------
575393# postprocess
576394
@@ -583,15 +401,6 @@ def process(self, stack, stmt):
583401 return res
584402
585403
586- def Tokens2Unicode (stream ):
587- result = ""
588-
589- for _ , value in stream :
590- result += u (value )
591-
592- return result
593-
594-
595404class OutputFilter (object ):
596405 varname_prefix = ''
597406
@@ -704,34 +513,3 @@ def _process(self, stream, varname, has_nl):
704513 # Close quote
705514 yield sql .Token (T .Text , '"' )
706515 yield sql .Token (T .Punctuation , ';' )
707-
708-
709- class Limit (object ):
710- """Get the LIMIT of a query.
711-
712- If not defined, return -1 (SQL specification for no LIMIT query)
713- """
714- def process (self , stack , stream ):
715- index = 7
716- stream = list (stream )
717- stream .reverse ()
718-
719- # Run over all tokens in the stream from the end
720- for token_type , value in stream :
721- index -= 1
722-
723- # if index and token_type in Keyword:
724- if index and token_type in Keyword and value == 'LIMIT' :
725- return stream [4 - index ][1 ]
726-
727- return - 1
728-
729-
730- def compact (stream ):
731- """Function that return a compacted version of the stream"""
732- pipe = Pipeline ()
733-
734- pipe .append (StripComments ())
735- pipe .append (StripWhitespace )
736-
737- return pipe (stream )
0 commit comments