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