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