Hi, Martin!

Please forgive me for unchecked code. Can I take second chance?
So, you want to perform
*create.insertInto(AUTHOR,
        AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
      .values(100, "Hermann", "Hesse")
      .values(101, "Alfred", "Döblin");
*
but* *arbitrary-length collection of objects
Java-method that will be evaluated exactly as above is:
*public void saveAuthors(List<Author> authors) {
        InsertValuesStep3<AuthorRecord, Integer, String, String> insertInto
= dsl.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME);
        for (Author author : authors) {
            insertInto = insertInto.values(author.id(), author.firstName(),
author.lastName());
        }
        insertInto.execute();
    }*

Scala-code will be :
*def saveAuthors(authors: Seq[Author]) {
    authors.foldLeft(dsl.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME,
AUTHOR.LAST_NAME)) {
      case (insert, Author(id, firstName, lastName)) => insert.values(id,
firstName, lastName)
    } execute
*
*}*
But, more natural java code for this task is:
*public void saveAuthors(List<Author> authors) {
        InsertValuesStep3<AuthorRecord, Integer, String, String> insertInto
= dsl.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME);
        for (Author author : authors) {
            insertInto.values(author.id(), author.firstName(),
author.lastName());
        }
        insertInto.execute();
}    *
This leads to scala :
def saveAuthors(authors: Seq[Author]) {
    val insert = dsl.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME,
AUTHOR.LAST_NAME)
    authors.foreach(author => insert.values(author.id, author.firstName,
author.lastName))
    insert execute
}

But jooq has also Record API. May be it will be more appropriative for you
task?
Java code:
public void saveAuthors(List<Author> authors) {
        List<AuthorRecord> inserts = new ArrayList<AuthorRecord>();
        for (Author author : authors) {
            AuthorRecord record = new AuthorRecord();
            record.from(author);
            inserts.add(record);
        }
        dsl.batchInsert(inserts).execute();
}
Scala:
  def asRecord(author: Author): AuthorRecord = {
    val record = new AuthorRecord()
    record.from(author)
    record
  }

  def batchSaveAuthors(authors: Seq[Author]) {
    dsl.batchInsert(ListBuffer(authors.map{asRecord(_)}: _*)).execute();
  }
I think that asRecord can be done as implicit, but I am newbie in Scala and
can't figure how to do it.


On Mon, May 20, 2013 at 8:50 PM, Christopher Martin <[email protected]>wrote:

> Whoops, that fold actually needs to be a foldLeft.
>
>
>
> On Monday, May 20, 2013 1:45:54 PM UTC-4, Christopher Martin wrote:
>>
>> This is getting ugly.
>>
>> import java.lang.Integer
>> type T = InsertValuesStep2[**AuthorRecord, Integer, String, String]
>> authors.fold(create.**insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME,
>> AUTHOR.LAST_NAME)) {
>>   (q: T, a: Author) => q.values(a.id: Integer, a.firstName, a.lastName)
>> }
>>
>> And I'd really like to use the "set" style methods, in which case working
>> with the DSL types becomes a huge mess.
>>
>>
>>
>> On Monday, May 20, 2013 2:36:39 AM UTC-4, Danko Sedin wrote:
>>>
>>> Hi!
>>>
>>> You could try :
>>> ((authors.fold(create.**insertInto(AUTHOR,AUTHOR.ID, AUTHOR.FIRST_NAME,
>>> AUTHOR.LAST_NAME)))(**insertValues, value=> insertValues.values(value.id,
>>> value.firstName, value.lastName))).execute
>>>
>>>
>>>
>>> On Mon, May 20, 2013 at 8:24 AM, Christopher Martin 
>>> <[email protected]>wrote:
>>>
>>>> The docs give a lot of examples for doing INSERTs with some fixed
>>>> number of tuples, such as:
>>>>
>>>> create.insertInto(AUTHOR,
>>>>>         AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
>>>>>       .values(100, "Hermann", "Hesse")
>>>>>       .values(101, "Alfred", "Döblin");
>>>>
>>>>
>>>> But what about the case where I have some arbitrary-length collection
>>>> of objects? Say for instance I'm using a class
>>>>
>>>> case class Author(id: Int, firstName: String, lastName: String)
>>>>
>>>>
>>>> And I now have a Seq[Author] and want to insert them all in one query.
>>>> How would you go about writing that?
>>>>
>>>>  --
>>>> 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<https://groups.google.com/groups/opt_out>
>>>> .
>>>>
>>>>
>>>>
>>>
>>>  --
> 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.
>
>
>

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