summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2011-06-10 22:08:10 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2011-06-10 22:08:10 +0200
commitf6f2e20e4c61d50fbd5cbf0e685b03d8c5927a3a (patch)
treea7ab16a37315dbace58d6a87e9d4574eb5b382b2
parent47e7f56a52870bdb278225fc05866efcb5e65a1d (diff)
downloadsqlparse-f6f2e20e4c61d50fbd5cbf0e685b03d8c5927a3a.tar.gz
Fix removal of comments when strip_comments is True (fixes issue38).
-rw-r--r--CHANGES1
-rw-r--r--sqlparse/filters.py14
-rw-r--r--tests/test_regressions.py7
3 files changed, 18 insertions, 4 deletions
diff --git a/CHANGES b/CHANGES
index 37f63f5..63a4ab9 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,7 @@ In Development
Bug Fixes
* Improve parsing of floats (thanks to Kris).
* When formatting a statement a space before LIMIT was removed (issue35).
+ * Fix strip_comments flag (issue38, reported by ooberm...@gmail.com).
Release 0.1.2 (Nov 23, 2010)
diff --git a/sqlparse/filters.py b/sqlparse/filters.py
index b4f252e..813be99 100644
--- a/sqlparse/filters.py
+++ b/sqlparse/filters.py
@@ -57,10 +57,16 @@ class IdentifierCaseFilter(_CaseFilter):
class StripCommentsFilter(Filter):
+ def _get_next_comment(self, tlist):
+ # TODO(andi) Comment types should be unified, see related issue38
+ token = tlist.token_next_by_instance(0, sql.Comment)
+ if token is None:
+ token = tlist.token_next_by_type(0, T.Comment)
+ return token
+
def _process(self, tlist):
- clss = set([x.__class__ for x in tlist.tokens])
- while sql.Comment in clss:
- token = tlist.token_next_by_instance(0, sql.Comment)
+ token = self._get_next_comment(tlist)
+ while token:
tidx = tlist.token_index(token)
prev = tlist.token_prev(tidx, False)
next_ = tlist.token_next(tidx, False)
@@ -73,7 +79,7 @@ class StripCommentsFilter(Filter):
tlist.tokens[tidx] = sql.Token(T.Whitespace, ' ')
else:
tlist.tokens.pop(tidx)
- clss = set([x.__class__ for x in tlist.tokens])
+ token = self._get_next_comment(tlist)
def process(self, stack, stmt):
[self.process(stack, sgroup) for sgroup in stmt.get_sublists()]
diff --git a/tests/test_regressions.py b/tests/test_regressions.py
index cbe1234..6624d33 100644
--- a/tests/test_regressions.py
+++ b/tests/test_regressions.py
@@ -39,3 +39,10 @@ class RegressionTests(TestCaseBase):
self.ndiffAssertEqual(sql, "\n".join(["select *",
"from foo",
"where bar = 1 limit 1"]))
+
+ def test_issue38(self):
+ sql = sqlparse.format("SELECT foo; -- comment",
+ strip_comments=True)
+ self.ndiffAssertEqual(sql, "SELECT foo;")
+ sql = sqlparse.format("/* foo */", strip_comments=True)
+ self.ndiffAssertEqual(sql, "")