简体   繁体   中英

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"

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

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

Yes

Or are java 8 streams like active record DSL and whole snippet is converted into SQL queries including stream() part ?

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

You probably mean the JINQ library .

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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