Once again, I've come up with something which works but may not be the 
optimal usage of JOOQ (*u* is the USER table object, *user* is the POJO):

      final Record rec = sql.newRecord(u);
      rec.from(user);
      final Map<Field<?>, Object> toUpdate = new HashMap<>();
      for (Field<?> f : rec.fields()) {
          if (f == u.USERNAME) continue;
          final Object v = rec.getValue(f);
          if (v != null) toUpdate.put(f, v);
      }
      
sql.update(u).set(toUpdate).where(u.USERNAME.eq(user.getUsername())).execute();


On Friday, October 18, 2013 11:10:25 AM UTC+2, Marko Topolnik wrote:
>
> I've come across a related problem to the above. Now I need to to a 
> standard UPDATE...SET...WHERE, but I've got more than one type of 
> change--for example:
>
>       (passwordChange?
>             sql.update(u).set(u.PASSWORD, user.getPassword())
>                          .set(u.SALT, user.getSalt())
>           : sql.update(u).set(u.FIRST_NAME, user.getFirstName())
>                          .set(u.LAST_NAME, user.getLastName())
>                          .set(u.EMAIL, user.getEmail())
>       ).where(u.USERNAME.eq(user.getUsername())).execute();
>
> As you can see, there's still a bit of boilerplate there (in the 
> *set*invocations). I would like to use a similar approach as above with 
> records 
> (automatically select any non-null value from the *user* POJO as a field 
> to be updated), but I don't know how to incorporate the WHERE part when 
> storing a record (*username* is not the primary key, but it's a NOT NULL 
> UNIQUE column). I'm not in favor of an extra fetch from the DB to get the 
> primary key when a direct UPDATE is enough.
>
> --Marko
>

-- 
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/groups/opt_out.

Reply via email to