Hi Lukas!
I have a problem with the Binding. Probably doing something wrong. Let
assume the table Truth
create table TRUTH (TRUTH char(1) not null);
and the following binding class.
package ch.example.jooq;
import org.jooq.BindingSQLContext;
import org.jooq.RenderContext;
import org.jooq.conf.ParamType;
import org.jooq.impl.AbstractConverter;
import org.jooq.impl.DefaultBinding;
public class BooleanBinding extends DefaultBinding<Boolean, Boolean> {
public BooleanBinding() {
super(new AbstractConverter<Boolean, Boolean>(Boolean.class, Boolean.class)
{
@Override
public Boolean from(Boolean databaseObject) {
return databaseObject;
}
@Override
public Boolean to(Boolean userObject) {
return userObject;
}
});
}
@Override
public void sql(BindingSQLContext<Boolean> ctx) {
RenderContext render = ctx.render();
if (render.paramType() == ParamType.INLINED) {
final Boolean val = ctx.value();
if (val == null) {
render.keyword("null");
} else {
render.sql(val ? "'1'" : "'0'");
}
} else {
super.sql(ctx);
}
}
}
I configured the generator to use the binding and got the following
generated class:
/**
* This class is generated by jOOQ
*/
package ch.examples.tables;
import javax.annotation.Generated;
import org.jooq.Field;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.impl.TableImpl;
.....
/**
* This class is generated by jOOQ.
*/
@Generated(
value = {
"http://www.jooq.org",
"jOOQ version:3.7.0"
},
comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Truth extends TableImpl<TruthRecord> {
private static final long serialVersionUID = -1911463108;
/**
* The reference instance of <code>BE.TRUTH</code>
*/
public static final Truth TRUTH = new Truth();
/**
* The class holding records for this type
*/
@Override
public Class<TruthRecord> getRecordType() {
return TruthRecord.class;
}
/**
* The column <code>BE.TRUTH.TRUTH</code>.
*/
public final TableField<TruthRecord, Boolean> TRUTH_ = createField("TRUTH",
org.jooq.impl.SQLDataType.BOOLEAN, this, "", new BooleanBinding());
/**
* Create a <code>BE.TRUTH</code> table reference
*/
public Truth() {
this("TRUTH", null);
}
/**
* Create an aliased <code>BE.TRUTH</code> table reference
*/
public Truth(String alias) {
this(alias, TRUTH);
}
private Truth(String alias, Table<TruthRecord> aliased) {
this(alias, aliased, null);
}
private Truth(String alias, Table<TruthRecord> aliased, Field<?>[]
parameters) {
super(alias, Be.BE, aliased, parameters, "");
}
/**
* {@inheritDoc}
*/
@Override
public Truth as(String alias) {
return new Truth(alias, this);
}
/**
* Rename this table
*/
public Truth rename(String name) {
return new Truth(name, null);
}
}
When I generate queries, the custom binding is not used:
System.out.println(db.insertInto(TRUTH).select(select(inline(true))).getSQL());
System.out.println();
System.out.println(db.selectFrom(TRUTH).where(TRUTH.TRUTH_).getSQL());
-->
insert into TRUTH (TRUTH)
select 1
from dual
select TRUTH.TRUTH
from TRUTH
where (TRUTH.TRUTH = 1)
What do I do wrong?
cheers. stan.
On Tuesday, December 15, 2015 at 1:20:19 PM UTC+1, Lukas Eder wrote:
>
> Thanks for the feedback.
>
> I'm sorry for the misunderstanding. You obviously don't have to replace
> all of the work in DefaultBinding, only do the binding for what you
> consider a boolean type. You will then apply the binding only to the
> relevant columns in the code generator, e.g. by matching on
> <types>CHAR(1)</types>. It shouldn't be too much work.
>
> In any case, I agree that jOOQ should probably add some convenience
> bindings to prevent people from having to do this work themselves. I have
> created:
> https://github.com/jOOQ/jOOQ/issues/4807
>
> In fact, I was thinking of creating a commercial add-on with lots of
> commonly used bindings in them (also for XML, JSON, etc.). Customers of the
> Pro and Enterprise Editions would get this add-on for free.
>
> Cheers,
> 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.