JOOQ selectQuery for Record5

时间:2015-09-08 08:48:11

标签: java sql jooq

我是JOOQ的新手。

我需要使用SelectQuery并返回Result,但返回类型是Record。是否可以在Record5中使用selectQuery?还是有另一种解决方案吗?

    SelectQuery<Record> query = db.selectQuery();
    query.addSelect(WARE.NAME, RECEIPTITEM.SALEPRICE, RECEIPTITEM.QUANTITY, RECEIPT.TSCCREATED, WARE.WARECATEGORYID);
    query.addFrom(RECEIPT
            .join(RECEIPTITEM).on(RECEIPT.ID.eq(RECEIPTITEM.RECEIPTID))
            .join(OUTLET).on(OUTLET.ID.eq(RECEIPT.OUTLETID))
            .join(WARE).on(WARE.ID.eq(RECEIPTITEM.WAREID)));
    query.addConditions(RECEIPT.TSCCREATED.between(start, end)
            .and(OUTLET.OWNERID.eq(ownerId)));
    if (outletId != null) {
        query.addConditions(RECEIPT.OUTLETID.eq(outletId));
    }
    if (merchantId != null) {
        query.addConditions(RECEIPT.MERCHID.eq(merchantId));
    }

1 个答案:

答案 0 :(得分:1)

您无法使用jOOQ创建SelectQuery<Record5<...>>类型。

原因是SelectQuerywhich is part of the model API)允许您通过SelectQuery.addSelect()(您自己完成)向其添加新列。每次此类调用都会使Record5<...>类型无效。

但是,如果动态部分只是附加条件,则不必使用模型API来构造动态SQL。您可以像这样重写您的查询:

Condition condition = RECEIPT.TSCCREATED.between(start, end)
    .and(OUTLET.OWNERID.eq(ownerId));

if (outletId != null) {
    condition = condition.and(RECEIPT.OUTLETID.eq(outletId));
}
if (merchantId != null) {
    condition = condition.and(RECEIPT.MERCHID.eq(merchantId));
}

Select<Record5<String, BigDecimal, Integer, Timestamp, Long>> select =
db.select(
       WARE.NAME, 
       RECEIPTITEM.SALEPRICE, 
       RECEIPTITEM.QUANTITY, 
       RECEIPT.TSCCREATED, 
       WARE.WARECATEGORYID)
  .from(RECEIPT
      .join(RECEIPTITEM).on(RECEIPT.ID.eq(RECEIPTITEM.RECEIPTID))
      .join(OUTLET).on(OUTLET.ID.eq(RECEIPT.OUTLETID))
      .join(WARE).on(WARE.ID.eq(RECEIPTITEM.WAREID)))
  .where(condition);