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