Hello Christopher,

2015-06-16 0:20 GMT+02:00 <[email protected]>:

> I'm using JSONB for SQL generation, not execution. I need to create
> queries using the PostgreSQL 9.4 JSONB data type, which the jooq DSL
> doesn't appear to support, currently. For example, I need to be able to
> write expressions such as
>
>     cast(stringData, JSONB)
>
> and have it generate SQL of the form
>
>     cast(? as jsonb)
>
> I've got it working the *wrong* way, which is to declare a new constant in
> my project as:
>
>     public static final DataType<Object> JSONB = new
> DefaultDataType<>(POSTGRES_9_4, SQLDataType.OTHER, "jsonb");
>
> but the javadoc for DefaultDataType pretty clearly says I shouldn't do
> that. It's one redemption is that it works.
>

Good thing that the warning is effective :)

While this approach does work indeed, we don't want to make any API
guarantees for it. Unfortunately in Java, there is no "module visibility",
i.e. a visibility between package visibility and public, which would have
allowed jOOQ internals to call the above constructor, but not jOOQ clients.
This is why the constructor is public with a disclaimer as we might change
it or its behaviour in the future.

Currently, jOOQ's data type system is initialised statically like in the
above way. Each DefaultDataType registers itself in an internal static
registry. This is not good, as it can cause initialisation deadlocks:
https://github.com/jOOQ/jOOQ/issues/3777

Given that generated code depends on these data types, static
initialisation was the best way we've found so far. Should we find a better
technique, it may well be that your code won't work anymore.

Whether you can live with this risk is up to you.


> However, I can't seem to figure out what the "right" way to do this is.
> Any pointers?
>

The officially supported way would be to use an org.jooq.Binding and
transform the SQLDataType.OTHER (or SQLDataType.VARCHAR) to it.

SQLDataType.OTHER.asConvertedDataType(new JSONBBinding());


with

class JSONBBinding implements Binding<Object, Object> {
}


You would then move your casts into this JSONBBinding's sql() method.

See also:
http://www.jooq.org/doc/latest/manual/code-generation/custom-data-type-bindings

Hope this helps,
Lukas

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to