From 4b7b0d3a01d8061572137f82a53de7365f3a241c Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Wed, 15 Apr 2015 18:17:57 +0200 Subject: Update version number. --- CHANGES | 4 ++-- sqlparse/__init__.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 51a2551..9be9256 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,5 @@ -Development Version -------------------- +Release 0.1.15 (Apr 15, 2015) +----------------------------- Bug Fixes * Fix a regression for identifiers with square bracktes diff --git a/sqlparse/__init__.py b/sqlparse/__init__.py index c956f66..21028a4 100644 --- a/sqlparse/__init__.py +++ b/sqlparse/__init__.py @@ -6,7 +6,7 @@ """Parse SQL statements.""" -__version__ = '0.1.14' +__version__ = '0.1.15' # Setup namespace -- cgit v1.2.1 From 4f5195481a89e87a07a064d2c253bb6f74e911d4 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Wed, 15 Apr 2015 18:23:04 +0200 Subject: Switch back to development mode. --- CHANGES | 6 ++++++ sqlparse/__init__.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 9be9256..6f5b8eb 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,9 @@ +Development version +------------------- + +Nothing new yet + + Release 0.1.15 (Apr 15, 2015) ----------------------------- diff --git a/sqlparse/__init__.py b/sqlparse/__init__.py index 21028a4..6c0590a 100644 --- a/sqlparse/__init__.py +++ b/sqlparse/__init__.py @@ -6,7 +6,7 @@ """Parse SQL statements.""" -__version__ = '0.1.15' +__version__ = '0.1.16-dev' # Setup namespace -- cgit v1.2.1 From d9b717a12da7805d5a37b6b659300e449c42bd2f Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 19 Apr 2015 02:11:39 +0200 Subject: Improve detection of aliased identifiers (fixes #185). --- CHANGES | 3 ++- sqlparse/sql.py | 3 ++- tests/test_grouping.py | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 6f5b8eb..9e5bca1 100644 --- a/CHANGES +++ b/CHANGES @@ -1,7 +1,8 @@ Development version ------------------- -Nothing new yet +Bug Fixes +* Fix a regression in get_alias() introduced in 0.1.15 (issue185). Release 0.1.15 (Apr 15, 2015) diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 9fcb546..717661f 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -397,7 +397,8 @@ class TokenList(Token): return self._get_first_name(kw, keywords=True) # "name alias" or "complicated column expression alias" - if len(self.tokens) > 2: + if len(self.tokens) > 2 \ + and self.token_next_by_type(0, T.Whitespace) is not None: return self._get_first_name(reverse=True) return None diff --git a/tests/test_grouping.py b/tests/test_grouping.py index b6edafe..5ade830 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -186,6 +186,11 @@ class TestGrouping(TestCaseBase): self.assertEqual(len(p.tokens), 1) self.assertEqual(p.tokens[0].get_alias(), 'foo') + def test_alias_returns_none(self): # see issue185 + p = sqlparse.parse('foo.bar')[0] + self.assertEqual(len(p.tokens), 1) + self.assertEqual(p.tokens[0].get_alias(), None) + def test_idlist_function(self): # see issue10 too p = sqlparse.parse('foo(1) x, bar')[0] self.assert_(isinstance(p.tokens[0], sql.IdentifierList)) -- cgit v1.2.1 From 621e1d84607a158491ce80c47814e51a8c366498 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 26 Jul 2015 10:38:54 +0200 Subject: Don't treat DECLARE within BEGIN blocks as relevant to split level (fixes #193). --- CHANGES | 1 + sqlparse/engine/filter.py | 2 +- tests/test_regressions.py | 13 ++++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 9e5bca1..9e0b44b 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,7 @@ Development version Bug Fixes * Fix a regression in get_alias() introduced in 0.1.15 (issue185). +* Fix a bug in the splitter regarding DECLARE (issue193). Release 0.1.15 (Apr 15, 2015) diff --git a/sqlparse/engine/filter.py b/sqlparse/engine/filter.py index 9af2f99..e7ea0ec 100644 --- a/sqlparse/engine/filter.py +++ b/sqlparse/engine/filter.py @@ -40,7 +40,7 @@ class StatementFilter: unified = value.upper() - if unified == 'DECLARE' and self._is_create: + if unified == 'DECLARE' and self._is_create and self._begin_depth == 0: self._in_declare = True return 1 diff --git a/tests/test_regressions.py b/tests/test_regressions.py index e30ddf0..6da94dc 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -243,4 +243,15 @@ def test_null_with_as(): ' NULL AS c2', 'FROM t1' ]) - assert formatted == tformatted \ No newline at end of file + assert formatted == tformatted + + +def test_issue193_splitting_function(): + sql = """CREATE FUNCTION a(x VARCHAR(20)) RETURNS VARCHAR(20) +BEGIN + DECLARE y VARCHAR(20); + RETURN x; +END; +SELECT * FROM a.b;""" + splitted = sqlparse.split(sql) + assert len(splitted) == 2 -- cgit v1.2.1 From c66fbacf8b3095c6de264657362e403920edace2 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 26 Jul 2015 10:51:01 +0200 Subject: Don't add duplicate "\n" when using sqlformat tool (fixes #191). --- CHANGES | 1 + bin/sqlformat | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 9e0b44b..859fce8 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,7 @@ Development version Bug Fixes * Fix a regression in get_alias() introduced in 0.1.15 (issue185). * Fix a bug in the splitter regarding DECLARE (issue193). +* sqlformat command line tool doesn't duplicat newlines anymore (issue191). Release 0.1.15 (Apr 15, 2015) diff --git a/bin/sqlformat b/bin/sqlformat index fcee452..cecbed9 100755 --- a/bin/sqlformat +++ b/bin/sqlformat @@ -77,7 +77,7 @@ def main(): data = sys.stdin.read() else: try: - data = '\n'.join(open(args[0]).readlines()) + data = ''.join(open(args[0]).readlines()) except OSError: err = sys.exc_info()[1] # Python 2.5 compatibility _error('Failed to read %s: %s' % (args[0], err), exit_=1) -- cgit v1.2.1 From 8b5a957f4dad82fda0dc174538fdaf0860a9256a Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 26 Jul 2015 11:05:58 +0200 Subject: Recognize MSSQL temp tables and distinguish from MySQL comments (fixes #192). --- CHANGES | 2 ++ sqlparse/lexer.py | 6 +++--- tests/test_parse.py | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 859fce8..2a13faa 100644 --- a/CHANGES +++ b/CHANGES @@ -5,6 +5,8 @@ Bug Fixes * Fix a regression in get_alias() introduced in 0.1.15 (issue185). * Fix a bug in the splitter regarding DECLARE (issue193). * sqlformat command line tool doesn't duplicat newlines anymore (issue191). +* Don't mix up MySQL comments starting with hash and MSSQL + temp tables (issue192). Release 0.1.15 (Apr 15, 2015) diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py index 9533e8a..fd29f5c 100644 --- a/sqlparse/lexer.py +++ b/sqlparse/lexer.py @@ -164,10 +164,10 @@ class Lexer(object): tokens = { 'root': [ - (r'(--|#).*?(\r\n|\r|\n)', tokens.Comment.Single), + (r'(--|# ).*?(\r\n|\r|\n)', tokens.Comment.Single), # $ matches *before* newline, therefore we have two patterns # to match Comment.Single - (r'(--|#).*?$', tokens.Comment.Single), + (r'(--|# ).*?$', tokens.Comment.Single), (r'(\r\n|\r|\n)', tokens.Newline), (r'\s+', tokens.Whitespace), (r'/\*', tokens.Comment.Multiline, 'multiline-comments'), @@ -185,7 +185,7 @@ class Lexer(object): # FIXME(andi): VALUES shouldn't be listed here # see https://github.com/andialbrecht/sqlparse/pull/64 (r'VALUES', tokens.Keyword), - (r'@[^\W\d_]\w+', tokens.Name), + (r'(@|##|#)[^\W\d_]\w+', tokens.Name), # IN is special, it may be followed by a parenthesis, but # is never a functino, see issue183 (r'in\b(?=[ (])?', tokens.Keyword), diff --git a/tests/test_parse.py b/tests/test_parse.py index 857685b..6c9d6a6 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -293,3 +293,13 @@ def test_single_line_comments(sql): assert p.tokens[-1].ttype == T.Comment.Single +@pytest.mark.parametrize('sql', [ + 'foo', + '@foo', + '#foo', # see issue192 + '##foo' +]) +def test_names_and_special_names(sql): + p = sqlparse.parse(sql)[0] + assert len(p.tokens) == 1 + assert isinstance(p.tokens[0], sqlparse.sql.Identifier) -- cgit v1.2.1 From fb6b59dece40a608a1c6cf741a4ac6ee88d85d2f Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 26 Jul 2015 11:20:49 +0200 Subject: Ignore comments at beginning of statement when calling Statement.get_type (fixes #186). --- CHANGES | 2 ++ sqlparse/sql.py | 12 ++++++++++-- tests/test_regressions.py | 6 ++++++ 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' -- cgit v1.2.1 From 14ddedee47808ac1cc8e21f32907b01af6af4efc Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 26 Jul 2015 11:23:44 +0200 Subject: Prepare next release. --- CHANGES | 4 ++-- sqlparse/__init__.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 6b5b4b7..30de31e 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,5 @@ -Development version -------------------- +Release 0.1.16 (Jul 26, 2015) +----------------------------- Bug Fixes * Fix a regression in get_alias() introduced in 0.1.15 (issue185). diff --git a/sqlparse/__init__.py b/sqlparse/__init__.py index 6c0590a..b9aadc5 100644 --- a/sqlparse/__init__.py +++ b/sqlparse/__init__.py @@ -6,7 +6,7 @@ """Parse SQL statements.""" -__version__ = '0.1.16-dev' +__version__ = '0.1.16' # Setup namespace -- cgit v1.2.1 From 8987a3a6b540ea35bbb1f9c5f49f3306501289cd Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 26 Jul 2015 11:25:00 +0200 Subject: Switch back to development. --- CHANGES | 6 ++++++ sqlparse/__init__.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 30de31e..0c29db4 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,9 @@ +Development Version +------------------- + +Nothing yet. + + Release 0.1.16 (Jul 26, 2015) ----------------------------- diff --git a/sqlparse/__init__.py b/sqlparse/__init__.py index b9aadc5..83bb684 100644 --- a/sqlparse/__init__.py +++ b/sqlparse/__init__.py @@ -6,7 +6,7 @@ """Parse SQL statements.""" -__version__ = '0.1.16' +__version__ = '0.1.17-dev' # Setup namespace -- cgit v1.2.1