Hi Erez,

Thanks for your nice words.

Absolutely. The VALUES() syntax is a bit of a pain in SQL. This is why jOOQ
also supports MySQL's INSERT .. SET syntax (which is similar to that of an
UPDATE statement). There are various options on the InsertSetStep type:
https://www.jooq.org/javadoc/latest/org/jooq/InsertSetStep.html

E.g.

dsl.insertInto(STUFF)
   .set(STUFF.NAME, name)
   .set(STUFF.CREATOR, creator)
   ...
   .execute();


Or you can set a Map or a Record. You cannot specify a POJO because the API
would be rather ambiguous (set(Object)), but you can always create a Record
from a POJO and use that in the SET clause:

dsl.insertInto(STUFF)
   .set(dsl.newRecord(STUFF, pojo))
   .execute();


The DSLContext.newRecord(Table, Object) and the Record.from(Object) methods
are the inverse of the Record.into(Class) method that you've mentioned.

Hope this helps,
Lukas

2017-04-06 20:39 GMT+02:00 EREZ KATZ <[email protected]>:

> Hello,
>
> This is my first post to this group. I am new to jooq and so far I am very
> impressed if not relieved to see how much life is better for anyone doing
> DB work with Java.
>
> Currently, the values() method requires an argument sequence that matches
> the arguments of the columns() call that preceded it.
>
> consider the following code:
>
> public class StuffTable extends TableImpl<Record> {
>     public final TableField<Record, Integer> ID = createField("id",
> SQLDataType.INTEGER.length(30));
>     public final TableField<Record, String> STUFF_NAME = createField(
> "stuff_name", SQLDataType.VARCHAR.length(100));
>     public final TableField<Record, Integer> CREATOR = createField(
> "creator", SQLDataType.VARCHAR.length(100)));
>     public final TableField<Record, Integer> INSPIRATION = createField(
> "inspiration", SQLDataType.VARCHAR.length(100)));
>
>
>     public DonorFundsTable( ) {  super("donor_funds"); }
>
>
> }
> ...
>
>
> static STUFF_TABLE = new StuffTable();
> ...
>
>
> Record record = dslContext.insertInto(STUFF_TABLE)
>                 .columns(STUFF_TABLE.STUFF_NAME, STUFF_TABLE.CREATOR,
> STUFF_TABLE.INSPIRATION)
>                 .values(name, creator,inspiration)
>                 .returning(DONOR_FUNDS_TABLE.ID)
>                 .fetchOne();
>
>
> it is simple with 3 fields to keep the order of the columns and values in
> sync, less so and (much) more error prone if we had 10 or 20 fields.
>
> How about adding a valuesFromMap(...)  or valuesFromPojo(...)
> methods that given a Map object with values that map to the field names,
> or a POJO that would have getStuffName(), getCreator(), getInspiration()
> methods
> so the values could be simply pulled out of the object ?
>
>
> After all, this would mirror the  logic is in
> ResultQuery.fetchInto(Class<? extends E> type))..
>
> Thoughts?
>
> Cordially,
>
>  Erez Katz
>
> --
> 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.
>

-- 
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