Skip to content

Instantly share code, notes, and snippets.

@0x6e6562
Created December 23, 2015 02:10
Show Gist options
  • Save 0x6e6562/325c0020d1355d28bc08 to your computer and use it in GitHub Desktop.
Save 0x6e6562/325c0020d1355d28bc08 to your computer and use it in GitHub Desktop.
public class OffsetAwareGenerator extends JavaGenerator {
@Override
protected String getJavaTypeReference(Database db, DataTypeDefinition type) {
if (type.getUserType().equals("java.time.OffsetDateTime")) {
// TODO Not very type safe, extremely hacky
return "new org.jooq.impl.DefaultDataType<oracle.sql.TIMESTAMPTZ>(org.jooq.SQLDialect.ORACLE12C, oracle.sql.TIMESTAMPTZ.class, \"timestamp with time zone\")";
} else {
return super.getJavaTypeReference(db, type);
}
}
}
public class OffsetBinding implements Binding<TIMESTAMPTZ, OffsetDateTime> {
private final static OffsetConverter converter = new OffsetConverter();
@Override
public Converter<TIMESTAMPTZ, OffsetDateTime> converter() {
return converter;
}
@Override
public void sql(BindingSQLContext<OffsetDateTime> ctx) throws SQLException {
// I *think* this will only generate the PS bindings, whereas JOOQ will log the bound values if you have debug logging turned on
ctx.render().sql(ctx.variable());
}
@Override
public void register(BindingRegisterContext<OffsetDateTime> ctx) throws SQLException {
// Don't really understand the registration process here, but it seems to work
ctx.statement().registerOutParameter(ctx.index(), Types.TIMESTAMP_WITH_TIMEZONE);
}
@Override
public void set(BindingSetStatementContext<OffsetDateTime> ctx) throws SQLException {
PreparedStatement stmt = ctx.statement();
stmt.setObject(ctx.index(), converter.to(ctx.value()));
}
@Override
public void get(BindingGetResultSetContext<OffsetDateTime> ctx) throws SQLException {
TIMESTAMPTZ tz = (TIMESTAMPTZ) ctx.resultSet().getObject(ctx.index());
ctx.convert(converter).value(tz);
}
@Override
public void get(BindingGetStatementContext<OffsetDateTime> ctx) throws SQLException {
TIMESTAMPTZ tz = (TIMESTAMPTZ) ctx.statement().getObject(ctx.index());
ctx.convert(converter).value(tz);
}
@Override
public void get(BindingGetSQLInputContext<OffsetDateTime> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void set(BindingSetSQLOutputContext<OffsetDateTime> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
public class OffsetConverter implements Converter<TIMESTAMPTZ, OffsetDateTime> {
@Override
public OffsetDateTime from(TIMESTAMPTZ tz) {
// Uses unpacking routine from https://github.com/marschall/threeten-jpa
return timestamptzToOffsetDateTime(tz);
}
@Override
public TIMESTAMPTZ to(OffsetDateTime off) {
// Uses packing routine from https://github.com/marschall/threeten-jpa
return offsetDateTimeToTimestamptz(off);
}
@Override
public Class<TIMESTAMPTZ> fromType() {
return TIMESTAMPTZ.class;
}
@Override
public Class<OffsetDateTime> toType() {
return OffsetDateTime.class;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment