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 protected enum State {
69 * Go down in hierarchy.
70 * Pushes new state and verifies the old one.
72 * @param current the new state – currently entering
73 * @param expected expected previous states (any of them is valid)
74 * @return previous state
75 * @throws IllegalStateException if previous state was not one from expected
77 private State pushState(State current, EnumSet expected) {
78 State previous = state.peek();
80 if (expected.contains(previous)) {
84 throw new IllegalStateException("Formatter was in wrong state: " + previous + " when it should be in one of: " + expected);
88 protected State peekState(EnumSet expected) {
89 State current = state.peek();
91 if (expected.contains(current)) {
94 throw new IllegalStateException("Formatter is in wrong state: " + current + " when it should be in one of: " + expected);
100 * Go up in hierarchy.
101 * Pops the superior state/branch.
103 * @param expected expected superior state
104 * @return the superior state
105 * @throws IllegalStateException if superior state was not one from expected or if there is no
106 * more superior state (we are at root level)
108 private State popState(EnumSet expected) {
111 State superior = state.peek();
112 if (expected.contains(superior)) {
115 throw new IllegalStateException("Formatter had wrong superior state: " + superior + " when it should be in one of: " + expected);
117 } catch (EmptyStackException e) {
118 throw new IllegalStateException("Formatter was already at root level – there is nothing above that.", e);
123 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
124 pushState(State.DATABASE, EnumSet.of(State.ROOT));
128 public void writeEndDatabase() {
129 popState(EnumSet.of(State.ROOT));
133 public void writeStartResultSet() {
134 pushState(State.RESULT_SET, EnumSet.of(State.DATABASE));
139 public void writeEndResultSet() {
140 popState(EnumSet.of(State.DATABASE));
141 currentColumnsHeader = null;
145 public void writeQuery(String sql) {
146 peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
148 if (currentColumnsHeader == null) {
151 throw new IllegalStateException("Query string '" + sql + "' must be set before columns header – was already set: " + currentColumnsHeader);
156 public void writeParameters(List<? extends Parameter> parameters) {
157 peekState(EnumSet.of(State.RESULT_SET, State.UPDATES_RESULT));
159 if (currentColumnsHeader != null) {
160 throw new IllegalStateException("Parameters '" + parameters + "' must be set before columns header – was already set: " + currentColumnsHeader);
163 if (currentQuery == null) {
164 throw new IllegalStateException("Parameters '" + parameters + "' must be set after query – was not yet set.");
169 public void writeColumnsHeader(ColumnsHeader header) {
170 peekState(EnumSet.of(State.RESULT_SET));
172 if (currentColumnsHeader == null) {
173 currentColumnsHeader = header;
175 throw new IllegalStateException("Columns header can be set only once per result set – was already set: " + currentColumnsHeader);
180 public void writeStartRow() {
181 pushState(State.ROW, EnumSet.of(State.RESULT_SET));
182 currentColumnsCount = 0;
187 public void writeEndRow() {
188 popState(EnumSet.of(State.RESULT_SET));
192 public void writeColumnValue(Object value) {
193 peekState(EnumSet.of(State.ROW));
194 currentColumnsCount++;
196 int declaredCount = currentColumnsHeader.getColumnCount();
197 if (currentColumnsCount > declaredCount) {
198 throw new IllegalStateException("Current columns count is " + currentColumnsCount + " which is more than declared " + declaredCount + " in header.");
203 * @param value from ResultSet
204 * @return String representation of given value
206 protected String toString(Object value) {
208 * TODO: basic formatting: boolean, null, date, numbers…
210 return String.valueOf(value);
214 public void writeStartUpdatesResult() {
215 pushState(State.UPDATES_RESULT, EnumSet.of(State.DATABASE));
219 public void writeEndUpdatesResult() {
220 popState(EnumSet.of(State.DATABASE));
221 currentColumnsHeader = null;
225 public void writeUpdatedRowsCount(int updatedRowsCount) {
226 peekState(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;
244 protected boolean isCurrentColumnFirst() {
245 return currentColumnsCount == 1;
248 protected boolean isCurrentColumnLast() {
249 return currentColumnsCount == currentColumnsHeader.getColumnCount();
253 * @return row number, 1 = first
255 protected int getCurrentRowCount() {
256 return currentRowCount;
259 * TODO: write SQLWarning