Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExecuteContext.batchQueries() does not return all queries when using batchStore #9507

Closed
youdang opened this issue Nov 6, 2019 · 1 comment

Comments

@youdang
Copy link

youdang commented Nov 6, 2019

Your question:

I developed a customized ExecuteListener in order to print all SQLs, like below:

@Slf4j
public class JooqExecuteListener extends DefaultExecuteListener {
    @Override
    public void executeEnd(ExecuteContext ctx) {
        DSLContext dslContext = DSL.using(
            ctx.dialect(),
            SettingsTools.clone(ctx.settings()).withRenderFormatted(false)
        );
        Query[] queries = ctx.batchQueries();
        log.info("{}", formatted(queries));
    }

    private String formatted(DSLContext dslContext, Query[] queries) {
        return Arrays.stream(queries)
            .map(dslContext::renderInlined)
            .collect(Collectors.joining("\",\"", "[\"", "\"]"));
    }
}

Things go right in most cases, instead of batch execution:

// columns: id, name
...
// r1: id=1, name="foo"
FooRecord r1 = dsl.newRecord(FOO_TABLE);
// r2: id=2, name="bar"
FooRecord r2 = dsl.newRecord(FOO_TABLE);

dsl.batchStore(r1, r2).execute();

The log printed on the console is:

["INSERT INTO foo VALUES(1, 'foo')"]

r2 is not printed, What I expect is something like:

["INSERT INTO foo VALUES(1, 'foo'), (2, 'bar')"]

or maybe like this:

["INSERT INTO foo VALUES(1, 'foo')", "INSERT INTO foo VALUES(2, 'bar')"]

anyone can help me?

Versions:

  • jOOQ: 3.12.1
  • Java: 1.8
  • Database (include vendor): MySQL 5.7.27
  • OS: macOS Catalina
  • JDBC Driver (include name if inofficial driver): com.mysql.driver.Driver(mysql-connector-java: 5.1.46)
@lukaseder
Copy link
Member

lukaseder commented Jul 7, 2020

I'm sorry this was left unanswered for so long.

In jOOQ, we distinguish between "batching" (i.e. using the JDBC batch API methods), and creating "bulk statements" (e.g. adding several rows to single INSERT statements). The latter cannot be done with the batchStore() method. There's a pending feature request to add a bulkStore() method (and similar methods): #5887.

In the mean time, you can add records to INSERT statements like this:

insertInto(FOO)
  .set(record1).newRecord()
  .set(record2).newRecord()
  .set(record3).execute();

I hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants