[jOOQ/jOOQ#10918] XMLasDOMBinding and AbstractXMLasObjectBinding should implement Binding<XML, T>, not Binding<String, T>
This commit is contained in:
parent
efc6faf0e3
commit
44801b2c90
@ -42,6 +42,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* A data type representing the PostgreSQL <code>hstore</code> type.
|
||||
@ -80,6 +81,15 @@ public final class Hstore implements Serializable {
|
||||
return new Hstore(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link Hstore} instance from string data input, or
|
||||
* <code>null</code> if the input is <code>null</code>.
|
||||
*/
|
||||
@Nullable
|
||||
public static final Hstore hstoreOrNull(Map<String, String> data) {
|
||||
return data == null ? null : new Hstore(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return data.hashCode();
|
||||
|
||||
@ -40,6 +40,7 @@ package org.jooq;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* A JSON wrapper type for JSON data obtained from the database.
|
||||
@ -83,6 +84,15 @@ public final class JSON implements Serializable {
|
||||
return new JSON(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link JSON} instance from string data input, or
|
||||
* <code>null</code> if the input is <code>null</code>.
|
||||
*/
|
||||
@Nullable
|
||||
public static final JSON jsonOrNull(String data) {
|
||||
return data == null ? null : json(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return data.hashCode();
|
||||
|
||||
@ -40,6 +40,7 @@ package org.jooq;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* A JSON wrapper type for JSONB data obtained from the database.
|
||||
@ -83,6 +84,15 @@ public final class JSONB implements Serializable {
|
||||
return new JSONB(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link JSONB} instance from string data input, or
|
||||
* <code>null</code> if the input is <code>null</code>.
|
||||
*/
|
||||
@Nullable
|
||||
public static final JSONB jsonbOrNull(String data) {
|
||||
return data == null ? null : jsonb(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return data.hashCode();
|
||||
|
||||
@ -40,6 +40,7 @@ package org.jooq;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* An XML wrapper type for XML data obtained from the database.
|
||||
@ -83,6 +84,15 @@ public final class XML implements Serializable {
|
||||
return new XML(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link XML} instance from string data input, or
|
||||
* <code>null</code> if the input is <code>null</code>.
|
||||
*/
|
||||
@Nullable
|
||||
public static final XML xmlOrNull(String data) {
|
||||
return data == null ? null : xml(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return data.hashCode();
|
||||
|
||||
101
jOOQ/src/main/java/org/jooq/impl/AbstractXMLBinding.java
Normal file
101
jOOQ/src/main/java/org/jooq/impl/AbstractXMLBinding.java
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Other licenses:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Commercial licenses for this work are available. These replace the above
|
||||
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
|
||||
* database integrations.
|
||||
*
|
||||
* For more information, please visit: http://www.jooq.org/licenses
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.XML.xml;
|
||||
import static org.jooq.XML.xmlOrNull;
|
||||
import static org.jooq.tools.Convert.convert;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
import org.jooq.Binding;
|
||||
import org.jooq.BindingGetResultSetContext;
|
||||
import org.jooq.BindingGetSQLInputContext;
|
||||
import org.jooq.BindingGetStatementContext;
|
||||
import org.jooq.BindingRegisterContext;
|
||||
import org.jooq.BindingSQLContext;
|
||||
import org.jooq.BindingSetSQLOutputContext;
|
||||
import org.jooq.BindingSetStatementContext;
|
||||
import org.jooq.XML;
|
||||
|
||||
/**
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
abstract class AbstractXMLBinding<T> implements Binding<XML, T> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -2153155338260706262L;
|
||||
|
||||
@Override
|
||||
public final void sql(BindingSQLContext<T> ctx) throws SQLException {
|
||||
ctx.render().visit(DSL.val(ctx.convert(converter()).value()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void register(BindingRegisterContext<T> ctx) throws SQLException {
|
||||
ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void set(BindingSetStatementContext<T> ctx) throws SQLException {
|
||||
ctx.statement().setString(ctx.index(), convert(ctx.convert(converter()).value(), String.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void get(BindingGetResultSetContext<T> ctx) throws SQLException {
|
||||
ctx.convert(converter()).value(xmlOrNull(ctx.resultSet().getString(ctx.index())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void get(BindingGetStatementContext<T> ctx) throws SQLException {
|
||||
ctx.convert(converter()).value(xmlOrNull(ctx.statement().getString(ctx.index())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void set(BindingSetSQLOutputContext<T> ctx) throws SQLException {
|
||||
ctx.output().writeString(convert(ctx.convert(converter()).value(), String.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void get(BindingGetSQLInputContext<T> ctx) throws SQLException {
|
||||
ctx.convert(converter()).value(xmlOrNull(ctx.input().readString()));
|
||||
}
|
||||
}
|
||||
@ -56,6 +56,7 @@ import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.jooq.Converter;
|
||||
import org.jooq.XML;
|
||||
|
||||
/**
|
||||
* A binding that binds JAXB-annotated {@link Object} types to {@link SQLXML}
|
||||
@ -65,7 +66,7 @@ import org.jooq.Converter;
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public class AbstractXMLasObjectBinding<T> extends AbstractVarcharBinding<T> {
|
||||
public class AbstractXMLasObjectBinding<T> extends AbstractXMLBinding<T> {
|
||||
|
||||
|
||||
/**
|
||||
@ -73,18 +74,18 @@ public class AbstractXMLasObjectBinding<T> extends AbstractVarcharBinding<T> {
|
||||
*/
|
||||
private static final long serialVersionUID = -2153155338260706262L;
|
||||
|
||||
private final Converter<Object, T> converter;
|
||||
private final Converter<XML, T> converter;
|
||||
|
||||
protected AbstractXMLasObjectBinding(final Class<T> theType) {
|
||||
this.converter = new XMLasObjectConverter<>(theType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Converter<Object, T> converter() {
|
||||
public final Converter<XML, T> converter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
private static final class XMLasObjectConverter<T> implements Converter<Object, T> {
|
||||
private static final class XMLasObjectConverter<T> implements Converter<XML, T> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
@ -111,7 +112,7 @@ public class AbstractXMLasObjectBinding<T> extends AbstractVarcharBinding<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public T from(Object t) {
|
||||
public T from(XML t) {
|
||||
if (t == null)
|
||||
return null;
|
||||
|
||||
@ -119,7 +120,7 @@ public class AbstractXMLasObjectBinding<T> extends AbstractVarcharBinding<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object to(T u) {
|
||||
public XML to(T u) {
|
||||
if (u == null)
|
||||
return null;
|
||||
|
||||
@ -134,7 +135,7 @@ public class AbstractXMLasObjectBinding<T> extends AbstractVarcharBinding<T> {
|
||||
Marshaller m = ctx.createMarshaller();
|
||||
m.setProperty(Marshaller.JAXB_FRAGMENT, true);
|
||||
m.marshal(o, s);
|
||||
return s.toString();
|
||||
return XML.xml(s.toString());
|
||||
}
|
||||
catch (JAXBException e) {
|
||||
throw new DataBindingException(e);
|
||||
@ -142,8 +143,8 @@ public class AbstractXMLasObjectBinding<T> extends AbstractVarcharBinding<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Object> fromType() {
|
||||
return Object.class;
|
||||
public Class<XML> fromType() {
|
||||
return XML.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -37,6 +37,8 @@
|
||||
*/
|
||||
package org.jooq.impl;
|
||||
|
||||
import static org.jooq.XML.xml;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
@ -54,6 +56,7 @@ import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.jooq.Converter;
|
||||
import org.jooq.XML;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.DocumentFragment;
|
||||
@ -67,17 +70,17 @@ import org.xml.sax.SAXException;
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public class XMLasDOMBinding extends AbstractVarcharBinding<Node> {
|
||||
public class XMLasDOMBinding extends AbstractXMLBinding<Node> {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
*/
|
||||
private static final long serialVersionUID = -2153155338260706262L;
|
||||
private static final long serialVersionUID = -2153155338260706262L;
|
||||
|
||||
private final Converter<Object, Node> converter;
|
||||
private final Converter<XML, Node> converter;
|
||||
|
||||
public XMLasDOMBinding() {
|
||||
this.converter = new AbstractConverter<Object, Node>(Object.class, Node.class) {
|
||||
this.converter = new AbstractConverter<XML, Node>(XML.class, Node.class) {
|
||||
|
||||
/**
|
||||
* Generated UID
|
||||
@ -85,19 +88,19 @@ public class XMLasDOMBinding extends AbstractVarcharBinding<Node> {
|
||||
private static final long serialVersionUID = -2153155338260706262L;
|
||||
|
||||
@Override
|
||||
public Node from(Object t) {
|
||||
return t == null ? null : XMLasDOMBinding.fromString("" + t);
|
||||
public Node from(XML t) {
|
||||
return t == null ? null : XMLasDOMBinding.fromString(t.data());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object to(Node u) {
|
||||
return u == null ? null : XMLasDOMBinding.toString(u);
|
||||
public XML to(Node u) {
|
||||
return u == null ? null : xml(XMLasDOMBinding.toString(u));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Converter<Object, Node> converter() {
|
||||
public final Converter<XML, Node> converter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user