java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 10 Jan 2014 23:21:28 +0100
branchv_0
changeset 155 eb3676c6929b
parent 142 da1e38386d84
child 224 36db9fd27436
permissions -rw-r--r--
more JavaDoc
     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 java.util.ArrayList;
    21 import java.util.List;
    22 
    23 /**
    24  * <p>
    25  * Prefetches whole result set and computes column widths. Whole table is flushed at once in
    26  * {@linkplain #writeEndResultSet()}.
    27  * </p>
    28  *
    29  * <p>
    30  * Long values will not overflow the cells, but whole result set must be loaded into the memory.
    31  * </p>
    32  *
    33  * @author Ing. František Kučera (frantovo.cz)
    34  */
    35 public class TabularPrefetchingFormatter extends TabularFormatter {
    36 
    37 	public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
    38 	private ColumnsHeader currentHeader;
    39 	private List<Object[]> currentResultSet;
    40 	private Object[] currentRow;
    41 	private int currentColumnsCount;
    42 	private boolean prefetchDone = false;
    43 
    44 	public TabularPrefetchingFormatter(FormatterContext formatterContext) {
    45 		super(formatterContext);
    46 	}
    47 
    48 	@Override
    49 	protected int getCurrentColumnsCount() {
    50 		if (prefetchDone) {
    51 			return super.getCurrentColumnsCount();
    52 		} else {
    53 			return currentColumnsCount;
    54 		}
    55 	}
    56 
    57 	@Override
    58 	public void writeStartResultSet(ColumnsHeader header) {
    59 		currentResultSet = new ArrayList<>();
    60 		currentHeader = header;
    61 		initColumnWidths(header.getColumnCount());
    62 	}
    63 
    64 	@Override
    65 	public void writeStartRow() {
    66 		currentRow = new Object[currentHeader.getColumnCount()];
    67 		currentResultSet.add(currentRow);
    68 		currentColumnsCount = 0;
    69 	}
    70 
    71 	@Override
    72 	public void writeColumnValue(Object value) {
    73 		currentRow[currentColumnsCount] = value;
    74 		currentColumnsCount++;
    75 		String textRepresentation = toString(value);
    76 		/** TODO: count only printable characters (currently not an issue) */
    77 		updateColumnWidth(currentColumnsCount, textRepresentation.length());
    78 	}
    79 
    80 	@Override
    81 	public void writeEndRow() {
    82 		// do nothing
    83 	}
    84 
    85 	@Override
    86 	public void writeEndResultSet() {
    87 		prefetchDone = true;
    88 
    89 		super.writeStartResultSet(currentHeader);
    90 
    91 		for (Object[] row : currentResultSet) {
    92 			super.writeStartRow();
    93 			for (Object cell : row) {
    94 				super.writeColumnValue(cell);
    95 			}
    96 			super.writeEndRow();
    97 		}
    98 
    99 		currentColumnsCount = 0;
   100 		currentHeader = null;
   101 		currentRow = null;
   102 		currentResultSet = null;
   103 		super.writeEndResultSet();
   104 		prefetchDone = false;
   105 	}
   106 }