franta-hg@22
|
1 |
/**
|
franta-hg@22
|
2 |
* SQL-DK
|
franta-hg@22
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@22
|
4 |
*
|
franta-hg@22
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@22
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@250
|
7 |
* the Free Software Foundation, version 3 of the License.
|
franta-hg@22
|
8 |
*
|
franta-hg@22
|
9 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@22
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@22
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@22
|
12 |
* GNU General Public License for more details.
|
franta-hg@22
|
13 |
*
|
franta-hg@22
|
14 |
* You should have received a copy of the GNU General Public License
|
franta-hg@22
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@22
|
16 |
*/
|
franta-hg@22
|
17 |
package info.globalcode.sql.dk.formatting;
|
franta-hg@22
|
18 |
|
franta-hg@22
|
19 |
import info.globalcode.sql.dk.Parameter;
|
franta-hg@29
|
20 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
|
franta-hg@22
|
21 |
import java.util.EmptyStackException;
|
franta-hg@22
|
22 |
import java.util.EnumSet;
|
franta-hg@22
|
23 |
import java.util.List;
|
franta-hg@22
|
24 |
import java.util.Stack;
|
franta-hg@22
|
25 |
|
franta-hg@22
|
26 |
/**
|
franta-hg@155
|
27 |
* <ol>
|
franta-hg@155
|
28 |
* <li>ensures integrity – if methods are called in correct order and context</li>
|
franta-hg@155
|
29 |
* <li>provides default implmentations of methods that does not produce any output for given
|
franta-hg@155
|
30 |
* events</li>
|
franta-hg@155
|
31 |
* </ol>
|
franta-hg@22
|
32 |
*
|
franta-hg@22
|
33 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@22
|
34 |
*/
|
franta-hg@22
|
35 |
public abstract class AbstractFormatter implements Formatter {
|
franta-hg@22
|
36 |
|
franta-hg@22
|
37 |
private Stack<State> state = new Stack<>();
|
franta-hg@24
|
38 |
private FormatterContext formatterContext;
|
franta-hg@22
|
39 |
private ColumnsHeader currentColumnsHeader;
|
franta-hg@22
|
40 |
private String currentQuery;
|
franta-hg@22
|
41 |
private int currentColumnsCount;
|
franta-hg@25
|
42 |
private int currentRowCount;
|
franta-hg@248
|
43 |
private int resultSetCount;
|
franta-hg@22
|
44 |
|
franta-hg@24
|
45 |
public AbstractFormatter(FormatterContext formatterContext) {
|
franta-hg@24
|
46 |
this.formatterContext = formatterContext;
|
franta-hg@22
|
47 |
state.push(State.ROOT);
|
franta-hg@22
|
48 |
}
|
franta-hg@22
|
49 |
|
franta-hg@248
|
50 |
protected String getCurrentRelationName() {
|
franta-hg@248
|
51 |
if (getFormatterContext().getRelationNames() == null || getFormatterContext().getRelationNames().size() < resultSetCount) {
|
franta-hg@248
|
52 |
return "r" + resultSetCount;
|
franta-hg@248
|
53 |
} else {
|
franta-hg@248
|
54 |
return getFormatterContext().getRelationNames().get(resultSetCount - 1);
|
franta-hg@248
|
55 |
}
|
franta-hg@248
|
56 |
}
|
franta-hg@248
|
57 |
|
franta-hg@22
|
58 |
/*
|
franta-hg@22
|
59 |
* root
|
franta-hg@91
|
60 |
* .batch
|
franta-hg@91
|
61 |
* ..database
|
franta-hg@142
|
62 |
* ...statement
|
franta-hg@91
|
63 |
* ....@query
|
franta-hg@91
|
64 |
* ....@parameters
|
franta-hg@142
|
65 |
* ....resultSet
|
franta-hg@142
|
66 |
* .....row
|
franta-hg@142
|
67 |
* ......@columnValue
|
franta-hg@142
|
68 |
* ....@updatesResult
|
franta-hg@22
|
69 |
*/
|
franta-hg@22
|
70 |
protected enum State {
|
franta-hg@22
|
71 |
|
franta-hg@22
|
72 |
ROOT,
|
franta-hg@91
|
73 |
BATCH,
|
franta-hg@22
|
74 |
DATABASE,
|
franta-hg@142
|
75 |
STATEMENT,
|
franta-hg@22
|
76 |
RESULT_SET,
|
franta-hg@142
|
77 |
ROW
|
franta-hg@22
|
78 |
}
|
franta-hg@22
|
79 |
|
franta-hg@22
|
80 |
/**
|
franta-hg@22
|
81 |
* Go down in hierarchy.
|
franta-hg@22
|
82 |
* Pushes new state and verifies the old one.
|
franta-hg@22
|
83 |
*
|
franta-hg@22
|
84 |
* @param current the new state – currently entering
|
franta-hg@22
|
85 |
* @param expected expected previous states (any of them is valid)
|
franta-hg@22
|
86 |
* @return previous state
|
franta-hg@22
|
87 |
* @throws IllegalStateException if previous state was not one from expected
|
franta-hg@22
|
88 |
*/
|
franta-hg@22
|
89 |
private State pushState(State current, EnumSet expected) {
|
franta-hg@22
|
90 |
State previous = state.peek();
|
franta-hg@22
|
91 |
|
franta-hg@22
|
92 |
if (expected.contains(previous)) {
|
franta-hg@22
|
93 |
state.push(current);
|
franta-hg@22
|
94 |
return previous;
|
franta-hg@22
|
95 |
} else {
|
franta-hg@22
|
96 |
throw new IllegalStateException("Formatter was in wrong state: " + previous + " when it should be in one of: " + expected);
|
franta-hg@22
|
97 |
}
|
franta-hg@22
|
98 |
}
|
franta-hg@22
|
99 |
|
franta-hg@22
|
100 |
protected State peekState(EnumSet expected) {
|
franta-hg@22
|
101 |
State current = state.peek();
|
franta-hg@22
|
102 |
|
franta-hg@22
|
103 |
if (expected.contains(current)) {
|
franta-hg@22
|
104 |
return current;
|
franta-hg@22
|
105 |
} else {
|
franta-hg@22
|
106 |
throw new IllegalStateException("Formatter is in wrong state: " + current + " when it should be in one of: " + expected);
|
franta-hg@22
|
107 |
}
|
franta-hg@22
|
108 |
|
franta-hg@22
|
109 |
}
|
franta-hg@22
|
110 |
|
franta-hg@22
|
111 |
/**
|
franta-hg@22
|
112 |
* Go up in hierarchy.
|
franta-hg@22
|
113 |
* Pops the superior state/branch.
|
franta-hg@22
|
114 |
*
|
franta-hg@22
|
115 |
* @param expected expected superior state
|
franta-hg@22
|
116 |
* @return the superior state
|
franta-hg@22
|
117 |
* @throws IllegalStateException if superior state was not one from expected or if there is no
|
franta-hg@22
|
118 |
* more superior state (we are at root level)
|
franta-hg@22
|
119 |
*/
|
franta-hg@22
|
120 |
private State popState(EnumSet expected) {
|
franta-hg@22
|
121 |
try {
|
franta-hg@34
|
122 |
state.pop();
|
franta-hg@34
|
123 |
State superior = state.peek();
|
franta-hg@22
|
124 |
if (expected.contains(superior)) {
|
franta-hg@22
|
125 |
return superior;
|
franta-hg@22
|
126 |
} else {
|
franta-hg@22
|
127 |
throw new IllegalStateException("Formatter had wrong superior state: " + superior + " when it should be in one of: " + expected);
|
franta-hg@22
|
128 |
}
|
franta-hg@22
|
129 |
} catch (EmptyStackException e) {
|
franta-hg@22
|
130 |
throw new IllegalStateException("Formatter was already at root level – there is nothing above that.", e);
|
franta-hg@22
|
131 |
}
|
franta-hg@22
|
132 |
}
|
franta-hg@22
|
133 |
|
franta-hg@22
|
134 |
@Override
|
franta-hg@91
|
135 |
public void writeStartBatch() {
|
franta-hg@91
|
136 |
pushState(State.BATCH, EnumSet.of(State.ROOT));
|
franta-hg@248
|
137 |
resultSetCount = 0;
|
franta-hg@91
|
138 |
}
|
franta-hg@91
|
139 |
|
franta-hg@91
|
140 |
@Override
|
franta-hg@91
|
141 |
public void writeEndBatch() {
|
franta-hg@91
|
142 |
popState(EnumSet.of(State.ROOT));
|
franta-hg@91
|
143 |
}
|
franta-hg@91
|
144 |
|
franta-hg@91
|
145 |
@Override
|
franta-hg@29
|
146 |
public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
|
franta-hg@91
|
147 |
pushState(State.DATABASE, EnumSet.of(State.BATCH));
|
franta-hg@22
|
148 |
}
|
franta-hg@22
|
149 |
|
franta-hg@22
|
150 |
@Override
|
franta-hg@22
|
151 |
public void writeEndDatabase() {
|
franta-hg@91
|
152 |
popState(EnumSet.of(State.BATCH));
|
franta-hg@22
|
153 |
}
|
franta-hg@22
|
154 |
|
franta-hg@22
|
155 |
@Override
|
franta-hg@142
|
156 |
public void writeStartStatement() {
|
franta-hg@142
|
157 |
pushState(State.STATEMENT, EnumSet.of(State.DATABASE));
|
franta-hg@142
|
158 |
}
|
franta-hg@142
|
159 |
|
franta-hg@142
|
160 |
@Override
|
franta-hg@142
|
161 |
public void writeEndStatement() {
|
franta-hg@142
|
162 |
popState(EnumSet.of(State.DATABASE));
|
franta-hg@142
|
163 |
}
|
franta-hg@142
|
164 |
|
franta-hg@142
|
165 |
@Override
|
franta-hg@142
|
166 |
public void writeStartResultSet(ColumnsHeader header) {
|
franta-hg@142
|
167 |
pushState(State.RESULT_SET, EnumSet.of(State.STATEMENT));
|
franta-hg@248
|
168 |
resultSetCount++;
|
franta-hg@25
|
169 |
currentRowCount = 0;
|
franta-hg@142
|
170 |
currentColumnsHeader = header;
|
franta-hg@22
|
171 |
}
|
franta-hg@22
|
172 |
|
franta-hg@22
|
173 |
@Override
|
franta-hg@22
|
174 |
public void writeEndResultSet() {
|
franta-hg@142
|
175 |
popState(EnumSet.of(State.STATEMENT));
|
franta-hg@22
|
176 |
currentColumnsHeader = null;
|
franta-hg@22
|
177 |
}
|
franta-hg@22
|
178 |
|
franta-hg@22
|
179 |
@Override
|
franta-hg@22
|
180 |
public void writeQuery(String sql) {
|
franta-hg@142
|
181 |
peekState(EnumSet.of(State.STATEMENT));
|
franta-hg@22
|
182 |
|
franta-hg@22
|
183 |
if (currentColumnsHeader == null) {
|
franta-hg@22
|
184 |
currentQuery = sql;
|
franta-hg@22
|
185 |
} else {
|
franta-hg@22
|
186 |
throw new IllegalStateException("Query string '" + sql + "' must be set before columns header – was already set: " + currentColumnsHeader);
|
franta-hg@22
|
187 |
}
|
franta-hg@22
|
188 |
}
|
franta-hg@22
|
189 |
|
franta-hg@22
|
190 |
@Override
|
franta-hg@34
|
191 |
public void writeParameters(List<? extends Parameter> parameters) {
|
franta-hg@142
|
192 |
peekState(EnumSet.of(State.STATEMENT));
|
franta-hg@22
|
193 |
|
franta-hg@22
|
194 |
if (currentColumnsHeader != null) {
|
franta-hg@22
|
195 |
throw new IllegalStateException("Parameters '" + parameters + "' must be set before columns header – was already set: " + currentColumnsHeader);
|
franta-hg@22
|
196 |
}
|
franta-hg@22
|
197 |
|
franta-hg@88
|
198 |
if (currentQuery == null && parameters != null) {
|
franta-hg@22
|
199 |
throw new IllegalStateException("Parameters '" + parameters + "' must be set after query – was not yet set.");
|
franta-hg@22
|
200 |
}
|
franta-hg@22
|
201 |
}
|
franta-hg@22
|
202 |
|
franta-hg@22
|
203 |
@Override
|
franta-hg@22
|
204 |
public void writeStartRow() {
|
franta-hg@22
|
205 |
pushState(State.ROW, EnumSet.of(State.RESULT_SET));
|
franta-hg@22
|
206 |
currentColumnsCount = 0;
|
franta-hg@25
|
207 |
currentRowCount++;
|
franta-hg@22
|
208 |
}
|
franta-hg@22
|
209 |
|
franta-hg@22
|
210 |
@Override
|
franta-hg@22
|
211 |
public void writeEndRow() {
|
franta-hg@22
|
212 |
popState(EnumSet.of(State.RESULT_SET));
|
franta-hg@22
|
213 |
}
|
franta-hg@22
|
214 |
|
franta-hg@22
|
215 |
@Override
|
franta-hg@22
|
216 |
public void writeColumnValue(Object value) {
|
franta-hg@22
|
217 |
peekState(EnumSet.of(State.ROW));
|
franta-hg@22
|
218 |
currentColumnsCount++;
|
franta-hg@22
|
219 |
|
franta-hg@22
|
220 |
int declaredCount = currentColumnsHeader.getColumnCount();
|
franta-hg@22
|
221 |
if (currentColumnsCount > declaredCount) {
|
franta-hg@22
|
222 |
throw new IllegalStateException("Current columns count is " + currentColumnsCount + " which is more than declared " + declaredCount + " in header.");
|
franta-hg@22
|
223 |
}
|
franta-hg@22
|
224 |
}
|
franta-hg@22
|
225 |
|
franta-hg@22
|
226 |
@Override
|
franta-hg@142
|
227 |
public void writeUpdatesResult(int updatedRowsCount) {
|
franta-hg@142
|
228 |
peekState(EnumSet.of(State.STATEMENT));
|
franta-hg@22
|
229 |
}
|
franta-hg@22
|
230 |
|
franta-hg@101
|
231 |
@Override
|
franta-hg@101
|
232 |
public void close() throws FormatterException {
|
franta-hg@101
|
233 |
}
|
franta-hg@101
|
234 |
|
franta-hg@24
|
235 |
public FormatterContext getFormatterContext() {
|
franta-hg@24
|
236 |
return formatterContext;
|
franta-hg@22
|
237 |
}
|
franta-hg@22
|
238 |
|
franta-hg@22
|
239 |
protected ColumnsHeader getCurrentColumnsHeader() {
|
franta-hg@22
|
240 |
return currentColumnsHeader;
|
franta-hg@22
|
241 |
}
|
franta-hg@22
|
242 |
|
franta-hg@25
|
243 |
/**
|
franta-hg@25
|
244 |
* @return column number, 1 = first
|
franta-hg@25
|
245 |
*/
|
franta-hg@22
|
246 |
protected int getCurrentColumnsCount() {
|
franta-hg@22
|
247 |
return currentColumnsCount;
|
franta-hg@22
|
248 |
}
|
franta-hg@25
|
249 |
|
franta-hg@34
|
250 |
protected boolean isCurrentColumnFirst() {
|
franta-hg@34
|
251 |
return currentColumnsCount == 1;
|
franta-hg@34
|
252 |
}
|
franta-hg@34
|
253 |
|
franta-hg@34
|
254 |
protected boolean isCurrentColumnLast() {
|
franta-hg@34
|
255 |
return currentColumnsCount == currentColumnsHeader.getColumnCount();
|
franta-hg@34
|
256 |
}
|
franta-hg@34
|
257 |
|
franta-hg@25
|
258 |
/**
|
franta-hg@25
|
259 |
* @return row number, 1 = first
|
franta-hg@25
|
260 |
*/
|
franta-hg@25
|
261 |
protected int getCurrentRowCount() {
|
franta-hg@25
|
262 |
return currentRowCount;
|
franta-hg@25
|
263 |
}
|
franta-hg@22
|
264 |
}
|