def test_format_output_auto_expand(): settings = OutputSettings( table_format="psql", dcmlfmt="d", floatfmt="g", max_width=100 ) table_results = format_output( "Title", [("abc", "def")], ["head1", "head2"], "test status", settings ) table = [ "Title", "+---------+---------+", "| head1 | head2 |", "|---------+---------|", "| abc | def |", "+---------+---------+", "test status", ] assert list(table_results) == table expanded_results = format_output( "Title", [("abc", "def")], ["head1", "head2"], "test status", settings._replace(max_width=1), ) expanded = [ "Title", "-[ RECORD 1 ]-------------------------", "head1 | abc", "head2 | def", "test status", ] assert "\n".join(expanded_results) == "\n".join(expanded)
def test_format_output_auto_expand(): settings = OutputSettings( table_format='psql', dcmlfmt='d', floatfmt='g', max_width=100) table_results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], 'test status', settings) table = [ 'Title', '+---------+---------+', '| head1 | head2 |', '|---------+---------|', '| abc | def |', '+---------+---------+', 'test status' ] assert list(table_results) == table expanded_results = format_output( 'Title', [('abc', 'def')], ['head1', 'head2'], 'test status', settings._replace(max_width=1) ) expanded = [ 'Title', '-[ RECORD 1 ]-------------------------', 'head1 | abc', 'head2 | def', 'test status' ] assert '\n'.join(expanded_results) == '\n'.join(expanded)
def test_format_output_auto_expand(): table_results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], 'test status', 'psql', dcmlfmt='d', floatfmt='g', max_width=100) table = [ 'Title', '+---------+---------+\n| head1 | head2 |\n|---------+---------|\n| abc | def |\n+---------+---------+', 'test status' ] assert table_results == table expanded_results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], 'test status', 'psql', dcmlfmt='d', floatfmt='g', max_width=1) expanded = [ 'Title', u'-[ RECORD 0 ]-------------------------\nhead1 | abc\nhead2 | def\n', 'test status' ] assert expanded_results == expanded
def test_format_output_auto_expand(): table_results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], 'test status', 'psql', max_width=100) table = ['Title', '+---------+---------+\n| head1 | head2 |\n|---------+---------|\n| abc | def |\n+---------+---------+', 'test status'] assert table_results == table expanded_results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], 'test status', 'psql', max_width=1) expanded = ['Title', u'-[ RECORD 0 ]-------------------------\nhead1 | abc\nhead2 | def\n', 'test status'] assert expanded_results == expanded
def run(executor, sql, join=False, expanded=False, pgspecial=None, exception_formatter=None): " Return string output for the sql to be run " results = executor.run(sql, pgspecial, exception_formatter) formatted = [] for title, rows, headers, status, sql, success in results: formatted.extend( format_output(title, rows, headers, status, 'psql', dcmlfmt='d', floatfmt='g', expanded=expanded)) if join: formatted = '\n'.join(formatted) return formatted
def run(executor, sql, join=False): " Return string output for the sql to be run " result = [] for title, rows, headers, status in executor.run(sql): result.extend(format_output(title, rows, headers, status, 'psql')) if join: result = '\n'.join(result) return result
def format_results(results, table_format): out = [] for title, cur, headers, status, _, _ in results: fmt = format_output(title, cur, headers, status, table_format) out.append('\n'.join(fmt)) return '\n\n'.join(out)
def test_format_output(): results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], 'test status', 'psql') expected = [ 'Title', '+---------+---------+\n| head1 | head2 |\n|---------+---------|\n| abc | def |\n+---------+---------+', 'test status' ] assert results == expected
def test_format_output(): settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g') results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], 'test status', settings) expected = [ 'Title', '+---------+---------+', '| head1 | head2 |', '|---------+---------|', '| abc | def |', '+---------+---------+', 'test status' ] assert list(results) == expected
def test_format_output_truncate_off(): settings = OutputSettings(table_format="psql", dcmlfmt="d", floatfmt="g", max_field_width=None) long_field_value = ("first field " * 100).strip() results = format_output(None, [(long_field_value, )], ["head1"], None, settings) lines = list(results) assert lines[3] == f"| {long_field_value} |"
def test_format_output(): settings = OutputSettings(table_format="psql", dcmlfmt="d", floatfmt="g") results = format_output("Title", [("abc", "def")], ["head1", "head2"], "test status", settings) expected = [ "Title", "+-------+-------+", "| head1 | head2 |", "|-------+-------|", "| abc | def |", "+-------+-------+", "test status", ] assert list(results) == expected
def run(executor, sql, join=False, expanded=False, pgspecial=None, exception_formatter=None): " Return string output for the sql to be run " results = executor.run(sql, pgspecial, exception_formatter) formatted = [] for title, rows, headers, status, sql, success in results: formatted.extend(format_output(title, rows, headers, status, 'psql', expanded=expanded)) if join: formatted = '\n'.join(formatted) return formatted
def run(executor, sql, join=False, expanded=False, pgspecial=None, exception_formatter=None): " Return string output for the sql to be run " results = executor.run(sql, pgspecial, exception_formatter) formatted = [] settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g', expanded=expanded) for title, rows, headers, status, sql, success, is_special in results: formatted.extend(format_output(title, rows, headers, status, settings)) if join: formatted = '\n'.join(formatted) return formatted
def run_sql_async(view, sql): executor = executors[view.buffer_id()] panel = get_output_panel(view) logger.debug('Command: PgcliExecute: %r', sql) save_mode = get(view, 'pgcli_save_on_run_query_mode') # Make sure the output panel is visiblle sublime.active_window().run_command('pgcli_show_output_panel') # Put a leading datetime datestr = str(datetime.datetime.now()) + '\n\n' panel.run_command('append', {'characters': datestr, 'pos': 0}) results = executor.run(sql, pgspecial=special) try: for (title, cur, headers, status, _, _) in results: fmt = format_output(title, cur, headers, status, 'psql') out = ('\n'.join(fmt) + '\n\n' + str(datetime.datetime.now()) + '\n\n') panel.run_command('append', {'characters': out}) except psycopg2.DatabaseError as e: success = False out = str(e) + '\n\n' + str(datetime.datetime.now()) + '\n\n' panel.run_command('append', {'characters': out}) else: success = True if (view.file_name() and ((save_mode == 'always') or (save_mode == 'success' and success))): view.run_command('save') # Refresh the table names and column names if necessary. if has_meta_cmd(sql): logger.debug('Need completions refresh') url = get(view, 'pgcli_url') refresher = CompletionRefresher() refresher.refresh(executor, special=special, callbacks=( lambda c: swap_completer(c, url))) # Refresh search_path to set default schema. if has_change_path_cmd(sql): logger.debug('Refreshing search path') url = get(view, 'pgcli_url') with completer_lock: completers[url].set_search_path(executor.search_path()) logger.debug('Search path: %r', completers[url].search_path)
def run( executor, sql, join=False, expanded=False, pgspecial=None, exception_formatter=None ): " Return string output for the sql to be run " results = executor.run(sql, pgspecial, exception_formatter) formatted = [] settings = OutputSettings( table_format="psql", dcmlfmt="d", floatfmt="g", expanded=expanded ) for title, rows, headers, status, sql, success, is_special in results: formatted.extend(format_output(title, rows, headers, status, settings)) if join: formatted = "\n".join(formatted) return formatted
def run_sql_async(view, sql, panel): executor = executors[view.buffer_id()] logger.debug('Command: PgcliExecute: %r', sql) save_mode = get(view, 'pgcli_save_on_run_query_mode') # Make sure the output panel is visiblle sublime.active_window().run_command('pgcli_show_output_panel') # Put a leading datetime datestr = str(datetime.datetime.now()) + '\n\n' panel.run_command('append', {'characters': datestr, 'pos': 0}) results = executor.run(sql, pgspecial=special) try: for (title, cur, headers, status, _, _) in results: fmt = format_output(title, cur, headers, status, 'psql') out = ('\n'.join(fmt) + '\n\n' + str(datetime.datetime.now()) + '\n\n') panel.run_command('append', {'characters': out}) except psycopg2.DatabaseError as e: success = False out = str(e) + '\n\n' + str(datetime.datetime.now()) + '\n\n' panel.run_command('append', {'characters': out}) else: success = True if (view.file_name() and ((save_mode == 'always') or (save_mode == 'success' and success))): view.run_command('save') # Refresh the table names and column names if necessary. if has_meta_cmd(sql): logger.debug('Need completions refresh') url = get(view, 'pgcli_url') refresher = CompletionRefresher() refresher.refresh(executor, special=special, callbacks=(lambda c: swap_completer(c, url))) # Refresh search_path to set default schema. if has_change_path_cmd(sql): logger.debug('Refreshing search path') url = get(view, 'pgcli_url') with completer_lock: completers[url].set_search_path(executor.search_path()) logger.debug('Search path: %r', completers[url].search_path)
def test_format_output_truncate_on(): settings = OutputSettings(table_format="psql", dcmlfmt="d", floatfmt="g", max_field_width=10) results = format_output( None, [("first field value", "second field value")], ["head1", "head2"], None, settings, ) expected = [ "+------------+------------+", "| head1 | head2 |", "|------------+------------|", "| first f... | second ... |", "+------------+------------+", ] assert list(results) == expected
def test_format_output(): results = format_output('Title', [('abc', 'def')], ['head1', 'head2'], 'test status', 'psql') expected = ['Title', '+---------+---------+\n| head1 | head2 |\n|---------+---------|\n| abc | def |\n+---------+---------+', 'test status'] assert results == expected