1

I discovered a strange method in JOOQ api.

Batch batch(Query query, Object[]... bindings);

I am curious how to call it without specifying arguments manually.

public int g(Object[]... args) {
    return args.length;
}

Fails:

assertEquals(2, g(new Object[] { new Object[0], new Object[0] }));
Daniil Iaitskov
  • 5,525
  • 8
  • 39
  • 49
  • 2
    Possible duplicate of [When do you use varargs in Java?](http://stackoverflow.com/questions/766559/when-do-you-use-varargs-in-java) – Anton Balaniuc Mar 10 '17 at 13:14

2 Answers2

1

What does the API mean?

The intent of the API is to provide a set of bind values for a batch statement in a matrix form where each outer array element corresponds to an individual statement in the batch, and each inner array element corresponds to a bind value.

How is it called?

There are several ways. Let's assume this query here:

Query query = DSL.query("INSERT INTO t(a, b) VALUES (?, ?)", null, null);

E.g. using classic matrix creation in Java (the varargs isn't helpful here):

batch(query, new Object[][] {
    { 1, "a" }, // Record 1 to insert
    { 2, "b" }, // Record 2 to insert
    { 3, "c" }  // Record 3 to insert
});

Or, by using individual arrays (this is where varargs may help):

batch(query, 
    new Object[] { 1, "a" }, // Record 1 to insert
    new Object[] { 2, "b" }, // Record 2 to insert
    new Object[] { 3, "c" }  // Record 3 to insert
);

The latter is particularly useful if you have some pre-existing jOOQ record structure, and you want to pass them as bind variables to a batch, e.g. by using Record.intoArray():

batch(query,
    record1.intoArray(),
    record2.intoArray(),
    record3.intoArray()
);
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
0

Your test fails because Java implicitly creates an array for the three dot notation. In your case the method g gets an array with one element and the first element of this array is again an array (your array with two instances of Object).

Hence you can omit the additional array:

assertEquals(2, g(new Object[0], new Object[0]));

So you can use the batch() method by just passing the vararg arguments with an explicit array:

batch(query, new Object(), new Object())
siom
  • 1,610
  • 1
  • 14
  • 21