jooq 和 java 8 流 SQL 代
jooq and java 8 streams SQL generation
在下面我从 online material 找到的 jooq 片段中,有一个从 "jooq ends here" 到 "stream starts"
的过渡
这是否意味着 SQL 查询生成发生在 fetch() 之前?
之后 stream() 启动,所有内容都在内存中 java 进程
或者 java 8 个流,如活动记录 DSL 和整个片段被转换成 SQL 查询,包括 stream() 部分?
这是因为我在许多在线示例中看到了在流内部完成 sortBy / groupingBy 的示例,而这些示例也可以在 SQL 中完成
DSL.using(c)
.select(
COLUMNS.TABLE_NAME,
COLUMNS.COLUMN_NAME,
COLUMNS.TYPE_NAME
)
.from(COLUMNS)
.orderBy(
COLUMNS.TABLE_CATALOG,
COLUMNS.TABLE_SCHEMA,
COLUMNS.TABLE_NAME,
COLUMNS.ORDINAL_POSITION
)
.fetch() // jOOQ ends here
.stream() // Streams start here
.collect(groupingBy(
r -> r.getValue(COLUMNS.TABLE_NAME),
LinkedHashMap::new,
mapping(
r -> new Column(
r.getValue(COLUMNS.COLUMN_NAME),
r.getValue(COLUMNS.TYPE_NAME)
),
toList()
)
))
.forEach(
(table, columns) -> {
// Just emit a CREATE TABLE statement
System.out.println(
"CREATE TABLE " + table + " (");
// Map each "Column" type into a String
// containing the column specification,
// and join them using comma and
// newline. Done!
System.out.println(
columns.stream()
.map(col -> " " + col.name +
" " + col.type)
.collect(Collectors.joining(",\n"))
);
System.out.println(");");
}
);
Does this mean SQL query generation happens till fetch() ? And later on afterwards stream() starts, everything is in memory inside java process
是
Or are java 8 streams like active record DSL and whole snippet is converted into SQL queries including stream() part ?
没有
This is because I have seen examples where sortBy / groupingBy are being done inside streams in many online samples when they can be done in SQL as well
你的意思可能是 JINQ library.
虽然 jOOQ 允许您访问 Java 8 Stream
API(因为每个 jOOQ 结果实际上都是任何列表中的 List
, and you can call List.stream()
),它不会将流操作转换为 SQL。虽然像 JINQ 这样的库证明这是可能的,但原则上,Stream
API 提供的功能比 SQL 语言少 很多 不符合 jOOQ 为用户提供完整 SQL 语言访问权限的愿景。
在下面我从 online material 找到的 jooq 片段中,有一个从 "jooq ends here" 到 "stream starts"
的过渡这是否意味着 SQL 查询生成发生在 fetch() 之前? 之后 stream() 启动,所有内容都在内存中 java 进程
或者 java 8 个流,如活动记录 DSL 和整个片段被转换成 SQL 查询,包括 stream() 部分?
这是因为我在许多在线示例中看到了在流内部完成 sortBy / groupingBy 的示例,而这些示例也可以在 SQL 中完成
DSL.using(c)
.select(
COLUMNS.TABLE_NAME,
COLUMNS.COLUMN_NAME,
COLUMNS.TYPE_NAME
)
.from(COLUMNS)
.orderBy(
COLUMNS.TABLE_CATALOG,
COLUMNS.TABLE_SCHEMA,
COLUMNS.TABLE_NAME,
COLUMNS.ORDINAL_POSITION
)
.fetch() // jOOQ ends here
.stream() // Streams start here
.collect(groupingBy(
r -> r.getValue(COLUMNS.TABLE_NAME),
LinkedHashMap::new,
mapping(
r -> new Column(
r.getValue(COLUMNS.COLUMN_NAME),
r.getValue(COLUMNS.TYPE_NAME)
),
toList()
)
))
.forEach(
(table, columns) -> {
// Just emit a CREATE TABLE statement
System.out.println(
"CREATE TABLE " + table + " (");
// Map each "Column" type into a String
// containing the column specification,
// and join them using comma and
// newline. Done!
System.out.println(
columns.stream()
.map(col -> " " + col.name +
" " + col.type)
.collect(Collectors.joining(",\n"))
);
System.out.println(");");
}
);
Does this mean SQL query generation happens till fetch() ? And later on afterwards stream() starts, everything is in memory inside java process
是
Or are java 8 streams like active record DSL and whole snippet is converted into SQL queries including stream() part ?
没有
This is because I have seen examples where sortBy / groupingBy are being done inside streams in many online samples when they can be done in SQL as well
你的意思可能是 JINQ library.
虽然 jOOQ 允许您访问 Java 8 Stream
API(因为每个 jOOQ 结果实际上都是任何列表中的 List
, and you can call List.stream()
),它不会将流操作转换为 SQL。虽然像 JINQ 这样的库证明这是可能的,但原则上,Stream
API 提供的功能比 SQL 语言少 很多 不符合 jOOQ 为用户提供完整 SQL 语言访问权限的愿景。