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