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