java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 20 Dec 2013 22:58:02 +0100
branchv_0
changeset 24 65e3fffae091
parent 22 37fe883f8410
child 25 4c118af3e855
permissions -rw-r--r--
formatter: FormatterContext
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;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
import java.util.EmptyStackException;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
import java.util.EnumSet;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
import java.util.List;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
import java.util.Stack;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
/**
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
 * @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
    29
 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
public abstract class AbstractFormatter implements Formatter {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	private Stack<State> state = new Stack<>();
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    33
	private FormatterContext formatterContext;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	private ColumnsHeader currentColumnsHeader;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
	private String currentQuery;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	private int currentColumnsCount;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    38
	public AbstractFormatter(FormatterContext formatterContext) {
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
    39
		this.formatterContext = formatterContext;
22
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		state.push(State.ROOT);
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
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
	 * root
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	 * .database
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	 * ..resultSet
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	 * ...@query
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	 * ...@parameters
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	 * ...@columnsHeader
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	 * ...row
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	 * ....@columnValue
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	 * ..updatesResult
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	 * ...@query
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	 * ...@parameters
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	 * ...@updatedRowsCount
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	 * ...generatedKeys
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	 * ....resultSet (see above)
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
	protected enum State {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		ROOT,
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		DATABASE,
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		RESULT_SET,
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		ROW,
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		UPDATES_RESULT,
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		GENERATED_KEYS
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
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
	 * Go down in hierarchy.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	 * 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
    72
	 *
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	 * @param current the new state – currently entering
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	 * @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
    75
	 * @return previous state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	 * @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
    77
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	private State pushState(State current, EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		State previous = state.peek();
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
		if (expected.contains(previous)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
			state.push(current);
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			return previous;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
			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
    86
		}
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
	protected State peekState(EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
		State current = state.peek();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		if (expected.contains(current)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
			return current;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
			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
    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
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
	 * Go up in hierarchy.
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	 * Pops the superior state/branch.
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
	 * @param expected expected superior state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	 * @return the superior state
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	 * @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
   107
	 * 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
   108
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	private State popState(EnumSet expected) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
		try {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
			State superior = state.pop();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
			if (expected.contains(superior)) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
				return superior;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
			} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
				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
   116
			}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
		} catch (EmptyStackException e) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
			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
   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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
	public void writeStartDatabase() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
		pushState(State.DATABASE, EnumSet.of(State.ROOT));
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
	public void writeEndDatabase() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
		popState(EnumSet.of(State.ROOT));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
	public void writeStartResultSet() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
		pushState(State.RESULT_SET, EnumSet.of(State.DATABASE, State.GENERATED_KEYS));
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
	public void writeEndResultSet() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   139
		popState(EnumSet.of(State.DATABASE, State.GENERATED_KEYS));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   140
		currentColumnsHeader = null;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   141
	}
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
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
	public void writeQuery(String sql) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   145
		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
   146
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
		if (currentColumnsHeader == null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
			currentQuery = sql;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   149
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   150
			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
   151
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   152
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   154
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   155
	public void writeParameters(List<Parameter> parameters) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
		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
   157
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
		if (currentColumnsHeader != null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
			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
   160
		}
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
		if (currentQuery == null) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
			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
   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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
	public void writeColumnsHeader(ColumnsHeader header) {
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
			currentColumnsHeader = header;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   173
		} else {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
			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
   175
		}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
	}
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
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   179
	public void writeStartRow() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
		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
   181
		currentColumnsCount = 0;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   182
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   183
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   184
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   185
	public void writeEndRow() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   186
		popState(EnumSet.of(State.RESULT_SET));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   187
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   188
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   189
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   190
	public void writeColumnValue(Object value) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   191
		peekState(EnumSet.of(State.ROW));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
		currentColumnsCount++;
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
		int declaredCount = currentColumnsHeader.getColumnCount();
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   195
		if (currentColumnsCount > declaredCount) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   196
			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
   197
		}
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 writeStartUpdatesResult() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
		pushState(State.RESULT_SET, EnumSet.of(State.DATABASE));
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 writeEndUpdatesResult() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
		popState(EnumSet.of(State.DATABASE));
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   208
		currentColumnsHeader = null;
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   212
	public void writeUpdatedRowsCount(int updatedRowsCount) {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   213
		peekState(EnumSet.of(State.UPDATES_RESULT));
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
	public void writeStartGeneratedKeys() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
		pushState(State.GENERATED_KEYS, EnumSet.of(State.UPDATES_RESULT));
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   221
	@Override
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   222
	public void writeEndGeneratedKeys() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   223
		popState(EnumSet.of(State.UPDATES_RESULT));
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
24
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   226
	public FormatterContext getFormatterContext() {
65e3fffae091 formatter: FormatterContext
František Kučera <franta-hg@frantovo.cz>
parents: 22
diff changeset
   227
		return formatterContext;
22
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
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   230
	protected ColumnsHeader getCurrentColumnsHeader() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   231
		return currentColumnsHeader;
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   232
	}
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   233
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   234
	protected int getCurrentColumnsCount() {
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   235
		return currentColumnsCount;
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
	 * TODO: write SQLWarning
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   239
	 */
37fe883f8410 formatter: interface and abstract class
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   240
}