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