JDBC driver class can be specified in the database configuration as an optional parameter (useful especially while embedding jdbc-dk-driver into other application that does not support automatic driver discovery)
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.Xmlns;
22 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
23 import static info.globalcode.sql.dk.Functions.notNull;
24 import info.globalcode.sql.dk.NamedParameter;
25 import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
26 import java.sql.Array;
27 import java.sql.SQLException;
28 import java.util.LinkedHashMap;
29 import java.util.List;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 import javax.xml.namespace.QName;
36 * <p>Prints machine-readable output – XML document containing resultsets and updates count. Good
37 * choice for further processing – e.g. XSL transformation.</p>
41 * @author Ing. František Kučera (frantovo.cz)
43 public class XmlFormatter extends AbstractXmlFormatter {
45 public static final String NAME = "xml"; // bash-completion:formatter
46 public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
47 private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
48 private final boolean labeledColumns;
50 public XmlFormatter(FormatterContext formatterContext) {
51 super(formatterContext);
52 labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
56 public void writeStartBatch() {
57 super.writeStartBatch();
59 printStartElement(qname("batchResult"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
63 public void writeEndBatch() {
64 super.writeEndBatch();
70 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
71 super.writeStartDatabase(databaseDefinition);
72 Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
73 printStartElement(qname("database"), attributes);
77 public void writeEndDatabase() {
78 super.writeEndDatabase();
83 public void writeStartStatement() {
84 super.writeStartStatement();
85 printStartElement(qname("statement"));
89 public void writeEndStatement() {
90 super.writeEndStatement();
95 public void writeQuery(String sql) {
96 super.writeQuery(sql);
97 printTextElement(qname("sql"), null, sql);
101 public void writeParameters(List<? extends Parameter> parameters) {
102 super.writeParameters(parameters);
104 for (Parameter p : notNull(parameters)) {
106 Map<QName, String> attributes = new LinkedHashMap<>(2);
107 if (p instanceof NamedParameter) {
108 attributes.put(qname("name"), ((NamedParameter) p).getName());
110 attributes.put(qname("type"), p.getType().name());
112 printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
118 public void writeStartResultSet(ColumnsHeader header) {
119 super.writeStartResultSet(header);
120 printStartElement(qname("resultSet"));
122 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
123 Map<QName, String> attributes = new LinkedHashMap<>(4);
124 attributes.put(qname("label"), cd.getLabel());
125 attributes.put(qname("name"), cd.getName());
126 attributes.put(qname("typeName"), cd.getTypeName());
127 attributes.put(qname("type"), String.valueOf(cd.getType()));
128 printEmptyElement(qname("columnHeader"), attributes);
133 public void writeEndResultSet() {
134 super.writeEndResultSet();
139 public void writeStartRow() {
140 super.writeStartRow();
141 printStartElement(qname("row"));
145 public void writeColumnValue(Object value) {
146 super.writeColumnValue(value);
148 Map<QName, String> attributes = null;
149 if (labeledColumns) {
150 attributes = new LinkedHashMap<>(2);
151 attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
155 if (attributes == null) {
156 attributes = new LinkedHashMap<>(2);
158 attributes.put(qname("null"), "true");
159 printEmptyElement(qname("column"), attributes);
160 } else if (value instanceof Array) {
164 Array sqlArray = (Array) value;
166 Object[] array = (Object[]) sqlArray.getArray();
167 printStartElement(qname("column"), attributes);
170 } catch (SQLException e) {
171 log.log(Level.SEVERE, "Unable to format array", e);
172 writeColumnValue(String.valueOf(value));
177 printTextElement(qname("column"), attributes, toString(value));
181 private void printArray(Object[] array) {
182 printStartElement(qname("array"));
183 for (Object o : array) {
184 if (o instanceof Object[]) {
185 printStartElement(qname("item"));
186 printArray((Object[]) o);
189 printTextElement(qname("item"), null, String.valueOf(o));
196 public void writeEndRow() {
202 public void writeUpdatesResult(int updatedRowsCount) {
203 super.writeUpdatesResult(updatedRowsCount);
204 printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
207 protected String toString(Object value) {
208 return String.valueOf(value);