java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularPrefetchingFormatter.java
branchv_0
changeset 142 da1e38386d84
parent 88 102ba0fcb07f
child 155 eb3676c6929b
equal deleted inserted replaced
141:ecd17aad1178 142:da1e38386d84
    15  * You should have received a copy of the GNU General Public License
    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/>.
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    17  */
    18 package info.globalcode.sql.dk.formatting;
    18 package info.globalcode.sql.dk.formatting;
    19 
    19 
    20 import info.globalcode.sql.dk.Parameter;
       
    21 import java.util.ArrayList;
    20 import java.util.ArrayList;
    22 import java.util.List;
    21 import java.util.List;
    23 
    22 
    24 /**
    23 /**
    25  * Prefetches whole result set and computes column widths. Whole table is flushed at once in
    24  * Prefetches whole result set and computes column widths. Whole table is flushed at once in
    30  * @author Ing. František Kučera (frantovo.cz)
    29  * @author Ing. František Kučera (frantovo.cz)
    31  */
    30  */
    32 public class TabularPrefetchingFormatter extends TabularFormatter {
    31 public class TabularPrefetchingFormatter extends TabularFormatter {
    33 
    32 
    34 	public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
    33 	public static final String NAME = "tabular-prefetching"; // bash-completion:formatter
    35 	private String currentSql;
       
    36 	private List<? extends Parameter> currentParameters;
       
    37 	private ColumnsHeader currentHeader;
    34 	private ColumnsHeader currentHeader;
    38 	private List<Object[]> currentResultSet;
    35 	private List<Object[]> currentResultSet;
    39 	private Object[] currentRow;
    36 	private Object[] currentRow;
    40 	private int currentColumnsCount;
    37 	private int currentColumnsCount;
    41 	private boolean prefetchDone = false;
    38 	private boolean prefetchDone = false;
    52 			return currentColumnsCount;
    49 			return currentColumnsCount;
    53 		}
    50 		}
    54 	}
    51 	}
    55 
    52 
    56 	@Override
    53 	@Override
    57 	public void writeStartResultSet() {
    54 	public void writeStartResultSet(ColumnsHeader header) {
    58 		currentResultSet = new ArrayList<>();
    55 		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;
    56 		currentHeader = header;
    74 		initColumnWidths(header.getColumnCount());
    57 		initColumnWidths(header.getColumnCount());
    75 	}
    58 	}
    76 
    59 
    77 	@Override
    60 	@Override
    97 
    80 
    98 	@Override
    81 	@Override
    99 	public void writeEndResultSet() {
    82 	public void writeEndResultSet() {
   100 		prefetchDone = true;
    83 		prefetchDone = true;
   101 
    84 
   102 		super.writeStartResultSet();
    85 		super.writeStartResultSet(currentHeader);
   103 		super.writeQuery(currentSql);
       
   104 		super.writeParameters(currentParameters);
       
   105 		super.writeColumnsHeader(currentHeader);
       
   106 
    86 
   107 		for (Object[] row : currentResultSet) {
    87 		for (Object[] row : currentResultSet) {
   108 			super.writeStartRow();
    88 			super.writeStartRow();
   109 			for (Object cell : row) {
    89 			for (Object cell : row) {
   110 				super.writeColumnValue(cell);
    90 				super.writeColumnValue(cell);
   111 			}
    91 			}
   112 			super.writeEndRow();
    92 			super.writeEndRow();
   113 		}
    93 		}
   114 
    94 
   115 		currentColumnsCount = 0;
    95 		currentColumnsCount = 0;
   116 		currentSql = null;
       
   117 		currentParameters = null;
       
   118 		currentHeader = null;
    96 		currentHeader = null;
   119 		currentRow = null;
    97 		currentRow = null;
   120 		currentResultSet = null;
    98 		currentResultSet = null;
   121 		super.writeEndResultSet();
    99 		super.writeEndResultSet();
   122 		prefetchDone = false;
   100 		prefetchDone = false;