3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.formatting;
20 import info.globalcode.sql.dk.Parameter;
21 import java.util.EmptyStackException;
22 import java.util.EnumSet;
23 import java.util.List;
24 import java.util.Stack;
28 * @author Ing. František Kučera (frantovo.cz)
30 public abstract class AbstractFormatter implements Formatter {
32 private Stack<State> state = new Stack<>();
33 private FormatterContext formatterContext;
34 private ColumnsHeader currentColumnsHeader;
35 private String currentQuery;
36 private int currentColumnsCount;
37 private int currentRowCount;
39 public AbstractFormatter(FormatterContext formatterContext) {
40 this.formatterContext = formatterContext;
41 state.push(State.ROOT);
56 * ...@updatedRowsCount
58 * ....resultSet (see above)
60 protected enum State {
71 * Go down in hierarchy.
72 * Pushes new state and verifies the old one.
74 * @param current the new state – currently entering
75 * @param expected expected previous states (any of them is valid)
76 * @return previous state
77 * @throws IllegalStateException if previous state was not one from expected
79 private State pushState(State current, EnumSet expected) {
80 State previous = state.peek();
82 if (expected.contains(previous)) {
86 throw new IllegalStateException("Formatter was in wrong state: " + previous + " when it should be in one of: " + expected);
90 protected State peekState(EnumSet expected) {
91 State current = state.peek();
93 if (expected.contains(current)) {
96 throw new IllegalStateException("Formatter is in wrong state: " + current + " when it should be in one of: " + expected);
102 * Go up in hierarchy.
103 * Pops the superior state/branch.
105 * @param expected expected superior state
106 * @return the superior state
107 * @throws IllegalStateException if superior state was not one from expected or if there is no
108 * more superior state (we are at root level)
110 private State popState(EnumSet expected) {
112 State superior = state.pop();
113 if (expected.contains(superior)) {
116 throw new IllegalStateException("Formatter had wrong superior state: " + superior + " when it should be in one of: " + expected);
118 } catch (EmptyStackException e) {
119 throw new IllegalStateException("Formatter was already at root level – there is nothing above that.", e);
124 public void writeStartDatabase() {
125 pushState(State.DATABASE, EnumSet.of(State.ROOT));
129 public void writeEndDatabase() {
130 popState(EnumSet.of(State.ROOT));
134 public void writeStartResultSet() {
135 pushState(State.RESULT_SET, EnumSet.of(State.DATABASE, State.GENERATED_KEYS));
140 public void writeEndResultSet() {
141 popState(EnumSet.of(State.DATABASE, State.GENERATED_KEYS));
142 currentColumnsHeader = null;
146 public void writeQuery(String sql) {
147 peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
149 if (currentColumnsHeader == null) {
152 throw new IllegalStateException("Query string '" + sql + "' must be set before columns header – was already set: " + currentColumnsHeader);
157 public void writeParameters(List<Parameter> parameters) {
158 peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
160 if (currentColumnsHeader != null) {
161 throw new IllegalStateException("Parameters '" + parameters + "' must be set before columns header – was already set: " + currentColumnsHeader);
164 if (currentQuery == null) {
165 throw new IllegalStateException("Parameters '" + parameters + "' must be set after query – was not yet set.");
170 public void writeColumnsHeader(ColumnsHeader header) {
171 peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
173 if (currentColumnsHeader == null) {
174 currentColumnsHeader = header;
176 throw new IllegalStateException("Columns header can be set only once per result set – was already set: " + currentColumnsHeader);
181 public void writeStartRow() {
182 pushState(State.ROW, EnumSet.of(State.RESULT_SET));
183 currentColumnsCount = 0;
188 public void writeEndRow() {
189 popState(EnumSet.of(State.RESULT_SET));
193 public void writeColumnValue(Object value) {
194 peekState(EnumSet.of(State.ROW));
195 currentColumnsCount++;
197 int declaredCount = currentColumnsHeader.getColumnCount();
198 if (currentColumnsCount > declaredCount) {
199 throw new IllegalStateException("Current columns count is " + currentColumnsCount + " which is more than declared " + declaredCount + " in header.");
204 public void writeStartUpdatesResult() {
205 pushState(State.RESULT_SET, EnumSet.of(State.DATABASE));
209 public void writeEndUpdatesResult() {
210 popState(EnumSet.of(State.DATABASE));
211 currentColumnsHeader = null;
215 public void writeUpdatedRowsCount(int updatedRowsCount) {
216 peekState(EnumSet.of(State.UPDATES_RESULT));
220 public void writeStartGeneratedKeys() {
221 pushState(State.GENERATED_KEYS, EnumSet.of(State.UPDATES_RESULT));
225 public void writeEndGeneratedKeys() {
226 popState(EnumSet.of(State.UPDATES_RESULT));
229 public FormatterContext getFormatterContext() {
230 return formatterContext;
233 protected ColumnsHeader getCurrentColumnsHeader() {
234 return currentColumnsHeader;
238 * @return column number, 1 = first
240 protected int getCurrentColumnsCount() {
241 return currentColumnsCount;
245 * @return row number, 1 = first
247 protected int getCurrentRowCount() {
248 return currentRowCount;
251 * TODO: write SQLWarning