>> Hint: You will need to rewrite or cast the expression.Is there a way to solve this issues?
Should type INET be supported by jooq natively?
Thanks for the hint ... works nicely :-)
Maybe you should use VARCHAR as the SQL data type and convert the value using CAST(? as inet) and CAST(? as text) when sending / receiving an inet address from the database.
InetAddress makes sense for mapping into java world, alternatively java.lang.String could make sense, too
Be aware that InetAddress might cause unwanted DNS lookups (eventually depending on OS) when instantiating the object.
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
package my.package;
import org.apache.commons.net.util.SubnetUtils;
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.Converter;
import org.jooq.impl.DSL;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;
import java.util.Objects;
/**
* We're binding <T> = Object (unknown JDBC type), and <U> = SubnetUtils.SubnetInfo (user type)
* @author aaron.axisa
*/
public class PostgresCIDRSubnetInfoBinding implements Binding<Object, SubnetUtils.SubnetInfo> {
@Override
public Converter<Object, SubnetUtils.SubnetInfo> converter() {
return CidrConverter.getInstance();
}
/**
* The converter does all the conversion
*/
private static class CidrConverter implements Converter<Object, SubnetUtils.SubnetInfo> {
private static CidrConverter INSTANCE = new CidrConverter();
private CidrConverter() {
// we want spring to override the static INSTANCE when it launches
INSTANCE = this;
}
/**
* @return The singleton instance for {@link CidrConverter}
*/
public static CidrConverter getInstance() {
return INSTANCE;
}
@Override
public SubnetUtils.SubnetInfo from(final Object t) {
// convert the Postgres data type to json
return t == null ? null : new SubnetUtils(t.toString()).getInfo();
}
@Override
public Object to(final SubnetUtils.SubnetInfo u) {
// convert the json to the Postgres data type
return u == null ? null : u.getAddress()+u.getCidrSignature(); // TODO
}
@Override
public Class<Object> fromType() {
return Object.class;
}
@Override
public Class<SubnetUtils.SubnetInfo> toType() {
return SubnetUtils.SubnetInfo.class;
}
}
// Rending a bind variable for the binding context's value and casting it to the cidr type
@Override
public void sql(BindingSQLContext<SubnetUtils.SubnetInfo> ctx) throws SQLException {
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::cidr");
}
// Registering VARCHAR types for JDBC CallableStatement OUT parameters
@Override
public void register(BindingRegisterContext<SubnetUtils.SubnetInfo> ctx) throws SQLException {
ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);
}
// Converting the SubnetInfo to a String value and setting that on a JDBC PreparedStatement
@Override
public void set(BindingSetStatementContext<SubnetUtils.SubnetInfo> ctx) throws SQLException {
ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null));
}
// Getting a String value from a JDBC ResultSet and converting that to a SubnetInfo
@Override
public void get(BindingGetResultSetContext<SubnetUtils.SubnetInfo> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
}
// Getting a String value from a JDBC CallableStatement and converting that to a JsonElement
@Override
public void get(BindingGetStatementContext<SubnetUtils.SubnetInfo> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
}
// Setting a value on a JDBC SQLOutput (useful for Oracle OBJECT types)
@Override
public void set(BindingSetSQLOutputContext<SubnetUtils.SubnetInfo> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
// Getting a value from a JDBC SQLInput (useful for Oracle OBJECT types)
@Override
public void get(BindingGetSQLInputContext<SubnetUtils.SubnetInfo> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}<customTypes>
<customType>
<name>SubnetInfo</name>
<type>org.apache.commons.net.util.SubnetUtils.SubnetInfo</type>
<binding>my.package.PostgresCIDRSubnetInfoBinding</binding>
</customType>
</customTypes>
<forcedTypes>
<forcedType>
<name>SubnetInfo</name>
<types>CIDR</types>
</forcedType>
</forcedTypes>--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.