franta-hg@32: /** franta-hg@32: * SQL-DK franta-hg@32: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@32: * franta-hg@32: * This program is free software: you can redistribute it and/or modify franta-hg@32: * it under the terms of the GNU General Public License as published by franta-hg@32: * the Free Software Foundation, either version 3 of the License, or franta-hg@32: * (at your option) any later version. franta-hg@32: * franta-hg@32: * This program is distributed in the hope that it will be useful, franta-hg@32: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@32: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@32: * GNU General Public License for more details. franta-hg@32: * franta-hg@32: * You should have received a copy of the GNU General Public License franta-hg@32: * along with this program. If not, see . franta-hg@32: */ franta-hg@32: package info.globalcode.sql.dk.formatting; franta-hg@32: franta-hg@34: import info.globalcode.sql.dk.ColorfulPrintWriter; franta-hg@37: import static info.globalcode.sql.dk.ColorfulPrintWriter.*; franta-hg@34: franta-hg@32: /** franta-hg@32: * franta-hg@32: * @author Ing. František Kučera (frantovo.cz) franta-hg@32: */ franta-hg@32: public class TabularFormatter extends AbstractFormatter { franta-hg@32: franta-hg@32: public static final String NAME = "tabular"; franta-hg@34: private ColorfulPrintWriter out; franta-hg@37: private boolean firstResult = true; franta-hg@32: franta-hg@32: public TabularFormatter(FormatterContext formatterContext) { franta-hg@32: super(formatterContext); franta-hg@34: out = new ColorfulPrintWriter(formatterContext.getOutputStream()); franta-hg@34: } franta-hg@34: franta-hg@34: @Override franta-hg@37: public void writeStartResultSet() { franta-hg@37: super.writeStartResultSet(); franta-hg@37: printResultSeparator(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeColumnsHeader(ColumnsHeader header) { franta-hg@37: super.writeColumnsHeader(header); franta-hg@37: franta-hg@37: for (ColumnDescriptor cd : header.getColumnDescriptors()) { franta-hg@37: out.print(TerminalStyle.Bright, cd.getLabel()); franta-hg@37: out.print(" ("); franta-hg@37: out.print(cd.getTypeName()); franta-hg@37: out.print(")"); franta-hg@37: if (!cd.isLastColumn()) { franta-hg@37: out.print(TerminalColor.Green, " | "); franta-hg@37: } franta-hg@37: } franta-hg@37: out.println(); franta-hg@37: out.flush(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@34: public void writeColumnValue(Object value) { franta-hg@34: super.writeColumnValue(value); franta-hg@34: franta-hg@34: if (!isCurrentColumnFirst()) { franta-hg@37: out.print(TerminalColor.Green, " | "); franta-hg@34: } franta-hg@37: franta-hg@37: out.print(TerminalColor.Cyan, String.valueOf(value)); franta-hg@34: } franta-hg@34: franta-hg@34: @Override franta-hg@34: public void writeEndRow() { franta-hg@34: super.writeEndRow(); franta-hg@34: out.println(); franta-hg@34: out.flush(); franta-hg@32: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeEndResultSet() { franta-hg@37: super.writeEndResultSet(); franta-hg@37: out.print(TerminalColor.Yellow, "Record count: "); franta-hg@37: out.println(getCurrentRowCount()); franta-hg@37: out.flush(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeStartUpdatesResult() { franta-hg@37: super.writeStartUpdatesResult(); franta-hg@37: printResultSeparator(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeUpdatedRowsCount(int updatedRowsCount) { franta-hg@37: super.writeUpdatedRowsCount(updatedRowsCount); franta-hg@37: out.print(TerminalColor.Red, "Updated records: "); franta-hg@37: out.println(updatedRowsCount); franta-hg@37: out.flush(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeEndDatabase() { franta-hg@37: super.writeEndDatabase(); franta-hg@37: out.flush(); franta-hg@37: } franta-hg@37: franta-hg@37: private void printResultSeparator() { franta-hg@37: if (firstResult) { franta-hg@37: firstResult = false; franta-hg@37: } else { franta-hg@37: out.println(); franta-hg@37: } franta-hg@37: } franta-hg@32: }