Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Features
--------
* Update query processing functions to allow automatic show_warnings to work for more code paths like DDL.
* Rework reconnect logic to actually reconnect or create a new connection instead of simply changing the database (#746).
* Configurable string for missing values (NULLs) in outputs.


Bug Fixes
Expand Down
18 changes: 17 additions & 1 deletion mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from urllib.parse import parse_qs, unquote, urlparse

from cli_helpers.tabular_output import TabularOutputFormatter, preprocessors
from cli_helpers.tabular_output.output_formatter import MISSING_VALUE as DEFAULT_MISSING_VALUE
from cli_helpers.utils import strip_ansi
import click
from configobj import ConfigObj
Expand Down Expand Up @@ -153,6 +154,7 @@ def __init__(
self.destructive_warning = c_dest_warning if warn is None else warn
self.login_path_as_host = c["main"].as_bool("login_path_as_host")
self.post_redirect_command = c['main'].get('post_redirect_command')
self.null_string = c['main'].get('null_string')

# read from cli argument or user config file
self.auto_vertical_output = auto_vertical_output or c["main"].as_bool("auto_vertical_output")
Expand Down Expand Up @@ -791,6 +793,7 @@ def output_res(res: Generator[tuple], start: float) -> None:
headers,
special.is_expanded_output(),
special.is_redirected(),
self.null_string,
max_width,
)

Expand Down Expand Up @@ -823,6 +826,7 @@ def output_res(res: Generator[tuple], start: float) -> None:
headers,
special.is_expanded_output(),
special.is_redirected(),
self.null_string,
max_width,
)
self.echo("")
Expand Down Expand Up @@ -1285,6 +1289,7 @@ def run_query(self, query: str, new_line: bool = True) -> None:
headers,
special.is_expanded_output(),
special.is_redirected(),
self.null_string,
)
for line in output:
click.echo(line, nl=new_line)
Expand All @@ -1299,6 +1304,7 @@ def run_query(self, query: str, new_line: bool = True) -> None:
headers,
special.is_expanded_output(),
special.is_redirected(),
self.null_string,
)
for line in output:
click.echo(line, nl=new_line)
Expand All @@ -1310,6 +1316,7 @@ def format_output(
headers: list[str] | None,
expanded: bool = False,
is_redirected: bool = False,
null_string: str | None = None,
max_width: int | None = None,
) -> itertools.chain[str]:
if is_redirected:
Expand All @@ -1320,7 +1327,16 @@ def format_output(
expanded = expanded or use_formatter.format_name == "vertical"
output: itertools.chain[str] = itertools.chain()

output_kwargs = {"dialect": "unix", "disable_numparse": True, "preserve_whitespace": True, "style": self.output_style}
output_kwargs = {
"dialect": "unix",
"disable_numparse": True,
"preserve_whitespace": True,
"style": self.output_style,
}
default_kwargs = use_formatter._output_formats[use_formatter.format_name].formatter_args

if null_string is not None and default_kwargs.get('missing_value') == DEFAULT_MISSING_VALUE:
output_kwargs['missing_value'] = null_string

if use_formatter.format_name not in sql_format.supported_formats:
output_kwargs["preprocessors"] = (preprocessors.align_decimals,)
Expand Down
5 changes: 5 additions & 0 deletions mycli/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ table_format = ascii
# Recommended: csv.
redirect_format = csv

# How to display the missing value (ie NULL). Only certain table formats
# support configuring the missing value. CSV for example always uses the
# empty string, and JSON formats use native nulls.
null_string = <null>

# A command to run after a successful output redirect, with {} to be replaced
# with the escaped filename. Mac example: echo {} | pbcopy. Escaping is not
# reliable/safe on Windows.
Expand Down
2 changes: 1 addition & 1 deletion test/features/fixture_data/help_commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
| \pipe_once | \| command | Send next result to a subprocess. |
| \timing | \t | Toggle timing of commands. |
| connect | \r | Reconnect to the database. Optional database argument. |
| delimiter | <null> | Change SQL delimiter. |
| delimiter | <nope> | Change SQL delimiter. |
| exit | \q | Exit. |
| help | \? | Show this help. |
| nopager | \n | Disable pager, print to stdout. |
Expand Down
2 changes: 1 addition & 1 deletion test/features/steps/crud_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def step_see_null_selected(context):
+--------+\r
| NULL |\r
+--------+\r
| <null> |\r
| <nope> |\r
+--------+
"""
).strip()
Expand Down
5 changes: 5 additions & 0 deletions test/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ table_format = ascii
# Recommended: csv.
redirect_format = csv

# How to display the missing value (ie NULL). Only certain table formats
# support configuring the missing value. CSV for example always uses the
# empty string, and JSON formats use native nulls.
null_string = <nope>

# A command to run after a successful output redirect, with {} to be replaced
# with the escaped filename. Mac example: echo {} | pbcopy. Escaping is not
# reliable/safe on Windows.
Expand Down