feat: Add query.setHint() to support sql hint as inline comment in select queries (#3365)

* feat: Add query.setHint() to support sql hint as inline comment in select queries

This is for ORM queries only.

An ORM query like:

    new QCustomer()
      .setHint("FirstRows")
      .select(QCustomer.Alias.id, QCustomer.Alias.name)
      .findList();

Produces SQL that includes the hint as an inline comment like:

  select /*+ FirstRows */ t0.id, t0.name
  from customer t0

* Fix test QOrderTest
This commit is contained in:
Rob Bygrave
2024-03-20 21:48:43 +13:00
committed by GitHub
parent 8b858a073a
commit 540ceab96f
11 changed files with 94 additions and 7 deletions
@@ -912,6 +912,17 @@ public abstract class TQRootBean<T, R> {
return root;
}
/**
* Set a SQL query hint.
* <p>
* This results in an inline comment that immediately follows
* after the select keyword in the form: {@code /*+ hint *\/ }
*/
public R setHint(String hint) {
query.setHint(hint);
return root;
}
/**
* Set the profile location.
* <p>
@@ -79,6 +79,19 @@ class QOrderTest {
DB.delete(customer);
}
@Test
void hint() {
LoggedSql.start();
new QCustomer()
.setHint("FirstRows")
.select(QCustomer.Alias.id, QCustomer.Alias.name)
.findList();
List<String> sql = LoggedSql.stop();
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("select /*+ FirstRows */ /* QOrderTest.hint */ t0.id, t0.name from be_customer t0");
}
@Test
void fetchQueryWithBatch() {
LoggedSql.start();
@@ -159,8 +172,7 @@ class QOrderTest {
@Test
void viaFetchGraph_withNested_fetchQuery() {
DB.getDefault();
DB.cacheManager().clearAll();
LoggedSql.start();
final Order found = new QOrder()
@@ -181,8 +193,7 @@ class QOrderTest {
@Test
void viaFetchGraph_withNested_fetchCache() {
DB.getDefault();
DB.cacheManager().clearAll();
// ensure the customer is loaded in the L2 cache
new QCustomer().id.eq(customer.getId()).findOne();