summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2011-07-24 00:37:47 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2011-07-24 00:37:47 +0200
commit693d7a31c1fff960763ff3ba485142ce67ff708f (patch)
tree683cd31a2bf67fe212d9f1dc145ddd39eb3a50ee
parent0e1cb7ac237cedeac30d26c2ccaa2ea34395b643 (diff)
downloadsqlparse-693d7a31c1fff960763ff3ba485142ce67ff708f.tar.gz
Split statements with IF as functions correctly (fixes issue33).
-rw-r--r--CHANGES2
-rw-r--r--sqlparse/engine/filter.py6
-rw-r--r--tests/test_split.py7
3 files changed, 14 insertions, 1 deletions
diff --git a/CHANGES b/CHANGES
index 1297634..4c3dc65 100644
--- a/CHANGES
+++ b/CHANGES
@@ -8,6 +8,8 @@ Bug Fixes
* Avoid parsing names as keywords (issue39, reported by djo...@taket.org).
* Make sure identifier lists in subselects are grouped (issue40,
reported by djo...@taket.org).
+ * Split statements with IF as functions correctly (issue33, reported
+ by charles....@unige.ch).
Release 0.1.2 (Nov 23, 2010)
diff --git a/sqlparse/engine/filter.py b/sqlparse/engine/filter.py
index 241f5ea..89d9b15 100644
--- a/sqlparse/engine/filter.py
+++ b/sqlparse/engine/filter.py
@@ -21,11 +21,13 @@ class StatementFilter(TokenFilter):
self._in_declare = False
self._in_dbldollar = False
self._is_create = False
+ self._begin_depth = 0
def _reset(self):
self._in_declare = False
self._in_dbldollar = False
self._is_create = False
+ self._begin_depth = 0
def _change_splitlevel(self, ttype, value):
# PostgreSQL
@@ -51,6 +53,7 @@ class StatementFilter(TokenFilter):
return 1
if unified == 'BEGIN':
+ self._begin_depth += 1
if self._in_declare: # FIXME(andi): This makes no sense.
return 0
return 0
@@ -58,13 +61,14 @@ class StatementFilter(TokenFilter):
if unified == 'END':
# Should this respect a preceeding BEGIN?
# In CASE ... WHEN ... END this results in a split level -1.
+ self._begin_depth = max(0, self._begin_depth-1)
return -1
if ttype is T.Keyword.DDL and unified.startswith('CREATE'):
self._is_create = True
return 0
- if unified in ('IF', 'FOR') and self._is_create:
+ if unified in ('IF', 'FOR') and self._is_create and self._begin_depth > 0:
return 1
# Default
diff --git a/tests/test_split.py b/tests/test_split.py
index 1995ca5..a794b76 100644
--- a/tests/test_split.py
+++ b/tests/test_split.py
@@ -103,3 +103,10 @@ class SQLSplitTest(TestCaseBase):
'SELECT 2;')
stmts = sqlparse.split(sql)
self.assertEqual(len(stmts), 2)
+
+ def test_if_function(self): # see issue 33
+ # don't let IF as a function confuse the splitter
+ sql = ('CREATE TEMPORARY TABLE tmp SELECT IF(a=1, a, b) AS o FROM one; '
+ 'SELECT t FROM two')
+ stmts = sqlparse.split(sql)
+ self.assertEqual(len(stmts), 2)