summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2020-09-30 08:11:53 +0200
committerAndi Albrecht <albrecht.andi@gmail.com>2020-09-30 08:11:53 +0200
commitcd4a723ee509437166b0f3e0fd62322ecc7bbd99 (patch)
treecd009dc2b36475f21e2f17f645dc56d0dd219598
parent3e8076dbe225f84592ba2e3029b06cb438c05acd (diff)
downloadsqlparse-cd4a723ee509437166b0f3e0fd62322ecc7bbd99.tar.gz
Don't make parsing of square bracket identifiers too greedy (fixes #583).
-rw-r--r--CHANGELOG1
-rw-r--r--sqlparse/keywords.py2
-rw-r--r--tests/test_parse.py6
3 files changed, 8 insertions, 1 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 789ee62..2de2a6a 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -20,6 +20,7 @@ Bug Fixes
* Improved parsing of IN(...) statements (issue566, pr567 by hurcy).
* Preserve line breaks when removing comments (issue484).
+* Fix parsing error when using square bracket notation (issue583).
Release 0.3.1 (Feb 29, 2020)
diff --git a/sqlparse/keywords.py b/sqlparse/keywords.py
index 91edf8c..933b323 100644
--- a/sqlparse/keywords.py
+++ b/sqlparse/keywords.py
@@ -73,7 +73,7 @@ SQL_REGEX = {
# sqlite names can be escaped with [square brackets]. left bracket
# cannot be preceded by word character or a right bracket --
# otherwise it's probably an array index
- (r'(?<![\w\])])(\[[^\]]+\])', tokens.Name),
+ (r'(?<![\w\])])(\[[^\]\[]+\])', tokens.Name),
(r'((LEFT\s+|RIGHT\s+|FULL\s+)?(INNER\s+|OUTER\s+|STRAIGHT\s+)?'
r'|(CROSS\s+|NATURAL\s+)?)?JOIN\b', tokens.Keyword),
(r'END(\s+IF|\s+LOOP|\s+WHILE)?\b', tokens.Keyword),
diff --git a/tests/test_parse.py b/tests/test_parse.py
index 6cce909..5fe6ed2 100644
--- a/tests/test_parse.py
+++ b/tests/test_parse.py
@@ -100,6 +100,12 @@ def test_parse_square_brackets_notation_isnt_too_greedy():
assert t[0].tokens[-1].get_real_name() == '[bar]'
+def test_parse_square_brackets_notation_isnt_too_greedy2():
+ # see issue583
+ t = sqlparse.parse('[(foo[i])]')[0].tokens
+ assert isinstance(t[0], sql.SquareBrackets) # not Identifier!
+
+
def test_parse_keyword_like_identifier():
# see issue47
t = sqlparse.parse('foo.key')[0].tokens