java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 07 Jan 2014 21:54:59 +0100
branchv_0
changeset 142 da1e38386d84
parent 101 97b0d9069133
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 info.globalcode.sql.dk.Parameter;
    21 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    22 import java.util.EmptyStackException;
    23 import java.util.EnumSet;
    24 import java.util.List;
    25 import java.util.Stack;
    26 
    27 /**
    28  *
    29  * @author Ing. František Kučera (frantovo.cz)
    30  */
    31 public abstract class AbstractFormatter implements Formatter {
    32 
    33 	private Stack<State> state = new Stack<>();
    34 	private FormatterContext formatterContext;
    35 	private ColumnsHeader currentColumnsHeader;
    36 	private String currentQuery;
    37 	private int currentColumnsCount;
    38 	private int currentRowCount;
    39 
    40 	public AbstractFormatter(FormatterContext formatterContext) {
    41 		this.formatterContext = formatterContext;
    42 		state.push(State.ROOT);
    43 	}
    44 
    45 	/*
    46 	 * root
    47 	 * .batch
    48 	 * ..database
    49 	 * ...statement
    50 	 * ....@query
    51 	 * ....@parameters
    52 	 * ....resultSet
    53 	 * .....row
    54 	 * ......@columnValue
    55 	 * ....@updatesResult
    56 	 */
    57 	protected enum State {
    58 
    59 		ROOT,
    60 		BATCH,
    61 		DATABASE,
    62 		STATEMENT,
    63 		RESULT_SET,
    64 		ROW
    65 	}
    66 
    67 	/**
    68 	 * Go down in hierarchy.
    69 	 * Pushes new state and verifies the old one.
    70 	 *
    71 	 * @param current the new state – currently entering
    72 	 * @param expected expected previous states (any of them is valid)
    73 	 * @return previous state
    74 	 * @throws IllegalStateException if previous state was not one from expected
    75 	 */
    76 	private State pushState(State current, EnumSet expected) {
    77 		State previous = state.peek();
    78 
    79 		if (expected.contains(previous)) {
    80 			state.push(current);
    81 			return previous;
    82 		} else {
    83 			throw new IllegalStateException("Formatter was in wrong state: " + previous + " when it should be in one of: " + expected);
    84 		}
    85 	}
    86 
    87 	protected State peekState(EnumSet expected) {
    88 		State current = state.peek();
    89 
    90 		if (expected.contains(current)) {
    91 			return current;
    92 		} else {
    93 			throw new IllegalStateException("Formatter is in wrong state: " + current + " when it should be in one of: " + expected);
    94 		}
    95 
    96 	}
    97 
    98 	/**
    99 	 * Go up in hierarchy.
   100 	 * Pops the superior state/branch.
   101 	 *
   102 	 * @param expected expected superior state
   103 	 * @return the superior state
   104 	 * @throws IllegalStateException if superior state was not one from expected or if there is no
   105 	 * more superior state (we are at root level)
   106 	 */
   107 	private State popState(EnumSet expected) {
   108 		try {
   109 			state.pop();
   110 			State superior = state.peek();
   111 			if (expected.contains(superior)) {
   112 				return superior;
   113 			} else {
   114 				throw new IllegalStateException("Formatter had wrong superior state: " + superior + " when it should be in one of: " + expected);
   115 			}
   116 		} catch (EmptyStackException e) {
   117 			throw new IllegalStateException("Formatter was already at root level – there is nothing above that.", e);
   118 		}
   119 	}
   120 
   121 	@Override
   122 	public void writeStartBatch() {
   123 		pushState(State.BATCH, EnumSet.of(State.ROOT));
   124 	}
   125 
   126 	@Override
   127 	public void writeEndBatch() {
   128 		popState(EnumSet.of(State.ROOT));
   129 	}
   130 
   131 	@Override
   132 	public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
   133 		pushState(State.DATABASE, EnumSet.of(State.BATCH));
   134 	}
   135 
   136 	@Override
   137 	public void writeEndDatabase() {
   138 		popState(EnumSet.of(State.BATCH));
   139 	}
   140 
   141 	@Override
   142 	public void writeStartStatement() {
   143 		pushState(State.STATEMENT, EnumSet.of(State.DATABASE));
   144 	}
   145 
   146 	@Override
   147 	public void writeEndStatement() {
   148 		popState(EnumSet.of(State.DATABASE));
   149 	}
   150 
   151 	@Override
   152 	public void writeStartResultSet(ColumnsHeader header) {
   153 		pushState(State.RESULT_SET, EnumSet.of(State.STATEMENT));
   154 		currentRowCount = 0;
   155 		currentColumnsHeader = header;
   156 	}
   157 
   158 	@Override
   159 	public void writeEndResultSet() {
   160 		popState(EnumSet.of(State.STATEMENT));
   161 		currentColumnsHeader = null;
   162 	}
   163 
   164 	@Override
   165 	public void writeQuery(String sql) {
   166 		peekState(EnumSet.of(State.STATEMENT));
   167 
   168 		if (currentColumnsHeader == null) {
   169 			currentQuery = sql;
   170 		} else {
   171 			throw new IllegalStateException("Query string '" + sql + "' must be set before columns header – was already set: " + currentColumnsHeader);
   172 		}
   173 	}
   174 
   175 	@Override
   176 	public void writeParameters(List<? extends Parameter> parameters) {
   177 		peekState(EnumSet.of(State.STATEMENT));
   178 
   179 		if (currentColumnsHeader != null) {
   180 			throw new IllegalStateException("Parameters '" + parameters + "' must be set before columns header – was already set: " + currentColumnsHeader);
   181 		}
   182 
   183 		if (currentQuery == null && parameters != null) {
   184 			throw new IllegalStateException("Parameters '" + parameters + "' must be set after query – was not yet set.");
   185 		}
   186 	}
   187 
   188 	@Override
   189 	public void writeStartRow() {
   190 		pushState(State.ROW, EnumSet.of(State.RESULT_SET));
   191 		currentColumnsCount = 0;
   192 		currentRowCount++;
   193 	}
   194 
   195 	@Override
   196 	public void writeEndRow() {
   197 		popState(EnumSet.of(State.RESULT_SET));
   198 	}
   199 
   200 	@Override
   201 	public void writeColumnValue(Object value) {
   202 		peekState(EnumSet.of(State.ROW));
   203 		currentColumnsCount++;
   204 
   205 		int declaredCount = currentColumnsHeader.getColumnCount();
   206 		if (currentColumnsCount > declaredCount) {
   207 			throw new IllegalStateException("Current columns count is " + currentColumnsCount + " which is more than declared " + declaredCount + " in header.");
   208 		}
   209 	}
   210 
   211 	@Override
   212 	public void writeUpdatesResult(int updatedRowsCount) {
   213 		peekState(EnumSet.of(State.STATEMENT));
   214 	}
   215 
   216 	@Override
   217 	public void close() throws FormatterException {
   218 	}
   219 
   220 	public FormatterContext getFormatterContext() {
   221 		return formatterContext;
   222 	}
   223 
   224 	protected ColumnsHeader getCurrentColumnsHeader() {
   225 		return currentColumnsHeader;
   226 	}
   227 
   228 	/**
   229 	 * @return column number, 1 = first
   230 	 */
   231 	protected int getCurrentColumnsCount() {
   232 		return currentColumnsCount;
   233 	}
   234 
   235 	protected boolean isCurrentColumnFirst() {
   236 		return currentColumnsCount == 1;
   237 	}
   238 
   239 	protected boolean isCurrentColumnLast() {
   240 		return currentColumnsCount == currentColumnsHeader.getColumnCount();
   241 	}
   242 
   243 	/**
   244 	 * @return row number, 1 = first
   245 	 */
   246 	protected int getCurrentRowCount() {
   247 		return currentRowCount;
   248 	}
   249 }