franta-hg@29
|
1 |
/**
|
franta-hg@29
|
2 |
* SQL-DK
|
franta-hg@29
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@29
|
4 |
*
|
franta-hg@29
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@29
|
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@29
|
8 |
*
|
franta-hg@29
|
9 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@29
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@29
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@29
|
12 |
* GNU General Public License for more details.
|
franta-hg@29
|
13 |
*
|
franta-hg@29
|
14 |
* You should have received a copy of the GNU General Public License
|
franta-hg@29
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@29
|
16 |
*/
|
franta-hg@29
|
17 |
package info.globalcode.sql.dk.formatting;
|
franta-hg@29
|
18 |
|
franta-hg@128
|
19 |
import info.globalcode.sql.dk.Parameter;
|
franta-hg@128
|
20 |
import info.globalcode.sql.dk.Xmlns;
|
franta-hg@128
|
21 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
|
franta-hg@128
|
22 |
import static info.globalcode.sql.dk.Functions.notNull;
|
franta-hg@128
|
23 |
import info.globalcode.sql.dk.NamedParameter;
|
franta-hg@206
|
24 |
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
|
franta-hg@165
|
25 |
import java.sql.Array;
|
franta-hg@233
|
26 |
import java.sql.ResultSet;
|
franta-hg@165
|
27 |
import java.sql.SQLException;
|
franta-hg@225
|
28 |
import java.sql.SQLXML;
|
franta-hg@245
|
29 |
import java.sql.Types;
|
franta-hg@233
|
30 |
import java.util.ArrayList;
|
franta-hg@245
|
31 |
import java.util.Collections;
|
franta-hg@245
|
32 |
import java.util.HashMap;
|
franta-hg@128
|
33 |
import java.util.LinkedHashMap;
|
franta-hg@128
|
34 |
import java.util.List;
|
franta-hg@128
|
35 |
import java.util.Map;
|
franta-hg@165
|
36 |
import java.util.logging.Level;
|
franta-hg@165
|
37 |
import java.util.logging.Logger;
|
franta-hg@128
|
38 |
import javax.xml.namespace.QName;
|
franta-hg@128
|
39 |
|
franta-hg@29
|
40 |
/**
|
franta-hg@206
|
41 |
* <p>
|
franta-hg@206
|
42 |
* Prints machine-readable output – XML document containing resultsets and updates count. Good
|
franta-hg@155
|
43 |
* choice for further processing – e.g. XSL transformation.</p>
|
franta-hg@155
|
44 |
*
|
franta-hg@206
|
45 |
* <p>
|
franta-hg@245
|
46 |
* XML format is defined in the <a href="https://relational-pipes.globalcode.info/">Relational
|
franta-hg@245
|
47 |
* pipes</a> specification.
|
franta-hg@245
|
48 |
* </p>
|
franta-hg@29
|
49 |
*
|
franta-hg@29
|
50 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@29
|
51 |
*/
|
franta-hg@207
|
52 |
@PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, defaultValue = "false", type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element")
|
franta-hg@128
|
53 |
public class XmlFormatter extends AbstractXmlFormatter {
|
franta-hg@29
|
54 |
|
franta-hg@245
|
55 |
private static final String XML_ELEMENT_RELATION = "relation";
|
franta-hg@245
|
56 |
private static final String XML_ELEMENT_NAME = "name";
|
franta-hg@245
|
57 |
private static final String XML_ELEMENT_RECORD = "record";
|
franta-hg@245
|
58 |
private static final String XML_ELEMENT_ATTRIBUTE = "attribute";
|
franta-hg@245
|
59 |
private static final String XML_ATTRIBUTE_NAME = "name";
|
franta-hg@245
|
60 |
private static final String XML_ATTRIBUTE_TYPE = "type";
|
franta-hg@245
|
61 |
private static final String XML_ATTRIBUTE_STATEMENT = "statement";
|
franta-hg@245
|
62 |
private static final String XML_NS_PREFIX_SQLDK = "sql-dk";
|
franta-hg@245
|
63 |
|
franta-hg@245
|
64 |
private static final String RELPIPE_TYPE_BOOLEAN = "boolean";
|
franta-hg@245
|
65 |
private static final String RELPIPE_TYPE_INTEGER = "integer";
|
franta-hg@245
|
66 |
private static final String RELPIPE_TYPE_STRING = "string";
|
franta-hg@245
|
67 |
private static final Map<Integer, String> RELPIPE_TYPES;
|
franta-hg@245
|
68 |
|
franta-hg@245
|
69 |
static {
|
franta-hg@245
|
70 |
Map<Integer, String> m = new HashMap<>();
|
franta-hg@245
|
71 |
m.put(Types.BOOLEAN, RELPIPE_TYPE_BOOLEAN);
|
franta-hg@245
|
72 |
m.put(Types.BIT, RELPIPE_TYPE_BOOLEAN); // TODO: relpipe "boolean" can not be null in the current version
|
franta-hg@251
|
73 |
m.put(Types.INTEGER, RELPIPE_TYPE_INTEGER);
|
franta-hg@245
|
74 |
// TODO: add more types when supported in Relational pipes
|
franta-hg@245
|
75 |
m.put(Types.CHAR, RELPIPE_TYPE_STRING);
|
franta-hg@245
|
76 |
m.put(Types.VARCHAR, RELPIPE_TYPE_STRING);
|
franta-hg@245
|
77 |
RELPIPE_TYPES = Collections.unmodifiableMap(m);
|
franta-hg@245
|
78 |
}
|
franta-hg@245
|
79 |
|
franta-hg@79
|
80 |
public static final String NAME = "xml"; // bash-completion:formatter
|
franta-hg@131
|
81 |
public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
|
franta-hg@165
|
82 |
private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
|
franta-hg@131
|
83 |
private final boolean labeledColumns;
|
franta-hg@29
|
84 |
|
franta-hg@245
|
85 |
private String currentDatabaseName;
|
franta-hg@245
|
86 |
private int statementCounter;
|
franta-hg@248
|
87 |
|
franta-hg@245
|
88 |
|
franta-hg@29
|
89 |
public XmlFormatter(FormatterContext formatterContext) {
|
franta-hg@29
|
90 |
super(formatterContext);
|
franta-hg@131
|
91 |
labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
|
franta-hg@29
|
92 |
}
|
franta-hg@128
|
93 |
|
franta-hg@245
|
94 |
private QName qname(String localPart) {
|
franta-hg@245
|
95 |
return new QName(Xmlns.RELPIPE, localPart);
|
franta-hg@245
|
96 |
}
|
franta-hg@245
|
97 |
|
franta-hg@245
|
98 |
private QName qnameDK(String localPart) {
|
franta-hg@245
|
99 |
return new QName(Xmlns.SQLDK, localPart, XML_NS_PREFIX_SQLDK);
|
franta-hg@245
|
100 |
}
|
franta-hg@245
|
101 |
|
franta-hg@128
|
102 |
@Override
|
franta-hg@128
|
103 |
public void writeStartBatch() {
|
franta-hg@128
|
104 |
super.writeStartBatch();
|
franta-hg@128
|
105 |
printStartDocument();
|
franta-hg@245
|
106 |
Map<QName, String> attributes = new LinkedHashMap<>(2);
|
franta-hg@245
|
107 |
attributes.put(qname("xmlns"), Xmlns.RELPIPE);
|
franta-hg@245
|
108 |
attributes.put(new QName(null, XML_NS_PREFIX_SQLDK, "xmlns"), Xmlns.SQLDK);
|
franta-hg@245
|
109 |
printStartElement(qname("relpipe"), attributes);
|
franta-hg@245
|
110 |
statementCounter = 0;
|
franta-hg@245
|
111 |
|
franta-hg@128
|
112 |
}
|
franta-hg@128
|
113 |
|
franta-hg@128
|
114 |
@Override
|
franta-hg@128
|
115 |
public void writeEndBatch() {
|
franta-hg@128
|
116 |
super.writeEndBatch();
|
franta-hg@128
|
117 |
printEndElement();
|
franta-hg@128
|
118 |
printEndDocument();
|
franta-hg@128
|
119 |
}
|
franta-hg@128
|
120 |
|
franta-hg@128
|
121 |
@Override
|
franta-hg@128
|
122 |
public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
|
franta-hg@128
|
123 |
super.writeStartDatabase(databaseDefinition);
|
franta-hg@245
|
124 |
currentDatabaseName = databaseDefinition.getName();
|
franta-hg@128
|
125 |
Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
|
franta-hg@245
|
126 |
printEmptyElement(qnameDK("database-start"), attributes);
|
franta-hg@128
|
127 |
}
|
franta-hg@128
|
128 |
|
franta-hg@128
|
129 |
@Override
|
franta-hg@128
|
130 |
public void writeEndDatabase() {
|
franta-hg@128
|
131 |
super.writeEndDatabase();
|
franta-hg@245
|
132 |
Map<QName, String> attributes = currentDatabaseName == null ? null : singleAttribute(qname("name"), currentDatabaseName);
|
franta-hg@245
|
133 |
printEmptyElement(qnameDK("database-end"), attributes);
|
franta-hg@245
|
134 |
}
|
franta-hg@245
|
135 |
|
franta-hg@245
|
136 |
private String getCurrentStatementName() {
|
franta-hg@245
|
137 |
return "s" + statementCounter;
|
franta-hg@128
|
138 |
}
|
franta-hg@128
|
139 |
|
franta-hg@128
|
140 |
@Override
|
franta-hg@142
|
141 |
public void writeStartStatement() {
|
franta-hg@142
|
142 |
super.writeStartStatement();
|
franta-hg@245
|
143 |
statementCounter++;
|
franta-hg@245
|
144 |
printEmptyElement(qnameDK("statement-start"), singleAttribute(qname("id"), getCurrentStatementName()));
|
franta-hg@128
|
145 |
}
|
franta-hg@128
|
146 |
|
franta-hg@128
|
147 |
@Override
|
franta-hg@142
|
148 |
public void writeEndStatement() {
|
franta-hg@142
|
149 |
super.writeEndStatement();
|
franta-hg@245
|
150 |
printEmptyElement(qnameDK("statement-end"), singleAttribute(qname("id"), getCurrentStatementName()));
|
franta-hg@128
|
151 |
}
|
franta-hg@128
|
152 |
|
franta-hg@128
|
153 |
@Override
|
franta-hg@128
|
154 |
public void writeQuery(String sql) {
|
franta-hg@128
|
155 |
super.writeQuery(sql);
|
franta-hg@245
|
156 |
printTextElement(qnameDK("sql"), singleAttribute(qname(XML_ATTRIBUTE_STATEMENT), getCurrentStatementName()), sql);
|
franta-hg@128
|
157 |
}
|
franta-hg@128
|
158 |
|
franta-hg@128
|
159 |
@Override
|
franta-hg@128
|
160 |
public void writeParameters(List<? extends Parameter> parameters) {
|
franta-hg@128
|
161 |
super.writeParameters(parameters);
|
franta-hg@128
|
162 |
|
franta-hg@245
|
163 |
if (parameters != null && parameters.size() > 0) {
|
franta-hg@128
|
164 |
|
franta-hg@245
|
165 |
printStartElement(qnameDK("parameters"), singleAttribute(qname(XML_ATTRIBUTE_STATEMENT), getCurrentStatementName()));
|
franta-hg@245
|
166 |
|
franta-hg@245
|
167 |
for (Parameter p : notNull(parameters)) {
|
franta-hg@245
|
168 |
|
franta-hg@245
|
169 |
Map<QName, String> attributes = new LinkedHashMap<>(2);
|
franta-hg@245
|
170 |
if (p instanceof NamedParameter) {
|
franta-hg@245
|
171 |
attributes.put(qname(XML_ATTRIBUTE_NAME), ((NamedParameter) p).getName());
|
franta-hg@245
|
172 |
}
|
franta-hg@245
|
173 |
attributes.put(qname(XML_ATTRIBUTE_TYPE), p.getType().name());
|
franta-hg@245
|
174 |
|
franta-hg@245
|
175 |
printTextElement(qnameDK("parameter"), attributes, String.valueOf(p.getValue()));
|
franta-hg@128
|
176 |
}
|
franta-hg@128
|
177 |
|
franta-hg@245
|
178 |
printEndElement();
|
franta-hg@245
|
179 |
|
franta-hg@128
|
180 |
}
|
franta-hg@128
|
181 |
|
franta-hg@128
|
182 |
}
|
franta-hg@128
|
183 |
|
franta-hg@128
|
184 |
@Override
|
franta-hg@142
|
185 |
public void writeStartResultSet(ColumnsHeader header) {
|
franta-hg@142
|
186 |
super.writeStartResultSet(header);
|
franta-hg@245
|
187 |
printStartElement(qname(XML_ELEMENT_RELATION), singleAttribute(qnameDK(XML_ATTRIBUTE_STATEMENT), getCurrentStatementName()));
|
franta-hg@245
|
188 |
printTextElement(qname(XML_ELEMENT_NAME), null, getCurrentRelationName());
|
franta-hg@128
|
189 |
|
franta-hg@245
|
190 |
printStartElement(qname("attributes-metadata"));
|
franta-hg@128
|
191 |
for (ColumnDescriptor cd : header.getColumnDescriptors()) {
|
franta-hg@245
|
192 |
Map<QName, String> attributes = new LinkedHashMap<>(6);
|
franta-hg@245
|
193 |
|
franta-hg@245
|
194 |
attributes.put(qname(XML_ATTRIBUTE_NAME), cd.getLabel());
|
franta-hg@245
|
195 |
attributes.put(qname(XML_ATTRIBUTE_TYPE), toRelpipeType(cd.getType()));
|
franta-hg@245
|
196 |
|
franta-hg@245
|
197 |
attributes.put(qnameDK("tableName"), cd.getTableName());
|
franta-hg@245
|
198 |
attributes.put(qnameDK("columnName"), cd.getName());
|
franta-hg@245
|
199 |
attributes.put(qnameDK("jdbcTypeName"), cd.getTypeName());
|
franta-hg@245
|
200 |
attributes.put(qnameDK("jdbcType"), String.valueOf(cd.getType()));
|
franta-hg@245
|
201 |
|
franta-hg@245
|
202 |
printEmptyElement(qname("attribute-metadata"), attributes);
|
franta-hg@128
|
203 |
}
|
franta-hg@245
|
204 |
printEndElement();
|
franta-hg@245
|
205 |
}
|
franta-hg@245
|
206 |
|
franta-hg@245
|
207 |
/**
|
franta-hg@245
|
208 |
* @param jdbcType value from {@linkplain Types}
|
franta-hg@245
|
209 |
* @return
|
franta-hg@245
|
210 |
*/
|
franta-hg@245
|
211 |
private String toRelpipeType(int jdbcType) {
|
franta-hg@245
|
212 |
return RELPIPE_TYPES.getOrDefault(jdbcType, RELPIPE_TYPE_STRING);
|
franta-hg@128
|
213 |
}
|
franta-hg@129
|
214 |
|
franta-hg@129
|
215 |
@Override
|
franta-hg@142
|
216 |
public void writeEndResultSet() {
|
franta-hg@142
|
217 |
super.writeEndResultSet();
|
franta-hg@142
|
218 |
printEndElement();
|
franta-hg@142
|
219 |
}
|
franta-hg@142
|
220 |
|
franta-hg@142
|
221 |
@Override
|
franta-hg@129
|
222 |
public void writeStartRow() {
|
franta-hg@129
|
223 |
super.writeStartRow();
|
franta-hg@245
|
224 |
printStartElement(qname(XML_ELEMENT_RECORD));
|
franta-hg@129
|
225 |
}
|
franta-hg@129
|
226 |
|
franta-hg@129
|
227 |
@Override
|
franta-hg@129
|
228 |
public void writeColumnValue(Object value) {
|
franta-hg@129
|
229 |
super.writeColumnValue(value);
|
franta-hg@131
|
230 |
|
franta-hg@131
|
231 |
Map<QName, String> attributes = null;
|
franta-hg@131
|
232 |
if (labeledColumns) {
|
franta-hg@151
|
233 |
attributes = new LinkedHashMap<>(2);
|
franta-hg@245
|
234 |
attributes.put(qname(XML_ATTRIBUTE_NAME), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
|
franta-hg@245
|
235 |
attributes.put(qname(XML_ATTRIBUTE_TYPE), toRelpipeType(getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getType()));
|
franta-hg@131
|
236 |
}
|
franta-hg@131
|
237 |
|
franta-hg@151
|
238 |
if (value == null) {
|
franta-hg@151
|
239 |
if (attributes == null) {
|
franta-hg@151
|
240 |
attributes = new LinkedHashMap<>(2);
|
franta-hg@151
|
241 |
}
|
franta-hg@245
|
242 |
// TODO: synchronize syntax with Relational pipes (after adding support of null values)
|
franta-hg@245
|
243 |
attributes.put(qnameDK("null"), "true");
|
franta-hg@245
|
244 |
printEmptyElement(qname(XML_ELEMENT_ATTRIBUTE), attributes);
|
franta-hg@165
|
245 |
} else if (value instanceof Array) {
|
franta-hg@206
|
246 |
|
franta-hg@165
|
247 |
Array sqlArray = (Array) value;
|
franta-hg@165
|
248 |
try {
|
franta-hg@165
|
249 |
Object[] array = (Object[]) sqlArray.getArray();
|
franta-hg@245
|
250 |
printStartElement(qname(XML_ELEMENT_ATTRIBUTE), attributes);
|
franta-hg@165
|
251 |
printArray(array);
|
franta-hg@165
|
252 |
printEndElement();
|
franta-hg@165
|
253 |
} catch (SQLException e) {
|
franta-hg@233
|
254 |
// FIXME: rewrite array formatting, remember array mode, don't try sqlArray.getArray() again and again if it has failed
|
franta-hg@165
|
255 |
log.log(Level.SEVERE, "Unable to format array", e);
|
franta-hg@233
|
256 |
try {
|
franta-hg@233
|
257 |
ResultSet arrayResultSet = sqlArray.getResultSet();
|
franta-hg@233
|
258 |
//int columnCount = arrayResultSet.getMetaData().getColumnCount();
|
franta-hg@233
|
259 |
ArrayList<Object> arrayList = new ArrayList<>();
|
franta-hg@233
|
260 |
while (arrayResultSet.next()) {
|
franta-hg@233
|
261 |
arrayList.add(arrayResultSet.getObject(2));
|
franta-hg@233
|
262 |
// for (int i = 1; i <= columnCount; i++) {
|
franta-hg@233
|
263 |
// log.log(Level.INFO, "Array column {0} = {1}", new Object[]{i, arrayResultSet.getObject(i)});
|
franta-hg@233
|
264 |
// }
|
franta-hg@233
|
265 |
}
|
franta-hg@233
|
266 |
|
franta-hg@245
|
267 |
printStartElement(qname(XML_ELEMENT_ATTRIBUTE), attributes);
|
franta-hg@233
|
268 |
// FIXME: instanceof SQLXML, see below
|
franta-hg@233
|
269 |
printArray(arrayList.toArray());
|
franta-hg@233
|
270 |
printEndElement();
|
franta-hg@233
|
271 |
|
franta-hg@233
|
272 |
} catch (SQLException e2) {
|
franta-hg@233
|
273 |
// FIXME: fix logging, error recovery
|
franta-hg@233
|
274 |
log.log(Level.SEVERE, "Second level fuck up !!!", e2);
|
franta-hg@233
|
275 |
}
|
franta-hg@233
|
276 |
|
franta-hg@165
|
277 |
writeColumnValue(String.valueOf(value));
|
franta-hg@165
|
278 |
}
|
franta-hg@206
|
279 |
|
franta-hg@233
|
280 |
} else if (value instanceof SQLXML) { // FIXME: move to separate method, to AbstractFormatter?
|
franta-hg@225
|
281 |
SQLXML xml = (SQLXML) value;
|
franta-hg@225
|
282 |
// TODO: parse DOM/SAX and transplant XML, don't escape (optional)
|
franta-hg@225
|
283 |
try {
|
franta-hg@245
|
284 |
printTextElement(qname(XML_ELEMENT_ATTRIBUTE), attributes, xml.getString());
|
franta-hg@225
|
285 |
} catch (SQLException e) {
|
franta-hg@225
|
286 |
log.log(Level.SEVERE, "Unable to format XML", e);
|
franta-hg@225
|
287 |
writeColumnValue(String.valueOf(value));
|
franta-hg@225
|
288 |
}
|
franta-hg@151
|
289 |
} else {
|
franta-hg@245
|
290 |
printTextElement(qname(XML_ELEMENT_ATTRIBUTE), attributes, toString(value));
|
franta-hg@151
|
291 |
}
|
franta-hg@129
|
292 |
}
|
franta-hg@206
|
293 |
|
franta-hg@165
|
294 |
private void printArray(Object[] array) {
|
franta-hg@245
|
295 |
// TODO: synchronize array syntax with Relational pipes
|
franta-hg@245
|
296 |
printStartElement(qnameDK("array"));
|
franta-hg@165
|
297 |
for (Object o : array) {
|
franta-hg@165
|
298 |
if (o instanceof Object[]) {
|
franta-hg@245
|
299 |
printStartElement(qnameDK("item"));
|
franta-hg@165
|
300 |
printArray((Object[]) o);
|
franta-hg@165
|
301 |
printEndElement();
|
franta-hg@165
|
302 |
} else {
|
franta-hg@245
|
303 |
printTextElement(qnameDK("item"), null, String.valueOf(o));
|
franta-hg@165
|
304 |
}
|
franta-hg@165
|
305 |
}
|
franta-hg@165
|
306 |
printEndElement();
|
franta-hg@165
|
307 |
}
|
franta-hg@129
|
308 |
|
franta-hg@129
|
309 |
@Override
|
franta-hg@129
|
310 |
public void writeEndRow() {
|
franta-hg@129
|
311 |
super.writeEndRow();
|
franta-hg@129
|
312 |
printEndElement();
|
franta-hg@129
|
313 |
}
|
franta-hg@129
|
314 |
|
franta-hg@129
|
315 |
@Override
|
franta-hg@142
|
316 |
public void writeUpdatesResult(int updatedRowsCount) {
|
franta-hg@142
|
317 |
super.writeUpdatesResult(updatedRowsCount);
|
franta-hg@245
|
318 |
printTextElement(qnameDK("updatedRecords"), singleAttribute(qnameDK(XML_ATTRIBUTE_STATEMENT), getCurrentStatementName()), String.valueOf(updatedRowsCount));
|
franta-hg@129
|
319 |
}
|
franta-hg@129
|
320 |
|
franta-hg@129
|
321 |
protected String toString(Object value) {
|
franta-hg@129
|
322 |
return String.valueOf(value);
|
franta-hg@129
|
323 |
}
|
franta-hg@29
|
324 |
}
|