argos/dmp-backend/core/src/main/java/eu/eudat/configurations/typedefinition/XMLType.java

95 lines
2.8 KiB
Java

package eu.eudat.configurations.typedefinition;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
/**
* Store and retrieve a PostgreSQL "xml" column as a Java string.
*/
public class XMLType implements UserType<String> {
private final int[] sqlTypesSupported = new int[]{Types.VARCHAR};
public int[] sqlTypes() {
return sqlTypesSupported;
}
@Override
public int getSqlType() {
return sqlTypesSupported[0];
}
public Class<String> returnedClass() {
return String.class;
}
public boolean equals(String x, String y) throws HibernateException {
if (x == null) {
return y == null;
} else {
return x.equals(y);
}
}
public int hashCode(String x) throws HibernateException {
return x == null ? -1 : x.hashCode();
}
@Override
public String nullSafeGet(ResultSet resultSet, int i, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws SQLException {
assert (i >= 0);
String xmldoc = resultSet.getString(i);
return resultSet.wasNull() ? null : xmldoc;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
assert (names.length == 1);
String xmldoc = rs.getString(names[0]);
return rs.wasNull() ? null : xmldoc;
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.OTHER);
} else {
st.setObject(index, value, Types.OTHER);
}
}
public String deepCopy(String value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(String value) throws HibernateException {
return value;
}
public String assemble(Serializable cached, Object owner) throws HibernateException {
return (String) cached;
}
public String replace(String original, String target, Object owner) throws HibernateException {
return original;
}
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
return nullSafeGet(rs, names, owner);
}
public void nullSafeSet(PreparedStatement st, String value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
nullSafeSet(st, value, index);
}
}