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 * ...@updatedRowsCount
59 * ....resultSet (see above)
61 protected enum State {
72 * Go down in hierarchy.
73 * Pushes new state and verifies the old one.
75 * @param current the new state – currently entering
76 * @param expected expected previous states (any of them is valid)
77 * @return previous state
78 * @throws IllegalStateException if previous state was not one from expected
80 private State pushState(State current, EnumSet expected) {
81 State previous = state.peek();
83 if (expected.contains(previous)) {
87 throw new IllegalStateException("Formatter was in wrong state: " + previous + " when it should be in one of: " + expected);
91 protected State peekState(EnumSet expected) {
92 State current = state.peek();
94 if (expected.contains(current)) {
97 throw new IllegalStateException("Formatter is in wrong state: " + current + " when it should be in one of: " + expected);
103 * Go up in hierarchy.
104 * Pops the superior state/branch.
106 * @param expected expected superior state
107 * @return the superior state
108 * @throws IllegalStateException if superior state was not one from expected or if there is no
109 * more superior state (we are at root level)
111 private State popState(EnumSet expected) {
114 State superior = state.peek();
115 if (expected.contains(superior)) {
118 throw new IllegalStateException("Formatter had wrong superior state: " + superior + " when it should be in one of: " + expected);
120 } catch (EmptyStackException e) {
121 throw new IllegalStateException("Formatter was already at root level – there is nothing above that.", e);
126 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
127 pushState(State.DATABASE, EnumSet.of(State.ROOT));
131 public void writeEndDatabase() {
132 popState(EnumSet.of(State.ROOT));
136 public void writeStartResultSet() {
137 pushState(State.RESULT_SET, EnumSet.of(State.DATABASE, State.GENERATED_KEYS));
142 public void writeEndResultSet() {
143 popState(EnumSet.of(State.DATABASE, State.GENERATED_KEYS));
144 currentColumnsHeader = null;
148 public void writeQuery(String sql) {
149 peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
151 if (currentColumnsHeader == null) {
154 throw new IllegalStateException("Query string '" + sql + "' must be set before columns header – was already set: " + currentColumnsHeader);
159 public void writeParameters(List<? extends Parameter> parameters) {
160 peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
162 if (currentColumnsHeader != null) {
163 throw new IllegalStateException("Parameters '" + parameters + "' must be set before columns header – was already set: " + currentColumnsHeader);
166 if (currentQuery == null) {
167 throw new IllegalStateException("Parameters '" + parameters + "' must be set after query – was not yet set.");
172 public void writeColumnsHeader(ColumnsHeader header) {
173 peekState(EnumSet.of(State.RESULT_SET, State.GENERATED_KEYS));
175 if (currentColumnsHeader == null) {
176 currentColumnsHeader = header;
178 throw new IllegalStateException("Columns header can be set only once per result set – was already set: " + currentColumnsHeader);
183 public void writeStartRow() {
184 pushState(State.ROW, EnumSet.of(State.RESULT_SET));
185 currentColumnsCount = 0;
190 public void writeEndRow() {
191 popState(EnumSet.of(State.RESULT_SET));
195 public void writeColumnValue(Object value) {
196 peekState(EnumSet.of(State.ROW));
197 currentColumnsCount++;
199 int declaredCount = currentColumnsHeader.getColumnCount();
200 if (currentColumnsCount > declaredCount) {
201 throw new IllegalStateException("Current columns count is " + currentColumnsCount + " which is more than declared " + declaredCount + " in header.");
206 * @param value from ResultSet
207 * @return String representation of given value
209 protected String toString(Object value) {
211 * TODO: basic formatting: boolean, null, date, numbers…
213 return String.valueOf(value);
217 public void writeStartUpdatesResult() {
218 pushState(State.UPDATES_RESULT, EnumSet.of(State.DATABASE));
222 public void writeEndUpdatesResult() {
223 popState(EnumSet.of(State.DATABASE));
224 currentColumnsHeader = null;
228 public void writeUpdatedRowsCount(int updatedRowsCount) {
229 peekState(EnumSet.of(State.UPDATES_RESULT));
233 public void writeStartGeneratedKeys() {
234 pushState(State.GENERATED_KEYS, EnumSet.of(State.UPDATES_RESULT));
238 public void writeEndGeneratedKeys() {
239 popState(EnumSet.of(State.UPDATES_RESULT));
242 public FormatterContext getFormatterContext() {
243 return formatterContext;
246 protected ColumnsHeader getCurrentColumnsHeader() {
247 return currentColumnsHeader;
251 * @return column number, 1 = first
253 protected int getCurrentColumnsCount() {
254 return currentColumnsCount;
257 protected boolean isCurrentColumnFirst() {
258 return currentColumnsCount == 1;
261 protected boolean isCurrentColumnLast() {
262 return currentColumnsCount == currentColumnsHeader.getColumnCount();
266 * @return row number, 1 = first
268 protected int getCurrentRowCount() {
269 return currentRowCount;
272 * TODO: write SQLWarning