From fa4bf85c3ccd2937a8d71f09ee8340d955c3f14a Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Mon, 26 Jan 2026 09:14:44 -0500 Subject: [PATCH] better solution for binary value rendering After https://github.com/dbcli/cli_helpers/pull/100 we can use the new convert_to_undecoded_string preprocessor to guarantee that binary values are rendered as hex literals. It would be neat if in a future PR we added an option to _never_ render binaries as hex literals (but just emit their contents). The align_decimals preprocessor would seem to have no effect and should be removed in a separate commit. The bugfix here is covered under the existing changelog entry. --- mycli/main.py | 6 +++++- pyproject.toml | 2 +- test/test_tabular_output.py | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mycli/main.py b/mycli/main.py index 9514f613..c4fd6d30 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -1408,7 +1408,11 @@ def format_output( output_kwargs['missing_value'] = null_string if use_formatter.format_name not in sql_format.supported_formats: - output_kwargs["preprocessors"] = (preprocessors.align_decimals,) + # will run before preprocessors defined as part of the format in cli_helpers + output_kwargs["preprocessors"] = ( + preprocessors.convert_to_undecoded_string, + preprocessors.align_decimals, + ) if title: # Only print the title if it's not None. output = itertools.chain(output, [title]) diff --git a/pyproject.toml b/pyproject.toml index d48f5a89..8bbe011c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,7 +17,7 @@ dependencies = [ "sqlparse>=0.3.0,<0.6.0", "sqlglot[rs] == 27.*", "configobj >= 5.0.5", - "cli_helpers[styles] >= 2.8.1", + "cli_helpers[styles] >= 2.9.0", "pyperclip >= 1.8.1", "pycryptodomex", "pyfzf >= 0.3.1", diff --git a/test/test_tabular_output.py b/test/test_tabular_output.py index 48146bbe..2ad234f7 100644 --- a/test/test_tabular_output.py +++ b/test/test_tabular_output.py @@ -112,3 +112,7 @@ def description(self): ('abc', 1, NULL, 10.0e0, X'aa') , ('d', 456, '1', 0.5e0, X'aabb') ;""") + # Test binary output format is a hex string + assert list(mycli.change_table_format("psql")) == [SQLResult(None, None, None, "Changed table format to psql")] + output = mycli.format_output(None, FakeCursor(), headers, False, False) + assert '0xaabb' in '\n'.join(output)