java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
branchv_0
changeset 238 4a1864c3e867
parent 224 36db9fd27436
child 250 aae5009bd0af
equal deleted inserted replaced
237:7e08730da258 238:4a1864c3e867
       
     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 		postprocessPrefetchedResultSet(currentHeader, currentResultSet);
       
    90 
       
    91 		super.writeStartResultSet(currentHeader);
       
    92 
       
    93 		for (Object[] row : currentResultSet) {
       
    94 			super.writeStartRow();
       
    95 			for (Object cell : row) {
       
    96 				super.writeColumnValue(cell);
       
    97 			}
       
    98 			super.writeEndRow();
       
    99 		}
       
   100 
       
   101 		currentColumnsCount = 0;
       
   102 		currentHeader = null;
       
   103 		currentRow = null;
       
   104 		currentResultSet = null;
       
   105 		super.writeEndResultSet();
       
   106 		prefetchDone = false;
       
   107 	}
       
   108 
       
   109 	/**
       
   110 	 * Optional post-processing – override in sub-classes if needed.
       
   111 	 * Don't forget to {@linkplain #updateColumnWidth(int, int)}
       
   112 	 *
       
   113 	 * @param currentHeader
       
   114 	 * @param currentResultSet
       
   115 	 */
       
   116 	protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
       
   117 	}
       
   118 
       
   119 }