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.formatting.AbstractXmlFormatter.qname;
|
franta-hg@128
|
24 |
import static info.globalcode.sql.dk.Functions.notNull;
|
franta-hg@128
|
25 |
import info.globalcode.sql.dk.NamedParameter;
|
franta-hg@128
|
26 |
import java.util.LinkedHashMap;
|
franta-hg@128
|
27 |
import java.util.List;
|
franta-hg@128
|
28 |
import java.util.Map;
|
franta-hg@128
|
29 |
import javax.xml.namespace.QName;
|
franta-hg@128
|
30 |
|
franta-hg@29
|
31 |
/**
|
franta-hg@155
|
32 |
* <p>Prints machine-readable output – XML document containing resultsets and updates count. Good
|
franta-hg@155
|
33 |
* choice for further processing – e.g. XSL transformation.</p>
|
franta-hg@155
|
34 |
*
|
franta-hg@155
|
35 |
* <p>TODO: XSD</p>
|
franta-hg@29
|
36 |
*
|
franta-hg@29
|
37 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@29
|
38 |
*/
|
franta-hg@128
|
39 |
public class XmlFormatter extends AbstractXmlFormatter {
|
franta-hg@29
|
40 |
|
franta-hg@79
|
41 |
public static final String NAME = "xml"; // bash-completion:formatter
|
franta-hg@131
|
42 |
public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
|
franta-hg@131
|
43 |
private final boolean labeledColumns;
|
franta-hg@29
|
44 |
|
franta-hg@29
|
45 |
public XmlFormatter(FormatterContext formatterContext) {
|
franta-hg@29
|
46 |
super(formatterContext);
|
franta-hg@131
|
47 |
labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
|
franta-hg@29
|
48 |
}
|
franta-hg@128
|
49 |
|
franta-hg@128
|
50 |
@Override
|
franta-hg@128
|
51 |
public void writeStartBatch() {
|
franta-hg@128
|
52 |
super.writeStartBatch();
|
franta-hg@128
|
53 |
printStartDocument();
|
franta-hg@154
|
54 |
printStartElement(qname("batchResult"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
|
franta-hg@128
|
55 |
}
|
franta-hg@128
|
56 |
|
franta-hg@128
|
57 |
@Override
|
franta-hg@128
|
58 |
public void writeEndBatch() {
|
franta-hg@128
|
59 |
super.writeEndBatch();
|
franta-hg@128
|
60 |
printEndElement();
|
franta-hg@128
|
61 |
printEndDocument();
|
franta-hg@128
|
62 |
}
|
franta-hg@128
|
63 |
|
franta-hg@128
|
64 |
@Override
|
franta-hg@128
|
65 |
public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
|
franta-hg@128
|
66 |
super.writeStartDatabase(databaseDefinition);
|
franta-hg@128
|
67 |
Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
|
franta-hg@128
|
68 |
printStartElement(qname("database"), attributes);
|
franta-hg@128
|
69 |
}
|
franta-hg@128
|
70 |
|
franta-hg@128
|
71 |
@Override
|
franta-hg@128
|
72 |
public void writeEndDatabase() {
|
franta-hg@128
|
73 |
super.writeEndDatabase();
|
franta-hg@128
|
74 |
printEndElement();
|
franta-hg@128
|
75 |
}
|
franta-hg@128
|
76 |
|
franta-hg@128
|
77 |
@Override
|
franta-hg@142
|
78 |
public void writeStartStatement() {
|
franta-hg@142
|
79 |
super.writeStartStatement();
|
franta-hg@142
|
80 |
printStartElement(qname("statement"));
|
franta-hg@128
|
81 |
}
|
franta-hg@128
|
82 |
|
franta-hg@128
|
83 |
@Override
|
franta-hg@142
|
84 |
public void writeEndStatement() {
|
franta-hg@142
|
85 |
super.writeEndStatement();
|
franta-hg@128
|
86 |
printEndElement();
|
franta-hg@128
|
87 |
}
|
franta-hg@128
|
88 |
|
franta-hg@128
|
89 |
@Override
|
franta-hg@128
|
90 |
public void writeQuery(String sql) {
|
franta-hg@128
|
91 |
super.writeQuery(sql);
|
franta-hg@128
|
92 |
printTextElement(qname("sql"), null, sql);
|
franta-hg@128
|
93 |
}
|
franta-hg@128
|
94 |
|
franta-hg@128
|
95 |
@Override
|
franta-hg@128
|
96 |
public void writeParameters(List<? extends Parameter> parameters) {
|
franta-hg@128
|
97 |
super.writeParameters(parameters);
|
franta-hg@128
|
98 |
|
franta-hg@128
|
99 |
for (Parameter p : notNull(parameters)) {
|
franta-hg@128
|
100 |
|
franta-hg@128
|
101 |
Map<QName, String> attributes = new LinkedHashMap<>(2);
|
franta-hg@128
|
102 |
if (p instanceof NamedParameter) {
|
franta-hg@128
|
103 |
attributes.put(qname("name"), ((NamedParameter) p).getName());
|
franta-hg@128
|
104 |
}
|
franta-hg@128
|
105 |
attributes.put(qname("type"), p.getType().name());
|
franta-hg@128
|
106 |
|
franta-hg@128
|
107 |
printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
|
franta-hg@128
|
108 |
}
|
franta-hg@128
|
109 |
|
franta-hg@128
|
110 |
}
|
franta-hg@128
|
111 |
|
franta-hg@128
|
112 |
@Override
|
franta-hg@142
|
113 |
public void writeStartResultSet(ColumnsHeader header) {
|
franta-hg@142
|
114 |
super.writeStartResultSet(header);
|
franta-hg@142
|
115 |
printStartElement(qname("resultSet"));
|
franta-hg@128
|
116 |
|
franta-hg@128
|
117 |
for (ColumnDescriptor cd : header.getColumnDescriptors()) {
|
franta-hg@128
|
118 |
Map<QName, String> attributes = new LinkedHashMap<>(4);
|
franta-hg@128
|
119 |
attributes.put(qname("label"), cd.getLabel());
|
franta-hg@128
|
120 |
attributes.put(qname("name"), cd.getName());
|
franta-hg@128
|
121 |
attributes.put(qname("typeName"), cd.getTypeName());
|
franta-hg@128
|
122 |
attributes.put(qname("type"), String.valueOf(cd.getType()));
|
franta-hg@128
|
123 |
printEmptyElement(qname("columnHeader"), attributes);
|
franta-hg@128
|
124 |
}
|
franta-hg@128
|
125 |
}
|
franta-hg@129
|
126 |
|
franta-hg@129
|
127 |
@Override
|
franta-hg@142
|
128 |
public void writeEndResultSet() {
|
franta-hg@142
|
129 |
super.writeEndResultSet();
|
franta-hg@142
|
130 |
printEndElement();
|
franta-hg@142
|
131 |
}
|
franta-hg@142
|
132 |
|
franta-hg@142
|
133 |
@Override
|
franta-hg@129
|
134 |
public void writeStartRow() {
|
franta-hg@129
|
135 |
super.writeStartRow();
|
franta-hg@129
|
136 |
printStartElement(qname("row"));
|
franta-hg@129
|
137 |
}
|
franta-hg@129
|
138 |
|
franta-hg@129
|
139 |
@Override
|
franta-hg@129
|
140 |
public void writeColumnValue(Object value) {
|
franta-hg@129
|
141 |
super.writeColumnValue(value);
|
franta-hg@131
|
142 |
|
franta-hg@131
|
143 |
Map<QName, String> attributes = null;
|
franta-hg@131
|
144 |
if (labeledColumns) {
|
franta-hg@151
|
145 |
attributes = new LinkedHashMap<>(2);
|
franta-hg@131
|
146 |
attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
|
franta-hg@131
|
147 |
}
|
franta-hg@131
|
148 |
|
franta-hg@151
|
149 |
if (value == null) {
|
franta-hg@151
|
150 |
if (attributes == null) {
|
franta-hg@151
|
151 |
attributes = new LinkedHashMap<>(2);
|
franta-hg@151
|
152 |
}
|
franta-hg@163
|
153 |
attributes.put(qname("null"), "true");
|
franta-hg@163
|
154 |
printEmptyElement(qname("column"), attributes);
|
franta-hg@151
|
155 |
} else {
|
franta-hg@151
|
156 |
printTextElement(qname("column"), attributes, toString(value));
|
franta-hg@151
|
157 |
}
|
franta-hg@129
|
158 |
}
|
franta-hg@129
|
159 |
|
franta-hg@129
|
160 |
@Override
|
franta-hg@129
|
161 |
public void writeEndRow() {
|
franta-hg@129
|
162 |
super.writeEndRow();
|
franta-hg@129
|
163 |
printEndElement();
|
franta-hg@129
|
164 |
}
|
franta-hg@129
|
165 |
|
franta-hg@129
|
166 |
@Override
|
franta-hg@142
|
167 |
public void writeUpdatesResult(int updatedRowsCount) {
|
franta-hg@142
|
168 |
super.writeUpdatesResult(updatedRowsCount);
|
franta-hg@129
|
169 |
printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
|
franta-hg@129
|
170 |
}
|
franta-hg@129
|
171 |
|
franta-hg@129
|
172 |
protected String toString(Object value) {
|
franta-hg@129
|
173 |
return String.valueOf(value);
|
franta-hg@129
|
174 |
}
|
franta-hg@29
|
175 |
}
|