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