Created
October 4, 2015 03:59
-
-
Save takeouchida/e7edc17bdaba5db912a7 to your computer and use it in GitHub Desktop.
A binding class between Postgres inet type and Java string with JOOQ.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See this for details. | |
// http://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings/ | |
package postgrestypes.bindings; | |
import org.jooq.*; | |
import org.jooq.impl.DSL; | |
import java.sql.SQLException; | |
import java.sql.SQLFeatureNotSupportedException; | |
import java.sql.Types; | |
import java.util.Objects; | |
public class PostgresInetBinding implements Binding<Object, String> { | |
@Override | |
public Converter<Object, String> converter() { | |
return new Converter<Object, String>() { | |
@Override | |
public String from(Object databaseObject) { | |
return databaseObject == null ? null : databaseObject.toString(); | |
} | |
@Override | |
public Object to(String userObject) { | |
return userObject; | |
} | |
@Override | |
public Class<Object> fromType() { | |
return Object.class; | |
} | |
@Override | |
public Class<String> toType() { | |
return String.class; | |
} | |
}; | |
} | |
@Override | |
public void sql(BindingSQLContext<String> ctx) throws SQLException { | |
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::inet"); | |
} | |
@Override | |
public void register(BindingRegisterContext<String> ctx) throws SQLException { | |
ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR); | |
} | |
@Override | |
public void set(BindingSetStatementContext<String> ctx) throws SQLException { | |
ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null)); | |
} | |
@Override | |
public void set(BindingSetSQLOutputContext<String> ctx) throws SQLException { | |
throw new SQLFeatureNotSupportedException(); | |
} | |
@Override | |
public void get(BindingGetResultSetContext<String> ctx) throws SQLException { | |
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index())); | |
} | |
@Override | |
public void get(BindingGetStatementContext<String> ctx) throws SQLException { | |
ctx.convert(converter()).value(ctx.statement().getString(ctx.index())); | |
} | |
@Override | |
public void get(BindingGetSQLInputContext<String> ctx) throws SQLException { | |
throw new SQLFeatureNotSupportedException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment