简体   繁体   English

jooq和java 8流SQL生成

[英]jooq and java 8 streams SQL generation

In below jooq snippet that I found from online material , there is a transition from "jooq ends here" to "stream starts" 在我从在线资料中找到的下面的jooq片段中,有一个从“jooq ends here”到“stream starts”的过渡

Does this mean SQL query generation happens till fetch() ? 这是否意味着SQL查询生成发生在fetch()之前? And later on afterwards stream() starts, everything is in memory inside java process 后来在stream()启动之后,一切都在java进程的内存中

Or are java 8 streams like active record DSL and whole snippet is converted into SQL queries including stream() part ? 或者java 8流如活动记录DSL和整个片段是否转换为SQL查询,包括stream()部分?

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 这是因为我已经看到了在许多在线样本的流内部完成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() ? 这是否意味着SQL查询生成发生在fetch()之前? And later on afterwards stream() starts, everything is in memory inside java process 后来在stream()启动之后,一切都在java进程的内存中

Yes

Or are java 8 streams like active record DSL and whole snippet is converted into SQL queries including stream() part ? 或者java 8流如活动记录DSL和整个片段是否转换为SQL查询,包括stream()部分?

No 没有

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 这是因为我已经看到了在许多在线样本的流内部完成sortBy / groupingBy的例子,当它们也可以在SQL中完成时

You probably mean the JINQ library . 你可能意味着JINQ库

While jOOQ gives you access to the Java 8 Stream API (because every jOOQ result is, in fact, a List , and you can call List.stream() on any list), it does not translate stream operations to SQL. 虽然jOOQ允许您访问Java 8 Stream API(因为每个jOOQ结果实际上是一个List ,并且您可以在任何列表上调用List.stream() ),但它不会将流操作转换为SQL。 While libraries like JINQ prove that this is possible, in principle, the Stream API offers a lot less functionality than the SQL language, which does not fit jOOQ's vision of giving users full SQL language access. 虽然像JINQ这样的库证明了这是可能的,但原则上, Stream API提供功能远远少于SQL语言,这不符合jOOQ为用户提供完整SQL语言访问的愿景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM