diff options
author | astroshot <astroshot@outlook.com> | 2023-06-23 13:05:58 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-23 07:05:58 +0200 |
commit | 69dcceb5f6d3ecbcc9e7a0a48c9bdabeed470dcd (patch) | |
tree | bcec4d19e6de6997f023f087504466a3da0380e4 | |
parent | 6b868bbfe831678d10c7535eb2e7356fcfb42068 (diff) |
Fix sql-insert format emits NULL as 'None' (#1409)
* Sub: Fix issue #1408
Body:
1. Fix issue #1408 sql-insert format emits NULL as 'None';
2. Fix DUAL displays as ""DUAL"";
==== End ====
* Sub: Update changelog.rst
Body:
==== End ====
* Sub: Optimize if logic
Body:
==== End ====
-rw-r--r-- | changelog.rst | 1 | ||||
-rw-r--r-- | pgcli/auth.py | 4 | ||||
-rw-r--r-- | pgcli/packages/formatter/sqlformatter.py | 9 | ||||
-rw-r--r-- | tests/formatter/test_sqlformatter.py | 4 |
4 files changed, 12 insertions, 6 deletions
diff --git a/changelog.rst b/changelog.rst index af4d6db4..7f73a0ba 100644 --- a/changelog.rst +++ b/changelog.rst @@ -23,6 +23,7 @@ Bug fixes: * Fix \ev not producing a correctly quoted "schema"."view" * Fix 'invalid connection option "dsn"' ([issue 1373](https://github.com/dbcli/pgcli/issues/1373)). * Fix explain mode when used with `expand`, `auto_expand`, or `--explain-vertical-output` ([issue 1393](https://github.com/dbcli/pgcli/issues/1393)). +* Fix sql-insert format emits NULL as 'None' ([issue 1408](https://github.com/dbcli/pgcli/issues/1408)). 3.5.0 (2022/09/15): =================== diff --git a/pgcli/auth.py b/pgcli/auth.py index 3d2fbeb9..2f1e5526 100644 --- a/pgcli/auth.py +++ b/pgcli/auth.py @@ -26,7 +26,9 @@ def keyring_initialize(keyring_enabled, *, logger): try: keyring = importlib.import_module("keyring") - except ModuleNotFoundError as e: + except ( + ModuleNotFoundError + ) as e: # ImportError for Python 2, ModuleNotFoundError for Python 3 logger.warning("import keyring failed: %r.", e) diff --git a/pgcli/packages/formatter/sqlformatter.py b/pgcli/packages/formatter/sqlformatter.py index 5bf25fec..5224eff6 100644 --- a/pgcli/packages/formatter/sqlformatter.py +++ b/pgcli/packages/formatter/sqlformatter.py @@ -14,10 +14,13 @@ preprocessors = () def escape_for_sql_statement(value): + if value is None: + return "NULL" + if isinstance(value, bytes): return f"X'{value.hex()}'" - else: - return "'{}'".format(value) + + return "'{}'".format(value) def adapter(data, headers, table_format=None, **kwargs): @@ -29,7 +32,7 @@ def adapter(data, headers, table_format=None, **kwargs): else: table_name = table[1] else: - table_name = '"DUAL"' + table_name = "DUAL" if table_format == "sql-insert": h = '", "'.join(headers) yield 'INSERT INTO "{}" ("{}") VALUES'.format(table_name, h) diff --git a/tests/formatter/test_sqlformatter.py b/tests/formatter/test_sqlformatter.py index b8cd9c2b..016ed956 100644 --- a/tests/formatter/test_sqlformatter.py +++ b/tests/formatter/test_sqlformatter.py @@ -34,7 +34,7 @@ def test_output_sql_insert(): "Jackson", "jackson_test@gmail.com", "132454789", - "", + None, "2022-09-09 19:44:32.712343+08", "2022-09-09 19:44:32.712343+08", ] @@ -58,7 +58,7 @@ def test_output_sql_insert(): output_list = [l for l in output] expected = [ 'INSERT INTO "user" ("id", "name", "email", "phone", "description", "created_at", "updated_at") VALUES', - " ('1', 'Jackson', 'jackson_test@gmail.com', '132454789', '', " + " ('1', 'Jackson', 'jackson_test@gmail.com', '132454789', NULL, " + "'2022-09-09 19:44:32.712343+08', '2022-09-09 19:44:32.712343+08')", ";", ] |