java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 30 Dec 2013 00:01:39 +0100
branchv_0
changeset 101 97b0d9069133
parent 98 4d420f8b3320
child 142 da1e38386d84
permissions -rw-r--r--
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)
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
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
    49
	 * ...resultSet
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
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
	 * ....@columnsHeader
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
	 * ....row
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
    54
	 * .....@columnValue
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
	 * ...updatesResult
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
	 * ....@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
    57
	 * ....@parameters
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
    58
	 * ....@updatedRowsCount
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	protected enum State {
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
		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
    63
		BATCH,
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		DATABASE,
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		RESULT_SET,
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		ROW,
41
514df5061f59 No special formatting for generated keys. Use 'INSERT … RETURNING id' or 'INSERT … RETURNING *' to get generated keys.
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
    67
		UPDATES_RESULT
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
	}
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
	/**
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	 * Go down in hierarchy.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	 * 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
    73
	 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	 * @param current the new state – currently entering
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	 * @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
    76
	 * @return previous state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	 * @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
    78
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
	private State pushState(State current, EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		State previous = state.peek();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		if (expected.contains(previous)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			state.push(current);
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			return previous;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
			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
    87
		}
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	protected State peekState(EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		State current = state.peek();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		if (expected.contains(current)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
			return current;
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 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
    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
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
	 * Go up in hierarchy.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
	 * Pops the superior state/branch.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	 * @param expected expected superior state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	 * @return the superior state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	 * @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
   108
	 * 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
   109
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
	private State popState(EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
		try {
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   112
			state.pop();
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   113
			State superior = state.peek();
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
			if (expected.contains(superior)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
				return superior;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
			} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
				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
   118
			}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
		} catch (EmptyStackException e) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
			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
   121
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
	}
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
	@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
   125
	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
   126
		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
   127
	}
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
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
	@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
   130
	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
   131
		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
   132
	}
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
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
	@Override
29
d66858b4b563 more configuration, more JAXB, more formatters
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
   135
	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
   136
		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
   137
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   139
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   140
	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
   141
		popState(EnumSet.of(State.BATCH));
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   142
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   143
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   145
	public void writeStartResultSet() {
41
514df5061f59 No special formatting for generated keys. Use 'INSERT … RETURNING id' or 'INSERT … RETURNING *' to get generated keys.
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   146
		pushState(State.RESULT_SET, EnumSet.of(State.DATABASE));
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   147
		currentRowCount = 0;
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 writeEndResultSet() {
41
514df5061f59 No special formatting for generated keys. Use 'INSERT … RETURNING id' or 'INSERT … RETURNING *' to get generated keys.
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   152
		popState(EnumSet.of(State.DATABASE));
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
		currentColumnsHeader = null;
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
	public void writeQuery(String sql) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
		peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
		if (currentColumnsHeader == null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
			currentQuery = sql;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
			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
   164
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
	@Override
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   168
	public void writeParameters(List<? extends Parameter> parameters) {
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
		peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
		if (currentColumnsHeader != null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   172
			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
   173
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
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
   175
		if (currentQuery == null && parameters != null) {
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
			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
   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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
	public void writeColumnsHeader(ColumnsHeader header) {
41
514df5061f59 No special formatting for generated keys. Use 'INSERT … RETURNING id' or 'INSERT … RETURNING *' to get generated keys.
František Kučera <franta-hg@frantovo.cz>
parents: 39
diff changeset
   182
		peekState(EnumSet.of(State.RESULT_SET));
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
			currentColumnsHeader = header;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   186
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   187
			throw new IllegalStateException("Columns header can be set only once per result set – was already set: " + currentColumnsHeader);
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   191
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
	public void writeStartRow() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   193
		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
   194
		currentColumnsCount = 0;
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   195
		currentRowCount++;
22
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   198
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
	public void writeEndRow() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   200
		popState(EnumSet.of(State.RESULT_SET));
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 writeColumnValue(Object value) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
		peekState(EnumSet.of(State.ROW));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
		currentColumnsCount++;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   208
		int declaredCount = currentColumnsHeader.getColumnCount();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   209
		if (currentColumnsCount > declaredCount) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   210
			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
   211
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   212
	}
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
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   215
	public void writeStartUpdatesResult() {
37
9e6f8e5d5f98 support SQL commands returning more ResultSets + remove COMMAND_TYPE (type is now derived from result returned from SQL – it is not needed to specify the type on CLI)
František Kučera <franta-hg@frantovo.cz>
parents: 34
diff changeset
   216
		pushState(State.UPDATES_RESULT, EnumSet.of(State.DATABASE));
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   219
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   220
	public void writeEndUpdatesResult() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   221
		popState(EnumSet.of(State.DATABASE));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   222
		currentColumnsHeader = null;
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
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   226
	public void writeUpdatedRowsCount(int updatedRowsCount) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
		peekState(EnumSet.of(State.UPDATES_RESULT));
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
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
   230
	@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
   231
	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
   232
	}
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
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   234
	public FormatterContext getFormatterContext() {
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   235
		return formatterContext;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   236
	}
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
	protected ColumnsHeader getCurrentColumnsHeader() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   239
		return currentColumnsHeader;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   240
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   241
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   242
	/**
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   243
	 * @return column number, 1 = first
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   244
	 */
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   245
	protected int getCurrentColumnsCount() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   246
		return currentColumnsCount;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   247
	}
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   248
34
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   249
	protected boolean isCurrentColumnFirst() {
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   250
		return currentColumnsCount == 1;
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   251
	}
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
	protected boolean isCurrentColumnLast() {
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   254
		return currentColumnsCount == currentColumnsHeader.getColumnCount();
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   255
	}
9335cf31c0f2 first working version
František Kučera <franta-hg@frantovo.cz>
parents: 29
diff changeset
   256
25
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   257
	/**
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   258
	 * @return row number, 1 = first
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   259
	 */
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   260
	protected int getCurrentRowCount() {
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   261
		return currentRowCount;
4c118af3e855 formatter: currentRowCount
František Kučera <franta-hg@frantovo.cz>
parents: 24
diff changeset
   262
	}
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   263
}