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