1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/XmlFormatter.java Mon Mar 04 20:15:24 2019 +0100
1.3 @@ -0,0 +1,245 @@
1.4 +/**
1.5 + * SQL-DK
1.6 + * Copyright © 2013 František Kučera (frantovo.cz)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +package info.globalcode.sql.dk.formatting;
1.22 +
1.23 +import info.globalcode.sql.dk.Parameter;
1.24 +import info.globalcode.sql.dk.Xmlns;
1.25 +import info.globalcode.sql.dk.configuration.DatabaseDefinition;
1.26 +import static info.globalcode.sql.dk.Functions.notNull;
1.27 +import info.globalcode.sql.dk.NamedParameter;
1.28 +import info.globalcode.sql.dk.configuration.PropertyDeclaration;
1.29 +import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
1.30 +import java.sql.Array;
1.31 +import java.sql.ResultSet;
1.32 +import java.sql.SQLException;
1.33 +import java.sql.SQLXML;
1.34 +import java.util.ArrayList;
1.35 +import java.util.LinkedHashMap;
1.36 +import java.util.List;
1.37 +import java.util.Map;
1.38 +import java.util.logging.Level;
1.39 +import java.util.logging.Logger;
1.40 +import javax.xml.namespace.QName;
1.41 +
1.42 +/**
1.43 + * <p>
1.44 + * Prints machine-readable output – XML document containing resultsets and updates count. Good
1.45 + * choice for further processing – e.g. XSL transformation.</p>
1.46 + *
1.47 + * <p>
1.48 + * TODO: XSD</p>
1.49 + *
1.50 + * @author Ing. František Kučera (frantovo.cz)
1.51 + */
1.52 +@PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, defaultValue = "false", type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element")
1.53 +public class XmlFormatter extends AbstractXmlFormatter {
1.54 +
1.55 + public static final String NAME = "xml"; // bash-completion:formatter
1.56 + public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
1.57 + private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
1.58 + private final boolean labeledColumns;
1.59 +
1.60 + public XmlFormatter(FormatterContext formatterContext) {
1.61 + super(formatterContext);
1.62 + labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
1.63 + }
1.64 +
1.65 + @Override
1.66 + public void writeStartBatch() {
1.67 + super.writeStartBatch();
1.68 + printStartDocument();
1.69 + printStartElement(qname("batchResult"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
1.70 + }
1.71 +
1.72 + @Override
1.73 + public void writeEndBatch() {
1.74 + super.writeEndBatch();
1.75 + printEndElement();
1.76 + printEndDocument();
1.77 + }
1.78 +
1.79 + @Override
1.80 + public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
1.81 + super.writeStartDatabase(databaseDefinition);
1.82 + Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
1.83 + printStartElement(qname("database"), attributes);
1.84 + }
1.85 +
1.86 + @Override
1.87 + public void writeEndDatabase() {
1.88 + super.writeEndDatabase();
1.89 + printEndElement();
1.90 + }
1.91 +
1.92 + @Override
1.93 + public void writeStartStatement() {
1.94 + super.writeStartStatement();
1.95 + printStartElement(qname("statement"));
1.96 + }
1.97 +
1.98 + @Override
1.99 + public void writeEndStatement() {
1.100 + super.writeEndStatement();
1.101 + printEndElement();
1.102 + }
1.103 +
1.104 + @Override
1.105 + public void writeQuery(String sql) {
1.106 + super.writeQuery(sql);
1.107 + printTextElement(qname("sql"), null, sql);
1.108 + }
1.109 +
1.110 + @Override
1.111 + public void writeParameters(List<? extends Parameter> parameters) {
1.112 + super.writeParameters(parameters);
1.113 +
1.114 + for (Parameter p : notNull(parameters)) {
1.115 +
1.116 + Map<QName, String> attributes = new LinkedHashMap<>(2);
1.117 + if (p instanceof NamedParameter) {
1.118 + attributes.put(qname("name"), ((NamedParameter) p).getName());
1.119 + }
1.120 + attributes.put(qname("type"), p.getType().name());
1.121 +
1.122 + printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
1.123 + }
1.124 +
1.125 + }
1.126 +
1.127 + @Override
1.128 + public void writeStartResultSet(ColumnsHeader header) {
1.129 + super.writeStartResultSet(header);
1.130 + printStartElement(qname("resultSet"));
1.131 +
1.132 + for (ColumnDescriptor cd : header.getColumnDescriptors()) {
1.133 + Map<QName, String> attributes = new LinkedHashMap<>(4);
1.134 + attributes.put(qname("label"), cd.getLabel());
1.135 + attributes.put(qname("name"), cd.getName());
1.136 + attributes.put(qname("typeName"), cd.getTypeName());
1.137 + attributes.put(qname("type"), String.valueOf(cd.getType()));
1.138 + printEmptyElement(qname("columnHeader"), attributes);
1.139 + }
1.140 + }
1.141 +
1.142 + @Override
1.143 + public void writeEndResultSet() {
1.144 + super.writeEndResultSet();
1.145 + printEndElement();
1.146 + }
1.147 +
1.148 + @Override
1.149 + public void writeStartRow() {
1.150 + super.writeStartRow();
1.151 + printStartElement(qname("row"));
1.152 + }
1.153 +
1.154 + @Override
1.155 + public void writeColumnValue(Object value) {
1.156 + super.writeColumnValue(value);
1.157 +
1.158 + Map<QName, String> attributes = null;
1.159 + if (labeledColumns) {
1.160 + attributes = new LinkedHashMap<>(2);
1.161 + attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
1.162 + }
1.163 +
1.164 + if (value == null) {
1.165 + if (attributes == null) {
1.166 + attributes = new LinkedHashMap<>(2);
1.167 + }
1.168 + attributes.put(qname("null"), "true");
1.169 + printEmptyElement(qname("column"), attributes);
1.170 + } else if (value instanceof Array) {
1.171 +
1.172 + Array sqlArray = (Array) value;
1.173 + try {
1.174 + Object[] array = (Object[]) sqlArray.getArray();
1.175 + printStartElement(qname("column"), attributes);
1.176 + printArray(array);
1.177 + printEndElement();
1.178 + } catch (SQLException e) {
1.179 + // FIXME: rewrite array formatting, remember array mode, don't try sqlArray.getArray() again and again if it has failed
1.180 + log.log(Level.SEVERE, "Unable to format array", e);
1.181 + try {
1.182 + ResultSet arrayResultSet = sqlArray.getResultSet();
1.183 + //int columnCount = arrayResultSet.getMetaData().getColumnCount();
1.184 + ArrayList<Object> arrayList = new ArrayList<>();
1.185 + while (arrayResultSet.next()) {
1.186 + arrayList.add(arrayResultSet.getObject(2));
1.187 + // for (int i = 1; i <= columnCount; i++) {
1.188 + // log.log(Level.INFO, "Array column {0} = {1}", new Object[]{i, arrayResultSet.getObject(i)});
1.189 + // }
1.190 + }
1.191 +
1.192 + printStartElement(qname("column"), attributes);
1.193 + // FIXME: instanceof SQLXML, see below
1.194 + printArray(arrayList.toArray());
1.195 + printEndElement();
1.196 +
1.197 + } catch (SQLException e2) {
1.198 + // FIXME: fix logging, error recovery
1.199 + log.log(Level.SEVERE, "Second level fuck up !!!", e2);
1.200 + }
1.201 +
1.202 + writeColumnValue(String.valueOf(value));
1.203 + }
1.204 +
1.205 + } else if (value instanceof SQLXML) { // FIXME: move to separate method, to AbstractFormatter?
1.206 + SQLXML xml = (SQLXML) value;
1.207 + // TODO: parse DOM/SAX and transplant XML, don't escape (optional)
1.208 + try {
1.209 + printTextElement(qname("column"), attributes, xml.getString());
1.210 + } catch (SQLException e) {
1.211 + log.log(Level.SEVERE, "Unable to format XML", e);
1.212 + writeColumnValue(String.valueOf(value));
1.213 + }
1.214 + } else {
1.215 + printTextElement(qname("column"), attributes, toString(value));
1.216 + }
1.217 + }
1.218 +
1.219 + private void printArray(Object[] array) {
1.220 + printStartElement(qname("array"));
1.221 + for (Object o : array) {
1.222 + if (o instanceof Object[]) {
1.223 + printStartElement(qname("item"));
1.224 + printArray((Object[]) o);
1.225 + printEndElement();
1.226 + } else {
1.227 + printTextElement(qname("item"), null, String.valueOf(o));
1.228 + }
1.229 + }
1.230 + printEndElement();
1.231 + }
1.232 +
1.233 + @Override
1.234 + public void writeEndRow() {
1.235 + super.writeEndRow();
1.236 + printEndElement();
1.237 + }
1.238 +
1.239 + @Override
1.240 + public void writeUpdatesResult(int updatedRowsCount) {
1.241 + super.writeUpdatesResult(updatedRowsCount);
1.242 + printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
1.243 + }
1.244 +
1.245 + protected String toString(Object value) {
1.246 + return String.valueOf(value);
1.247 + }
1.248 +}