- Type Parameters:
T- The database type - i.e. any type available fromSQLDataTypeU- The user type
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
AbstractBinding,AbstractXMLasObjectBinding,BlobBinding,ClobBinding,DateAsTimestampBinding,DefaultBinding,LocalDateAsLocalDateTimeBinding,NClobBinding,XMLasDOMBinding
This SPI is used by jOOQ users to implement support for custom data types
that would otherwise not be supported by jOOQ and/or JDBC. All of jOOQ's
internal support for bind variable types is implemented in
DefaultBinding.
When Binding is invoked
A Binding is invoked whenever jOOQ interacts with JDBC's various
types, including:
get(BindingGetResultSetContext): To read from a JDBCResultSetget(BindingGetStatementContext): To read from a JDBCCallableStatement(OUTparameters).register(BindingRegisterContext)may be needed to declareOUTparameters beforehand.get(BindingGetSQLInputContext): To read from a JDBCSQLInput(UDT attributes)set(BindingSetStatementContext): To write to a JDBCPreparedStatement(sql(BindingSQLContext)may be needed to specify the bind value syntax)set(BindingSetSQLOutputContext): To read from a JDBCSQLOutput(UDT attributes)
Unlike a Converter, a Binding applies only to
JDBC interactions. It does not influence how nested data structures (such as
DSL.multiset(TableLike) or DSL.row(SelectField...)) are
mapped to Java objects, for example.
Creating user defined DataTypes
jOOQ provides built in data types through SQLDataType. Users can
define their own data types programmatically by calling
DataType.asConvertedDataType(Converter) or
DataType.asConvertedDataType(Binding), for example. Custom data types
can also be defined on generated code using the
<forcedType/> configuration, see the
manual for more details.
Ad-hoc
converters allow for attaching a converter directly to a SQL expression
in order to keep related logic close together, see the Converter
Javadoc for more details.
- Author:
- Lukas Eder
-
Method Summary
Modifier and TypeMethodDescriptionGet an array binding for this binding, ornullif no such array binding is available.default @Nullable Binding<?, ?> Get an array component binding for this binding, ornullif this binding isn't for an array type.A converter that can convert between the database type and the custom type.default @Nullable FormatterGet an optionalFormatterimplementation, instructing jOOQ on how to embed the user type inSQLDataType.JSON,SQLDataType.JSONB, andSQLDataType.XMLcontexts.voidget(BindingGetResultSetContext<U> ctx) Get aResultSet'sOUTvalue.voidget(BindingGetSQLInputContext<U> ctx) Get aSQLInput'sOUTvalue.voidget(BindingGetStatementContext<U> ctx) Get aCallableStatement'sOUTvalue.static <T,U> @NotNull Binding <T, U> of(Converter<T, U> converter, Consumer<? super BindingSQLContext<U>> sqlContext, Consumer<? super BindingGetResultSetContext<U>> getResultSetContext, Consumer<? super BindingSetStatementContext<U>> setStatementContext) Construct a binding from functions.static <T,U> @NotNull Binding <T, U> of(Converter<T, U> converter, Consumer<? super BindingSQLContext<U>> sqlContext, Consumer<? super BindingGetResultSetContext<U>> getResultSetContext, Consumer<? super BindingSetStatementContext<U>> setStatementContext, Consumer<? super BindingRegisterContext<U>> registerContext, Consumer<? super BindingGetStatementContext<U>> getStatementContext) Construct a binding from functions.static <T,U> @NotNull Binding <T, U> of(Converter<T, U> converter, Consumer<? super BindingSQLContext<U>> sqlContext, Consumer<? super BindingGetResultSetContext<U>> getResultSetContext, Consumer<? super BindingSetStatementContext<U>> setStatementContext, Consumer<? super BindingRegisterContext<U>> registerContext, Consumer<? super BindingGetStatementContext<U>> getStatementContext, Consumer<? super BindingGetSQLInputContext<U>> getSqlInputContext, Consumer<? super BindingSetSQLOutputContext<U>> setSqlOutputContext) Construct a binding from functions.voidregister(BindingRegisterContext<U> ctx) Register aCallableStatement'sOUTparameter.voidset(BindingSetSQLOutputContext<U> ctx) Set aSQLOutput'sINparameter.voidset(BindingSetStatementContext<U> ctx) Set aPreparedStatement'sINparameter.voidsql(BindingSQLContext<U> ctx) Generate SQL code for the bind variable.
-
Method Details
-
converter
A converter that can convert between the database type and the custom type.While the
Converterproperty of a binding is not optional (Converter.fromType()andConverter.toType()are needed by jOOQ's internals), the conversion implementation (i.e.Converter.from(Object)andConverter.to(Object)) isn't strictly required if implementations don't rely on it. If these conversion implementations are non-functional, it is advised to hint at this fact inConverter.fromSupported()andConverter.toSupported(). -
formatter
Get an optionalFormatterimplementation, instructing jOOQ on how to embed the user type inSQLDataType.JSON,SQLDataType.JSONB, andSQLDataType.XMLcontexts.This may be necessary when embedding user-defined types in
DSL.multiset(TableLike)queries, when the user-defined type serialises differently in aNestedCollectionEmulation.JSONBemulation than standalone, for example.A
nullformatter will have no effect on generated SQL.Note: while it is possible to implement a custom formatter for array types, it is usually simpler to just delegate to the
arrayComponentBinding()for this, and let jOOQ's internals handle the intricate mapping of generic arrays to XML or JSON. -
arrayBinding
Get an array binding for this binding, ornullif no such array binding is available. -
arrayComponentBinding
Get an array component binding for this binding, ornullif this binding isn't for an array type.Implementations must ensure that the resulting type is
Binding<X, Y>, if this Binding's type variables resolve toT = X[]andU = Y[], as this cannot be enforced by the Java compiler. -
sql
Generate SQL code for the bind variable.Implementations should generate SQL code onto
BindingSQLContext.render(), given the context's bind variable located atBindingSQLContext.value(). Examples of such SQL code are:"?": Default implementations can simply generate a question mark.
"123": Implementations may choose to inline bind variables to influence execution plan generation.
Context.paramType()contains information whether inlined bind variables are expected in the current context.
"CAST(? AS DATE)": Cast a database to a more specific type. This can be useful in databases like Oracle, which map bothDATEandTIMESTAMPSQL types toTimestamp.
Context.castMode()may contain some hints about whether casting is suggested in the current context.
"?::json": Vendor-specific bind variables can be supported, e.g.SQLDialect.POSTGRES's JSON data type.
Implementations must provide consistent behaviour between
sql(BindingSQLContext)andset(BindingSetStatementContext), i.e. when bind variables are inlined, then they must not be bound to thePreparedStatementinset(BindingSetStatementContext)- Parameters:
ctx- The context object containing all argument objects.- Throws:
SQLException- Implementations are allowed to pass on allSQLExceptions to the caller to be wrapped inDataAccessExceptions.
-
register
Register aCallableStatement'sOUTparameter.- Parameters:
ctx- The context object containing all argument objects.- Throws:
SQLException- Implementations are allowed to pass on allSQLExceptions to the caller to be wrapped inDataAccessExceptions.
-
set
Set aPreparedStatement'sINparameter.- Parameters:
ctx- The context object containing all argument objects.- Throws:
SQLException- Implementations are allowed to pass on allSQLExceptions to the caller to be wrapped inDataAccessExceptions.
-
set
Set aSQLOutput'sINparameter.- Parameters:
ctx- The context object containing all argument objects.- Throws:
SQLException- Implementations are allowed to pass on allSQLExceptions to the caller to be wrapped inDataAccessExceptions.
-
get
Get aResultSet'sOUTvalue.Implementations are expected to produce a value by calling
BindingGetResultSetContext.value(Object), passing the resulting value to the method.- Parameters:
ctx- The context object containing all argument objects.- Throws:
SQLException- Implementations are allowed to pass on allSQLExceptions to the caller to be wrapped inDataAccessExceptions.
-
get
Get aCallableStatement'sOUTvalue.Implementations are expected to produce a value by calling
BindingGetStatementContext.value(Object), passing the resulting value to the method.- Parameters:
ctx- The context object containing all argument objects.- Throws:
SQLException- Implementations are allowed to pass on allSQLExceptions to the caller to be wrapped inDataAccessExceptions.
-
get
Get aSQLInput'sOUTvalue.Implementations are expected to produce a value by calling
BindingGetSQLInputContext.value(Object), passing the resulting value to the method.- Parameters:
ctx- The context object containing all argument objects.- Throws:
SQLException- Implementations are allowed to pass on allSQLExceptions to the caller to be wrapped inDataAccessExceptions.
-
of
@NotNull static <T,U> @NotNull Binding<T,U> of(Converter<T, U> converter, Consumer<? super BindingSQLContext<U>> sqlContext, Consumer<? super BindingGetResultSetContext<U>> getResultSetContext, Consumer<? super BindingSetStatementContext<U>> setStatementContext) Construct a binding from functions. -
of
@NotNull static <T,U> @NotNull Binding<T,U> of(Converter<T, U> converter, Consumer<? super BindingSQLContext<U>> sqlContext, Consumer<? super BindingGetResultSetContext<U>> getResultSetContext, Consumer<? super BindingSetStatementContext<U>> setStatementContext, Consumer<? super BindingRegisterContext<U>> registerContext, Consumer<? super BindingGetStatementContext<U>> getStatementContext) Construct a binding from functions. -
of
@NotNull static <T,U> @NotNull Binding<T,U> of(Converter<T, U> converter, Consumer<? super BindingSQLContext<U>> sqlContext, Consumer<? super BindingGetResultSetContext<U>> getResultSetContext, Consumer<? super BindingSetStatementContext<U>> setStatementContext, Consumer<? super BindingRegisterContext<U>> registerContext, Consumer<? super BindingGetStatementContext<U>> getStatementContext, Consumer<? super BindingGetSQLInputContext<U>> getSqlInputContext, Consumer<? super BindingSetSQLOutputContext<U>> setSqlOutputContext) Construct a binding from functions.
-