java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Tue, 07 Jan 2014 21:54:59 +0100
branchv_0
changeset 142 da1e38386d84
parent 101 97b0d9069133
child 155 eb3676c6929b
permissions -rw-r--r--
Formatters: structural change – new level „statement“ → query and parameters are no more duplicated into each result set or updates result
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
/**
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 * @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
    30
 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
public abstract class AbstractFormatter implements Formatter {
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
	private Stack<State> state = new Stack<>();
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    34
	private FormatterContext formatterContext;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	private ColumnsHeader currentColumnsHeader;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	private String currentQuery;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	private int currentColumnsCount;
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
    38
	private int currentRowCount;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    40
	public AbstractFormatter(FormatterContext formatterContext) {
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    41
		this.formatterContext = formatterContext;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
		state.push(State.ROOT);
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	/*
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	 * 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
    47
	 * .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
    48
	 * ..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
    49
	 * ...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
    50
	 * ....@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
    51
	 * ....@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
    52
	 * ....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
    53
	 * .....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
    54
	 * ......@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
    55
	 * ....@updatesResult
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	protected enum State {
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,
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
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,
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		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
    64
		ROW
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	/**
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	 * Go down in hierarchy.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	 * 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
    70
	 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	 * @param current the new state – currently entering
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	 * @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
    73
	 * @return previous state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	 * @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
    75
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	private State pushState(State current, EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		State previous = state.peek();
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
		if (expected.contains(previous)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			state.push(current);
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			return previous;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			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
    84
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
	protected State peekState(EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
		State current = state.peek();
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
		if (expected.contains(current)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
			return current;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
			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
    94
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
	}
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
	 * Go up in hierarchy.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	 * Pops the superior state/branch.
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
	 * @param expected expected superior state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
	 * @return the superior state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	 * @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
   105
	 * 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
   106
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	private State popState(EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		try {
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   109
			state.pop();
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   110
			State superior = state.peek();
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
			if (expected.contains(superior)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
				return superior;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
			} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
				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
   115
			}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
		} catch (EmptyStackException e) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
			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
   118
		}
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
	@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
   122
	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
   123
		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
   124
	}
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
   125
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
   126
	@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
   127
	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
   128
		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
   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
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   132
	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
   133
		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
   134
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
	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
   138
		popState(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
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
   142
	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
   143
		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
   144
	}
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
   145
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
   146
	@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
   147
	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
   148
		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
   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 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
   153
		pushState(State.RESULT_SET, EnumSet.of(State.STATEMENT));
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   154
		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
   155
		currentColumnsHeader = header;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
	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
   160
		popState(EnumSet.of(State.STATEMENT));
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
		currentColumnsHeader = null;
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
	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
   166
		peekState(EnumSet.of(State.STATEMENT));
22
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
		if (currentColumnsHeader == null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
			currentQuery = sql;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
			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
   172
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   173
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
	@Override
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   176
	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
   177
		peekState(EnumSet.of(State.STATEMENT));
22
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
		if (currentColumnsHeader != null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
			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
   181
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   182
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
   183
		if (currentQuery == null && parameters != null) {
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   184
			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
   185
		}
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   188
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   189
	public void writeStartRow() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   190
		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
   191
		currentColumnsCount = 0;
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   192
		currentRowCount++;
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   195
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   196
	public void writeEndRow() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   197
		popState(EnumSet.of(State.RESULT_SET));
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 writeColumnValue(Object value) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
		peekState(EnumSet.of(State.ROW));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
		currentColumnsCount++;
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
		int declaredCount = currentColumnsHeader.getColumnCount();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
		if (currentColumnsCount > declaredCount) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
			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
   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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   211
	@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
   212
	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
   213
		peekState(EnumSet.of(State.STATEMENT));
22
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
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
   216
	@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
   217
	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
   218
	}
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
   219
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   220
	public FormatterContext getFormatterContext() {
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   221
		return formatterContext;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   222
	}
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
	protected ColumnsHeader getCurrentColumnsHeader() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   225
		return currentColumnsHeader;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   226
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   228
	/**
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   229
	 * @return column number, 1 = first
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   230
	 */
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   231
	protected int getCurrentColumnsCount() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   232
		return currentColumnsCount;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   233
	}
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   234
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   235
	protected boolean isCurrentColumnFirst() {
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   236
		return currentColumnsCount == 1;
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   237
	}
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   238
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   239
	protected boolean isCurrentColumnLast() {
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   240
		return currentColumnsCount == currentColumnsHeader.getColumnCount();
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   241
	}
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
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 row number, 1 = first
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   245
	 */
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   246
	protected int getCurrentRowCount() {
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   247
		return currentRowCount;
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   248
	}
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   249
}