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@29
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@29
|
8 |
* (at your option) any later version.
|
franta-hg@29
|
9 |
*
|
franta-hg@29
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@29
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@29
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@29
|
13 |
* GNU General Public License for more details.
|
franta-hg@29
|
14 |
*
|
franta-hg@29
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@29
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@29
|
17 |
*/
|
franta-hg@29
|
18 |
package info.globalcode.sql.dk.formatting;
|
franta-hg@29
|
19 |
|
franta-hg@128
|
20 |
import info.globalcode.sql.dk.Parameter;
|
franta-hg@128
|
21 |
import info.globalcode.sql.dk.Xmlns;
|
franta-hg@128
|
22 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
|
franta-hg@128
|
23 |
import static info.globalcode.sql.dk.Functions.notNull;
|
franta-hg@128
|
24 |
import info.globalcode.sql.dk.NamedParameter;
|
franta-hg@206
|
25 |
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
|
franta-hg@165
|
26 |
import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
|
franta-hg@165
|
27 |
import java.sql.Array;
|
franta-hg@233
|
28 |
import java.sql.ResultSet;
|
franta-hg@165
|
29 |
import java.sql.SQLException;
|
franta-hg@225
|
30 |
import java.sql.SQLXML;
|
franta-hg@233
|
31 |
import java.util.ArrayList;
|
franta-hg@128
|
32 |
import java.util.LinkedHashMap;
|
franta-hg@128
|
33 |
import java.util.List;
|
franta-hg@128
|
34 |
import java.util.Map;
|
franta-hg@165
|
35 |
import java.util.logging.Level;
|
franta-hg@165
|
36 |
import java.util.logging.Logger;
|
franta-hg@128
|
37 |
import javax.xml.namespace.QName;
|
franta-hg@128
|
38 |
|
franta-hg@29
|
39 |
/**
|
franta-hg@206
|
40 |
* <p>
|
franta-hg@206
|
41 |
* Prints machine-readable output – XML document containing resultsets and updates count. Good
|
franta-hg@155
|
42 |
* choice for further processing – e.g. XSL transformation.</p>
|
franta-hg@155
|
43 |
*
|
franta-hg@206
|
44 |
* <p>
|
franta-hg@206
|
45 |
* TODO: XSD</p>
|
franta-hg@29
|
46 |
*
|
franta-hg@29
|
47 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@29
|
48 |
*/
|
franta-hg@207
|
49 |
@PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, defaultValue = "false", type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element")
|
franta-hg@128
|
50 |
public class XmlFormatter extends AbstractXmlFormatter {
|
franta-hg@29
|
51 |
|
franta-hg@79
|
52 |
public static final String NAME = "xml"; // bash-completion:formatter
|
franta-hg@131
|
53 |
public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
|
franta-hg@165
|
54 |
private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
|
franta-hg@131
|
55 |
private final boolean labeledColumns;
|
franta-hg@29
|
56 |
|
franta-hg@29
|
57 |
public XmlFormatter(FormatterContext formatterContext) {
|
franta-hg@29
|
58 |
super(formatterContext);
|
franta-hg@131
|
59 |
labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
|
franta-hg@29
|
60 |
}
|
franta-hg@128
|
61 |
|
franta-hg@128
|
62 |
@Override
|
franta-hg@128
|
63 |
public void writeStartBatch() {
|
franta-hg@128
|
64 |
super.writeStartBatch();
|
franta-hg@128
|
65 |
printStartDocument();
|
franta-hg@154
|
66 |
printStartElement(qname("batchResult"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
|
franta-hg@128
|
67 |
}
|
franta-hg@128
|
68 |
|
franta-hg@128
|
69 |
@Override
|
franta-hg@128
|
70 |
public void writeEndBatch() {
|
franta-hg@128
|
71 |
super.writeEndBatch();
|
franta-hg@128
|
72 |
printEndElement();
|
franta-hg@128
|
73 |
printEndDocument();
|
franta-hg@128
|
74 |
}
|
franta-hg@128
|
75 |
|
franta-hg@128
|
76 |
@Override
|
franta-hg@128
|
77 |
public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
|
franta-hg@128
|
78 |
super.writeStartDatabase(databaseDefinition);
|
franta-hg@128
|
79 |
Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
|
franta-hg@128
|
80 |
printStartElement(qname("database"), attributes);
|
franta-hg@128
|
81 |
}
|
franta-hg@128
|
82 |
|
franta-hg@128
|
83 |
@Override
|
franta-hg@128
|
84 |
public void writeEndDatabase() {
|
franta-hg@128
|
85 |
super.writeEndDatabase();
|
franta-hg@128
|
86 |
printEndElement();
|
franta-hg@128
|
87 |
}
|
franta-hg@128
|
88 |
|
franta-hg@128
|
89 |
@Override
|
franta-hg@142
|
90 |
public void writeStartStatement() {
|
franta-hg@142
|
91 |
super.writeStartStatement();
|
franta-hg@142
|
92 |
printStartElement(qname("statement"));
|
franta-hg@128
|
93 |
}
|
franta-hg@128
|
94 |
|
franta-hg@128
|
95 |
@Override
|
franta-hg@142
|
96 |
public void writeEndStatement() {
|
franta-hg@142
|
97 |
super.writeEndStatement();
|
franta-hg@128
|
98 |
printEndElement();
|
franta-hg@128
|
99 |
}
|
franta-hg@128
|
100 |
|
franta-hg@128
|
101 |
@Override
|
franta-hg@128
|
102 |
public void writeQuery(String sql) {
|
franta-hg@128
|
103 |
super.writeQuery(sql);
|
franta-hg@128
|
104 |
printTextElement(qname("sql"), null, sql);
|
franta-hg@128
|
105 |
}
|
franta-hg@128
|
106 |
|
franta-hg@128
|
107 |
@Override
|
franta-hg@128
|
108 |
public void writeParameters(List<? extends Parameter> parameters) {
|
franta-hg@128
|
109 |
super.writeParameters(parameters);
|
franta-hg@128
|
110 |
|
franta-hg@128
|
111 |
for (Parameter p : notNull(parameters)) {
|
franta-hg@128
|
112 |
|
franta-hg@128
|
113 |
Map<QName, String> attributes = new LinkedHashMap<>(2);
|
franta-hg@128
|
114 |
if (p instanceof NamedParameter) {
|
franta-hg@128
|
115 |
attributes.put(qname("name"), ((NamedParameter) p).getName());
|
franta-hg@128
|
116 |
}
|
franta-hg@128
|
117 |
attributes.put(qname("type"), p.getType().name());
|
franta-hg@128
|
118 |
|
franta-hg@128
|
119 |
printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
|
franta-hg@128
|
120 |
}
|
franta-hg@128
|
121 |
|
franta-hg@128
|
122 |
}
|
franta-hg@128
|
123 |
|
franta-hg@128
|
124 |
@Override
|
franta-hg@142
|
125 |
public void writeStartResultSet(ColumnsHeader header) {
|
franta-hg@142
|
126 |
super.writeStartResultSet(header);
|
franta-hg@142
|
127 |
printStartElement(qname("resultSet"));
|
franta-hg@128
|
128 |
|
franta-hg@128
|
129 |
for (ColumnDescriptor cd : header.getColumnDescriptors()) {
|
franta-hg@128
|
130 |
Map<QName, String> attributes = new LinkedHashMap<>(4);
|
franta-hg@128
|
131 |
attributes.put(qname("label"), cd.getLabel());
|
franta-hg@128
|
132 |
attributes.put(qname("name"), cd.getName());
|
franta-hg@128
|
133 |
attributes.put(qname("typeName"), cd.getTypeName());
|
franta-hg@128
|
134 |
attributes.put(qname("type"), String.valueOf(cd.getType()));
|
franta-hg@128
|
135 |
printEmptyElement(qname("columnHeader"), attributes);
|
franta-hg@128
|
136 |
}
|
franta-hg@128
|
137 |
}
|
franta-hg@129
|
138 |
|
franta-hg@129
|
139 |
@Override
|
franta-hg@142
|
140 |
public void writeEndResultSet() {
|
franta-hg@142
|
141 |
super.writeEndResultSet();
|
franta-hg@142
|
142 |
printEndElement();
|
franta-hg@142
|
143 |
}
|
franta-hg@142
|
144 |
|
franta-hg@142
|
145 |
@Override
|
franta-hg@129
|
146 |
public void writeStartRow() {
|
franta-hg@129
|
147 |
super.writeStartRow();
|
franta-hg@129
|
148 |
printStartElement(qname("row"));
|
franta-hg@129
|
149 |
}
|
franta-hg@129
|
150 |
|
franta-hg@129
|
151 |
@Override
|
franta-hg@129
|
152 |
public void writeColumnValue(Object value) {
|
franta-hg@129
|
153 |
super.writeColumnValue(value);
|
franta-hg@131
|
154 |
|
franta-hg@131
|
155 |
Map<QName, String> attributes = null;
|
franta-hg@131
|
156 |
if (labeledColumns) {
|
franta-hg@151
|
157 |
attributes = new LinkedHashMap<>(2);
|
franta-hg@131
|
158 |
attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
|
franta-hg@131
|
159 |
}
|
franta-hg@131
|
160 |
|
franta-hg@151
|
161 |
if (value == null) {
|
franta-hg@151
|
162 |
if (attributes == null) {
|
franta-hg@151
|
163 |
attributes = new LinkedHashMap<>(2);
|
franta-hg@151
|
164 |
}
|
franta-hg@163
|
165 |
attributes.put(qname("null"), "true");
|
franta-hg@163
|
166 |
printEmptyElement(qname("column"), attributes);
|
franta-hg@165
|
167 |
} else if (value instanceof Array) {
|
franta-hg@206
|
168 |
|
franta-hg@165
|
169 |
Array sqlArray = (Array) value;
|
franta-hg@165
|
170 |
try {
|
franta-hg@165
|
171 |
Object[] array = (Object[]) sqlArray.getArray();
|
franta-hg@165
|
172 |
printStartElement(qname("column"), attributes);
|
franta-hg@165
|
173 |
printArray(array);
|
franta-hg@165
|
174 |
printEndElement();
|
franta-hg@165
|
175 |
} catch (SQLException e) {
|
franta-hg@233
|
176 |
// FIXME: rewrite array formatting, remember array mode, don't try sqlArray.getArray() again and again if it has failed
|
franta-hg@165
|
177 |
log.log(Level.SEVERE, "Unable to format array", e);
|
franta-hg@233
|
178 |
try {
|
franta-hg@233
|
179 |
ResultSet arrayResultSet = sqlArray.getResultSet();
|
franta-hg@233
|
180 |
//int columnCount = arrayResultSet.getMetaData().getColumnCount();
|
franta-hg@233
|
181 |
ArrayList<Object> arrayList = new ArrayList<>();
|
franta-hg@233
|
182 |
while (arrayResultSet.next()) {
|
franta-hg@233
|
183 |
arrayList.add(arrayResultSet.getObject(2));
|
franta-hg@233
|
184 |
// for (int i = 1; i <= columnCount; i++) {
|
franta-hg@233
|
185 |
// log.log(Level.INFO, "Array column {0} = {1}", new Object[]{i, arrayResultSet.getObject(i)});
|
franta-hg@233
|
186 |
// }
|
franta-hg@233
|
187 |
}
|
franta-hg@233
|
188 |
|
franta-hg@233
|
189 |
printStartElement(qname("column"), attributes);
|
franta-hg@233
|
190 |
// FIXME: instanceof SQLXML, see below
|
franta-hg@233
|
191 |
printArray(arrayList.toArray());
|
franta-hg@233
|
192 |
printEndElement();
|
franta-hg@233
|
193 |
|
franta-hg@233
|
194 |
} catch (SQLException e2) {
|
franta-hg@233
|
195 |
// FIXME: fix logging, error recovery
|
franta-hg@233
|
196 |
log.log(Level.SEVERE, "Second level fuck up !!!", e2);
|
franta-hg@233
|
197 |
}
|
franta-hg@233
|
198 |
|
franta-hg@165
|
199 |
writeColumnValue(String.valueOf(value));
|
franta-hg@165
|
200 |
}
|
franta-hg@206
|
201 |
|
franta-hg@233
|
202 |
} else if (value instanceof SQLXML) { // FIXME: move to separate method, to AbstractFormatter?
|
franta-hg@225
|
203 |
SQLXML xml = (SQLXML) value;
|
franta-hg@225
|
204 |
// TODO: parse DOM/SAX and transplant XML, don't escape (optional)
|
franta-hg@225
|
205 |
try {
|
franta-hg@225
|
206 |
printTextElement(qname("column"), attributes, xml.getString());
|
franta-hg@225
|
207 |
} catch (SQLException e) {
|
franta-hg@225
|
208 |
log.log(Level.SEVERE, "Unable to format XML", e);
|
franta-hg@225
|
209 |
writeColumnValue(String.valueOf(value));
|
franta-hg@225
|
210 |
}
|
franta-hg@151
|
211 |
} else {
|
franta-hg@151
|
212 |
printTextElement(qname("column"), attributes, toString(value));
|
franta-hg@151
|
213 |
}
|
franta-hg@129
|
214 |
}
|
franta-hg@206
|
215 |
|
franta-hg@165
|
216 |
private void printArray(Object[] array) {
|
franta-hg@165
|
217 |
printStartElement(qname("array"));
|
franta-hg@165
|
218 |
for (Object o : array) {
|
franta-hg@165
|
219 |
if (o instanceof Object[]) {
|
franta-hg@165
|
220 |
printStartElement(qname("item"));
|
franta-hg@165
|
221 |
printArray((Object[]) o);
|
franta-hg@165
|
222 |
printEndElement();
|
franta-hg@165
|
223 |
} else {
|
franta-hg@165
|
224 |
printTextElement(qname("item"), null, String.valueOf(o));
|
franta-hg@165
|
225 |
}
|
franta-hg@165
|
226 |
}
|
franta-hg@165
|
227 |
printEndElement();
|
franta-hg@165
|
228 |
}
|
franta-hg@129
|
229 |
|
franta-hg@129
|
230 |
@Override
|
franta-hg@129
|
231 |
public void writeEndRow() {
|
franta-hg@129
|
232 |
super.writeEndRow();
|
franta-hg@129
|
233 |
printEndElement();
|
franta-hg@129
|
234 |
}
|
franta-hg@129
|
235 |
|
franta-hg@129
|
236 |
@Override
|
franta-hg@142
|
237 |
public void writeUpdatesResult(int updatedRowsCount) {
|
franta-hg@142
|
238 |
super.writeUpdatesResult(updatedRowsCount);
|
franta-hg@129
|
239 |
printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
|
franta-hg@129
|
240 |
}
|
franta-hg@129
|
241 |
|
franta-hg@129
|
242 |
protected String toString(Object value) {
|
franta-hg@129
|
243 |
return String.valueOf(value);
|
franta-hg@129
|
244 |
}
|
franta-hg@29
|
245 |
}
|