mirror of https://github.com/dbcli/pgcli
Improve formatting of array output
Before this change, e.g. `SELECT '{1}'::numeric` would output `[Decimal(1)]`. Now it instead outputs `{1}`.
This commit is contained in:
parent
9d59fa5a8a
commit
c616a8b422
|
@ -5,6 +5,7 @@ Features:
|
|||
---------
|
||||
|
||||
* Add fish-style auto-suggestion from history. (Thanks: `Amjith Ramanujam`_)
|
||||
* Improved formatting of arrays in output (Thanks: `Joakim Koljonen`_)
|
||||
* Remove the ``...`` in the continuation prompt and use empty space instead. (Thanks: `Amjith Ramanujam`_)
|
||||
|
||||
Bug Fixes:
|
||||
|
|
|
@ -946,6 +946,19 @@ def format_output(title, cur, headers, status, settings):
|
|||
case_function = settings.case_function
|
||||
formatter = TabularOutputFormatter(format_name=table_format)
|
||||
|
||||
def format_array(val):
|
||||
if val is None:
|
||||
return settings.missingval
|
||||
if not isinstance(val, list):
|
||||
return val
|
||||
return '{' + ','.join(unicode(format_array(e)) for e in val) + '}'
|
||||
|
||||
def format_arrays(data, headers, column_types, **kwargs):
|
||||
for row in data:
|
||||
row[:] = [format_array(val) if isinstance(val, list) else val for val in row]
|
||||
|
||||
return data, headers
|
||||
|
||||
output_kwargs = {
|
||||
'sep_title': 'RECORD {n}',
|
||||
'sep_character': '-',
|
||||
|
@ -953,7 +966,7 @@ def format_output(title, cur, headers, status, settings):
|
|||
'missing_value': settings.missingval,
|
||||
'integer_format': settings.dcmlfmt,
|
||||
'float_format': settings.floatfmt,
|
||||
'preprocessors': (format_numbers, ),
|
||||
'preprocessors': (format_numbers, format_arrays),
|
||||
'disable_numparse': True,
|
||||
'preserve_whitespace': True
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# coding=utf-8
|
||||
import os
|
||||
import platform
|
||||
import mock
|
||||
from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
try:
|
||||
|
@ -56,6 +58,52 @@ def test_format_output():
|
|||
assert results == expected
|
||||
|
||||
|
||||
def test_format_array_output(executor):
|
||||
statement = u"""
|
||||
SELECT
|
||||
array[1, 2, 3]::bigint[] as bigint_array,
|
||||
'{{1,2},{3,4}}'::numeric[] as nested_numeric_array,
|
||||
'{å,魚,текст}'::text[] as 配列
|
||||
UNION ALL
|
||||
SELECT '{}', NULL, array[NULL]
|
||||
"""
|
||||
results = run(executor, statement)
|
||||
expected = [
|
||||
u'+----------------+------------------------+--------------+\n'
|
||||
u'| bigint_array | nested_numeric_array | 配列 |\n'
|
||||
u'|----------------+------------------------+--------------|\n'
|
||||
u'| {1,2,3} | {{1,2},{3,4}} | {å,魚,текст} |\n'
|
||||
u'| {} | <null> | {<null>} |\n'
|
||||
u'+----------------+------------------------+--------------+',
|
||||
u'SELECT 2'
|
||||
]
|
||||
assert results == expected
|
||||
|
||||
|
||||
def test_format_array_output_expanded(executor):
|
||||
statement = u"""
|
||||
SELECT
|
||||
array[1, 2, 3]::bigint[] as bigint_array,
|
||||
'{{1,2},{3,4}}'::numeric[] as nested_numeric_array,
|
||||
'{å,魚,текст}'::text[] as 配列
|
||||
UNION ALL
|
||||
SELECT '{}', NULL, array[NULL]
|
||||
"""
|
||||
results = run(executor, statement, expanded=True)
|
||||
expected = [
|
||||
u'-[ RECORD 1 ]-------------------------\n'
|
||||
u'bigint_array | {1,2,3}\n'
|
||||
u'nested_numeric_array | {{1,2},{3,4}}\n'
|
||||
u'配列 | {å,魚,текст}\n'
|
||||
u'-[ RECORD 2 ]-------------------------\n'
|
||||
u'bigint_array | {}\n'
|
||||
u'nested_numeric_array | <null>\n'
|
||||
u'配列 | {<null>}\n',
|
||||
u'SELECT 2'
|
||||
]
|
||||
assert results == expected
|
||||
|
||||
|
||||
def test_format_output_auto_expand():
|
||||
settings = OutputSettings(
|
||||
table_format='psql', dcmlfmt='d', floatfmt='g', max_width=100)
|
||||
|
|
Loading…
Reference in New Issue