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