Skip to content

Instantly share code, notes, and snippets.

@0x6e6562
Last active December 22, 2015 21:08
Show Gist options
  • Save 0x6e6562/51b0caf1ebb6d06d1ee2 to your computer and use it in GitHub Desktop.
Save 0x6e6562/51b0caf1ebb6d06d1ee2 to your computer and use it in GitHub Desktop.
<customTypes>
<customType>
<name>Instant</name>
<type>java.time.Instant</type>
<binding>InstantBinding</binding>
</customType>
</customTypes>
<forcedTypes>
<forcedType>
<name>Instant</name>
<expression>.*DATEZ.*</expression>
<types>TIMESTAMP\(6\)\ WITH\ TIME\ ZONE</types>
</forcedType>
</forcedTypes>
CREATE TABLE z (
id NUMBER(19) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
datez TIMESTAMP WITH TIME ZONE NOT NULL
);
public class InstantAwareGenerator extends JavaGenerator {
@Override
protected String getJavaTypeReference(Database db, DataTypeDefinition type) {
if (type.getUserType().equals("java.time.Instant")) {
// TODO Not very type safe
return "new org.jooq.impl.DefaultDataType<Object>(org.jooq.SQLDialect.ORACLE12C, Object.class, \"timestamp with time zone\")";
} else {
return super.getJavaTypeReference(db, type);
}
}
}
public class InstantBinding implements Binding<Object, Instant> {
private final Converter<Object, Instant> converter;
public InstantBinding() {
this.converter = new InstantConverter();
}
@Override
public Converter<Object, Instant> converter() {
return converter;
}
@Override
public void sql(BindingSQLContext<Instant> ctx) throws SQLException {
ctx.render().sql(ctx.variable());
}
@Override
public void register(BindingRegisterContext<Instant> ctx) throws SQLException {
ctx.statement().registerOutParameter(ctx.index(), Types.TIMESTAMP_WITH_TIMEZONE);
}
@Override
public void set(BindingSetStatementContext<Instant> ctx) throws SQLException {
PreparedStatement stmt = ctx.statement();
Connection con = stmt.getConnection();
if (con.isWrapperFor(OracleConnection.class)){
OracleConnection oracleConnection= con.unwrap(OracleConnection.class);
TIMESTAMPTZ t = new TIMESTAMPTZ(oracleConnection, Timestamp.from(ctx.value()), InstantConverter.UTC);
stmt.setObject(ctx.index(),t);
} else{
throw new UnsupportedOperationException();
}
}
@Override
public void get(BindingGetResultSetContext<Instant> ctx) throws SQLException {
ResultSet rs = ctx.resultSet();
Object t = rs.getObject(ctx.index());
Connection con = ctx.configuration().connectionProvider().acquire();
if (con.isWrapperFor(OracleConnection.class)){
OracleConnection oracleConnection= con.unwrap(OracleConnection.class);
TIMESTAMPTZ tz = (TIMESTAMPTZ) t;
Timestamp ts = tz.timestampValue(oracleConnection);
ctx.value(ts.toInstant());
}
}
@Override
public void get(BindingGetStatementContext<Instant> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void set(BindingSetSQLOutputContext<Instant> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void get(BindingGetSQLInputContext<Instant> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment