

People have been using MyBatis for many years precisely for these purposes, or maybe built home grown, velocity template based frameworks to produce dynamic SQL using native SQL strings. jOOQ’s plain SQL templating capabilities are a much underestimated feature set. We’ve written a dynamic SQL query with jOOQ, but without using much type safety (we could still use some type safe, generated schema object references if we wanted to). SELECT 0, count(*), row_number() OVER (ORDER BY 0) AS rn List(field("table_schema"), field("table_type"))) For example, to run a quick and dirty jOOQ query like this on H2 or PostgreSQL, for example:

The main use case is to use plain SQL and also plain SQL templating. There are two main use cases for using text blocks in jOOQ: Plain SQL
Java 13 text blocks code#
Starting with Java 13, when enabling preview features, you can now make use of text blocks, or “multi line strings”, which are very useful for embedding static SQL strings in Java code (and XML, JSON, regular expressions, etc). In such cases, you can still benefit from jOOQ’s many secondary features, including for example its nice integration with the Stream API, export functionality, and much more. Too limited when running very advanced vendor specific SQL, such as Oracle’s MODEL or MATCH_RECOGNIZE clauses.Overkill for some very simple quick and dirty SQL queries.The above example might not look a great deal to some, since the example is very simple but if think when we have a very complex String for example below JSON string, and if we write this JSON as String literal it would not be much readable.Most jOOQ users use the jOOQ DSL API, which provides compile time type safety and an easy way to write dynamic SQL.īut occasionally, this DSL get in the way, because it might be.String query = """ SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB` WHERE `CITY` = 'INDIANAPOLIS' ORDER BY `EMP_ID`, `LAST_NAME` """ We can close the string with three double-quote characters. We can use three double-quote characters ( """) followed by zero or more whitespaces followed by a line terminator.Now with Text Block, we can write the above query in a more readable format and don't need to provide a newline character and don't need to provide an escape character.String query = "SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`\n" + "WHERE `CITY` = 'INDIANAPOLIS'\n" + "ORDER BY `EMP_ID`, `LAST_NAME` \n" We often see SQL strings like below in the DAO layer, which when executed on the database either through the JPA provider or directly with PreparedStatement bypassing JPA.Text Block """ line 1 line 2 line 3 """ This is equivalent to "line 1\n" + "line 2\n" + "line 3\n" The best way to explain would be to consider how we embed a snippet of HTML, XML, SQL, or JSON in a string literal.It was released as preview in java 13 & 14 but from java 15 it is standard feature.

Text Block is created with the goal of - Simplifying the task of writing a java program by making it easy to express strings that span several lines of source code - Enhance the readability of strings that denotes code written in non-java languages such as HTML, SQL, Javascript, etc.In another word, its makes multi-line string literals more readable.A Text Block is a multi-line string literal that avoids the need for most escape sequences.Improving Multi-line String literals readability.
