From 27ad2564f30f1021a96addafb7b7eb6a7504c28f Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 13 Sep 2020 09:25:56 +0200 Subject: Don't flush token streams iff they only contain whitespace tokens (fixes #496). --- CHANGELOG | 5 +++++ sqlparse/engine/statement_splitter.py | 2 +- tests/test_grouping.py | 4 ---- tests/test_split.py | 7 +++++++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index dfa7e9d..2c6e854 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,11 @@ Notable Changes * Remove support for end-of-life Python 2.7 and 3.4. Python 3.5+ is now required. +* Remaining strings that only consist of whitespaces are not treated as + statements anymore. Code that ignored the last element from + sqlparse.split() should be updated accordingly since that function + now doesn't return an empty string as the last element in some + cases (issue496). Bug Fixes diff --git a/sqlparse/engine/statement_splitter.py b/sqlparse/engine/statement_splitter.py index fb22c65..40cfec3 100644 --- a/sqlparse/engine/statement_splitter.py +++ b/sqlparse/engine/statement_splitter.py @@ -103,5 +103,5 @@ class StatementSplitter: self.consume_ws = True # Yield pending statement (if any) - if self.tokens: + if self.tokens and not all(t.is_whitespace for t in self.tokens): yield sql.Statement(self.tokens) diff --git a/tests/test_grouping.py b/tests/test_grouping.py index 879ba29..cf629e9 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -399,10 +399,6 @@ def test_statement_get_type(): assert f(' update foo').get_type() == 'UPDATE' assert f('\nupdate foo').get_type() == 'UPDATE' assert f('foo').get_type() == 'UNKNOWN' - # Statements that have a whitespace after the closing semicolon - # are parsed as two statements where later only consists of the - # trailing whitespace. - assert f('\n').get_type() == 'UNKNOWN' def test_identifier_with_operators(): diff --git a/tests/test_split.py b/tests/test_split.py index 8214756..c073298 100644 --- a/tests/test_split.py +++ b/tests/test_split.py @@ -139,6 +139,13 @@ def test_split_simple(): assert stmts[1] == 'select * from bar;' +def test_split_ignores_empty_newlines(): + stmts = sqlparse.split('select foo;\nselect bar;\n') + assert len(stmts) == 2 + assert stmts[0] == 'select foo;' + assert stmts[1] == 'select bar;' + + def test_split_quotes_with_new_line(): stmts = sqlparse.split('select "foo\nbar"') assert len(stmts) == 1 -- cgit v1.2.1