summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2015-07-26 11:20:49 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2015-07-26 11:20:49 +0200
commitfb6b59dece40a608a1c6cf741a4ac6ee88d85d2f (patch)
tree2d013f2e9d2de4fffab927ca03e33dee0ee609c9
parent8b5a957f4dad82fda0dc174538fdaf0860a9256a (diff)
downloadsqlparse-fb6b59dece40a608a1c6cf741a4ac6ee88d85d2f.tar.gz
Ignore comments at beginning of statement when calling Statement.get_type (fixes #186).
-rw-r--r--CHANGES2
-rw-r--r--sqlparse/sql.py12
-rw-r--r--tests/test_regressions.py6
3 files changed, 18 insertions, 2 deletions
diff --git a/CHANGES b/CHANGES
index 2a13faa..6b5b4b7 100644
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,8 @@ Bug Fixes
* sqlformat command line tool doesn't duplicat newlines anymore (issue191).
* Don't mix up MySQL comments starting with hash and MSSQL
temp tables (issue192).
+* Statement.get_type() now ignores comments at the beginning of
+ a statement (issue186).
Release 0.1.15 (Apr 15, 2015)
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index 717661f..5ecfbdc 100644
--- a/sqlparse/sql.py
+++ b/sqlparse/sql.py
@@ -240,15 +240,20 @@ class TokenList(Token):
def _groupable_tokens(self):
return self.tokens
- def token_first(self, ignore_whitespace=True):
+ def token_first(self, ignore_whitespace=True, ignore_comments=False):
"""Returns the first child token.
If *ignore_whitespace* is ``True`` (the default), whitespace
tokens are ignored.
+
+ if *ignore_comments* is ``True`` (default: ``False``), comments are
+ ignored too.
"""
for token in self.tokens:
if ignore_whitespace and token.is_whitespace():
continue
+ if ignore_comments and isinstance(token, Comment):
+ continue
return token
def token_next_by_instance(self, idx, clss):
@@ -468,8 +473,11 @@ class Statement(TokenList):
The returned value is a string holding an upper-cased reprint of
the first DML or DDL keyword. If the first token in this group
isn't a DML or DDL keyword "UNKNOWN" is returned.
+
+ Whitespaces and comments at the beginning of the statement
+ are ignored.
"""
- first_token = self.token_first()
+ first_token = self.token_first(ignore_comments=True)
if first_token is None:
# An "empty" statement that either has not tokens at all
# or only whitespace tokens.
diff --git a/tests/test_regressions.py b/tests/test_regressions.py
index 6da94dc..ea8cd56 100644
--- a/tests/test_regressions.py
+++ b/tests/test_regressions.py
@@ -255,3 +255,9 @@ END;
SELECT * FROM a.b;"""
splitted = sqlparse.split(sql)
assert len(splitted) == 2
+
+
+def test_issue186_get_type():
+ sql = "-- comment\ninsert into foo"
+ p = sqlparse.parse(sql)[0]
+ assert p.get_type() == 'INSERT'