feature request: additional values() options

7 views
Skip to first unread message

EREZ KATZ

unread,
Apr 6, 2017, 2:39:19 PM4/6/17
to jOOQ User Group
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

Lukas Eder

unread,
Apr 6, 2017, 2:45:58 PM4/6/17
to jooq...@googlegroups.com
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:

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

--
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 jooq-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

EREZ KATZ

unread,
Apr 7, 2017, 2:33:21 AM4/7/17
to jooq...@googlegroups.com, Dekel Yasso
Thank you for the quick reply!
It looks like it is definitely going to help:)
Erez

You received this message because you are subscribed to a topic in the Google Groups "jOOQ User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jooq-user/D637ANGdSqs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jooq-user+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages