Domanda

I recently encountered the following problem with buiding queries in jooq (version 3.1.0):

I want to build delete statement with order and limit constraints. So, my aim is to build something like this: DELETE FROM table ORDER BY field DESC LIMIT 1 (this is MySql syntax)

But i haven't found nesessary methods in result delete query object:

 DSLContext context = createContext();
 DeleteWhereStep delete = context.delete(createTable(table));
 DeleteConditionStep whereStep = delete.where(condition);
 whereStep.orderBy(...)//and no such method here

There are all nesessary methods in select statements and none for delete. Is it possible to set order and limit for delete request in jooq?

È stato utile?

Soluzione

As of jOOQ 3.2, these sorts of extensions are currently not implemented yet. Chances are, that #203 could be implemented in jOOQ 3.3, though.

In the mean time, you have two options:

Resort to plain SQL

i.e. write something like:

context.execute("DELETE FROM {0} ORDER BY {1} DESC LIMIT 1",
    createTable(table),
    field);

Manually transform your SQL statement into something equivalent

I suspect that the ORDER BY .. LIMIT extension to the MySQL DELETE statement is just sugar for:

DELETE FROM table t
WHERE t.id IN (
  SELECT id FROM table
  ORDER BY field LIMIT 1
)

Or with jOOQ:

context.delete(TABLE)
       .where(TABLE.ID.in(
           select(TABLE.ID)
          .from(TABLE)
          .orderBy(TABLE.FIELD)
          .limit(1)
       ))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top