Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(");");
       }
   );
like image 531
fortm Avatar asked Feb 22 '17 08:02

fortm


1 Answers

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.

like image 153
Lukas Eder Avatar answered Oct 06 '22 20:10

Lukas Eder