Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

### Features

- Add a `--json` option for batch query output (JSON Lines, one object per row), e.g. `litecli -e "select ..." --json`. (#263)

### Bug Fixes

- Expand `~` in configured log file paths before opening the log.
Expand Down
2 changes: 1 addition & 1 deletion litecli/liteclirc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ log_level = INFO
# Table format. Possible values:
# ascii, double, github, psql, plain, simple, grid, fancy_grid, pipe, orgtbl,
# rst, mediawiki, html, latex, latex_booktabs, textile, moinmoin, jira,
# vertical, tsv, csv.
# vertical, tsv, csv, jsonl.
# Recommended: ascii
table_format = ascii

Expand Down
8 changes: 8 additions & 0 deletions litecli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ def get_last_query(self) -> str | None:
)
@click.option("-t", "--table", is_flag=True, help="Display batch output in table format.")
@click.option("--csv", is_flag=True, help="Display batch output in CSV format.")
@click.option("--json", is_flag=True, help="Display batch output in JSON format (one JSON object per line).")
@click.option("--warn/--no-warn", default=None, help="Warn before running a destructive query.")
@click.option("-e", "--execute", type=str, help="Execute command and quit.")
@click.argument("database", default="", nargs=1)
Expand All @@ -942,6 +943,7 @@ def cli(
auto_vertical_output: bool,
table: bool,
csv: bool,
json: bool,
warn: bool | None,
execute: str | None,
liteclirc: str,
Expand Down Expand Up @@ -973,6 +975,9 @@ def cli(
try:
if csv:
litecli.formatter.format_name = "csv"
elif json:
# cli_helpers' JSON Lines adapter: one JSON object per row.
litecli.formatter.format_name = "jsonl"
elif not table:
litecli.formatter.format_name = "tsv"

Expand Down Expand Up @@ -1000,6 +1005,9 @@ def cli(

if csv:
litecli.formatter.format_name = "csv"
elif json:
# cli_helpers' JSON Lines adapter: one JSON object per row.
litecli.formatter.format_name = "jsonl"
elif not table:
litecli.formatter.format_name = "tsv"

Expand Down
31 changes: 31 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def test_execute_arg_with_csv(executor):
assert expected in "".join(result.output)


@dbtest
def test_execute_arg_with_json(executor):
run(executor, "create table test (a text)")
run(executor, 'insert into test values("abc")')

sql = "select * from test;"
runner = CliRunner()
result = runner.invoke(cli, args=CLI_ARGS + ["-e", sql] + ["--json"])
expected = '{"a":"abc"}\n'

assert result.exit_code == 0
assert expected in "".join(result.output)


@dbtest
def test_batch_mode(executor):
run(executor, """create table test(a text)""")
Expand Down Expand Up @@ -134,6 +148,23 @@ def test_batch_mode_csv(executor):
assert expected in "".join(result.output)


@dbtest
def test_batch_mode_json(executor):
run(executor, """create table test(a text, b text)""")
run(executor, """insert into test (a, b) values('abc', 'de\nf'), ('ghi', 'jkl')""")

sql = "select * from test;"

runner = CliRunner()
result = runner.invoke(cli, args=CLI_ARGS + ["--json"], input=sql)

# JSON Lines: one JSON object per row, embedded newlines escaped.
expected = '{"a":"abc","b":"de\\nf"}\n{"a":"ghi","b":"jkl"}\n'

assert result.exit_code == 0
assert expected in "".join(result.output)


def test_help_strings_end_with_periods():
"""Make sure click options have help text that end with a period."""
for param in cli.params:
Expand Down