java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 23 Dec 2013 11:50:24 +0100
branchv_0
changeset 37 9e6f8e5d5f98
parent 34 9335cf31c0f2
child 39 be8db46a38c3
permissions -rw-r--r--
support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk.formatting;
    19 
    20 import info.globalcode.sql.dk.ColorfulPrintWriter;
    21 import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
    22 
    23 /**
    24  *
    25  * @author Ing. František Kučera (frantovo.cz)
    26  */
    27 public class TabularFormatter extends AbstractFormatter {
    28 
    29 	public static final String NAME = "tabular";
    30 	private ColorfulPrintWriter out;
    31 	private boolean firstResult = true;
    32 
    33 	public TabularFormatter(FormatterContext formatterContext) {
    34 		super(formatterContext);
    35 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    36 	}
    37 
    38 	@Override
    39 	public void writeStartResultSet() {
    40 		super.writeStartResultSet();
    41 		printResultSeparator();
    42 	}
    43 
    44 	@Override
    45 	public void writeColumnsHeader(ColumnsHeader header) {
    46 		super.writeColumnsHeader(header);
    47 
    48 		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    49 			out.print(TerminalStyle.Bright, cd.getLabel());
    50 			out.print(" (");
    51 			out.print(cd.getTypeName());
    52 			out.print(")");
    53 			if (!cd.isLastColumn()) {
    54 				out.print(TerminalColor.Green, " | ");
    55 			}
    56 		}
    57 		out.println();
    58 		out.flush();
    59 	}
    60 
    61 	@Override
    62 	public void writeColumnValue(Object value) {
    63 		super.writeColumnValue(value);
    64 
    65 		if (!isCurrentColumnFirst()) {
    66 			out.print(TerminalColor.Green, " | ");
    67 		}
    68 
    69 		out.print(TerminalColor.Cyan, String.valueOf(value));
    70 	}
    71 
    72 	@Override
    73 	public void writeEndRow() {
    74 		super.writeEndRow();
    75 		out.println();
    76 		out.flush();
    77 	}
    78 
    79 	@Override
    80 	public void writeEndResultSet() {
    81 		super.writeEndResultSet();
    82 		out.print(TerminalColor.Yellow, "Record count: ");
    83 		out.println(getCurrentRowCount());
    84 		out.flush();
    85 	}
    86 
    87 	@Override
    88 	public void writeStartUpdatesResult() {
    89 		super.writeStartUpdatesResult();
    90 		printResultSeparator();
    91 	}
    92 
    93 	@Override
    94 	public void writeUpdatedRowsCount(int updatedRowsCount) {
    95 		super.writeUpdatedRowsCount(updatedRowsCount);
    96 		out.print(TerminalColor.Red, "Updated records: ");
    97 		out.println(updatedRowsCount);
    98 		out.flush();
    99 	}
   100 
   101 	@Override
   102 	public void writeEndDatabase() {
   103 		super.writeEndDatabase();
   104 		out.flush();
   105 	}
   106 
   107 	private void printResultSeparator() {
   108 		if (firstResult) {
   109 			firstResult = false;
   110 		} else {
   111 			out.println();
   112 		}
   113 	}
   114 }