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 info.globalcode.sql.dk.configuration.DatabaseDefinition;
22 import java.util.EmptyStackException;
23 import java.util.EnumSet;
24 import java.util.List;
25 import java.util.Stack;
29 * @author Ing. František Kučera (frantovo.cz)
31 public abstract class AbstractFormatter implements Formatter {
33 private Stack<State> state = new Stack<>();
34 private FormatterContext formatterContext;
35 private ColumnsHeader currentColumnsHeader;
36 private String currentQuery;
37 private int currentColumnsCount;
38 private int currentRowCount;
40 public AbstractFormatter(FormatterContext formatterContext) {
41 this.formatterContext = formatterContext;
42 state.push(State.ROOT);
57 protected enum State {
68 * Go down in hierarchy.
69 * Pushes new state and verifies the old one.
71 * @param current the new state – currently entering
72 * @param expected expected previous states (any of them is valid)
73 * @return previous state
74 * @throws IllegalStateException if previous state was not one from expected
76 private State pushState(State current, EnumSet expected) {
77 State previous = state.peek();
79 if (expected.contains(previous)) {
83 throw new IllegalStateException("Formatter was in wrong state: " + previous + " when it should be in one of: " + expected);
87 protected State peekState(EnumSet expected) {
88 State current = state.peek();
90 if (expected.contains(current)) {
93 throw new IllegalStateException("Formatter is in wrong state: " + current + " when it should be in one of: " + expected);
100 * Pops the superior state/branch.
102 * @param expected expected superior state
103 * @return the superior state
104 * @throws IllegalStateException if superior state was not one from expected or if there is no
105 * more superior state (we are at root level)
107 private State popState(EnumSet expected) {
110 State superior = state.peek();
111 if (expected.contains(superior)) {
114 throw new IllegalStateException("Formatter had wrong superior state: " + superior + " when it should be in one of: " + expected);
116 } catch (EmptyStackException e) {
117 throw new IllegalStateException("Formatter was already at root level – there is nothing above that.", e);
122 public void writeStartBatch() {
123 pushState(State.BATCH, EnumSet.of(State.ROOT));
127 public void writeEndBatch() {
128 popState(EnumSet.of(State.ROOT));
132 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
133 pushState(State.DATABASE, EnumSet.of(State.BATCH));
137 public void writeEndDatabase() {
138 popState(EnumSet.of(State.BATCH));
142 public void writeStartStatement() {
143 pushState(State.STATEMENT, EnumSet.of(State.DATABASE));
147 public void writeEndStatement() {
148 popState(EnumSet.of(State.DATABASE));
152 public void writeStartResultSet(ColumnsHeader header) {
153 pushState(State.RESULT_SET, EnumSet.of(State.STATEMENT));
155 currentColumnsHeader = header;
159 public void writeEndResultSet() {
160 popState(EnumSet.of(State.STATEMENT));
161 currentColumnsHeader = null;
165 public void writeQuery(String sql) {
166 peekState(EnumSet.of(State.STATEMENT));
168 if (currentColumnsHeader == null) {
171 throw new IllegalStateException("Query string '" + sql + "' must be set before columns header – was already set: " + currentColumnsHeader);
176 public void writeParameters(List<? extends Parameter> parameters) {
177 peekState(EnumSet.of(State.STATEMENT));
179 if (currentColumnsHeader != null) {
180 throw new IllegalStateException("Parameters '" + parameters + "' must be set before columns header – was already set: " + currentColumnsHeader);
183 if (currentQuery == null && parameters != null) {
184 throw new IllegalStateException("Parameters '" + parameters + "' must be set after query – was not yet set.");
189 public void writeStartRow() {
190 pushState(State.ROW, EnumSet.of(State.RESULT_SET));
191 currentColumnsCount = 0;
196 public void writeEndRow() {
197 popState(EnumSet.of(State.RESULT_SET));
201 public void writeColumnValue(Object value) {
202 peekState(EnumSet.of(State.ROW));
203 currentColumnsCount++;
205 int declaredCount = currentColumnsHeader.getColumnCount();
206 if (currentColumnsCount > declaredCount) {
207 throw new IllegalStateException("Current columns count is " + currentColumnsCount + " which is more than declared " + declaredCount + " in header.");
212 public void writeUpdatesResult(int updatedRowsCount) {
213 peekState(EnumSet.of(State.STATEMENT));
217 public void close() throws FormatterException {
220 public FormatterContext getFormatterContext() {
221 return formatterContext;
224 protected ColumnsHeader getCurrentColumnsHeader() {
225 return currentColumnsHeader;
229 * @return column number, 1 = first
231 protected int getCurrentColumnsCount() {
232 return currentColumnsCount;
235 protected boolean isCurrentColumnFirst() {
236 return currentColumnsCount == 1;
239 protected boolean isCurrentColumnLast() {
240 return currentColumnsCount == currentColumnsHeader.getColumnCount();
244 * @return row number, 1 = first
246 protected int getCurrentRowCount() {
247 return currentRowCount;