Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UPSERTでSET句がフォーマットエラーになるケースがある #20

Open
tamiyamoto opened this issue Sep 27, 2022 · 1 comment

Comments

@tamiyamoto
Copy link

Version

$ usqlfmt --version
usqlfmt 0.1.4

フォーマットできないケース

ケース1

INSERT INTO sample_table
(col1, col2)
VALUES
($1, $2)
ON CONFLICT ON CONSTRAINT sample_table_pkc DO
UPDATE
SET
col1=CASE WHEN $1 IS NULL 'NULL' ELSE 'NOT NULL' END
,col2=$2
;

ケース2

INSERT INTO sample_table
(col1, col2)
VALUES
($1, $2)
ON CONFLICT ON CONSTRAINT sample_table_pkc DO
UPDATE
SET
col1=COALESCE($1, 'NULL')
,col2=$2
;

フォーマットできるケース

ケース1

SET句の項目が1つのみ

INSERT INTO sample_table
(col1)
VALUES
($1)
ON CONFLICT ON CONSTRAINT sample_table_pkc DO
UPDATE
SET
col1=CASE WHEN $1 IS NULL 'NULL' ELSE 'NOT NULL' END
;

ケース2

SET句の項目が1つのみ

INSERT INTO sample_table
(col1, col2)
VALUES
($1, $2)
ON CONFLICT ON CONSTRAINT sample_table_pkc DO
UPDATE
SET
col1=CASE WHEN $1 IS NULL 'NULL' ELSE 'NOT NULL' END
;

ケース3

SET句の項目は複数だが条件式はなし

INSERT INTO sample_table
(col1, col2)
VALUES
($1, $2)
ON CONFLICT ON CONSTRAINT sample_table_pkc DO
UPDATE
SET
col1=$1
,col2=$2
;

ケース4

UPSERTではなくUPDATEのみ

UPDATE sample_table
SET
col1=CASE WHEN $1 IS NULL 'NULL' ELSE 'NOT NULL' END
,col2=$2
;
@ghost
Copy link

ghost commented Sep 27, 2022

エラーファイルに記載されているエラーメッセージ

/*Traceback (most recent call last):
  File "uroborosqlfmt/exceptions.py", line 47, in wrap_try_except
  File "sqlparse/filters.py", line 386, in _process
  File "uroborosqlfmt/filters.py", line 1450, in _process_on
  File "uroborosqlfmt/filters.py", line 1613, in _adjust_comparisons_indent
  File "uroborosqlfmt/filters.py", line 352, in __init__
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "uroborosqlfmt/exceptions.py", line 47, in wrap_try_except
  File "sqlparse/filters.py", line 386, in _process
  File "uroborosqlfmt/filters.py", line 840, in _process_statement
  File "sqlparse/filters.py", line 473, in _process_default
  File "sqlparse/filters.py", line 473, in <listcomp>
  File "uroborosqlfmt/filters.py", line 517, in _process
  File "uroborosqlfmt/exceptions.py", line 50, in wrap_try_except
  File "uroborosqlfmt/exceptions.py", line 20, in __init__
AttributeError: 'IndexError' object has no attribute 'message'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "uroborosqlfmt/api.py", line 39, in format_dir
  File "uroborosqlfmt/__init__.py", line 104, in format_sql
  File "sqlparse/engine/__init__.py", line 68, in run
  File "sqlparse/engine/__init__.py", line 65, in _run1
  File "uroborosqlfmt/filters.py", line 466, in process
  File "sqlparse/filters.py", line 478, in process
  File "uroborosqlfmt/filters.py", line 517, in _process
  File "uroborosqlfmt/exceptions.py", line 50, in wrap_try_except
  File "uroborosqlfmt/exceptions.py", line 20, in __init__
AttributeError: 'AttributeError' object has no attribute 'message'

*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants