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