diff options
| author | Fredy Wijaya <fredy.wijaya@gmail.com> | 2018-12-13 16:44:29 -0600 |
|---|---|---|
| committer | Andi Albrecht <albrecht.andi@gmail.com> | 2019-03-10 08:03:05 +0100 |
| commit | fa7def01d00fc7c5c3047b077bc0f1a4cdc1d10a (patch) | |
| tree | 2187017bc7dd709028d8f9e505cb6f4be2ed1ae6 /sqlparse/engine | |
| parent | 53dd1d566aefef305b13030cac14ae6fa71cadf7 (diff) | |
| download | sqlparse-fa7def01d00fc7c5c3047b077bc0f1a4cdc1d10a.tar.gz | |
Fix formatting on INSERT (fixes #329)
This patch fixes the formatting on INSERT by creating a new
instance of sql.Values to group all the values.
SQL: insert into foo values (1, 'foo'), (2, 'bar'), (3, 'baz')
Before:
insert into foo
values (1,
'foo'), (2,
'bar'), (3,
'baz')
After:
insert into foo
values (1, 'foo'),
(2, 'bar'),
(3, 'baz')
Diffstat (limited to 'sqlparse/engine')
| -rw-r--r-- | sqlparse/engine/grouping.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index 5ff819c..afc9123 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -327,6 +327,18 @@ def align_comments(tlist): tidx, token = tlist.token_next_by(i=sql.Comment, idx=tidx) +def group_values(tlist): + tidx, token = tlist.token_next_by(m=(T.Keyword, 'VALUES')) + start_idx = tidx + end_idx = -1 + while token: + if isinstance(token, sql.Parenthesis): + end_idx = tidx + tidx, token = tlist.token_next(tidx) + if end_idx != -1: + tlist.group_tokens(sql.Values, start_idx, end_idx, extend=True) + + def group(stmt): for func in [ group_comments, @@ -354,6 +366,7 @@ def group(stmt): align_comments, group_identifier_list, + group_values, ]: func(stmt) return stmt |
