summaryrefslogtreecommitdiff
path: root/sqlparse
diff options
context:
space:
mode:
authorJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-03-24 19:13:22 +0100
committerJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-03-24 19:13:22 +0100
commit9e4feb233a45620afca7192e127d88d25fe6b390 (patch)
tree164f654fd376d4d23a13a227130df0e12d10adc9 /sqlparse
parent2c96cdc940c166603118cbab3b3589c7b49c978c (diff)
downloadsqlparse-9e4feb233a45620afca7192e127d88d25fe6b390.tar.gz
Optimized output of SQL code
Diffstat (limited to 'sqlparse')
-rw-r--r--sqlparse/filters.py17
1 files changed, 12 insertions, 5 deletions
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index 528a5cb..c880f58 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -76,7 +76,8 @@ class StripComments(Filter):
def StripWhitespace(stream):
"Strip the useless whitespaces from a stream leaving only the minimal ones"
last_type = None
- ignore_group = Whitespace, Whitespace.Newline, Comparison, Punctuation
+ has_space = False
+ ignore_group = frozenset((Comparison, Punctuation))
for token_type, value in stream:
# We got a previous token
@@ -86,14 +87,20 @@ def StripWhitespace(stream):
if token_type in Whitespace:
print '\t', repr(token_type), repr(value)
- if last_type in ignore_group:
- continue
- value = ' '
+ has_space = True
+ continue
# Ignore first empty spaces and dot-commas
- elif token_type in ignore_group:
+ elif token_type in (Whitespace, Whitespace.Newline, ignore_group):
continue
+ # Yield a whitespace if it can't be ignored
+ if has_space:
+ if not ignore_group.intersection((last_type, token_type)):
+ yield Whitespace, ' '
+ has_space = False
+
+ # Yield the token and set its type for checking with the next one
yield token_type, value
last_type = token_type