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