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
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * 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
    11
 * 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
    12
 * 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
    13
 * GNU General Public License for more details.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk.formatting;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
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
    21
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
    22
import java.util.EmptyStackException;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import java.util.EnumSet;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.util.List;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
import java.util.Stack;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
/**
155
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    28
 * <ol>
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    29
 * <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
    30
 * <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
    31
 * events</li>
eb3676c6929b more JavaDoc
František Kučera <franta-hg@frantovo.cz>
parents: 142
diff changeset
    32
 * </ol>
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 * @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
    35
 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
public abstract class AbstractFormatter implements Formatter {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	private Stack<State> state = new Stack<>();
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    39
	private FormatterContext formatterContext;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	private ColumnsHeader currentColumnsHeader;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	private String currentQuery;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	private int currentColumnsCount;
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    43
	private int currentRowCount;
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	/*
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	 * 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
    52
	 * .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
    53
	 * ..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
    54
	 * ...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
    55
	 * ....@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
    56
	 * ....@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
    57
	 * ....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
    58
	 * .....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
    59
	 * ......@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
    60
	 * ....@updatesResult
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	protected enum State {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		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
    65
		BATCH,
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		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
    67
		STATEMENT,
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		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
    69
		ROW
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	}
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
	/**
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	 * Go down in hierarchy.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	 * 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
    75
	 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	 * @param current the new state – currently entering
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	 * @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
    78
	 * @return previous state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	 * @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
    80
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
	private State pushState(State current, EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		State previous = state.peek();
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
		if (expected.contains(previous)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
			state.push(current);
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
			return previous;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
			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
    89
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
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
	protected State peekState(EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		State current = state.peek();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
		if (expected.contains(current)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
			return current;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
			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
    99
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
	}
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
	/**
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	 * Go up in hierarchy.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	 * Pops the superior state/branch.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	 * @param expected expected superior state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
	 * @return the superior state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	 * @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
   110
	 * 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
   111
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
	private State popState(EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
		try {
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   114
			state.pop();
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   115
			State superior = state.peek();
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
			if (expected.contains(superior)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
				return superior;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
			} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
				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
   120
			}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
		} catch (EmptyStackException e) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
			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
   123
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
	@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
   127
	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
   128
		pushState(State.BATCH, 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
   129
	}
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
   130
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
   131
	@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
   132
	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
   133
		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
   134
	}
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
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
	@Override
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   137
	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
   138
		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
   139
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   140
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   141
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   142
	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
   143
		popState(EnumSet.of(State.BATCH));
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   145
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   146
	@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
   147
	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
   148
		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
   149
	}
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
   150
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
   151
	@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
   152
	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
   153
		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
   154
	}
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
   155
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
	@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
   157
	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
   158
		pushState(State.RESULT_SET, EnumSet.of(State.STATEMENT));
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   159
		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
   160
		currentColumnsHeader = header;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
	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
   165
		popState(EnumSet.of(State.STATEMENT));
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
		currentColumnsHeader = null;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
	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
   171
		peekState(EnumSet.of(State.STATEMENT));
22
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
		if (currentColumnsHeader == null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
			currentQuery = sql;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
			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
   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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
	@Override
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   181
	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
   182
		peekState(EnumSet.of(State.STATEMENT));
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   183
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   184
		if (currentColumnsHeader != null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   185
			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
   186
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   187
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
   188
		if (currentQuery == null && parameters != null) {
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   189
			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
   190
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   191
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   193
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   194
	public void writeStartRow() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   195
		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
   196
		currentColumnsCount = 0;
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   197
		currentRowCount++;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   198
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   200
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   201
	public void writeEndRow() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
		popState(EnumSet.of(State.RESULT_SET));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   204
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
	public void writeColumnValue(Object value) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
		peekState(EnumSet.of(State.ROW));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   208
		currentColumnsCount++;
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
		int declaredCount = currentColumnsHeader.getColumnCount();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   211
		if (currentColumnsCount > declaredCount) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   212
			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
   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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   216
	@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
   217
	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
   218
		peekState(EnumSet.of(State.STATEMENT));
22
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
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
   221
	@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
   222
	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
   223
	}
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
   224
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   225
	public FormatterContext getFormatterContext() {
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   226
		return formatterContext;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   228
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   229
	protected ColumnsHeader getCurrentColumnsHeader() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   230
		return currentColumnsHeader;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   231
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   232
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   233
	/**
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   234
	 * @return column number, 1 = first
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   235
	 */
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   236
	protected int getCurrentColumnsCount() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   237
		return currentColumnsCount;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   238
	}
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   239
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   240
	protected boolean isCurrentColumnFirst() {
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   241
		return currentColumnsCount == 1;
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   242
	}
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   243
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   244
	protected boolean isCurrentColumnLast() {
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   245
		return currentColumnsCount == currentColumnsHeader.getColumnCount();
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   246
	}
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   247
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   248
	/**
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   249
	 * @return row number, 1 = first
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   250
	 */
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   251
	protected int getCurrentRowCount() {
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   252
		return currentRowCount;
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   253
	}
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   254
}