bschoening commented on code in PR #4750:
URL: https://github.com/apache/cassandra/pull/4750#discussion_r3121140647
##########
pylib/cqlshlib/displaying.py:
##########
@@ -126,3 +126,86 @@ def color_ljust(self, width, fill=' '):
)
NO_COLOR_MAP = dict()
+
+class TablePrinter:
+ def print_header(self, formatted_names):
+ raise NotImplementedError
+
+ def print_rows(self, formatted_names, formatted_values):
+ raise NotImplementedError
+
+ def finish(self):
+ pass
+
+class TabularTablePrinter(TablePrinter):
+ def __init__(self, shell, tty, row_count_offset=0):
+ self._shell = shell
+ self._tty = tty
+ self._row_count_offset = row_count_offset
+ self._with_header = True
+
+ def print_header(self, formatted_names):
+ pass
+
+ def print_rows(self, formatted_names, formatted_values, with_header=None):
+ if with_header is None:
+ with_header = self._with_header
+ if self._shell.expand_enabled:
+ self._shell.print_formatted_result_vertically(
+ formatted_names, formatted_values, self._row_count_offset)
+ else:
+ self._shell.print_formatted_result(
+ formatted_names, formatted_values, with_header, self._tty)
+ if formatted_values:
+ self._row_count_offset += len(formatted_values)
+ self._with_header = self._tty
+
+ def finish(self):
+ pass
+
+class CsvTablePrinter(TablePrinter):
+ def __init__(self, shell):
+ import csv
+ self._writer = csv.writer(shell.query_out)
+ self._header_written = False
+
+ def print_header(self, formatted_names):
+ self._colnames = [n.strval for n in formatted_names]
+
+ def print_rows(self, formatted_names, formatted_values):
+ if not self._header_written:
+ self._writer.writerow(self._colnames)
+ self._header_written = True
+ if formatted_values is None:
+ return
+ for row in formatted_values:
+ self._writer.writerow([col.strval for col in row])
+
+ def finish(self):
+ pass
+
+class JsonTablePrinter(TablePrinter):
+ def __init__(self, shell):
+ self._shell = shell
+ self._colnames = None
+ self._first_row = True
+
+ def print_header(self, formatted_names):
+ self._colnames = [n.strval for n in formatted_names]
+ self._shell.writeresult('[')
+
+ def print_rows(self, formatted_names, formatted_values):
+ import json
+ if formatted_values is None:
+ return
+ for row in formatted_values:
+ row_dict = {self._colnames[i]: col.strval for i, col in
enumerate(row)}
+ serialized = json.dumps(row_dict)
Review Comment:
Check Cassandra types UUID, Decimal, or LocalDate in JSON and ensure they
have corresponding unit tests. Strings in formatted values may require escaping.
##########
pylib/cqlshlib/cqlshmain.py:
##########
@@ -990,15 +1001,18 @@ def print_all(result, table_meta, tty):
self.writeresult('%d more decoding errors suppressed.'
% (len(self.decoding_errors) - 2), color=RED)
- def print_static_result(self, result, table_meta, with_header, tty,
row_count_offset=0):
+ def print_static_result(self, result, table_meta, with_header, tty,
row_count_offset=0, printer=None):
if not result.column_names and not table_meta:
return
column_names = result.column_names or list(table_meta.columns.keys())
formatted_names = [self.myformat_colname(name, table_meta) for name in
column_names]
+
if not result.current_rows:
- # print header only
- self.print_formatted_result(formatted_names, None,
with_header=True, tty=tty)
+ if printer is None or isinstance(printer, TabularTablePrinter):
Review Comment:
probably shouldn't allow printer to be undefined. Couldn't this be
delegated TabularTablePrinter.print_header()?
##########
pylib/cqlshlib/displaying.py:
##########
@@ -126,3 +126,86 @@ def color_ljust(self, width, fill=' '):
)
NO_COLOR_MAP = dict()
+
+class TablePrinter:
+ def print_header(self, formatted_names):
+ raise NotImplementedError
+
+ def print_rows(self, formatted_names, formatted_values):
+ raise NotImplementedError
+
Review Comment:
add a static factory method:
@staticmethod
def factory(format_type, output=sys.stdout):
"""Centralized factory method to return the correct printer."""
format_map = {
'csv': CSVPrinter,
'json': JSONPrinter,
'table': TabularTablePrinter, # Default ASCII table
}
# Default to GridPrinter if format is unknown
printer_cls = format_map.get(format_type.lower(),
TabularTablePrinter)
return printer_cls(output)
##########
pylib/cqlshlib/cqlshmain.py:
##########
@@ -956,32 +962,37 @@ def perform_simple_statement(self, statement):
def print_result(self, result, table_meta):
self.decoding_errors = []
- self.writeresult("")
+ if self.mode == 'csv':
Review Comment:
see comment below in TablePrinter, this should be moved into a factory method
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]