diff options
| -rw-r--r-- | CHANGES | 3 | ||||
| -rw-r--r-- | sqlparse/engine/grouping.py | 4 | ||||
| -rw-r--r-- | tests/test_grouping.py | 14 | ||||
| -rw-r--r-- | tests/test_regressions.py | 20 |
4 files changed, 39 insertions, 2 deletions
@@ -1,7 +1,8 @@ Development Version ------------------- -* Nothing new yet. +Enhancements +* Improve parsing of assignments in UPDATE statements (issue90). Release 0.1.7 (Apr 06, 2013) diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index d60ee5a..07f9392 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -125,7 +125,9 @@ def group_comparison(tlist): return (token.ttype in (T.String.Symbol, T.Name, T.Number, T.Number.Integer, T.Literal, T.Literal.Number.Integer) - or isinstance(token, (sql.Identifier,))) + or isinstance(token, (sql.Identifier,)) + or (token.ttype is T.Keyword + and token.value.upper() in ['NULL', ])) _group_left_right(tlist, T.Operator.Comparison, None, sql.Comparison, check_left=_parts_valid, check_right=_parts_valid) diff --git a/tests/test_grouping.py b/tests/test_grouping.py index 674982f..b22e543 100644 --- a/tests/test_grouping.py +++ b/tests/test_grouping.py @@ -242,3 +242,17 @@ def test_identifier_consumes_ordering(): # issue89 assert ids[0].get_ordering() == 'DESC' assert ids[1].get_name() == 'c2' assert ids[1].get_ordering() is None + + +def test_comparison_with_keywords(): # issue90 + # in fact these are assignments, but for now we don't distinguish them + p = sqlparse.parse('foo = NULL')[0] + assert len(p.tokens) == 1 + assert isinstance(p.tokens[0], sql.Comparison) + assert len(p.tokens[0].tokens) == 5 + assert p.tokens[0].tokens[0].value == 'foo' + assert p.tokens[0].tokens[-1].value == 'NULL' + # make sure it's case-insensitive + p = sqlparse.parse('foo = null')[0] + assert len(p.tokens) == 1 + assert isinstance(p.tokens[0], sql.Comparison) diff --git a/tests/test_regressions.py b/tests/test_regressions.py index e9d890b..423f2d6 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -198,3 +198,23 @@ def test_format_accepts_encoding(): # issue20 else: tformatted = 'insert into foo\nvalues (1); -- Песня про надежду\n' assert formatted == tformatted + + +def test_issue90(): + sql = ('UPDATE "gallery_photo" SET "owner_id" = 4018, "deleted_at" = NULL,' + ' "width" = NULL, "height" = NULL, "rating_votes" = 0,' + ' "rating_score" = 0, "thumbnail_width" = NULL,' + ' "thumbnail_height" = NULL, "price" = 1, "description" = NULL') + formatted = sqlparse.format(sql, reindent=True) + tformatted = '\n'.join(['UPDATE "gallery_photo"', + 'SET "owner_id" = 4018,', + ' "deleted_at" = NULL,', + ' "width" = NULL,', + ' "height" = NULL,', + ' "rating_votes" = 0,', + ' "rating_score" = 0,', + ' "thumbnail_width" = NULL,', + ' "thumbnail_height" = NULL,', + ' "price" = 1,', + ' "description" = NULL']) + assert formatted == tformatted |
