这个fetch能不能多做"elegantly"?
Can this fetch be done more "elegantly"?
我正在使用 jOOQ,我不太确定我是否按照我应该的方式使用它。我想知道我下面的代码是否可以写得更优雅。通过“优雅地”我的意思是基本上改变关于列的任何东西是非常烦人的。如果我添加、删除或更改列的顺序,我将不得不更改所有结果和 Table<>
对象等。我很确定我将来会遇到像下面这样的请求几次因此我想知道我是否可以简化这个。
此外,我注意到 on.fetch()
和类似的结果是 Result<Record6<..>>
。我看了一下那些 RecordX
类。其中有 22 个。我不认为我永远需要它,但如果我想阅读 23 列怎么办?
public List<StoreItemDTO> getItems(Long storeId) {
// Get all item_ids for the store
SelectConditionStep<Record1<Long>> where = this.ctx
.select(STORE_ITEM.ID)
.from(STORE_ITEM)
.where(STORE_ITEM.STORE_ID.eq(storeId));
// Get all store_item_details (all languages) according to the fetched item_ids
Table<Record5<Long, Long, String, String, Long>> storeItemDetails = this.ctx
.select(
STORE_ITEM_DETAILS.ID,
STORE_ITEM_DETAILS.STORE_ITEM_ID,
STORE_ITEM_DETAILS.NAME,
STORE_ITEM_DETAILS.DESCRIPTION,
STORE_ITEM_DETAILS.STORE_LANGUAGE_ID
)
.from(STORE_ITEM_DETAILS)
.where(STORE_ITEM_DETAILS.STORE_ITEM_ID.in(where))
.asTable("storeItemDetails");
// Join the result and get the items for the store in all languages
SelectOnConditionStep<Record6<Long, Long, Long, String, String, Long>> on = this.ctx
.select(
STORE_ITEM.ID,
STORE_ITEM.STORE_ID,
storeItemDetails.field(STORE_ITEM_DETAILS.ID),
storeItemDetails.field(STORE_ITEM_DETAILS.NAME),
storeItemDetails.field(STORE_ITEM_DETAILS.DESCRIPTION),
storeItemDetails.field(STORE_ITEM_DETAILS.STORE_LANGUAGE_ID)
)
.from(STORE_ITEM)
.join(storeItemDetails)
.on(storeItemDetails.field(STORE_ITEM_DETAILS.STORE_ITEM_ID).eq(STORE_ITEM.ID));
Result<Record6<Long, Long, Long, String, String, Long>> fetch = on.fetch();
// ..
return null;
}
如果不需要类型安全,请不要使用它。例如。以下工作也很好
// Get all item_ids for the store
SelectConditionStep<?> where = this.ctx...
// Get all store_item_details (all languages) according to the fetched item_ids
Table<?> storeItemDetails = this.ctx...
// Join the result and get the items for the store in all languages
SelectOnConditionStep<Record6<Long, Long, Long, String, String, Long>> on = this.ctx...
当编译器可以执行类型推断时,扩展类型安全对编译器来说非常好,例如当你写作时:
UNION
s
IN
谓词
- 当您将结果提取到 lambda 表达式中时
- 等等
您可能会写下所有这些类型,因为您正在使用 IDE auto-completion,并且您的 IDE 首先提出最具体的建议。但是使用通配符也同样有效,在上面的例子中是这样的,例如:
Result<?> fetch = this.ctx
// ..
.fetch();
for(Record record : fetch) {
String value = record.getValue(STORE_ITEM_DETAILS.NAME);
System.out.println(value);
}
I don't think that I'll ever need that but what if I wanted to read 23 columns?
没有重大变化。另见手册:
http://www.jooq.org/doc/latest/manual/sql-execution/fetching/record-n
您仍然可以选择是否使用显式记录类型(记录上没有学位):
Result<Record> result = query.fetch();
...或者是否使用通配符:
Result<?> result = query.fetch();
我正在使用 jOOQ,我不太确定我是否按照我应该的方式使用它。我想知道我下面的代码是否可以写得更优雅。通过“优雅地”我的意思是基本上改变关于列的任何东西是非常烦人的。如果我添加、删除或更改列的顺序,我将不得不更改所有结果和 Table<>
对象等。我很确定我将来会遇到像下面这样的请求几次因此我想知道我是否可以简化这个。
此外,我注意到 on.fetch()
和类似的结果是 Result<Record6<..>>
。我看了一下那些 RecordX
类。其中有 22 个。我不认为我永远需要它,但如果我想阅读 23 列怎么办?
public List<StoreItemDTO> getItems(Long storeId) {
// Get all item_ids for the store
SelectConditionStep<Record1<Long>> where = this.ctx
.select(STORE_ITEM.ID)
.from(STORE_ITEM)
.where(STORE_ITEM.STORE_ID.eq(storeId));
// Get all store_item_details (all languages) according to the fetched item_ids
Table<Record5<Long, Long, String, String, Long>> storeItemDetails = this.ctx
.select(
STORE_ITEM_DETAILS.ID,
STORE_ITEM_DETAILS.STORE_ITEM_ID,
STORE_ITEM_DETAILS.NAME,
STORE_ITEM_DETAILS.DESCRIPTION,
STORE_ITEM_DETAILS.STORE_LANGUAGE_ID
)
.from(STORE_ITEM_DETAILS)
.where(STORE_ITEM_DETAILS.STORE_ITEM_ID.in(where))
.asTable("storeItemDetails");
// Join the result and get the items for the store in all languages
SelectOnConditionStep<Record6<Long, Long, Long, String, String, Long>> on = this.ctx
.select(
STORE_ITEM.ID,
STORE_ITEM.STORE_ID,
storeItemDetails.field(STORE_ITEM_DETAILS.ID),
storeItemDetails.field(STORE_ITEM_DETAILS.NAME),
storeItemDetails.field(STORE_ITEM_DETAILS.DESCRIPTION),
storeItemDetails.field(STORE_ITEM_DETAILS.STORE_LANGUAGE_ID)
)
.from(STORE_ITEM)
.join(storeItemDetails)
.on(storeItemDetails.field(STORE_ITEM_DETAILS.STORE_ITEM_ID).eq(STORE_ITEM.ID));
Result<Record6<Long, Long, Long, String, String, Long>> fetch = on.fetch();
// ..
return null;
}
如果不需要类型安全,请不要使用它。例如。以下工作也很好
// Get all item_ids for the store
SelectConditionStep<?> where = this.ctx...
// Get all store_item_details (all languages) according to the fetched item_ids
Table<?> storeItemDetails = this.ctx...
// Join the result and get the items for the store in all languages
SelectOnConditionStep<Record6<Long, Long, Long, String, String, Long>> on = this.ctx...
当编译器可以执行类型推断时,扩展类型安全对编译器来说非常好,例如当你写作时:
UNION
sIN
谓词- 当您将结果提取到 lambda 表达式中时
- 等等
您可能会写下所有这些类型,因为您正在使用 IDE auto-completion,并且您的 IDE 首先提出最具体的建议。但是使用通配符也同样有效,在上面的例子中是这样的,例如:
Result<?> fetch = this.ctx
// ..
.fetch();
for(Record record : fetch) {
String value = record.getValue(STORE_ITEM_DETAILS.NAME);
System.out.println(value);
}
I don't think that I'll ever need that but what if I wanted to read 23 columns?
没有重大变化。另见手册: http://www.jooq.org/doc/latest/manual/sql-execution/fetching/record-n
您仍然可以选择是否使用显式记录类型(记录上没有学位):
Result<Record> result = query.fetch();
...或者是否使用通配符:
Result<?> result = query.fetch();