diff --git a/jOOQ-website/src/main/java/Transform.java b/jOOQ-website/src/main/java/Transform.java
index 789091eb7c..ec5754e13f 100644
--- a/jOOQ-website/src/main/java/Transform.java
+++ b/jOOQ-website/src/main/java/Transform.java
@@ -38,42 +38,42 @@
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
* and Maintenance Agreement for more details: http://www.jooq.org/eula
*/
-import static org.joox.JOOX.$;
-
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PrintStream;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.sax.SAXResult;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-
-import org.apache.commons.lang3.StringUtils;
-import org.apache.fop.apps.FOUserAgent;
-import org.apache.fop.apps.Fop;
-import org.apache.fop.apps.FopFactory;
-import org.apache.fop.apps.MimeConstants;
-import org.jooq.Constants;
-import org.joox.Context;
-import org.joox.Each;
-import org.joox.Filter;
-import org.joox.Match;
+import static org.joox.JOOX.$;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.fop.apps.FOUserAgent;
+import org.apache.fop.apps.Fop;
+import org.apache.fop.apps.FopFactory;
+import org.apache.fop.apps.MimeConstants;
+import org.jooq.Constants;
+import org.joox.Context;
+import org.joox.Each;
+import org.joox.Filter;
+import org.joox.Match;
/**
* XSL transformation utility for HTML pages
diff --git a/jOOQ-website/src/main/resources/manual-2.5.xml b/jOOQ-website/src/main/resources/manual-2.5.xml
index 19f2292ae0..b260421b8f 100644
--- a/jOOQ-website/src/main/resources/manual-2.5.xml
+++ b/jOOQ-website/src/main/resources/manual-2.5.xml
@@ -37,7 +37,7 @@
This manual is divided into six main sections:# Overview
Java and SQL have come a long way. SQL is an "ancient", yet established and well-understood technology. Java is a legacy too, although its platform JVM allows for many new and contemporary languages built on top of it. Yet, after all these years, libraries dealing with the interface between SQL and Java have come and gone, leaving JPA to be a standard that is accepted only with doubts, short of any surviving options. @@ -126,21 +126,21 @@
SQL was never meant to be abstracted. To be confined in the narrow boundaries of heavy mappers, hiding the beauty and simplicity of relational data. SQL was never meant to be object-oriented. SQL was never meant to be anything other than... SQL!
-These chapters contain a quick overview of how to get started with this manual and with jOOQ. While the subsequent chapters contain a lot of reference information, this chapter here just wraps up the essentials.
-This section helps you correctly interpret this manual in the context of jOOQ.
@@ -150,28 +150,21 @@ The following are code blocks: -These are useful to provide examples in code. Often, with jOOQ, it is even more useful to compare SQL code with its corresponding Java/jOOQ code. When this is done, the blocks are aligned side-by-side, with SQL usually being on the left, and Java usually being on the right:
-@@ -179,13 +172,11 @@ create.select()]]>
-@@ -206,16 +197,16 @@ Factory create = new Factory(connection, SQLDialect.ORACLE);]]>
See the manual's section about
For the examples in this manual, the same database will always be referred to. It essentially consists of these entities created using the Oracle dialect
-More entities, types (e.g. UDT's, ARRAY types, ENUM types, etc), stored procedures and packages are introduced for specific examples
-jOOQ has originally been created as a library for complete abstraction of JDBC and all database interaction. Various best practices that are frequently encountered in pre-existing software products are applied to this library. This includes:
@@ -291,23 +282,23 @@ CREATE TABLE book_to_book_store (The following sections explain about various use cases for using jOOQ in your application.
-
This is the most simple of all use cases, allowing for construction of valid SQL for any database. In this use case, you will not use
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -319,23 +310,23 @@ String sql = create.select(fieldByName("BOOK","TITLE"), fieldByName("AUTHOR","FI
In addition to using jOOQ as a
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -347,29 +338,29 @@ String sql = create.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Instead of any tool mentioned in the previous chapters, you can also use jOOQ directly to execute your jOOQ-generated SQL statements. This will add a lot of convenience on top of the previously discussed API for typesafe SQL construction, when you can re-use the information from generated classes to fetch records and custom data types. An example is given here:
-jOOQ doesn't stop here, though! You can execute any SQL with jOOQ. In other words, you can use any other SQL building tool and run the SQL statements with jOOQ. An example is given here:
-
If you wish to use jOOQ as a SQL executor with (or without) code generation, the following sections of the manual will be of interest to you:
@@ -389,17 +380,17 @@ Result
This is probably the most complete use-case for jOOQ: Use all of jOOQ's features. Apart from jOOQ's fluent API for query construction, jOOQ can also help you execute everyday CRUD operations. An example is given here:
-If you wish to use all of jOOQ's features, the following sections of the manual will be of interest to you (including all sub-sections): @@ -423,12 +414,12 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
jOOQ isn't just a library that helps you
If you're a power user of your favourite, feature-rich database, jOOQ will help you access all of your database's vendor-specific features, such as OLAP features, stored procedures, user-defined types, vendor-specific SQL, functions, etc. Examples are given throughout this manual.
-Don't have time to read the full manual? Here are a couple of tutorials that will get you into the most essential parts of jOOQ as quick as possible.
-This manual section is intended for new users, to help them get a running application with jOOQ, quickly.
-
If you haven't already downloaded it, download jOOQ:
https://sourceforge.net/projects/jooq/files/Release/
@@ -476,12 +467,12 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
Alternatively, you can create a Maven dependency:
-
For this example, we'll be using MySQL. If you haven't already downloaded MySQL Connector/J, download it here:
@@ -491,17 +482,17 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
If you don't have a MySQL instance up and running yet, get XAMPP now! XAMPP is a simple installation bundle for Apache, MySQL, PHP and Perl
-We're going to create a database called "guestbook" and a corresponding "posts" table. Connect to MySQL via your command line client and type the following:
-
In this step, we're going to use jOOQ's command line tools to generate classes that map to the Posts table we just created. More detailed information about how to set up the jOOQ code generator can be found here:
Replace the username with whatever user has the appropriate privileges to query the database meta data. You'll also want to look at the other values and replace as necessary. Here are the two interesting properties:
@@ -582,15 +572,15 @@ CREATE TABLE `posts` ( Once you have the JAR files and guestbook.xml in your temp directory, type this (use colons instead of semi-colons on UNIX/Linux systems): -Note the prefix slash before guestbook.xml. Even though it's in our working directory, we need to prepend a slash, as the configuration file is loaded from the classpath. Replace the filenames with your filenames. In this example, jOOQ {jooq-version} is being used. If everything has worked, you should see this in your console output:
-Let's just write a vanilla main class in the project containing the generated classes:
-This is pretty standard code for establishing a MySQL connection.
-Let's add a simple query:
-
First get an instance of
We then use jOOQ's DSL to return an instance of Result. We'll be using this result in the next step.
Factory so we can write a simple SELECT query. We pass an instance of the MySQL connection to Factory. Note that the factory doesn't close the connection. We'll have to do that ourselves.
@@ -713,29 +701,29 @@ Result
After the line where we retrieve the results, let's iterate over the results and print out the data:
-The full program should now look like this:
-
jOOQ has grown to be a comprehensive SQL library. For more information, please consider the manual:
http://www.jooq.org/manual/
@@ -814,37 +801,37 @@ public class Main {
This tutorial is the courtesy of Ikai Lan. See the original source here:
http://ikaisays.com/2011/11/01/getting-started-with-jooq-a-tutorial/
Feel free to contribute a tutorial!
-Feel free to contribute a tutorial!
-Feel free to contribute a tutorial!
-jOOQ has no dependencies on any third-party libraries. This rule has some exceptions:
@@ -859,14 +846,14 @@ public class Main {In order to build jOOQ, please download the sources from https://github.com/jOOQ/jOOQ and use Maven to build it, preferably in Eclipse.
-SQL is a declarative language that is hard to integrate into procedural, object-oriented, functional or any other type of programming languages. jOOQ's philosophy is to give SQL the credit it deserves and integrate SQL itself as an "internal domain specific language" directly into Java.
@@ -876,12 +863,12 @@ public class Main {
This section explains all about the various syntax elements involved with jOOQ's SQL building capabilities. For a complete overview of all syntax elements, please refer to the manual's section about
jOOQ exposes a lot of interfaces and hides most implementation facts from client code. The reasons for this are:
@@ -902,13 +889,13 @@ public class Main {With jOOQ 2.0, static factory methods have been introduced in order to make client code look more like SQL. Ideally, when working with jOOQ, you will simply static import all methods from the Factory class:
-Note, that when working with Eclipse, you could also add the Factory to your favourites. This will allow to access functions even more fluently:
-@@ -923,20 +910,19 @@ Factory.concat(Factory.trim(FIRST_NAME), Factory.trim(LAST_NAME));
As a Configuration object, a Factory can construct
While jOOQ tries to represent the SQL standard as much as possible, many features are vendor-specific to a given database and to its "SQL dialect". jOOQ models this using the
Some parts of the jOOQ API are officially supported only by a given subset of the supported SQL dialects. For instance, the
jOOQ API methods which are not annotated with the
The aforementioned Support annotation does not only designate, which databases natively support a feature. It also indicates that a feature is simulated by jOOQ for some databases lacking this feature. An example of this is the
Nevertheless, the IS DISTINCT FROM predicate is supported in all dialects, as its semantics can be expressed with an equivalent
jOOQ has a historic affinity to Oracle's SQL extensions. If something is supported in Oracle SQL, it has a high probability of making it into the jOOQ API
-
While you can use jOOQ for
If you're in a J2EE or Spring context, however, you may wish to use a
The jOOQ Factory allows for some optional configuration elements to be used by advanced users. The
For example, if you want to indicate to jOOQ, that it should inline all bind variables, and execute static
Subsequent sections of the manual contain some more in-depth explanations about these settings:
@@ -1042,12 +1028,12 @@ Factory create = new Factory(connection, dialect, settings);]]> Please refer to the jOOQ runtime configuration XSD for more details:
You may wish to design your database in a way that you have several instances of your schema. This is useful when you want to cleanly separate data belonging to several customers / organisation units / branches / users and put each of those entities' data in a separate database or schema.
@@ -1067,7 +1053,7 @@ Factory create = new Factory(connection, dialect, settings);]]>
When a user from My Book World logs in, you want them to access the MY_BOOK_WORLD schema using classes generated from DEV. This can be achieved with the
The query executed with a Factory equipped with the above mapping will in fact produce this SQL statement:
-Even if AUTHOR was generated from DEV.
@@ -1093,7 +1079,7 @@ create.selectFrom(AUTHOR).fetch(); Your development database may not be restricted to hold only one DEV schema. You may also have a LOG schema and a MASTER schema. Let's say the MASTER schema is shared among all customers, but each customer has their own LOG schema instance. Then you can enhance your RenderMapping like this (e.g. using an XML configuration file): -Note, you can load the above XML file like this:
-This will map generated classes from DEV to MY_BOOK_WORLD, from LOG to MY_BOOK_WORLD_LOG, but leave the MASTER schema alone. Whenever you want to change your mapping configuration, you will have to create a new Factory @@ -1124,40 +1110,40 @@ create.selectFrom(AUTHOR).fetch(); Another option to switch schema names is to use a default schema for the Factory's underlying Connection. Many RDBMS support a USE or SET SCHEMA command, which you can call like this:
-Queries generated from the above Factory will produce this kind of SQL statement:
-If you wish not to render any schema name at all, use the following Settings property for this:
-Not only schemata can be mapped, but also tables. If you are not the owner of the database your application connects to, you might need to install your schema with some sort of prefix to every table. In our examples, this might mean that you will have to map DEV.AUTHOR to something MY_BOOK_WORLD.MY_APP__AUTHOR, where MY_APP__ is a prefix applied to all of your tables. This can be achieved by creating the following mapping:
-The query executed with a Factory equipped with the above mapping will in fact produce this SQL statement:
-Table mapping and schema mapping can be applied independently, by specifying several MappedSchema entries in the above configuration. jOOQ will process them in order of appearance and map at first match. Note that you can always omit a MappedSchema's output value, in case of which, only the table mapping is applied. If you omit a MappedSchema's input value, the table mapping is applied to all schemata! @@ -1186,20 +1172,20 @@ create.selectFrom(AUTHOR).fetch();
Note that the manual's section about
There are a couple of subclasses for the general Factory. Each SQL dialect has its own dialect-specific factory. For instance, if you're only using the MySQL dialect, you can choose to create a new Factory using any of the following types:
-The advantage of using a dialect-specific Factory lies in the fact, that you have access to more proprietary RDMBS functionality. This may include:
@@ -1210,23 +1196,23 @@ MySQLFactory create = new MySQLFactory(connection);Another type of Factory subclasses are each generated schema's factories. If you generate your schema TEST, then you will have access to a TestFactory. By default, such a schema-specific Factory will not render the schema name.
-
jOOQ currently supports 6 types of SQL statements. All of these statements are constructed from a Factory instance with an optional
jOOQ ships with its own DSL (or Domain Specific Language) that simulates SQL in Java. This means, that you can write SQL statements almost as if Java natively supported it, just like .NET's C# does with LINQ to SQL.
@@ -1234,22 +1220,21 @@ MySQLFactory create = new MySQLFactory(connection); Here is an example to illustrate what that means: -
We'll see how the aliasing works later in the section about
Many other frameworks have similar APIs with similar feature sets. Yet, what makes jOOQ special is its informal
Unlike other, simpler frameworks that use "fluent APIs" or "method chaining", jOOQ's BNF-based interface hierarchy will not allow bad query syntax. The following will not compile, for instance:
-
Historically, jOOQ started out as an object-oriented SQL builder library like any other. This meant that all queries and their syntactic components were modeled as so-called
This query is equivalent to the one shown before using the DSL syntax. In fact, internally, the DSL API constructs precisely this QueryObject. Note, that you can always access the SelectQuery object to switch between DSL and non-DSL APIs:
-
Note, that for historic reasons, the DSL API mixes mutable and immutable behaviour with respect to the internal representation of the
Mutability may be removed in a future version of jOOQ.
-
When you don't just perform
Details about the various clauses of this query will be provided in subsequent sections
@@ -1396,14 +1380,14 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
A very similar, but limited API is available, if you want to select from single physical tables in order to retrieve
As you can see, there is no way to further restrict/project the selected fields. This just selects all known TableFields in the supplied Table, and it also binds <R extends Record> to your Table's associated Record. An example of such a Query would then be:
-The simple SELECT API is limited in the way that it does not support any of these clauses: @@ -1417,41 +1401,39 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
In most parts of this manual, it is assumed that you do not use the simple SELECT API. For more information about the simple SELECT API, see the manual's section about
The SELECT clause lets you project your own record types, referencing table fields, functions, arithmetic expressions, etc. The Factory provides several methods for expressing a SELECT clause:
-Some commonly used projections can be easily created using convenience methods:
-
See more details about functions and expressions in the manual's section about
The SQL FROM clause allows for specifying any number of
Read more about aliasing in the manual's section about
Note, in order to access the DbmsXplan package, you can use the
Read more about dual or dummy tables in the manual's section about
jOOQ supports many different types of standard SQL JOIN operations:
@@ -1545,7 +1523,7 @@ new Factory(SQLDialect.POSTGRES).selectOne().getSQL();]]> All of these JOIN methods can be called on
The two syntaxes will produce the same SQL statement. However, calling "join" on
In case of ambiguity, you can also supply field references for your foreign keys, or the generated foreign key reference to the onKey() method. @@ -1607,136 +1583,129 @@ JOIN BOOK ON BOOK.AUTHOR_ID = AUTHOR.ID]]> Most often, you will provide jOOQ with JOIN conditions in the JOIN .. ON clause. SQL supports a different means of specifying how two tables are to be joined. This is the JOIN .. USING clause. Instead of a condition, you supply a set of fields whose names are common to both tables to the left and right of a JOIN operation. This can be useful when your database schema has a high degree of relational normalisation. An example:
-In schemas with high degrees of normalisation, you may also choose to use NATURAL JOIN, which takes no JOIN arguments as it joins using all fields that are common to the table expressions to the left and to the right of the JOIN operator. An example:
-Oracle SQL ships with a special syntax available for OUTER JOIN clauses. According to the Oracle documentation about partitioned outer joins this can be used to fill gaps for simplified analytical calculations. jOOQ only supports putting the PARTITION BY clause to the right of the OUTER JOIN clause. The following example will create at least one record per AUTHOR and per existing value in BOOK.PUBLISHED_IN, regardless if an AUTHOR has actually published a book in that year.
-
The WHERE clause can be used for JOIN or filter predicates, in order to restrict the data returned by the
The above syntax is convenience provided by jOOQ, allowing you to connect the
You will find more information about creating
The Oracle database knows a very succinct syntax for creating hierarchical queries: the CONNECT BY clause, which is fully supported by jOOQ, including all related functions and pseudo-columns. A more or less formal definition of this clause is given here:
-An example for an iterative query, iterating through values between 1 and 5 is this:
-Here's a more complex example where you can recursively fetch directories in your database, and concatenate them to a path:
-The output might then look like this
-
Some of the supported functions and pseudo-columns are these (available from the
Note that this syntax is also supported in the CUBRID database.
-
GROUP BY can be used to create unique groups of data, to form aggregations, to remove duplicates and for other reasons. It will transform your previously defined
As defined in the SQL standard, when grouping, you may no longer project any columns that are not a formal part of the GROUP BY clause, or
This will return an arbitrary title per author. jOOQ supports this syntax, as jOOQ is not doing any checks internally, about the consistence of tables/fields/functions that you provide it.
@@ -1808,68 +1775,64 @@ GROUP BY AUTHOR_ID]]>
jOOQ supports empty GROUP BY () clauses as well. This will result in
Some databases support the SQL standard grouping functions and some extensions thereof. See the manual's section about
The HAVING clause is commonly used to further restrict data resulting from a previously issued
According to the SQL standard, you may omit the GROUP BY clause and still issue a HAVING clause. This will implicitly GROUP BY (). jOOQ also supports this syntax. The following example selects one record, only if there are at least 4 books in the books table:
-Databases are allowed to return data in any arbitrary order, unless you explicitly declare that order in the ORDER BY clause. In jOOQ, this is straight-forward:
-
Any jOOQ
Note, how one() is used as a convenience short-cut for inline(1) @@ -1897,109 +1859,106 @@ ORDER BY 1 ASC, 2 DESC]]> A few databases support the SQL standard "null ordering" clause in sort specification lists, to define whether NULL values should come first or last in an ordered result.
-
If your database doesn't support this syntax, jOOQ simulates it using a
Using
But writing these things can become quite verbose. jOOQ supports a convenient syntax for specifying sort mappings. The same query can be written in jOOQ as such:
-More complex sort indirections can be provided using a Map:
-Of course, you can combine this feature with the previously discussed NULLS FIRST / NULLS LAST feature. So, if in fact these two books are the ones you like least, you can put all NULLS FIRST (all the other books):
-
The SQL standard defines that a "query expression" can be ordered, and that query expressions can contain
While being extremely useful for every application that does paging, or just to limit result sets to reasonable sizes, this clause is not yet part of any SQL standard (up until SQL:2008). Hence, there exist a variety of possible implementations in various SQL dialects, concerning this limit clause. jOOQ chose to implement the LIMIT .. OFFSET clause as understood and supported by MySQL, H2, HSQLDB, Postgres, and SQLite. Here is an example of how to apply limits with jOOQ:
-This will limit the result to 1 books starting with the 2nd book (starting at offset 0!). limit() is supported in all dialects, offset() in all but Sybase ASE, which has no reasonable means to simulate it. This is how jOOQ simulates the above query in various SQL dialects:
-As you can see, jOOQ will take care of the incredibly painful ROW_NUMBER() OVER() (or ROWNUM for Oracle) filtering in subselects for you, you'll just have to write limit(1).offset(2) in any dialect. @@ -2044,39 +2003,37 @@ AND ROWNUM_98843777 <= 3
As can be seen in the above example, writing correct SQL can be quite tricky, depending on the SQL dialect. For instance, with SQL Server, you cannot have an ORDER BY clause in a subquery, unless you also have a TOP clause. This is illustrated by the fact that jOOQ renders a TOP 100 PERCENT clause for you. The same applies to the fact that ROW_NUMBER() OVER() needs an ORDER BY windowing clause, even if you don't provide one to the jOOQ query. By default, jOOQ adds ordering by the first column of your projection.
-For inter-process synchronisation and other reasons, you may choose to use the SELECT .. FOR UPDATE clause to indicate to the database, that a set of cells or records should be locked by a given transaction for subsequent updates. With jOOQ, this can be achieved as such:
-The above example will produce a record-lock, locking the whole record for updates. Some databases also support cell-locks using FOR UPDATE OF ..
-Oracle goes a bit further and also allows to specify the actual locking behaviour. It features these additional clauses, which are all supported by jOOQ: @@ -2090,15 +2047,15 @@ FOR UPDATE OF TITLE]]> With jOOQ, you can use those Oracle extensions as such:
-The SQL standard specifies a FOR UPDATE clause to be applicable for cursors. Most databases interpret this as being applicable for all SELECT statements. An exception to this rule are the CUBRID and SQL Server databases, that do not allow for any FOR UPDATE clause in a regular SQL SELECT statement. jOOQ simulates the FOR UPDATE behaviour, by locking record by record with JDBC. JDBC allows for specifying the flags TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE for any statement, and then using ResultSet.updateXXX() methods to produce a cell-lock / row-lock. Here's a simplified example in JDBC:
-The main drawback of this approach is the fact that the database has to maintain a scrollable cursor, whose records are locked one by one. This can cause a major risk of deadlocks or race conditions if the JDBC driver can recover from the unsuccessful locking, if two Java threads execute the following statements:
-So use this technique with care, possibly only ever locking single rows! @@ -2135,12 +2092,12 @@ SELECT * FROM author ORDER BY id DESC;]]>
Note, that jOOQ also supports optimistic locking, if you're doing simple CRUD. This is documented in the section's manual about
SQL allows to perform set operations as understood in standard set theory on result sets. These operations include unions, intersections, subtractions. For two subselects to be combinable by such a set operator, each subselect must return a
@@ -2168,76 +2124,73 @@ create.selectFrom(BOOK).where(BOOK.ID.equal(5)));]]>
As previously mentioned in the manual's section about the
If you are closely coupling your application to an Oracle (or CUBRID) database, you might need to be able to pass hints of the form /*+HINT*/ with your SQL statements to the Oracle database. For example:
-This can be done in jOOQ using the .hint() clause in your SELECT statement:
-Note that you can pass any string in the .hint() clause. If you use that clause, the passed string will always be put in between the SELECT [DISTINCT] keywords and the actual projection list
-The INSERT statement is used to insert new records into a database table. Records can either be supplied using a VALUES() constructor, or a SELECT statement. jOOQ supports both types of INSERT statements. An example of an INSERT statement using a VALUES() constructor is given here:
-The SQL standard specifies that multiple rows can be supplied to the VALUES() constructor in an INSERT statement. Here's an example of a multi-record INSERT
-jOOQ tries to stay close to actual SQL. In detail, however, Java's expressiveness is limited. That's why the values() clause is repeated for every record in multi-record inserts. @@ -2246,30 +2199,29 @@ VALUES (100, 'Hermann', 'Hesse'), Some RDBMS do not support inserting several records in a single statement. In those cases, jOOQ simulates multi-record INSERTs using the following SQL:
-MySQL (and some other RDBMS) allow for using a non-SQL-standard, UPDATE-like syntax for INSERT statements. This is also supported in jOOQ, should you prefer that syntax. The above INSERT statement can also be expressed as follows:
-
As you can see, this syntax is a bit more verbose, but also more type-safe, as every field can be matched with its value. Internally, the two syntaxes are strictly equivalent.
@@ -2280,12 +2232,12 @@ SELECT 101, 'Alfred', 'Döblin' FROM DUAL;
The MySQL database supports a very convenient way to INSERT or UPDATE a record. This is a non-standard extension to the SQL syntax, which is supported by jOOQ and simulated in other RDBMS, where this is possible (i.e. if they support the SQL standard
The Postgres database has native support for an INSERT .. RETURNING clause. This is a very powerful concept that is simulated for all other dialects using JDBC's
Some databases have poor support for returning generated keys after INSERTs. In those cases, jOOQ might need to issue another
The UPDATE statement is used to modify one or several pre-existing records in a database table. UPDATE statements are only possible on single tables. Support for multi-table updates will be implemented in the near future. An example update query is given here:
-The DELETE statement physically removes records from a database table. DELETE statements are only possible on single tables. Support for multi-table deletes will be implemented in the near future. An example delete query is given here:
-The MERGE statement is one of the most advanced standardised SQL constructs, which is supported by DB2, HSQLDB, Oracle, SQL Server and Sybase (MySQL has the similar INSERT .. ON DUPLICATE KEY UPDATE construct)
@@ -2387,15 +2335,14 @@ create.insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) The point of the standard MERGE statement is to take a TARGET table, and merge (INSERT, UPDATE) data from a SOURCE table into it. DB2, Oracle, SQL Server and Sybase also allow for DELETING some data and for adding many additional clauses. With jOOQ {jooq-version}, only Oracle's MERGE extensions are supported. Here is an example: -The H2 database ships with a somewhat less powerful but a little more intuitive syntax for its own version of the MERGE statement. An example more or less equivalent to the previous one can be seen here:
-
This syntax can be fully simulated by jOOQ for all other databases that support the SQL standard. For more information about the H2 MERGE syntax, see the documentation here:
http://www.h2database.com/html/grammar.html#merge
The TRUNCATE statement is the only DDL statement supported by jOOQ so far. It is popular in many databases when you want to bypass constraints for table truncation. Databases may behave differently, when a truncated table is referenced by other tables. For instance, they may fail if records from a truncated table are referenced, even with ON DELETE CASCADE clauses in place. Please, consider your database manual to learn more about its TRUNCATE implementation.
@@ -2443,44 +2389,42 @@ VALUES ('John', 'Hitchcock') The TRUNCATE syntax is trivial: -TRUNCATE is not supported by Ingres and SQLite. jOOQ will execute a DELETE FROM AUTHOR statement instead.
-The following sections explain the various types of table expressions supported by jOOQ
-
Most of the times, when thinking about a
The above example shows how AUTHOR and BOOK tables are joined in a
See the manual's section about
The strength of jOOQ's
As you can see in the above example, calling as() on generated tables returns an object of the same type as the table. This means that the resulting object can be used to dereference fields from the aliased table. This is quite powerful in terms of having your Java compiler check the syntax of your SQL statements. If you remove a column from a table, dereferencing that column from that table alias will cause compilation errors. @@ -2529,17 +2472,17 @@ create.select()
TODO document this
-
The
SQL and relational algebra distinguish between at least the following JOIN types (upper-case: SQL, lower-case: relational algebra):
@@ -2557,7 +2500,7 @@ create.select()
jOOQ supports all of these JOIN types (except semi-join and anti-join) directly on any
Note that most of jOOQ's JOIN operations give way to a similar DSL API hierarchy as previously seen in the manual's section about the
A
If you are closely coupling your application to an Oracle database, you can take advantage of some Oracle-specific features, such as the PIVOT clause, used for statistical analyses. The formal syntax definition is as follows:
-
The PIVOT clause is available from the
There is one operation in relational algebra that is not given a lot of attention, because it is rarely used in real-world applications. It is the relational division, the opposite operation of the cross product (or, relational multiplication). The following is an approximate definition of a relational division:
-With jOOQ, you can simplify using relational divisions by using the following syntax:
-The above roughly translates to
-Or in plain text: Find those TEXT values in C whose ID's correspond to all ID's in B. Note that from the above SQL statement, it is immediately clear that proper indexing is of the essence. Be sure to have indexes on all columns referenced from the on(...) and returning(...) clauses. @@ -2723,13 +2663,13 @@ WHERE NOT EXISTS (
The SQL standard specifies how SQL databases should implement ARRAY and TABLE types, as well as CURSOR types. Put simply, a CURSOR is a pointer to any materialised
Note, in order to access the DbmsXplan package, you can use the
The SQL standard specifies that the
Note, that some databases (H2, MySQL) can normally do without "dual". However, there exist some corner-cases with complex nested SELECT statements, where this will cause syntax errors (or parser bugs). To stay on the safe side, jOOQ will always render "dual" in those dialects.
-
jOOQ allows you to freely create arbitrary column expressions using a fluent expression construction API. Many expressions can be formed as functions from
In general, it is up to you whether you want to use the "prefix" notation or the "postfix" notation to create new column expressions based on existing ones. The "SQL way" would be to use the "prefix notation", with functions created from the
Table columns are the most simple implementations of a
Table columns implement a more specific interface called
See the manual's section about
Just like
Here is how it's done with jOOQ:
-When you alias Fields like above, you can access those Fields' values using the alias name:
-jOOQ's source code generator tries to find the most accurate type mapping between your vendor-specific data types and a matching Java type. For instance, most VARCHAR, CHAR, CLOB types will map to String. Most BINARY, BYTEA, BLOB types will map to byte[]. NUMERIC types will default to java.math.BigDecimal, but can also be any of java.math.BigInteger, Long, Integer, Short, Byte, Double, Float.
@@ -2905,27 +2840,27 @@ System.out.println("Books : " + record.getValue("books")); Sometimes, this automatic mapping might not be what you needed, or jOOQ cannot know the type of a field. In those cases you would write SQL type CASTs like this: -in jOOQ, you can write something like that:
-The same thing can be achieved by casting a Field directly to String.class, as TEXT is the default data type in Postgres to map to Java's String
-The complete CAST API in Field consists of these three methods:
-
Your database can do the math for you. Arithmetic operations are implemented just like
In order to express a SQL query like this one:
-You can write something like this in jOOQ:
-jOOQ also supports the Oracle-style syntax for adding days to a Field<? extends java.util.Date>
-For more advanced datetime arithmetic, use the Factory's timestampDiff() and dateDiff() functions, as well as jOOQ's built-in SQL standard INTERVAL data type support: @@ -2988,31 +2921,30 @@ public class Factory {
The SQL standard defines the concatenation operator to be an infix operator, similar to the ones we've seen in the chapter about ||. Some other dialects do not support this operator, but expect a concat() function, instead. jOOQ renders the right operator / function, depending on your
There are a variety of general functions supported by jOOQ As discussed in the chapter about
Please refer to the
Math can be done efficiently in the database before returning results to your Java application. In addition to the
Please refer to the
Interestingly, bitwise functions and bitwise arithmetic is not very popular among SQL databases. Most databases only support a few bitwise operations, while others ship with the full set of operators. jOOQ's API includes most bitwise operations as listed below. In order to avoid ambiguities with
String formatting can be done efficiently in the database before returning results to your Java application. As discussed in the chapter about
Note that the SQL standard specifies that patterns should follow the XQuery standards. In the real world, the POSIX regular expression standard is the most used one, some use Java regular expressions, and only a few ones use Perl regular expressions. jOOQ does not make any assumptions about regular expression syntax. For cross-database compatibility, please read the relevant database manuals carefully, to learn about the appropriate syntax. Please refer to the
This is a list of date and time functions supported by jOOQ's
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. See the manual's section about
This is a list of system functions supported by jOOQ's
Aggregate functions work just like functions, even if they have a slightly different semantics. Here are some example aggregate functions from the
Here's an example, counting the number of books any author has written:
-
Aggregate functions have strong limitations about when they may be used and when not. For instance, you can use aggregate functions in scalar queries. Typically, this means you only select aggregate functions, no
Oracle and some other databases support "ordered aggregate functions". This means you can provide an ORDER BY clause to an aggregate function, which will be taken into consideration when aggregating. The best example for this is Oracle's LISTAGG() (also known as GROUP_CONCAT in other
The above query might yield:
-
Oracle allows for restricting aggregate functions using the KEEP() clause, which is supported by jOOQ. In Oracle, some aggregate functions (MIN, MAX, SUM, AVG, COUNT, VARIANCE, or STDDEV) can be restricted by this clause, hence
@@ -3291,12 +3220,12 @@ GROUP BY AUTHOR_ID
In those databases that support over() on it. See the manual's section about
Most major RDBMS support the concept of window functions. jOOQ knows of implementations in DB2, Oracle, Postgres, SQL Server, and Sybase SQL Anywhere, and supports most of their specific syntaxes. Note, that H2 and HSQLDB have implemented ROW_NUMBER() functions, without true windowing support.
over() method. See the chapter about
SQL distinguishes between various window function types (e.g. "ranking functions"). Depending on the function, SQL expects mandatory PARTITION BY or ORDER BY clauses within the OVER() clause. jOOQ does not enforce those rules for two reasons:
@@ -3341,7 +3270,7 @@ GROUP BY AUTHOR_ID
Here are some simple examples of window functions with jOOQ:
Window functions can be used for things like calculating a "running total". The following example fetches transactions and the running total for every transaction going back to the beginning of the transaction table (ordered by booked_at). Window functions are accessible from the previously seen over() method:
LISTAGG(). These functions have a window function / analytical function variant, as well. For example:
-
In the previous chapter about
User-defined aggregate functions also implement over(). This is supported by Oracle in particular. See the manual's section about
The SQL standard defines special functions that can be used in the
In English, the ROLLUP() grouping function provides N+1 groupings, when N is the number of arguments to the ROLLUP() function. Each grouping has an additional group field from the ROLLUP() argument field list. The results of the second query might look something like this:
CUBE() is different from ROLLUP() in the way that it doesn't just create N+1 groupings, it creates all 2^N possible combinations between all group fields in the CUBE() function argument list. Let's re-consider our second query from before:
The results would then hold:
-
@@ -3562,7 +3485,7 @@ ORDER BY 1 NULLS FIRST, 2 NULLS FIRST
jOOQ fully supports all of these functions, as well as the utility functions GROUPING() and GROUPING_ID(), used for identifying the grouping set ID of a record. The
MySQL and CUBRID don't know any grouping functions, but they support a WITH ROLLUP clause, that is equivalent to simple ROLLUP() grouping functions. jOOQ simulates ROLLUP() in MySQL and CUBRID, by rendering this WITH ROLLUP clause. The following two statements mean the same:
Some databases support user-defined functions, which can be embedded in any SQL statement, if you're using jOOQ's
The above function will be made available from a generated
Note that user-defined functions returning
Some databases support user-defined aggregate functions, which can then be used along with
The above OBJECT type is then available to function declarations as such:
-
jOOQ's
The CASE expression is part of the standard SQL syntax. While some RDBMS also offer an IF expression, or a DECODE function, you can always rely on the two types of CASE syntax:
-
In jOOQ, both syntaxes are supported (The second one is simulated in Derby, which only knows the first one). Unfortunately, both case and else are reserved words in Java. jOOQ chose to use decode() from the Oracle DECODE function, and otherwise(), which means the same as else.
@@ -3747,15 +3666,15 @@ create.decode().value(AUTHOR.FIRST_NAME)
A CASE expression can be used anywhere where you can place a
Oracle knows a more succinct, but maybe less readable DECODE() function with a variable number of arguments. This function roughly does the same as the second case expression syntax. jOOQ supports the DECODE() function and simulates it using CASE expressions in all dialects other than Oracle:
-
Sort indirection is often implemented with a CASE clause of a SELECT's ORDER BY clause. See the manual's section about the
Sequences implement the
So if you have a sequence like this in Oracle:
-
You can then use your
Conditions or conditional expressions are widely used in SQL and in the jOOQ API. They can be used in
@@ -3865,22 +3783,21 @@ create.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Note that jOOQ does not model these values as actual
With jOOQ, most
@@ -3896,48 +3813,47 @@ BOOK.TITLE.notEqual("Animal Farm")]]>
Conditions can also be connected using
In SQL, as in most other languages,
The above example shows that the number of parentheses in Java can quickly explode. Proper indentation may become crucial in making such code readable. In order to understand how jOOQ composes combined conditional expressions, let's assign component expressions first:
-
Here are all boolean operators on the
In SQL, comparison predicates are formed using common comparison operators:
@@ -3976,7 +3891,7 @@ not() // Invert a condition (synonym for Factory.not(Conditi Unfortunately, Java does not support operator overloading, hence these operators are also implemented as methods in jOOQ, like any other SQL syntax elements. The relevant parts of the
Note that every operator is represented by two methods. A verbose one (such as equal()) and a two-character one (such as eq()). Both methods are the same. You may choose either one, depending on your taste. The manual will always use the more verbose one.
@@ -4009,30 +3924,28 @@ ge or greaterOrEqual(Select
If the right-hand side of a
For the example, the right-hand side of the quantified comparison predicates were filled with argument lists. But it is easy to imagine that the source of values results from a
Typically, the
In SQL, you cannot compare NULL with any value using
Some databases support the DISTINCT predicate, which serves as a convenient, NULL-safe
If your database does not natively support the DISTINCT predicate, jOOQ simulates it with an equivalent
The BETWEEN predicate can be seen as syntactic sugar for a pair of
Note the inclusiveness of range boundaries in the definition of the BETWEEN predicate. Intuitively, this is supported in jOOQ as such:
-The SQL standard defines the SYMMETRIC keyword to be used along with BETWEEN to indicate that you do not care which bound of the range is larger than the other. A database system should simply swap range bounds, in case the first bound is greater than the second one. jOOQ supports this keyword as well, simulating it if necessary.
-The simulation is done trivially:
-LIKE predicates are popular for simple wildcard-enabled pattern matching. Supported wildcards in all SQL databases are:
@@ -4180,24 +4085,22 @@ BOOK.PUBLISHED_IN.notBetweenSymmetric(1940, 1920)]]> With jOOQ, the LIKE predicate can be created from anyOften, your pattern may contain any of the wildcard characters "_" and "%", in case of which you may want to escape them. jOOQ does not automatically escape patterns in like() and notLike() methods. Instead, you can explicitly define an escape character as such:
-In the above predicate expressions, the exclamation mark character is passed as the escape character to escape wildcard characters "!_" and "!%", as well as to escape the escape character itself: "!!" @@ -4211,7 +4114,7 @@ BOOK.TITLE.notLike("%The !%-Sign Book%", '!')]]> In addition to the above, jOOQ provides a few convenience methods for common operations performed on strings using the LIKE predicate. Typical operations are "contains predicates", "starts with predicates", "ends with predicates", etc. Here is the full convenience API wrapping LIKE predicates:
-Note, that jOOQ escapes % and _ characters in value in some of the above predicate implementations. For simplicity, this has been omitted in this manual.
-
In SQL, apart from comparing a value against several values, the IN predicate can be used to create semi-joins or anti-joins. jOOQ knows the following methods on the
A sample IN predicate might look like this:
-
A good way to prevent this from happening is to use the
Slightly less intuitive, yet more powerful than the previously discussed
Note that in SQL, the projection of a subselect in an EXISTS predicate is irrelevant. To help you write queries like the above, you can use jOOQ's selectZero() or selectOne()
http://blog.jooq.org/2012/07/27/not-in-vs-not-exists-vs-left-join-is-null-mysql/
A DSL is a nice thing to have, it feels "fluent" and "natural", especially if it models a well-known language, such as SQL. But a DSL is always expressed in a host language (Java in this case), which was not made for exactly the same purposes as its hosted DSL. If it were, then jOOQ would be implemented on a compiler-level, similar to LINQ in .NET. But it's not, and so, the DSL is limited by language constraints of its host language. We have seen many functionalities where the DSL becomes a bit verbose. This can be especially true for:
@@ -4358,7 +4258,7 @@ notExists(create.selectOne().from(BOOK)
Plain SQL API methods are usually overloaded in three ways. Let's look at the condition query part constructor:
Please refer to the
Apart from the general factory methods, plain SQL is also available in various other contexts. For instance, when adding a .where("a = b") clause to a query. Hence, there exist several convenience methods where plain SQL can be inserted usefully. This is an example displaying all various use-cases in one single query:
-@@ -4466,12 +4366,12 @@ create.select(LAST_NAME, COUNT1, COUNT2)
Bind values are used in SQL / JDBC for various reasons. Among the most obvious ones are:
@@ -4490,31 +4390,31 @@ create.select(LAST_NAME, COUNT1, COUNT2)The following sections explain how you can introduce bind values in jOOQ, and how you can control the way they are rendered and bound to SQL.
-JDBC only knows indexed bind values. A typical example for using bind values with JDBC is this:
-With dynamic SQL, keeping track of the number of question marks and their corresponding index may turn out to be hard. jOOQ abstracts this and lets you provide the bind value right where it is needed. A trivial example is this:
-
Note the using of
You can also extract specific bind values by index from a query, if you wish to modify their underlying value after creating a query. This can be achieved as such:
-
For more details about jOOQ's internals, see the manual's section about
Some SQL access abstractions that are built on top of JDBC, or some that bypass JDBC may support named parameters. jOOQ allows you to give names to your parameters as well, although those names are not rendered to SQL strings by default. Here is an example of how to create named parameters using the
The
In order to actually render named parameter names in generated SQL, use the
Sometimes, you may wish to avoid rendering bind variables while still using custom values in SQL. jOOQ refers to that as "inlined" bind values. When bind values are inlined, they render the actual value in SQL rather than a JDBC question mark. Bind value inlining can be achieved in two ways:
@@ -4621,7 +4520,7 @@ WHERE LAST_NAME = :lastName]]> In both cases, your inlined bind values will be properly escaped to avoid SQL syntax errors and SQL injection. Some examples: -
Special care needs to be taken when using
All methods in the jOOQ API that allow for plain (unescaped, untreated) SQL contain a warning message in their relevant Javadoc, to remind you of the risk of SQL injection in what is otherwise a SQL-injection-safe API.
-
A
Every
The following additional methods are inherited from a common
A simple example can be provided by checking out jOOQ's internal representation of a (simplified)
This is how jOOQ renders such a condition:
-
See the manual's sections about
Every
Some additional methods are inherited from a common
This is how jOOQ binds values on such a condition:
-
See the manual's sections about
If a SQL clause is too complex to express with jOOQ, you can extend either one of the following types for use directly in a jOOQ query:
-
These classes are declared public and covered by jOOQ's integration tests. When you extend these classes, you will have to provide your own implementations for the
The above contract may be a bit tricky to understand at first. The best thing is to check out jOOQ source code and have a look at a couple of QueryParts, to see how it's done. Here's an example
If you don't need the integration of rather complex QueryParts into jOOQ, then you might be safer using simple
The above technique allows for creating rather complex SQL clauses that are currently not supported by jOOQ, without extending any of the
The only transient, non-serializable element in any jOOQ object is the
Another way of attaching QueryParts automatically, or rather providing them with a new
In a previous section of the manual, we've seen how jOOQ can be used to
The following sections of this manual will show how jOOQ is wrapping JDBC for SQL execution
-
Even if there are
Unlike JDBC, jOOQ has a lot of knowledge about a SQL query's structure and internals (see the manual's section about
Fetching is something that has been completely neglegted by JDBC and also by various other database abstraction libraries. Fetching is much more than just looping or listing records or mapped objects. There are so many ways you may want to fetch data from a database, it should be considered a first-class feature of any database abstraction API. Just to name a few, here are some of jOOQ's fetching modes:
@@ -5091,7 +4987,7 @@ Result
These means of fetching are also available from
These means of fetching are also available from
Note, that apart from the
jOOQ understands that SQL is much more expressive than Java, when it comes to the declarative typing of
When you use the
It is worth mentioning that Scala is much stronger in inferring types than Java. Yet still, this becomes quickly impracticable as @@ -5276,17 +5172,17 @@ for (tuple <- create
For these reasons jOOQ does not support generic tuples.
-
By default, jOOQ returns an
Note that most of these convenience methods are available both through
In a more functional operating mode, you might want to write callbacks that receive records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
Fetching data in records is fine as long as your application is not really layered, or as long as you're still writing code in the DAO layer. But if you have a more advanced application architecture, you may not want to allow for jOOQ artefacts to leak into other layers. You may choose to write POJOs (Plain Old Java Objects) as your primary DTOs (Data Transfer Objects), without any dependencies on jOOQ's
Just as with any other JPA implementation, you can put the
Please refer to the
Please refer to the
Note: Because of your manual setting of ID = 10, jOOQ's store() method will asume that you want to insert a new record. See the manual's section about
jOOQ currently doesn't support more complex data structures, the way Hibernate/JPA attempt to map relational data onto POJOs. While future developments in this direction are not excluded, jOOQ claims that generic mapping strategies lead to an enormous additional complexity that only serves very few use cases. You are likely to find a solution using any of jOOQ's various More complex data structures
Unlike JDBC's
As a
Many databases support returning several result sets, or cursors, from single queries. An example for this is Sybase ASE's sp_help command:
-The correct (and verbose) way to do this with JDBC is as follows:
-
As previously discussed in the chapter about
Using generics, the resulting structure is immediately clear.
-
Some queries take very long to execute, yet they are not crucial for the continuation of the main program. For instance, you could be generating a complicated report in a Swing application, and while this report is being calculated in your database, you want to display a background progress bar, allowing the user to pursue some other work. This can be achived simply with jOOQ, by creating a
Note, that instead of letting jOOQ spawn a new thread, you can also provide jOOQ with your own
When interacting with legacy applications, you may prefer to have jOOQ return a
Instead of operating on a JDBC ResultSet holding an open resource from your database, you can also let jOOQ's
The inverse of the above is possible too. Maybe, a legacy part of your application produces JDBC
Apart from a few extra features (see the manual's section about
Such a converter can be used in many parts of the jOOQ API. Some examples have been illustrated in the manual's section about
jOOQ ships with a built-in default
jOOQ also allows for generated source code to reference your own custom converters, in order to permanently replace a
With JDBC, you have full control over your SQL statements. You can decide yourself, if you want to execute a static
@@ -5829,12 +5719,12 @@ inlined.select(val(1)).where(val(1).equal(1)).fetch();]]>
Note that you don't have to inline all your bind values at once. If you know that a bind value is not really a variable and should be inlined explicitly, you can do so by using
With JDBC, you can easily execute several statements at once using the addBatch() method. Essentially, there are two modes in JDBC
@@ -5847,7 +5737,7 @@ inlined.select(val(1)).where(val(1).equal(1)).fetch();]]>In code, this looks like the following snippet:
-
Most databases support sequences of some sort, to provide you with unique values to be used for primary keys and other enumerations. If you're using jOOQ's
You can inline sequence references in jOOQ SQL statements. The following are examples of how to do that:
-
For more info about inlining sequence references in SQL statements, please refer to the manual's section about
Many RDBMS support the concept of "routines", usually calling them procedures and/or functions. These concepts have been around in programming languages for a while, also outside of databases. Famous languages distinguishing procedures from functions are:
@@ -5991,14 +5880,14 @@ create.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) If you're using jOOQ'sThe generated artefacts can then be used as follows:
-But you can also call the procedure using a generated convenience method in a global Routines class:
-
For more details about
The generated artefacts can then be used as follows:
-
For more info about inlining stored function references in SQL statements, please refer to the manual's section about
Oracle uses the concept of a PACKAGE to group several procedures/functions into a sort of namespace. The SQL 92 standard talks about "modules", to represent this concept, even if this is rarely implemented as such. This is reflected in jOOQ by the use of Java sub-packages in the
For more details about
Oracle UDTs can have object-oriented structures including member functions and procedures. With Oracle, you can do things like this:
-These member functions and procedures can simply be mapped to Java methods:
-
For more details about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's exporting functionality (see also the
The above query will result in an XML document looking like the following one:
-
The same result as an
See the XSD schema definition here, for a formal definition of the XML export format:
http://www.jooq.org/xsd/jooq-export-1.6.2.xsd
The above query will result in a CSV document looking like the following one:
-In addition to the standard behaviour, you can also specify a separator character, as well as a special string to represent NULL values (which cannot be represented in standard CSV):
-The above query will result in a JSON document looking like the following one:
-The above query will result in an HTML document looking like the following one
-The above query will result in a text document looking like the following one
-
A simple text representation can also be obtained by calling toString() on a Result object. See also the manual's section about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's importing functionality (see also exporting functionality). You can import data directly into a table from the formats described in the subsequent sections of this manual.
-
The below CSV data represents two author records that may have been exported previously, by jOOQ's
With jOOQ, you can load this data using various parameters from the loader API. A simple load may look like this:
-Here are various other examples:
-Any of the above configuration methods can be combined to achieve the type of load you need. Please refer to the API's Javadoc to learn about more details. Errors that occur during the load are reported by the execute method's result:
-This is not yet supported
-Your database application probably consists of 50% - 80% CRUD, whereas only the remaining 20% - 50% of querying is actual querying. Most often, you will operate on records of tables without using any advanced relational concepts. This is called CRUD for
@@ -6415,13 +6287,13 @@ Query query = error.query();]]> In normalised databases, every table has a primary key by which a tuple/record within that table can be uniquely identified. In simple cases, this is a (possibly auto-generated) number called ID. But in many cases, primary keys include several non-numeric columns. An important feature of such keys is the fact that in most databases, they are enforced using an index that allows for very fast random access to the table. A typical way to access / modify / delete a book is this: -
Normalised databases assume that a primary key is unique "forever", i.e. that a key, once inserted into a table, will never be changed or re-inserted after deletion. In order to use jOOQ's
For simplicity, the term "primary key" will be used in the sense of such a "main unique key" in this manual.
-
If you're using jOOQ's
See the manual's section about
Some remarks about storing: @@ -6503,11 +6375,11 @@ book2.store();]]> Deleting a record will remove it from the database. Here's how you delete records:
-@@ -6517,53 +6389,52 @@ book.delete();]]> In order to perform a refresh, use the following Java code:
-
CRUD operations can be combined with regular querying, if you select records from single database tables, as explained in the manual's section about
Many databases support the concept of IDENTITY values, or
If you're using jOOQ's
@@ -6572,8 +6443,8 @@ System.out.println(book.getId());]]>
These SQL dialects implement the standard very neatly.
-H2, MySQL, Postgres, SQL Server, Sybase ASE, Sybase SQL Anywhere @@ -6581,7 +6452,7 @@ id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1)]]>
These SQL dialects implement identites, but the DDL syntax doesn’t follow the standard
-Oracle @@ -6605,7 +6476,7 @@ id INTEGER NOT NULL IDENTITY]]>
Oracle does not know any identity columns at all. Instead, you will have to use a trigger and update the ID column yourself, using a custom sequence. Something along these lines:
-Note, that this approach can be employed in most databases supporting sequences and triggers! It is a lot more flexible than standard identities
-
Tables without UNIQUE keys are considered non-updatable by jOOQ, as jOOQ has no way of uniquely identifying such a record within the database. If you're using jOOQ's
Note, that some databases use internal rowid or object-id values to identify such records. jOOQ does not support these vendor-specific record meta-data.
-
jOOQ allows you to perform
If you're using jOOQ's
The MODIFIED column will contain a timestamp indicating the last modification timestamp for any book in the BOOK table. If you're using jOOQ and it's
As before, without the added TIMESTAMP column, optimistic locking is transparent to the API. @@ -6773,17 +6642,17 @@ book2.store();]]>
Note, for explicit pessimistic locking, please consider the manual's section about the
When inserting, updating, deleting a lot of records, you may wish to profit from JDBC batch operations, which can be performed by jOOQ. These are available through jOOQ's
Internally, jOOQ will render all the required SQL statements and execute them as a regular
If you're using jOOQ's
corresponds to the DAO's related generated POJO type
// getType();
-}]]>
Besides these base methods, generated DAO classes implement various useful fetch methods. An incomplete example is given here, for the BOOK table:
-Note that you can further subtype those pre-generated DAO classes, to add more useful DAO methods to them. Using such a DAO is simple:
-This is an eternal and religious debate. Pros and cons have been discussed time and again, and it still is a matter of taste, today. In this case, jOOQ clearly takes a side. jOOQ's exception strategy is simple: @@ -6919,12 +6786,12 @@ bookDao.delete(book);]]>
The following section about
The
Now, configure jOOQ's runtime to load your listener
-And log results any time with a snippet like this:
-This may result in the following log output:
-
Please read the
See also the manual's sections about
jOOQ logs all SQL queries and fetched result sets to its internal DEBUG logger, which is implemented as an
With the above configuration, let's fetch some data with jOOQ
-The above query may result in the following log output:
-Essentially, jOOQ will log @@ -7056,12 +6923,12 @@ Finishing : Total: 4.814ms, +3.375ms
If you wish to use your own logger (e.g. avoiding printing out sensitive data), you can deactivate jOOQ's logger using
Many users may have switched from higher-level abstractions such as Hibernate to jOOQ, because of Hibernate's hard-to-manage performance, when it comes to large database schemas and complex second-level caching strategies. jOOQ is not a lightweight database abstraction framework, and it comes with its own overhead. Please be sure to consider the following points:
@@ -7076,14 +6943,14 @@ Finishing : Total: 4.814ms, +3.375msDon't be put off by the above paragraphs. You should optimise wisely, i.e. only in places where you really need very high throughput to your database. jOOQ's overhead compared to plain JDBC is typically less than 1ms per query.
-
While optional, source code generation is one of jOOQ's main assets if you wish to increase developer productivity. jOOQ's code generator takes your database schema and reverse-engineers it into a set of Java classes modelling
The following chapters will show how to configure the code generator and how to generate various artefacts.
-There are three binaries available with jOOQ, to be downloaded from SourceForge or from Maven central:
@@ -7129,7 +6996,7 @@ Finishing : Total: 4.814ms, +3.375ms You need to tell jOOQ some things about your database connection. Here's an example of how to do it for an Oracle database -
There are also lots of advanced configuration parameters, which will be treated in the
@@ -7216,7 +7083,7 @@ Finishing : Total: 4.814ms, +3.375ms
Code generation works by calling this class with the above property file as argument.
Be sure that these elements are located on the classpath: @@ -7279,7 +7146,7 @@ Finishing : Total: 4.814ms, +3.375ms You can also use an ant task to generate your classes. As a rule of thumb, remove the dots "." and dashes "-" from the .properties file's property names to get the ant task's arguments:
-Note that when running code generation with ant's <java/> task, you may have to set fork="true":
-Using the official jOOQ-codegen-maven plugin, you can integrate source code generation in your Maven build process:
-
See the full example of a pom.xml including the jOOQ-codegen artefact here:
@@ -7386,7 +7253,7 @@ Finishing : Total: 4.814ms, +3.375ms
Before jOOQ 2.0.4, the code generator was configured using properties files. These files are still supported for source code generation, but their syntax won't be maintained any longer. If you wish to migrate to XML, you can migrate the file using this command on the command line
Using the migrate flag, jOOQ will read the properties file and output a corresponding XML file on system out @@ -7396,17 +7263,17 @@ Finishing : Total: 4.814ms, +3.375ms
Be sure, both jooq-{jooq-version}.jar and your generated package (see configuration) are located on your classpath. Once this is done, you can execute SQL statements with your generated classes.
-
In the
The following example shows how you can override the DefaultGeneratorStrategy to render table and column names the way they are defined in the database, rather than switching them to camel case:
-Within the <generator/> element, there are other configuration elements:
-Check out the some of the manual's "advanced" sections to find out more about the advanced configuration parameters. @@ -7603,7 +7470,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { Also, you can add some optional advanced configuration parameters for the generator:
-For increased convenience at the use-site, jOOQ generates "global" artefacts at the code generation root location, referencing tables, routines, sequences, etc. In detail, these global artefacts include the following:
@@ -7686,7 +7552,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { When referencing global artefacts from your client application, you would typically static import them as such: -
Every table in your database will generate a
@@ -7745,17 +7610,17 @@ create.insertInto(com.example.generated.Tables.MY_TABLE)
Table generation cannot be deactivated
-
Every table in your database will generate a
@@ -7816,17 +7681,17 @@ implements IBook {
Record generation can be deactivated using the records flag
-Every table in your database will generate a POJO implementation that looks like this:
-@@ -7879,24 +7744,24 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the pojos flag
-Every table in your database will generate an interface that looks like this:
-@@ -7911,18 +7776,18 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the interfaces flag
-
Every table in your database will generate a
DAO generation can be activated using the daos flag
-
Every sequence in your database will generate a
Sequence generation cannot be deactivated
-
Every procedure or function (routine) in your database will generate a
@@ -8019,17 +7884,17 @@ public class Book implements java.io.Serializable
Routine generation cannot be deactivated
-
Every UDT in your database will generate a
Besides the
UDT generation cannot be deactivated
-NOTE: This feature is deprecated in jOOQ 2.5.0 and will be removed as of jOOQ 3.0
@@ -8085,7 +7950,7 @@ public class Book implements java.io.Serializable As previously discussed, you can configure master data tables as follows: -The results of this will be a Java enum that looks similar to this:
-In the above example, you can see how the configured primary key is mapped to the id member, the configured literal column is mapped to the cd member and the configured description member is mapped to the description member and output as Javadoc. In other words, T_LANGUAGE is a table with 4 rows and at least three columns. @@ -8155,39 +8020,39 @@ public class Book implements java.io.Serializable The point of MasterDataTypes in jOOQ is that they behave exactly like true ENUM types. When the above T_LANGUAGE table is referenced by T_BOOK, instead of generating foreign key navigation methods and a LANGUAGE_ID Field<Integer>, a Field<TLanguage> is generated:
-Which can then be used in the BookRecord directly:
-You can use master data types when you're actually mapping master data to a Java enum. When the underlying table changes frequently, those updates will not be reflected by the statically generated code. Also, be aware that it will be difficult to perform actual JOIN operations on the underlying table with jOOQ, once the master data type is generated.
-
When using a custom type in jOOQ, you need to let jOOQ know about its associated
The above configuration will lead to AUTHOR.DATE_OF_BIRTH being generated like this:
-
This means that the bound type of <T> will be GregorianCalendar, wherever you reference DATE_OF_BIRTH. jOOQ will use your custom converter when binding variables and when fetching data from
We've seen previously in the chapter about
These chapters hold some information about tools to be used with jOOQ
-
The
Or when using programmatic settings:
-The in-process mode is useful for Swing applications or other, locally run Java programs accessing the database via jOOQ. In order to launch the jOOQ Console "in-process", specify the previously documented settings and launch the Console as follows:
-Only in the in-process mode, you can execute ad-hoc queries directly from the console, if you provide it with proper DatabaseDescriptor. These queries are executed from the Editor pane which features: @@ -8390,36 +8251,36 @@ catch (Exception ignore) {} In J2EE or other server/client environments, you may not be able to run the console in the same process as your application. You can then run the jOOQ Console in "headless" mode. In addition to the previously documented settings, you'll have to start a debugger server in your application process, that the console can connect to:
-Now start your application along with the debugger server and launch the console with this command:
-Depending on your distribution, you may have to manually add rsyntaxtextarea-1.5.0.jar and jOOQ artefacts on your classpath.
-These chapters hold some general jOOQ reference information
-Every RDMBS out there has its own little specialties. jOOQ considers those specialties as much as possible, while trying to standardise the behaviour in jOOQ. In order to increase the quality of jOOQ, some 70 unit tests are run for syntax and variable binding verification, as well as some 180 integration tests with an overall of around 1200 queries for any of these databases: @@ -8478,12 +8339,12 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
This section will soon contain a feature matrix, documenting what feature is available for which database.
-There is always a small mismatch between SQL data types and Java data types. This is for two reasons:
@@ -8494,21 +8355,21 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);This chapter should document the most important notes about SQL, JDBC and jOOQ data types.
-jOOQ currently doesn't explicitly support JDBC BLOB and CLOB data types. If you use any of these data types in your database, jOOQ will map them to byte[] and String instead. In simple cases (small data), this simplification is sufficient. In more sophisticated cases, you may have to bypass jOOQ, in order to deal with these data types and their respective resources. True support for LOBs is on the roadmap, though.
-
Some databases explicitly support unsigned integer data types. In most normal JDBC-based applications, they would just be mapped to their signed counterparts letting bit-wise shifting and tweaking to the user. jOOQ ships with a set of unsigned
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. SQL knows two different types of intervals:
@@ -8557,51 +8418,51 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);XML data types are currently not supported
-Geospacial data types are currently not supported
-Some databases support cursors returned from stored procedures. They are mapped to the following jOOQ data type:
-
In fact, such a cursor will be fetched immediately by jOOQ and wrapped in an
The SQL standard specifies ARRAY data types, that can be mapped to Java arrays as such:
-The above array type is supported by these SQL dialects: @@ -8616,23 +8477,23 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
Oracle has strongly-typed arrays and table types (as opposed to the previously seen anonymously typed arrays). These arrays are wrapped by
This chapter will soon contain an overview over jOOQ's API using a pseudo BNF notation.
-jOOQ lives in a very challenging ecosystem. The Java to SQL interface is still one of the most important system interfaces. Yet there are still a lot of open questions, best practices and no "true" standard has been established. This situation gave way to a lot of tools, APIs, utilities which essentially tackle the same problem domain as jOOQ. jOOQ has gotten great inspiration from pre-existing tools and this section should give them some credit. Here is a list of inspirational tools in alphabetical order:
@@ -8645,7 +8506,7 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);This manual is divided into six main sections:
@@ -88,12 +88,12 @@ -Java and SQL have come a long way. SQL is an "ancient", yet established and well-understood technology. Java is a legacy too, although its platform JVM allows for many new and contemporary languages built on top of it. Yet, after all these years, libraries dealing with the interface between SQL and Java have come and gone, leaving JPA to be a standard that is accepted only with doubts, short of any surviving options. @@ -126,18 +126,18 @@
SQL was never meant to be abstracted. To be confined in the narrow boundaries of heavy mappers, hiding the beauty and simplicity of relational data. SQL was never meant to be object-oriented. SQL was never meant to be anything other than... SQL!
-This section lists the various licenses that apply to different versions of jOOQ. Prior to version 3.2, jOOQ was shipped for free under the terms of the Apache Software License 2.0. With jOOQ 3.2, jOOQ became dual-licensed: Apache Software License 2.0 (for use with Open Source databases) and commercial (for use with commercial databases). Please contact sales@datageekery.com, should you have any questions regarding licensing.
Throughout the manual, the above trademarks are referenced without a formal ® (R) or ™ (TM) symbol. It is believed that referencing third-party trademarks in this manual or on the jOOQ website constitutes "fair use". Please contact us if you think that your trademark(s) are not properly attributed.
-These chapters contain a quick overview of how to get started with this manual and with jOOQ. While the subsequent chapters contain a lot of reference information, this chapter here just wraps up the essentials.
-This section helps you correctly interpret this manual in the context of jOOQ.
@@ -279,28 +279,21 @@ and Maintenance Agreement for more details: http://www.jooq.org/eula The following are code blocks: -These are useful to provide examples in code. Often, with jOOQ, it is even more useful to compare SQL code with its corresponding Java/jOOQ code. When this is done, the blocks are aligned side-by-side, with SQL usually being on the left, and Java usually being on the right:
-@@ -308,13 +301,11 @@ create.select()]]>
-@@ -335,16 +326,16 @@ Factory create = new Factory(connection, SQLDialect.ORACLE);]]>
See the manual's section about
For the examples in this manual, the same database will always be referred to. It essentially consists of these entities created using the Oracle dialect
-More entities, types (e.g. UDT's, ARRAY types, ENUM types, etc), stored procedures and packages are introduced for specific examples
-jOOQ has originally been created as a library for complete abstraction of JDBC and all database interaction. Various best practices that are frequently encountered in pre-existing software products are applied to this library. This includes:
@@ -420,23 +411,23 @@ CREATE TABLE book_to_book_store (The following sections explain about various use cases for using jOOQ in your application.
-
This is the most simple of all use cases, allowing for construction of valid SQL for any database. In this use case, you will not use
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -448,23 +439,23 @@ String sql = create.select(fieldByName("BOOK","TITLE"), fieldByName("AUTHOR","FI
In addition to using jOOQ as a
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -476,29 +467,29 @@ String sql = create.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Instead of any tool mentioned in the previous chapters, you can also use jOOQ directly to execute your jOOQ-generated SQL statements. This will add a lot of convenience on top of the previously discussed API for typesafe SQL construction, when you can re-use the information from generated classes to fetch records and custom data types. An example is given here:
-jOOQ doesn't stop here, though! You can execute any SQL with jOOQ. In other words, you can use any other SQL building tool and run the SQL statements with jOOQ. An example is given here:
-
If you wish to use jOOQ as a SQL executor with (or without) code generation, the following sections of the manual will be of interest to you:
@@ -518,17 +509,17 @@ Result
This is probably the most complete use-case for jOOQ: Use all of jOOQ's features. Apart from jOOQ's fluent API for query construction, jOOQ can also help you execute everyday CRUD operations. An example is given here:
-If you wish to use all of jOOQ's features, the following sections of the manual will be of interest to you (including all sub-sections): @@ -552,12 +543,12 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
jOOQ isn't just a library that helps you
If you're a power user of your favourite, feature-rich database, jOOQ will help you access all of your database's vendor-specific features, such as OLAP features, stored procedures, user-defined types, vendor-specific SQL, functions, etc. Examples are given throughout this manual.
-Don't have time to read the full manual? Here are a couple of tutorials that will get you into the most essential parts of jOOQ as quick as possible.
-This manual section is intended for new users, to help them get a running application with jOOQ, quickly.
-
If you haven't already downloaded it, download jOOQ:
https://sourceforge.net/projects/jooq/files/Release/
@@ -605,12 +596,12 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
Alternatively, you can create a Maven dependency:
-
For this example, we'll be using MySQL. If you haven't already downloaded MySQL Connector/J, download it here:
@@ -620,17 +611,17 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
If you don't have a MySQL instance up and running yet, get XAMPP now! XAMPP is a simple installation bundle for Apache, MySQL, PHP and Perl
-We're going to create a database called "guestbook" and a corresponding "posts" table. Connect to MySQL via your command line client and type the following:
-
In this step, we're going to use jOOQ's command line tools to generate classes that map to the Posts table we just created. More detailed information about how to set up the jOOQ code generator can be found here:
Replace the username with whatever user has the appropriate privileges to query the database meta data. You'll also want to look at the other values and replace as necessary. Here are the two interesting properties:
@@ -711,15 +701,15 @@ CREATE TABLE `posts` ( Once you have the JAR files and guestbook.xml in your temp directory, type this (use colons instead of semi-colons on UNIX/Linux systems): -Note the prefix slash before guestbook.xml. Even though it's in our working directory, we need to prepend a slash, as the configuration file is loaded from the classpath. Replace the filenames with your filenames. In this example, jOOQ {jooq-version} is being used. If everything has worked, you should see this in your console output:
-Let's just write a vanilla main class in the project containing the generated classes:
-This is pretty standard code for establishing a MySQL connection.
-Let's add a simple query:
-
First get an instance of
We then use jOOQ's DSL to return an instance of Result. We'll be using this result in the next step.
Factory so we can write a simple SELECT query. We pass an instance of the MySQL connection to Factory. Note that the factory doesn't close the connection. We'll have to do that ourselves.
@@ -840,29 +828,29 @@ Result
After the line where we retrieve the results, let's iterate over the results and print out the data:
-The full program should now look like this:
-
jOOQ has grown to be a comprehensive SQL library. For more information, please consider the manual:
http://www.jooq.org/manual/
@@ -941,37 +928,37 @@ public class Main {
This tutorial is the courtesy of Ikai Lan. See the original source here:
http://ikaisays.com/2011/11/01/getting-started-with-jooq-a-tutorial/
Feel free to contribute a tutorial!
-Feel free to contribute a tutorial!
-Feel free to contribute a tutorial!
-As any other library, jOOQ can be easily used in Scala, taking advantage of the many Scala language features such as for example:
@@ -994,7 +981,7 @@ public class Main { A short example jOOQ application in Scala might look like this: -
For more details about jOOQ's Scala integration, please refer to the manual's section about
jOOQ has no dependencies on any third-party libraries. This rule has some exceptions:
@@ -1057,14 +1044,14 @@ object Test { //In order to build jOOQ, please download the sources from https://github.com/jOOQ/jOOQ and use Maven to build it, preferably in Eclipse.
-SQL is a declarative language that is hard to integrate into procedural, object-oriented, functional or any other type of programming languages. jOOQ's philosophy is to give SQL the credit it deserves and integrate SQL itself as an "internal domain specific language" directly into Java.
@@ -1074,12 +1061,12 @@ object Test { //
This section explains all about the various syntax elements involved with jOOQ's SQL building capabilities. For a complete overview of all syntax elements, please refer to the manual's section about
jOOQ exposes a lot of interfaces and hides most implementation facts from client code. The reasons for this are:
@@ -1100,13 +1087,13 @@ object Test { //With jOOQ 2.0, static factory methods have been introduced in order to make client code look more like SQL. Ideally, when working with jOOQ, you will simply static import all methods from the Factory class:
-Note, that when working with Eclipse, you could also add the Factory to your favourites. This will allow to access functions even more fluently:
-@@ -1121,20 +1108,19 @@ Factory.concat(Factory.trim(FIRST_NAME), Factory.trim(LAST_NAME));
As a Configuration object, a Factory can construct
While jOOQ tries to represent the SQL standard as much as possible, many features are vendor-specific to a given database and to its "SQL dialect". jOOQ models this using the
Some parts of the jOOQ API are officially supported only by a given subset of the supported SQL dialects. For instance, the
jOOQ API methods which are not annotated with the
The aforementioned Support annotation does not only designate, which databases natively support a feature. It also indicates that a feature is simulated by jOOQ for some databases lacking this feature. An example of this is the
Nevertheless, the IS DISTINCT FROM predicate is supported in all dialects, as its semantics can be expressed with an equivalent
jOOQ has a historic affinity to Oracle's SQL extensions. If something is supported in Oracle SQL, it has a high probability of making it into the jOOQ API
-
While you can use jOOQ for
If you're in a J2EE or Spring context, however, you may wish to use a
The jOOQ Factory allows for some optional configuration elements to be used by advanced users. The
For example, if you want to indicate to jOOQ, that it should inline all bind variables, and execute static
Subsequent sections of the manual contain some more in-depth explanations about these settings:
@@ -1240,12 +1226,12 @@ Factory create = new Factory(connection, dialect, settings);]]> Please refer to the jOOQ runtime configuration XSD for more details:
You may wish to design your database in a way that you have several instances of your schema. This is useful when you want to cleanly separate data belonging to several customers / organisation units / branches / users and put each of those entities' data in a separate database or schema.
@@ -1265,7 +1251,7 @@ Factory create = new Factory(connection, dialect, settings);]]>
When a user from My Book World logs in, you want them to access the MY_BOOK_WORLD schema using classes generated from DEV. This can be achieved with the
The query executed with a Factory equipped with the above mapping will in fact produce this SQL statement:
-Even if AUTHOR was generated from DEV.
@@ -1291,7 +1277,7 @@ create.selectFrom(AUTHOR).fetch(); Your development database may not be restricted to hold only one DEV schema. You may also have a LOG schema and a MASTER schema. Let's say the MASTER schema is shared among all customers, but each customer has their own LOG schema instance. Then you can enhance your RenderMapping like this (e.g. using an XML configuration file): -Note, you can load the above XML file like this:
-This will map generated classes from DEV to MY_BOOK_WORLD, from LOG to MY_BOOK_WORLD_LOG, but leave the MASTER schema alone. Whenever you want to change your mapping configuration, you will have to create a new Factory @@ -1322,40 +1308,40 @@ create.selectFrom(AUTHOR).fetch(); Another option to switch schema names is to use a default schema for the Factory's underlying Connection. Many RDBMS support a USE or SET SCHEMA command, which you can call like this:
-Queries generated from the above Factory will produce this kind of SQL statement:
-If you wish not to render any schema name at all, use the following Settings property for this:
-Not only schemata can be mapped, but also tables. If you are not the owner of the database your application connects to, you might need to install your schema with some sort of prefix to every table. In our examples, this might mean that you will have to map DEV.AUTHOR to something MY_BOOK_WORLD.MY_APP__AUTHOR, where MY_APP__ is a prefix applied to all of your tables. This can be achieved by creating the following mapping:
-The query executed with a Factory equipped with the above mapping will in fact produce this SQL statement:
-Table mapping and schema mapping can be applied independently, by specifying several MappedSchema entries in the above configuration. jOOQ will process them in order of appearance and map at first match. Note that you can always omit a MappedSchema's output value, in case of which, only the table mapping is applied. If you omit a MappedSchema's input value, the table mapping is applied to all schemata! @@ -1384,20 +1370,20 @@ create.selectFrom(AUTHOR).fetch();
Note that the manual's section about
There are a couple of subclasses for the general Factory. Each SQL dialect has its own dialect-specific factory. For instance, if you're only using the MySQL dialect, you can choose to create a new Factory using any of the following types:
-The advantage of using a dialect-specific Factory lies in the fact, that you have access to more proprietary RDMBS functionality. This may include:
@@ -1408,23 +1394,23 @@ MySQLFactory create = new MySQLFactory(connection);Another type of Factory subclasses are each generated schema's factories. If you generate your schema TEST, then you will have access to a TestFactory. By default, such a schema-specific Factory will not render the schema name.
-
jOOQ currently supports 6 types of SQL statements. All of these statements are constructed from a Factory instance with an optional
jOOQ ships with its own DSL (or Domain Specific Language) that simulates SQL in Java. This means, that you can write SQL statements almost as if Java natively supported it, just like .NET's C# does with LINQ to SQL.
@@ -1432,22 +1418,21 @@ MySQLFactory create = new MySQLFactory(connection); Here is an example to illustrate what that means: -
We'll see how the aliasing works later in the section about
Many other frameworks have similar APIs with similar feature sets. Yet, what makes jOOQ special is its informal
Unlike other, simpler frameworks that use "fluent APIs" or "method chaining", jOOQ's BNF-based interface hierarchy will not allow bad query syntax. The following will not compile, for instance:
-
Historically, jOOQ started out as an object-oriented SQL builder library like any other. This meant that all queries and their syntactic components were modeled as so-called
This query is equivalent to the one shown before using the DSL syntax. In fact, internally, the DSL API constructs precisely this QueryObject. Note, that you can always access the SelectQuery object to switch between DSL and non-DSL APIs:
-
Note, that for historic reasons, the DSL API mixes mutable and immutable behaviour with respect to the internal representation of the
Mutability may be removed in a future version of jOOQ.
-
When you don't just perform
Details about the various clauses of this query will be provided in subsequent sections
@@ -1594,14 +1578,14 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
A very similar, but limited API is available, if you want to select from single physical tables in order to retrieve
As you can see, there is no way to further restrict/project the selected fields. This just selects all known TableFields in the supplied Table, and it also binds <R extends Record> to your Table's associated Record. An example of such a Query would then be:
-The simple SELECT API is limited in the way that it does not support any of these clauses: @@ -1615,41 +1599,39 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
In most parts of this manual, it is assumed that you do not use the simple SELECT API. For more information about the simple SELECT API, see the manual's section about
The SELECT clause lets you project your own record types, referencing table fields, functions, arithmetic expressions, etc. The Factory provides several methods for expressing a SELECT clause:
-Some commonly used projections can be easily created using convenience methods:
-
See more details about functions and expressions in the manual's section about
The SQL FROM clause allows for specifying any number of
Read more about aliasing in the manual's section about
Note, in order to access the DbmsXplan package, you can use the
Read more about dual or dummy tables in the manual's section about
jOOQ supports many different types of standard SQL JOIN operations:
@@ -1743,7 +1721,7 @@ new Factory(SQLDialect.POSTGRES).selectOne().getSQL();]]> All of these JOIN methods can be called on
The two syntaxes will produce the same SQL statement. However, calling "join" on
In case of ambiguity, you can also supply field references for your foreign keys, or the generated foreign key reference to the onKey() method. @@ -1805,137 +1781,130 @@ JOIN BOOK ON BOOK.AUTHOR_ID = AUTHOR.ID]]> Most often, you will provide jOOQ with JOIN conditions in the JOIN .. ON clause. SQL supports a different means of specifying how two tables are to be joined. This is the JOIN .. USING clause. Instead of a condition, you supply a set of fields whose names are common to both tables to the left and right of a JOIN operation. This can be useful when your database schema has a high degree of relational normalisation. An example:
-In schemas with high degrees of normalisation, you may also choose to use NATURAL JOIN, which takes no JOIN arguments as it joins using all fields that are common to the table expressions to the left and to the right of the JOIN operator. An example:
-Oracle SQL ships with a special syntax available for OUTER JOIN clauses. According to the Oracle documentation about partitioned outer joins this can be used to fill gaps for simplified analytical calculations. jOOQ only supports putting the PARTITION BY clause to the right of the OUTER JOIN clause. The following example will create at least one record per AUTHOR and per existing value in BOOK.PUBLISHED_IN, regardless if an AUTHOR has actually published a book in that year.
-
The WHERE clause can be used for JOIN or filter predicates, in order to restrict the data returned by the
The above syntax is convenience provided by jOOQ, allowing you to connect the
You will find more information about creating
The Oracle database knows a very succinct syntax for creating hierarchical queries: the CONNECT BY clause, which is fully supported by jOOQ, including all related functions and pseudo-columns. A more or less formal definition of this clause is given here:
-An example for an iterative query, iterating through values between 1 and 5 is this:
-Here's a more complex example where you can recursively fetch directories in your database, and concatenate them to a path:
-The output might then look like this
-
Some of the supported functions and pseudo-columns are these (available from the
GROUP BY can be used to create unique groups of data, to form aggregations, to remove duplicates and for other reasons. It will transform your previously defined
As defined in the SQL standard, when grouping, you may no longer project any columns that are not a formal part of the GROUP BY clause, or
This will return an arbitrary title per author. jOOQ supports this syntax, as jOOQ is not doing any checks internally, about the consistence of tables/fields/functions that you provide it.
@@ -2028,68 +1994,64 @@ GROUP BY AUTHOR_ID]]>
jOOQ supports empty GROUP BY () clauses as well. This will result in
Some databases support the SQL standard grouping functions and some extensions thereof. See the manual's section about
The HAVING clause is commonly used to further restrict data resulting from a previously issued
According to the SQL standard, you may omit the GROUP BY clause and still issue a HAVING clause. This will implicitly GROUP BY (). jOOQ also supports this syntax. The following example selects one record, only if there are at least 4 books in the books table:
-Databases are allowed to return data in any arbitrary order, unless you explicitly declare that order in the ORDER BY clause. In jOOQ, this is straight-forward:
-
Any jOOQ
Note, how one() is used as a convenience short-cut for inline(1) @@ -2117,87 +2078,84 @@ ORDER BY 1 ASC, 2 DESC]]> A few databases support the SQL standard "null ordering" clause in sort specification lists, to define whether NULL values should come first or last in an ordered result.
-
If your database doesn't support this syntax, jOOQ simulates it using a
Using
But writing these things can become quite verbose. jOOQ supports a convenient syntax for specifying sort mappings. The same query can be written in jOOQ as such:
-More complex sort indirections can be provided using a Map:
-Of course, you can combine this feature with the previously discussed NULLS FIRST / NULLS LAST feature. So, if in fact these two books are the ones you like least, you can put all NULLS FIRST (all the other books):
-@@ -2209,23 +2167,23 @@ ORDER BY CASE TITLE
jOOQ also supports Oracle's SIBLINGS keyword to be used with ORDER BY clauses for
While being extremely useful for every application that does paging, or just to limit result sets to reasonable sizes, this clause is not yet part of any SQL standard (up until SQL:2008). Hence, there exist a variety of possible implementations in various SQL dialects, concerning this limit clause. jOOQ chose to implement the LIMIT .. OFFSET clause as understood and supported by MySQL, H2, HSQLDB, Postgres, and SQLite. Here is an example of how to apply limits with jOOQ:
-This will limit the result to 1 books starting with the 2nd book (starting at offset 0!). limit() is supported in all dialects, offset() in all but Sybase ASE, which has no reasonable means to simulate it. This is how jOOQ simulates the above query in various SQL dialects:
-As you can see, jOOQ will take care of the incredibly painful ROW_NUMBER() OVER() (or ROWNUM for Oracle) filtering in subselects for you, you'll just have to write limit(1).offset(2) in any dialect. @@ -2270,39 +2228,37 @@ AND ROWNUM_98843777 <= 3
As can be seen in the above example, writing correct SQL can be quite tricky, depending on the SQL dialect. For instance, with SQL Server, you cannot have an ORDER BY clause in a subquery, unless you also have a TOP clause. This is illustrated by the fact that jOOQ renders a TOP 100 PERCENT clause for you. The same applies to the fact that ROW_NUMBER() OVER() needs an ORDER BY windowing clause, even if you don't provide one to the jOOQ query. By default, jOOQ adds ordering by the first column of your projection.
-For inter-process synchronisation and other reasons, you may choose to use the SELECT .. FOR UPDATE clause to indicate to the database, that a set of cells or records should be locked by a given transaction for subsequent updates. With jOOQ, this can be achieved as such:
-The above example will produce a record-lock, locking the whole record for updates. Some databases also support cell-locks using FOR UPDATE OF ..
-Oracle goes a bit further and also allows to specify the actual locking behaviour. It features these additional clauses, which are all supported by jOOQ: @@ -2316,15 +2272,15 @@ FOR UPDATE OF TITLE]]> With jOOQ, you can use those Oracle extensions as such:
-The SQL standard specifies a FOR UPDATE clause to be applicable for cursors. Most databases interpret this as being applicable for all SELECT statements. An exception to this rule are the CUBRID and SQL Server databases, that do not allow for any FOR UPDATE clause in a regular SQL SELECT statement. jOOQ simulates the FOR UPDATE behaviour, by locking record by record with JDBC. JDBC allows for specifying the flags TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE for any statement, and then using ResultSet.updateXXX() methods to produce a cell-lock / row-lock. Here's a simplified example in JDBC:
-The main drawback of this approach is the fact that the database has to maintain a scrollable cursor, whose records are locked one by one. This can cause a major risk of deadlocks or race conditions if the JDBC driver can recover from the unsuccessful locking, if two Java threads execute the following statements:
-So use this technique with care, possibly only ever locking single rows! @@ -2361,12 +2317,12 @@ SELECT * FROM author ORDER BY id DESC;]]>
Note, that jOOQ also supports optimistic locking, if you're doing simple CRUD. This is documented in the section's manual about
SQL allows to perform set operations as understood in standard set theory on result sets. These operations include unions, intersections, subtractions. For two subselects to be combinable by such a set operator, each subselect must return a
@@ -2394,76 +2349,73 @@ create.selectFrom(BOOK).where(BOOK.ID.equal(5)));]]>
As previously mentioned in the manual's section about the
If you are closely coupling your application to an Oracle (or CUBRID) database, you might need to be able to pass hints of the form /*+HINT*/ with your SQL statements to the Oracle database. For example:
-This can be done in jOOQ using the .hint() clause in your SELECT statement:
-Note that you can pass any string in the .hint() clause. If you use that clause, the passed string will always be put in between the SELECT [DISTINCT] keywords and the actual projection list
-The INSERT statement is used to insert new records into a database table. Records can either be supplied using a VALUES() constructor, or a SELECT statement. jOOQ supports both types of INSERT statements. An example of an INSERT statement using a VALUES() constructor is given here:
-The SQL standard specifies that multiple rows can be supplied to the VALUES() constructor in an INSERT statement. Here's an example of a multi-record INSERT
-jOOQ tries to stay close to actual SQL. In detail, however, Java's expressiveness is limited. That's why the values() clause is repeated for every record in multi-record inserts. @@ -2472,30 +2424,29 @@ VALUES (100, 'Hermann', 'Hesse'), Some RDBMS do not support inserting several records in a single statement. In those cases, jOOQ simulates multi-record INSERTs using the following SQL:
-MySQL (and some other RDBMS) allow for using a non-SQL-standard, UPDATE-like syntax for INSERT statements. This is also supported in jOOQ, should you prefer that syntax. The above INSERT statement can also be expressed as follows:
-
As you can see, this syntax is a bit more verbose, but also more type-safe, as every field can be matched with its value. Internally, the two syntaxes are strictly equivalent.
@@ -2506,12 +2457,12 @@ SELECT 101, 'Alfred', 'Döblin' FROM DUAL;
The MySQL database supports a very convenient way to INSERT or UPDATE a record. This is a non-standard extension to the SQL syntax, which is supported by jOOQ and simulated in other RDBMS, where this is possible (i.e. if they support the SQL standard
The Postgres database has native support for an INSERT .. RETURNING clause. This is a very powerful concept that is simulated for all other dialects using JDBC's
Some databases have poor support for returning generated keys after INSERTs. In those cases, jOOQ might need to issue another
The UPDATE statement is used to modify one or several pre-existing records in a database table. UPDATE statements are only possible on single tables. Support for multi-table updates will be implemented in the near future. An example update query is given here:
-The DELETE statement physically removes records from a database table. DELETE statements are only possible on single tables. Support for multi-table deletes will be implemented in the near future. An example delete query is given here:
-The MERGE statement is one of the most advanced standardised SQL constructs, which is supported by DB2, HSQLDB, Oracle, SQL Server and Sybase (MySQL has the similar INSERT .. ON DUPLICATE KEY UPDATE construct)
@@ -2613,15 +2560,14 @@ create.insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) The point of the standard MERGE statement is to take a TARGET table, and merge (INSERT, UPDATE) data from a SOURCE table into it. DB2, Oracle, SQL Server and Sybase also allow for DELETING some data and for adding many additional clauses. With jOOQ {jooq-version}, only Oracle's MERGE extensions are supported. Here is an example: -The H2 database ships with a somewhat less powerful but a little more intuitive syntax for its own version of the MERGE statement. An example more or less equivalent to the previous one can be seen here:
-
This syntax can be fully simulated by jOOQ for all other databases that support the SQL standard. For more information about the H2 MERGE syntax, see the documentation here:
http://www.h2database.com/html/grammar.html#merge
The TRUNCATE statement is the only DDL statement supported by jOOQ so far. It is popular in many databases when you want to bypass constraints for table truncation. Databases may behave differently, when a truncated table is referenced by other tables. For instance, they may fail if records from a truncated table are referenced, even with ON DELETE CASCADE clauses in place. Please, consider your database manual to learn more about its TRUNCATE implementation.
@@ -2669,44 +2614,42 @@ VALUES ('John', 'Hitchcock') The TRUNCATE syntax is trivial: -TRUNCATE is not supported by Ingres and SQLite. jOOQ will execute a DELETE FROM AUTHOR statement instead.
-The following sections explain the various types of table expressions supported by jOOQ
-
Most of the times, when thinking about a
The above example shows how AUTHOR and BOOK tables are joined in a
See the manual's section about
The strength of jOOQ's
As you can see in the above example, calling as() on generated tables returns an object of the same type as the table. This means that the resulting object can be used to dereference fields from the aliased table. This is quite powerful in terms of having your Java compiler check the syntax of your SQL statements. If you remove a column from a table, dereferencing that column from that table alias will cause compilation errors. @@ -2755,17 +2697,17 @@ create.select()
TODO document this
-
The
SQL and relational algebra distinguish between at least the following JOIN types (upper-case: SQL, lower-case: relational algebra):
@@ -2783,7 +2725,7 @@ create.select()
jOOQ supports all of these JOIN types (except semi-join and anti-join) directly on any
Note that most of jOOQ's JOIN operations give way to a similar DSL API hierarchy as previously seen in the manual's section about the
A
If you are closely coupling your application to an Oracle database, you can take advantage of some Oracle-specific features, such as the PIVOT clause, used for statistical analyses. The formal syntax definition is as follows:
-
The PIVOT clause is available from the
There is one operation in relational algebra that is not given a lot of attention, because it is rarely used in real-world applications. It is the relational division, the opposite operation of the cross product (or, relational multiplication). The following is an approximate definition of a relational division:
-With jOOQ, you can simplify using relational divisions by using the following syntax:
-The above roughly translates to
-Or in plain text: Find those TEXT values in C whose ID's correspond to all ID's in B. Note that from the above SQL statement, it is immediately clear that proper indexing is of the essence. Be sure to have indexes on all columns referenced from the on(...) and returning(...) clauses. @@ -2949,13 +2888,13 @@ WHERE NOT EXISTS (
The SQL standard specifies how SQL databases should implement ARRAY and TABLE types, as well as CURSOR types. Put simply, a CURSOR is a pointer to any materialised
Note, in order to access the DbmsXplan package, you can use the
The SQL standard specifies that the
Note, that some databases (H2, MySQL) can normally do without "dual". However, there exist some corner-cases with complex nested SELECT statements, where this will cause syntax errors (or parser bugs). To stay on the safe side, jOOQ will always render "dual" in those dialects.
-
Column expressions can be used in various SQL clauses in order to refer to one or several columns. This chapter explains how to form various types of column expressions with jOOQ. A particular type of column expression is given in the section about
In general, it is up to you whether you want to use the "prefix" notation or the "postfix" notation to create new column expressions based on existing ones. The "SQL way" would be to use the "prefix notation", with functions created from the
Table columns are the most simple implementations of a
Table columns implement a more specific interface called
See the manual's section about
Just like
Here is how it's done with jOOQ:
-When you alias Fields like above, you can access those Fields' values using the alias name:
-jOOQ's source code generator tries to find the most accurate type mapping between your vendor-specific data types and a matching Java type. For instance, most VARCHAR, CHAR, CLOB types will map to String. Most BINARY, BYTEA, BLOB types will map to byte[]. NUMERIC types will default to java.math.BigDecimal, but can also be any of java.math.BigInteger, Long, Integer, Short, Byte, Double, Float.
@@ -3136,27 +3070,27 @@ System.out.println("Books : " + record.getValue("books")); Sometimes, this automatic mapping might not be what you needed, or jOOQ cannot know the type of a field. In those cases you would write SQL type CASTs like this: -in jOOQ, you can write something like that:
-The same thing can be achieved by casting a Field directly to String.class, as TEXT is the default data type in Postgres to map to Java's String
-The complete CAST API in Field consists of these three methods:
-
Your database can do the math for you. Arithmetic operations are implemented just like
In order to express a SQL query like this one:
-You can write something like this in jOOQ:
-jOOQ also supports the Oracle-style syntax for adding days to a Field<? extends java.util.Date>
-For more advanced datetime arithmetic, use the Factory's timestampDiff() and dateDiff() functions, as well as jOOQ's built-in SQL standard INTERVAL data type support: @@ -3219,31 +3151,30 @@ public class Factory {
The SQL standard defines the concatenation operator to be an infix operator, similar to the ones we've seen in the chapter about ||. Some other dialects do not support this operator, but expect a concat() function, instead. jOOQ renders the right operator / function, depending on your
There are a variety of general functions supported by jOOQ As discussed in the chapter about
Please refer to the
Math can be done efficiently in the database before returning results to your Java application. In addition to the
Please refer to the
Interestingly, bitwise functions and bitwise arithmetic is not very popular among SQL databases. Most databases only support a few bitwise operations, while others ship with the full set of operators. jOOQ's API includes most bitwise operations as listed below. In order to avoid ambiguities with
String formatting can be done efficiently in the database before returning results to your Java application. As discussed in the chapter about
Note that the SQL standard specifies that patterns should follow the XQuery standards. In the real world, the POSIX regular expression standard is the most used one, some use Java regular expressions, and only a few ones use Perl regular expressions. jOOQ does not make any assumptions about regular expression syntax. For cross-database compatibility, please read the relevant database manuals carefully, to learn about the appropriate syntax. Please refer to the
This is a list of date and time functions supported by jOOQ's
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. See the manual's section about
This is a list of system functions supported by jOOQ's
Aggregate functions work just like functions, even if they have a slightly different semantics. Here are some example aggregate functions from the
Here's an example, counting the number of books any author has written:
-
Aggregate functions have strong limitations about when they may be used and when not. For instance, you can use aggregate functions in scalar queries. Typically, this means you only select aggregate functions, no
Oracle and some other databases support "ordered aggregate functions". This means you can provide an ORDER BY clause to an aggregate function, which will be taken into consideration when aggregating. The best example for this is Oracle's LISTAGG() (also known as GROUP_CONCAT in other
The above query might yield:
-
Oracle allows for restricting aggregate functions using the KEEP() clause, which is supported by jOOQ. In Oracle, some aggregate functions (MIN, MAX, SUM, AVG, COUNT, VARIANCE, or STDDEV) can be restricted by this clause, hence
@@ -3533,12 +3461,12 @@ GROUP BY AUTHOR_ID
In those databases that support over() on it. See the manual's section about
Most major RDBMS support the concept of window functions. jOOQ knows of implementations in DB2, Oracle, Postgres, SQL Server, and Sybase SQL Anywhere, and supports most of their specific syntaxes. Note, that H2 and HSQLDB have implemented ROW_NUMBER() functions, without true windowing support.
over() method. See the chapter about
SQL distinguishes between various window function types (e.g. "ranking functions"). Depending on the function, SQL expects mandatory PARTITION BY or ORDER BY clauses within the OVER() clause. jOOQ does not enforce those rules for two reasons:
@@ -3583,7 +3511,7 @@ GROUP BY AUTHOR_ID
Here are some simple examples of window functions with jOOQ:
Window functions can be used for things like calculating a "running total". The following example fetches transactions and the running total for every transaction going back to the beginning of the transaction table (ordered by booked_at). Window functions are accessible from the previously seen over() method:
LISTAGG(). These functions have a window function / analytical function variant, as well. For example:
-
In the previous chapter about
User-defined aggregate functions also implement over(). This is supported by Oracle in particular. See the manual's section about
The SQL standard defines special functions that can be used in the
In English, the ROLLUP() grouping function provides N+1 groupings, when N is the number of arguments to the ROLLUP() function. Each grouping has an additional group field from the ROLLUP() argument field list. The results of the second query might look something like this:
CUBE() is different from ROLLUP() in the way that it doesn't just create N+1 groupings, it creates all 2^N possible combinations between all group fields in the CUBE() function argument list. Let's re-consider our second query from before:
The results would then hold:
-
@@ -3804,7 +3726,7 @@ ORDER BY 1 NULLS FIRST, 2 NULLS FIRST
jOOQ fully supports all of these functions, as well as the utility functions GROUPING() and GROUPING_ID(), used for identifying the grouping set ID of a record. The
MySQL and CUBRID don't know any grouping functions, but they support a WITH ROLLUP clause, that is equivalent to simple ROLLUP() grouping functions. jOOQ simulates ROLLUP() in MySQL and CUBRID, by rendering this WITH ROLLUP clause. The following two statements mean the same:
Some databases support user-defined functions, which can be embedded in any SQL statement, if you're using jOOQ's
The above function will be made available from a generated
Note that user-defined functions returning
Some databases support user-defined aggregate functions, which can then be used along with
The above OBJECT type is then available to function declarations as such:
-
jOOQ's
The CASE expression is part of the standard SQL syntax. While some RDBMS also offer an IF expression, or a DECODE function, you can always rely on the two types of CASE syntax:
-
In jOOQ, both syntaxes are supported (The second one is simulated in Derby, which only knows the first one). Unfortunately, both case and else are reserved words in Java. jOOQ chose to use decode() from the Oracle DECODE function, and otherwise(), which means the same as else.
@@ -3989,15 +3907,15 @@ create.decode().value(AUTHOR.FIRST_NAME)
A CASE expression can be used anywhere where you can place a
Oracle knows a more succinct, but maybe less readable DECODE() function with a variable number of arguments. This function roughly does the same as the second case expression syntax. jOOQ supports the DECODE() function and simulates it using CASE expressions in all dialects other than Oracle:
-
Sort indirection is often implemented with a CASE clause of a SELECT's ORDER BY clause. See the manual's section about the
Sequences implement the
So if you have a sequence like this in Oracle:
-
You can then use your
According to the SQL standard, row value expressions can have a degree of more than one. This is commonly used in the
@@ -4093,14 +4010,14 @@ public static RowN row(Object... values) { ... }]]>
The
Conditions or conditional expressions are widely used in SQL and in the jOOQ API. They can be used in
@@ -4137,22 +4054,21 @@ public static RowN row(Object... values) { ... }]]>
Note that jOOQ does not model these values as actual
With jOOQ, most
@@ -4168,48 +4084,47 @@ BOOK.TITLE.notEqual("Animal Farm")]]>
Conditions can also be connected using
In SQL, as in most other languages,
The above example shows that the number of parentheses in Java can quickly explode. Proper indentation may become crucial in making such code readable. In order to understand how jOOQ composes combined conditional expressions, let's assign component expressions first:
-
Here are all boolean operators on the
In SQL, comparison predicates are formed using common comparison operators:
@@ -4248,7 +4162,7 @@ not() // Invert a condition (synonym for Factory.not(Conditi Unfortunately, Java does not support operator overloading, hence these operators are also implemented as methods in jOOQ, like any other SQL syntax elements. The relevant parts of the
Note that every operator is represented by two methods. A verbose one (such as equal()) and a two-character one (such as eq()). Both methods are the same. You may choose either one, depending on your taste. The manual will always use the more verbose one.
@@ -4281,30 +4195,28 @@ ge or greaterOrEqual(Select
If the right-hand side of a
For the example, the right-hand side of the quantified comparison predicates were filled with argument lists. But it is easy to imagine that the source of values results from a
Typically, the
In SQL, you cannot compare NULL with any value using
Some databases support the DISTINCT predicate, which serves as a convenient, NULL-safe
If your database does not natively support the DISTINCT predicate, jOOQ simulates it with an equivalent
The BETWEEN predicate can be seen as syntactic sugar for a pair of
Note the inclusiveness of range boundaries in the definition of the BETWEEN predicate. Intuitively, this is supported in jOOQ as such:
-The SQL standard defines the SYMMETRIC keyword to be used along with BETWEEN to indicate that you do not care which bound of the range is larger than the other. A database system should simply swap range bounds, in case the first bound is greater than the second one. jOOQ supports this keyword as well, simulating it if necessary.
-The simulation is done trivially:
-LIKE predicates are popular for simple wildcard-enabled pattern matching. Supported wildcards in all SQL databases are:
@@ -4452,24 +4356,22 @@ BOOK.PUBLISHED_IN.notBetweenSymmetric(1940).and(1920)]]> With jOOQ, the LIKE predicate can be created from anyOften, your pattern may contain any of the wildcard characters "_" and "%", in case of which you may want to escape them. jOOQ does not automatically escape patterns in like() and notLike() methods. Instead, you can explicitly define an escape character as such:
-In the above predicate expressions, the exclamation mark character is passed as the escape character to escape wildcard characters "!_" and "!%", as well as to escape the escape character itself: "!!" @@ -4483,7 +4385,7 @@ BOOK.TITLE.notLike("%The !%-Sign Book%", '!')]]> In addition to the above, jOOQ provides a few convenience methods for common operations performed on strings using the LIKE predicate. Typical operations are "contains predicates", "starts with predicates", "ends with predicates", etc. Here is the full convenience API wrapping LIKE predicates:
-Note, that jOOQ escapes % and _ characters in value in some of the above predicate implementations. For simplicity, this has been omitted in this manual.
-
In SQL, apart from comparing a value against several values, the IN predicate can be used to create semi-joins or anti-joins. jOOQ knows the following methods on the
A sample IN predicate might look like this:
-
A good way to prevent this from happening is to use the
Slightly less intuitive, yet more powerful than the previously discussed
Note that in SQL, the projection of a subselect in an EXISTS predicate is irrelevant. To help you write queries like the above, you can use jOOQ's selectZero() or selectOne()
http://blog.jooq.org/2012/07/27/not-in-vs-not-exists-vs-left-join-is-null-mysql/
When comparing dates, the SQL standard allows for using a special OVERLAPS predicate, which checks whether two date ranges overlap each other. The following can be said:
-
jOOQ supports the OVERLAPS predicate on
This allows for expressing the above predicates as such:
-
Unlike the standard (or any database implementing the standard), jOOQ also supports the OVERLAPS predicate for comparing arbitrary
A DSL is a nice thing to have, it feels "fluent" and "natural", especially if it models a well-known language, such as SQL. But a DSL is always expressed in a host language (Java in this case), which was not made for exactly the same purposes as its hosted DSL. If it were, then jOOQ would be implemented on a compiler-level, similar to LINQ in .NET. But it's not, and so, the DSL is limited by language constraints of its host language. We have seen many functionalities where the DSL becomes a bit verbose. This can be especially true for:
@@ -4676,7 +4574,7 @@ row(Date.valueOf('2010-01-01'), new DayToSecond(2)).overlaps(Date.valueOf('2010-
Plain SQL API methods are usually overloaded in three ways. Let's look at the condition query part constructor:
Please refer to the
Apart from the general factory methods, plain SQL is also available in various other contexts. For instance, when adding a .where("a = b") clause to a query. Hence, there exist several convenience methods where plain SQL can be inserted usefully. This is an example displaying all various use-cases in one single query:
-@@ -4784,12 +4682,12 @@ create.select(LAST_NAME, COUNT1, COUNT2)
Bind values are used in SQL / JDBC for various reasons. Among the most obvious ones are:
@@ -4808,31 +4706,31 @@ create.select(LAST_NAME, COUNT1, COUNT2)The following sections explain how you can introduce bind values in jOOQ, and how you can control the way they are rendered and bound to SQL.
-JDBC only knows indexed bind values. A typical example for using bind values with JDBC is this:
-With dynamic SQL, keeping track of the number of question marks and their corresponding index may turn out to be hard. jOOQ abstracts this and lets you provide the bind value right where it is needed. A trivial example is this:
-
Note the using of
You can also extract specific bind values by index from a query, if you wish to modify their underlying value after creating a query. This can be achieved as such:
-
For more details about jOOQ's internals, see the manual's section about
Some SQL access abstractions that are built on top of JDBC, or some that bypass JDBC may support named parameters. jOOQ allows you to give names to your parameters as well, although those names are not rendered to SQL strings by default. Here is an example of how to create named parameters using the
The
In order to actually render named parameter names in generated SQL, use the
Sometimes, you may wish to avoid rendering bind variables while still using custom values in SQL. jOOQ refers to that as "inlined" bind values. When bind values are inlined, they render the actual value in SQL rather than a JDBC question mark. Bind value inlining can be achieved in two ways:
@@ -4939,7 +4836,7 @@ WHERE LAST_NAME = :lastName]]> In both cases, your inlined bind values will be properly escaped to avoid SQL syntax errors and SQL injection. Some examples: -
Special care needs to be taken when using
All methods in the jOOQ API that allow for plain (unescaped, untreated) SQL contain a warning message in their relevant Javadoc, to remind you of the risk of SQL injection in what is otherwise a SQL-injection-safe API.
-
A
Every
The following additional methods are inherited from a common
A simple example can be provided by checking out jOOQ's internal representation of a (simplified)
This is how jOOQ renders such a condition:
-
See the manual's sections about
As mentioned in the previous chapter about
And then, use the above factory to render pretty-printed SQL:
-
The section about
Every
Some additional methods are inherited from a common
This is how jOOQ binds values on such a condition:
-
See the manual's sections about
If a SQL clause is too complex to express with jOOQ, you can extend either one of the following types for use directly in a jOOQ query:
-
These classes are declared public and covered by jOOQ's integration tests. When you extend these classes, you will have to provide your own implementations for the
The above contract may be a bit tricky to understand at first. The best thing is to check out jOOQ source code and have a look at a couple of QueryParts, to see how it's done. Here's an example
If you don't need the integration of rather complex QueryParts into jOOQ, then you might be safer using simple
The above technique allows for creating rather complex SQL clauses that are currently not supported by jOOQ, without extending any of the
The only transient, non-serializable element in any jOOQ object is the
Another way of attaching QueryParts automatically, or rather providing them with a new
jOOQ-Scala is a maven module used for leveraging some advanced Scala features for those users that wish to use jOOQ with Scala.
@@ -5365,7 +5259,7 @@ create.attach(select);]]> The following depicts a trait which wraps all fields: -The following depicts a trait which wraps numeric fields:
-An example query using such overloaded operators would then look like this:
-This feature is still being experimented with. With Scala Macros, it might be possible to inline a true SQL dialect into the Scala syntax, backed by the jOOQ API. Stay tuned!
-
In a previous section of the manual, we've seen how jOOQ can be used to
The following sections of this manual will show how jOOQ is wrapping JDBC for SQL execution
-
Even if there are
Unlike JDBC, jOOQ has a lot of knowledge about a SQL query's structure and internals (see the manual's section about
Fetching is something that has been completely neglegted by JDBC and also by various other database abstraction libraries. Fetching is much more than just looping or listing records or mapped objects. There are so many ways you may want to fetch data from a database, it should be considered a first-class feature of any database abstraction API. Just to name a few, here are some of jOOQ's fetching modes:
@@ -5586,7 +5479,7 @@ Result
These means of fetching are also available from
These means of fetching are also available from
Note, that apart from the
jOOQ understands that SQL is much more expressive than Java, when it comes to the declarative typing of
When you use the
By default, jOOQ returns an
Note that most of these convenience methods are available both through
In a more functional operating mode, you might want to write callbacks that receive records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
See also the manual's section about the
In a more functional operating mode, you might want to write callbacks that map records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
See also the manual's section about the
Fetching data in records is fine as long as your application is not really layered, or as long as you're still writing code in the DAO layer. But if you have a more advanced application architecture, you may not want to allow for jOOQ artefacts to leak into other layers. You may choose to write POJOs (Plain Old Java Objects) as your primary DTOs (Data Transfer Objects), without any dependencies on jOOQ's
Just as with any other JPA implementation, you can put the
Please refer to the
Please refer to the
Please refer to the
Note: Because of your manual setting of ID = 10, jOOQ's store() method will asume that you want to insert a new record. See the manual's section about
jOOQ currently doesn't support more complex data structures, the way Hibernate/JPA attempt to map relational data onto POJOs. While future developments in this direction are not excluded, jOOQ claims that generic mapping strategies lead to an enormous additional complexity that only serves very few use cases. You are likely to find a solution using any of jOOQ's various More complex data structures
Unlike JDBC's
As a
Many databases support returning several result sets, or cursors, from single queries. An example for this is Sybase ASE's sp_help command:
-The correct (and verbose) way to do this with JDBC is as follows:
-
As previously discussed in the chapter about
Using generics, the resulting structure is immediately clear.
-
Some queries take very long to execute, yet they are not crucial for the continuation of the main program. For instance, you could be generating a complicated report in a Swing application, and while this report is being calculated in your database, you want to display a background progress bar, allowing the user to pursue some other work. This can be achived simply with jOOQ, by creating a
Note, that instead of letting jOOQ spawn a new thread, you can also provide jOOQ with your own
When interacting with legacy applications, you may prefer to have jOOQ return a
Instead of operating on a JDBC ResultSet holding an open resource from your database, you can also let jOOQ's
The inverse of the above is possible too. Maybe, a legacy part of your application produces JDBC
Apart from a few extra features (see the manual's section about
Such a converter can be used in many parts of the jOOQ API. Some examples have been illustrated in the manual's section about
jOOQ ships with a built-in default
jOOQ also allows for generated source code to reference your own custom converters, in order to permanently replace a
With JDBC, you have full control over your SQL statements. You can decide yourself, if you want to execute a static
@@ -6345,12 +6233,12 @@ inlined.select(val(1)).where(val(1).equal(1)).fetch();]]>
Note that you don't have to inline all your bind values at once. If you know that a bind value is not really a variable and should be inlined explicitly, you can do so by using
As previously discussed in the chapter about
The above technique can be quite useful when you want to reuse expensive database resources. This can be the case when your statement is executed very frequently and your database would take non-negligible time to soft-parse the prepared statement and generate a new statement / cursor resource. @@ -6387,7 +6275,7 @@ finally { This is also modeled in jOOQ. However, the difference to JDBC is that closing a statement is the default action, whereas keeping it open has to be configured explicitly. This is better than JDBC, because the default action should be the one that is used most often. Keeping open statements is rarely done in average applications. Here's an example of how to keep open PreparedStatements with jOOQ:
-The above example shows how a query can be executed twice against the same underlying PreparedStatement. Unlike in other execution scenarios, you must not forget to close this query now
-With JDBC, you can easily execute several statements at once using the addBatch() method. Essentially, there are two modes in JDBC
@@ -6422,7 +6310,7 @@ finally {In code, this looks like the following snippet:
-
Most databases support sequences of some sort, to provide you with unique values to be used for primary keys and other enumerations. If you're using jOOQ's
You can inline sequence references in jOOQ SQL statements. The following are examples of how to do that:
-
For more info about inlining sequence references in SQL statements, please refer to the manual's section about
Many RDBMS support the concept of "routines", usually calling them procedures and/or functions. These concepts have been around in programming languages for a while, also outside of databases. Famous languages distinguishing procedures from functions are:
@@ -6566,14 +6453,14 @@ create.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) If you're using jOOQ'sThe generated artefacts can then be used as follows:
-But you can also call the procedure using a generated convenience method in a global Routines class:
-
For more details about
The generated artefacts can then be used as follows:
-
For more info about inlining stored function references in SQL statements, please refer to the manual's section about
Oracle uses the concept of a PACKAGE to group several procedures/functions into a sort of namespace. The SQL 92 standard talks about "modules", to represent this concept, even if this is rarely implemented as such. This is reflected in jOOQ by the use of Java sub-packages in the
For more details about
Oracle UDTs can have object-oriented structures including member functions and procedures. With Oracle, you can do things like this:
-These member functions and procedures can simply be mapped to Java methods:
-
For more details about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's exporting functionality (see also the
The above query will result in an XML document looking like the following one:
-
The same result as an
See the XSD schema definition here, for a formal definition of the XML export format:
http://www.jooq.org/xsd/jooq-export-1.6.2.xsd
The above query will result in a CSV document looking like the following one:
-In addition to the standard behaviour, you can also specify a separator character, as well as a special string to represent NULL values (which cannot be represented in standard CSV):
-The above query will result in a JSON document looking like the following one:
-Note: This format has changed in jOOQ 2.6.0
-The above query will result in an HTML document looking like the following one
-The above query will result in a text document looking like the following one
-
A simple text representation can also be obtained by calling toString() on a Result object. See also the manual's section about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's importing functionality (see also exporting functionality). You can import data directly into a table from the formats described in the subsequent sections of this manual.
-
The below CSV data represents two author records that may have been exported previously, by jOOQ's
With jOOQ, you can load this data using various parameters from the loader API. A simple load may look like this:
-Here are various other examples:
-Any of the above configuration methods can be combined to achieve the type of load you need. Please refer to the API's Javadoc to learn about more details. Errors that occur during the load are reported by the execute method's result:
-This is not yet supported
-Your database application probably consists of 50% - 80% CRUD, whereas only the remaining 20% - 50% of querying is actual querying. Most often, you will operate on records of tables without using any advanced relational concepts. This is called CRUD for
@@ -6998,13 +6869,13 @@ Query query = error.query();]]> In normalised databases, every table has a primary key by which a tuple/record within that table can be uniquely identified. In simple cases, this is a (possibly auto-generated) number called ID. But in many cases, primary keys include several non-numeric columns. An important feature of such keys is the fact that in most databases, they are enforced using an index that allows for very fast random access to the table. A typical way to access / modify / delete a book is this: -
Normalised databases assume that a primary key is unique "forever", i.e. that a key, once inserted into a table, will never be changed or re-inserted after deletion. In order to use jOOQ's
For simplicity, the term "primary key" will be used in the sense of such a "main unique key" in this manual.
-
If you're using jOOQ's
See the manual's section about
Some remarks about storing: @@ -7086,11 +6957,11 @@ book2.store();]]> Deleting a record will remove it from the database. Here's how you delete records:
-@@ -7100,53 +6971,52 @@ book.delete();]]> In order to perform a refresh, use the following Java code:
-
CRUD operations can be combined with regular querying, if you select records from single database tables, as explained in the manual's section about
Many databases support the concept of IDENTITY values, or
If you're using jOOQ's
@@ -7155,8 +7025,8 @@ System.out.println(book.getId());]]>
These SQL dialects implement the standard very neatly.
-H2, MySQL, Postgres, SQL Server, Sybase ASE, Sybase SQL Anywhere @@ -7164,7 +7034,7 @@ id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1)]]>
These SQL dialects implement identites, but the DDL syntax doesn’t follow the standard
-Oracle @@ -7188,7 +7058,7 @@ id INTEGER NOT NULL IDENTITY]]>
Oracle does not know any identity columns at all. Instead, you will have to use a trigger and update the ID column yourself, using a custom sequence. Something along these lines:
-Note, that this approach can be employed in most databases supporting sequences and triggers! It is a lot more flexible than standard identities
-
Tables without UNIQUE keys are considered non-updatable by jOOQ, as jOOQ has no way of uniquely identifying such a record within the database. If you're using jOOQ's
Note, that some databases use internal rowid or object-id values to identify such records. jOOQ does not support these vendor-specific record meta-data.
-
jOOQ allows you to perform
If you're using jOOQ's
The MODIFIED column will contain a timestamp indicating the last modification timestamp for any book in the BOOK table. If you're using jOOQ and it's
As before, without the added TIMESTAMP column, optimistic locking is transparent to the API. @@ -7356,17 +7224,17 @@ book2.store();]]>
Note, for explicit pessimistic locking, please consider the manual's section about the
When inserting, updating, deleting a lot of records, you may wish to profit from JDBC batch operations, which can be performed by jOOQ. These are available through jOOQ's
Internally, jOOQ will render all the required SQL statements and execute them as a regular
If you're using jOOQ's
corresponds to the DAO's related generated POJO type
// getType();
-}]]>
Besides these base methods, generated DAO classes implement various useful fetch methods. An incomplete example is given here, for the BOOK table:
-Note that you can further subtype those pre-generated DAO classes, to add more useful DAO methods to them. Using such a DAO is simple:
-This is an eternal and religious debate. Pros and cons have been discussed time and again, and it still is a matter of taste, today. In this case, jOOQ clearly takes a side. jOOQ's exception strategy is simple: @@ -7502,12 +7368,12 @@ bookDao.delete(book);]]>
The following section about
The
Now, configure jOOQ's runtime to load your listener
-And log results any time with a snippet like this:
-This may result in the following log output:
-
Please read the
See also the manual's sections about
jOOQ logs all SQL queries and fetched result sets to its internal DEBUG logger, which is implemented as an
With the above configuration, let's fetch some data with jOOQ
-The above query may result in the following log output:
-Essentially, jOOQ will log @@ -7682,12 +7548,12 @@ Finishing : Total: 4.814ms, +3.375ms
If you wish to use your own logger (e.g. avoiding printing out sensitive data), you can deactivate jOOQ's logger using
Many users may have switched from higher-level abstractions such as Hibernate to jOOQ, because of Hibernate's hard-to-manage performance, when it comes to large database schemas and complex second-level caching strategies. jOOQ is not a lightweight database abstraction framework, and it comes with its own overhead. Please be sure to consider the following points:
@@ -7702,14 +7568,14 @@ Finishing : Total: 4.814ms, +3.375msDon't be put off by the above paragraphs. You should optimise wisely, i.e. only in places where you really need very high throughput to your database. jOOQ's overhead compared to plain JDBC is typically less than 1ms per query.
-
While optional, source code generation is one of jOOQ's main assets if you wish to increase developer productivity. jOOQ's code generator takes your database schema and reverse-engineers it into a set of Java classes modelling
The following chapters will show how to configure the code generator and how to generate various artefacts.
-There are three binaries available with jOOQ, to be downloaded from SourceForge or from Maven central:
@@ -7755,7 +7621,7 @@ Finishing : Total: 4.814ms, +3.375ms You need to tell jOOQ some things about your database connection. Here's an example of how to do it for an Oracle database -
There are also lots of advanced configuration parameters, which will be treated in the
@@ -7842,7 +7708,7 @@ Finishing : Total: 4.814ms, +3.375ms
Code generation works by calling this class with the above property file as argument.
Be sure that these elements are located on the classpath: @@ -7905,7 +7771,7 @@ Finishing : Total: 4.814ms, +3.375ms You can also use an ant task to generate your classes. As a rule of thumb, remove the dots "." and dashes "-" from the .properties file's property names to get the ant task's arguments:
-Note that when running code generation with ant's <java/> task, you may have to set fork="true":
-Using the official jOOQ-codegen-maven plugin, you can integrate source code generation in your Maven build process:
-
See the full example of a pom.xml including the jOOQ-codegen artefact here:
@@ -8012,7 +7878,7 @@ Finishing : Total: 4.814ms, +3.375ms
Before jOOQ 2.0.4, the code generator was configured using properties files. These files are still supported for source code generation, but their syntax won't be maintained any longer. If you wish to migrate to XML, you can migrate the file using this command on the command line
Using the migrate flag, jOOQ will read the properties file and output a corresponding XML file on system out @@ -8022,17 +7888,17 @@ Finishing : Total: 4.814ms, +3.375ms
Be sure, both jooq-{jooq-version}.jar and your generated package (see configuration) are located on your classpath. Once this is done, you can execute SQL statements with your generated classes.
-
In the
The following example shows how you can override the DefaultGeneratorStrategy to render table and column names the way they are defined in the database, rather than switching them to camel case:
-Within the <generator/> element, there are other configuration elements:
-Check out the some of the manual's "advanced" sections to find out more about the advanced configuration parameters. @@ -8229,7 +8095,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { Also, you can add some optional advanced configuration parameters for the generator:
-For increased convenience at the use-site, jOOQ generates "global" artefacts at the code generation root location, referencing tables, routines, sequences, etc. In detail, these global artefacts include the following:
@@ -8312,7 +8177,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { When referencing global artefacts from your client application, you would typically static import them as such: -
Every table in your database will generate a
@@ -8371,17 +8235,17 @@ create.insertInto(com.example.generated.Tables.MY_TABLE)
Table generation cannot be deactivated
-
Every table in your database will generate a
@@ -8442,17 +8306,17 @@ implements IBook {
Record generation can be deactivated using the records flag
-Every table in your database will generate a POJO implementation that looks like this:
-@@ -8505,24 +8369,24 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the pojos flag
-Every table in your database will generate an interface that looks like this:
-@@ -8537,18 +8401,18 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the interfaces flag
-
Every table in your database will generate a
DAO generation can be activated using the daos flag
-
Every sequence in your database will generate a
Sequence generation cannot be deactivated
-
Every procedure or function (routine) in your database will generate a
@@ -8645,17 +8509,17 @@ public class Book implements java.io.Serializable
Routine generation cannot be deactivated
-
Every UDT in your database will generate a
Besides the
UDT generation cannot be deactivated
-NOTE: This feature is deprecated in jOOQ 2.5.0 and will be removed as of jOOQ 3.0
@@ -8711,7 +8575,7 @@ public class Book implements java.io.Serializable As previously discussed, you can configure master data tables as follows: -The results of this will be a Java enum that looks similar to this:
-In the above example, you can see how the configured primary key is mapped to the id member, the configured literal column is mapped to the cd member and the configured description member is mapped to the description member and output as Javadoc. In other words, T_LANGUAGE is a table with 4 rows and at least three columns. @@ -8781,39 +8645,39 @@ public class Book implements java.io.Serializable The point of MasterDataTypes in jOOQ is that they behave exactly like true ENUM types. When the above T_LANGUAGE table is referenced by T_BOOK, instead of generating foreign key navigation methods and a LANGUAGE_ID Field<Integer>, a Field<TLanguage> is generated:
-Which can then be used in the BookRecord directly:
-You can use master data types when you're actually mapping master data to a Java enum. When the underlying table changes frequently, those updates will not be reflected by the statically generated code. Also, be aware that it will be difficult to perform actual JOIN operations on the underlying table with jOOQ, once the master data type is generated.
-
When using a custom type in jOOQ, you need to let jOOQ know about its associated
The above configuration will lead to AUTHOR.DATE_OF_BIRTH being generated like this:
-
This means that the bound type of <T> will be GregorianCalendar, wherever you reference DATE_OF_BIRTH. jOOQ will use your custom converter when binding variables and when fetching data from
We've seen previously in the chapter about
These chapters hold some information about tools to be used with jOOQ
-
The
Or when using programmatic settings:
-The in-process mode is useful for Swing applications or other, locally run Java programs accessing the database via jOOQ. In order to launch the jOOQ Console "in-process", specify the previously documented settings and launch the Console as follows:
-Only in the in-process mode, you can execute ad-hoc queries directly from the console, if you provide it with proper DatabaseDescriptor. These queries are executed from the Editor pane which features: @@ -9016,36 +8876,36 @@ catch (Exception ignore) {} In J2EE or other server/client environments, you may not be able to run the console in the same process as your application. You can then run the jOOQ Console in "headless" mode. In addition to the previously documented settings, you'll have to start a debugger server in your application process, that the console can connect to:
-Now start your application along with the debugger server and launch the console with this command:
-Depending on your distribution, you may have to manually add rsyntaxtextarea-1.5.0.jar and jOOQ artefacts on your classpath.
-These chapters hold some general jOOQ reference information
-Every RDMBS out there has its own little specialties. jOOQ considers those specialties as much as possible, while trying to standardise the behaviour in jOOQ. In order to increase the quality of jOOQ, some 70 unit tests are run for syntax and variable binding verification, as well as some 180 integration tests with an overall of around 1200 queries for any of these databases: @@ -9104,12 +8964,12 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
This section will soon contain a feature matrix, documenting what feature is available for which database.
-There is always a small mismatch between SQL data types and Java data types. This is for two reasons:
@@ -9120,21 +8980,21 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);This chapter should document the most important notes about SQL, JDBC and jOOQ data types.
-jOOQ currently doesn't explicitly support JDBC BLOB and CLOB data types. If you use any of these data types in your database, jOOQ will map them to byte[] and String instead. In simple cases (small data), this simplification is sufficient. In more sophisticated cases, you may have to bypass jOOQ, in order to deal with these data types and their respective resources. True support for LOBs is on the roadmap, though.
-
Some databases explicitly support unsigned integer data types. In most normal JDBC-based applications, they would just be mapped to their signed counterparts letting bit-wise shifting and tweaking to the user. jOOQ ships with a set of unsigned
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. SQL knows two different types of intervals:
@@ -9183,51 +9043,51 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);XML data types are currently not supported
-Geospacial data types are currently not supported
-Some databases support cursors returned from stored procedures. They are mapped to the following jOOQ data type:
-
In fact, such a cursor will be fetched immediately by jOOQ and wrapped in an
The SQL standard specifies ARRAY data types, that can be mapped to Java arrays as such:
-The above array type is supported by these SQL dialects: @@ -9242,23 +9102,23 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
Oracle has strongly-typed arrays and table types (as opposed to the previously seen anonymously typed arrays). These arrays are wrapped by
This chapter will soon contain an overview over jOOQ's API using a pseudo BNF notation.
-jOOQ lives in a very challenging ecosystem. The Java to SQL interface is still one of the most important system interfaces. Yet there are still a lot of open questions, best practices and no "true" standard has been established. This situation gave way to a lot of tools, APIs, utilities which essentially tackle the same problem domain as jOOQ. jOOQ has gotten great inspiration from pre-existing tools and this section should give them some credit. Here is a list of inspirational tools in alphabetical order:
@@ -9271,7 +9131,7 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);This manual is divided into six main sections:
@@ -88,12 +88,12 @@ -Java and SQL have come a long way. SQL is an "ancient", yet established and well-understood technology. Java is a legacy too, although its platform JVM allows for many new and contemporary languages built on top of it. Yet, after all these years, libraries dealing with the interface between SQL and Java have come and gone, leaving JPA to be a standard that is accepted only with doubts, short of any surviving options. @@ -126,18 +126,18 @@
SQL was never meant to be abstracted. To be confined in the narrow boundaries of heavy mappers, hiding the beauty and simplicity of relational data. SQL was never meant to be object-oriented. SQL was never meant to be anything other than... SQL!
-This section lists the various licenses that apply to different versions of jOOQ. Prior to version 3.2, jOOQ was shipped for free under the terms of the Apache Software License 2.0. With jOOQ 3.2, jOOQ became dual-licensed: Apache Software License 2.0 (for use with Open Source databases) and commercial (for use with commercial databases). Please contact sales@datageekery.com, should you have any questions regarding licensing.
Throughout the manual, the above trademarks are referenced without a formal ® (R) or ™ (TM) symbol. It is believed that referencing third-party trademarks in this manual or on the jOOQ website constitutes "fair use". Please contact us if you think that your trademark(s) are not properly attributed.
-These chapters contain a quick overview of how to get started with this manual and with jOOQ. While the subsequent chapters contain a lot of reference information, this chapter here just wraps up the essentials.
-This section helps you correctly interpret this manual in the context of jOOQ.
@@ -279,28 +279,21 @@ and Maintenance Agreement for more details: http://www.jooq.org/eula The following are code blocks: -These are useful to provide examples in code. Often, with jOOQ, it is even more useful to compare SQL code with its corresponding Java/jOOQ code. When this is done, the blocks are aligned side-by-side, with SQL usually being on the left, and Java usually being on the right:
-@@ -308,13 +301,11 @@ create.select()]]>
-@@ -335,16 +326,16 @@ Factory create = new Factory(connection, SQLDialect.ORACLE);]]>
See the manual's section about
For the examples in this manual, the same database will always be referred to. It essentially consists of these entities created using the Oracle dialect
-More entities, types (e.g. UDT's, ARRAY types, ENUM types, etc), stored procedures and packages are introduced for specific examples
-jOOQ has originally been created as a library for complete abstraction of JDBC and all database interaction. Various best practices that are frequently encountered in pre-existing software products are applied to this library. This includes:
@@ -420,23 +411,23 @@ CREATE TABLE book_to_book_store (The following sections explain about various use cases for using jOOQ in your application.
-
This is the most simple of all use cases, allowing for construction of valid SQL for any database. In this use case, you will not use
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -448,23 +439,23 @@ String sql = create.select(fieldByName("BOOK","TITLE"), fieldByName("AUTHOR","FI
In addition to using jOOQ as a
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -476,29 +467,29 @@ String sql = create.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Instead of any tool mentioned in the previous chapters, you can also use jOOQ directly to execute your jOOQ-generated SQL statements. This will add a lot of convenience on top of the previously discussed API for typesafe SQL construction, when you can re-use the information from generated classes to fetch records and custom data types. An example is given here:
-jOOQ doesn't stop here, though! You can execute any SQL with jOOQ. In other words, you can use any other SQL building tool and run the SQL statements with jOOQ. An example is given here:
-
If you wish to use jOOQ as a SQL executor with (or without) code generation, the following sections of the manual will be of interest to you:
@@ -518,17 +509,17 @@ Result
This is probably the most complete use-case for jOOQ: Use all of jOOQ's features. Apart from jOOQ's fluent API for query construction, jOOQ can also help you execute everyday CRUD operations. An example is given here:
-If you wish to use all of jOOQ's features, the following sections of the manual will be of interest to you (including all sub-sections): @@ -552,12 +543,12 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
jOOQ isn't just a library that helps you
If you're a power user of your favourite, feature-rich database, jOOQ will help you access all of your database's vendor-specific features, such as OLAP features, stored procedures, user-defined types, vendor-specific SQL, functions, etc. Examples are given throughout this manual.
-Don't have time to read the full manual? Here are a couple of tutorials that will get you into the most essential parts of jOOQ as quick as possible.
-This manual section is intended for new users, to help them get a running application with jOOQ, quickly.
-
If you haven't already downloaded it, download jOOQ:
https://sourceforge.net/projects/jooq/files/Release/
@@ -605,12 +596,12 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
Alternatively, you can create a Maven dependency:
-
For this example, we'll be using MySQL. If you haven't already downloaded MySQL Connector/J, download it here:
@@ -620,17 +611,17 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
If you don't have a MySQL instance up and running yet, get XAMPP now! XAMPP is a simple installation bundle for Apache, MySQL, PHP and Perl
-We're going to create a database called "guestbook" and a corresponding "posts" table. Connect to MySQL via your command line client and type the following:
-
In this step, we're going to use jOOQ's command line tools to generate classes that map to the Posts table we just created. More detailed information about how to set up the jOOQ code generator can be found here:
Replace the username with whatever user has the appropriate privileges to query the database meta data. You'll also want to look at the other values and replace as necessary. Here are the two interesting properties:
@@ -711,15 +701,15 @@ CREATE TABLE `posts` ( Once you have the JAR files and guestbook.xml in your temp directory, type this (use colons instead of semi-colons on UNIX/Linux systems): -Note the prefix slash before guestbook.xml. Even though it's in our working directory, we need to prepend a slash, as the configuration file is loaded from the classpath. Replace the filenames with your filenames. In this example, jOOQ {jooq-version} is being used. If everything has worked, you should see this in your console output:
-Let's just write a vanilla main class in the project containing the generated classes:
-This is pretty standard code for establishing a MySQL connection.
-Let's add a simple query:
-
First get an instance of
We then use jOOQ's DSL to return an instance of Result. We'll be using this result in the next step.
Factory so we can write a simple SELECT query. We pass an instance of the MySQL connection to Factory. Note that the factory doesn't close the connection. We'll have to do that ourselves.
@@ -840,29 +828,29 @@ Result
After the line where we retrieve the results, let's iterate over the results and print out the data:
-The full program should now look like this:
-
jOOQ has grown to be a comprehensive SQL library. For more information, please consider the manual:
http://www.jooq.org/manual/
@@ -941,37 +928,37 @@ public class Main {
This tutorial is the courtesy of Ikai Lan. See the original source here:
http://ikaisays.com/2011/11/01/getting-started-with-jooq-a-tutorial/
Feel free to contribute a tutorial!
-Feel free to contribute a tutorial!
-Feel free to contribute a tutorial!
-As any other library, jOOQ can be easily used in Scala, taking advantage of the many Scala language features such as for example:
@@ -994,7 +981,7 @@ public class Main { A short example jOOQ application in Scala might look like this: -
For more details about jOOQ's Scala integration, please refer to the manual's section about
jOOQ has no dependencies on any third-party libraries. This rule has some exceptions:
@@ -1057,14 +1044,14 @@ object Test { //In order to build jOOQ, please download the sources from https://github.com/jOOQ/jOOQ and use Maven to build it, preferably in Eclipse.
-SQL is a declarative language that is hard to integrate into procedural, object-oriented, functional or any other type of programming languages. jOOQ's philosophy is to give SQL the credit it deserves and integrate SQL itself as an "internal domain specific language" directly into Java.
@@ -1074,12 +1061,12 @@ object Test { //
This section explains all about the various syntax elements involved with jOOQ's SQL building capabilities. For a complete overview of all syntax elements, please refer to the manual's section about
jOOQ exposes a lot of interfaces and hides most implementation facts from client code. The reasons for this are:
@@ -1100,13 +1087,13 @@ object Test { //With jOOQ 2.0, static factory methods have been introduced in order to make client code look more like SQL. Ideally, when working with jOOQ, you will simply static import all methods from the Factory class:
-Note, that when working with Eclipse, you could also add the Factory to your favourites. This will allow to access functions even more fluently:
-@@ -1121,20 +1108,19 @@ Factory.concat(Factory.trim(FIRST_NAME), Factory.trim(LAST_NAME));
As a Configuration object, a Factory can construct
While jOOQ tries to represent the SQL standard as much as possible, many features are vendor-specific to a given database and to its "SQL dialect". jOOQ models this using the
Some parts of the jOOQ API are officially supported only by a given subset of the supported SQL dialects. For instance, the
jOOQ API methods which are not annotated with the
The aforementioned Support annotation does not only designate, which databases natively support a feature. It also indicates that a feature is simulated by jOOQ for some databases lacking this feature. An example of this is the
Nevertheless, the IS DISTINCT FROM predicate is supported in all dialects, as its semantics can be expressed with an equivalent
jOOQ has a historic affinity to Oracle's SQL extensions. If something is supported in Oracle SQL, it has a high probability of making it into the jOOQ API
-
While you can use jOOQ for
If you're in a J2EE or Spring context, however, you may wish to use a
The jOOQ Factory allows for some optional configuration elements to be used by advanced users. The
For example, if you want to indicate to jOOQ, that it should inline all bind variables, and execute static
Subsequent sections of the manual contain some more in-depth explanations about these settings:
@@ -1240,12 +1226,12 @@ Factory create = new Factory(connection, dialect, settings);]]> Please refer to the jOOQ runtime configuration XSD for more details:
You may wish to design your database in a way that you have several instances of your schema. This is useful when you want to cleanly separate data belonging to several customers / organisation units / branches / users and put each of those entities' data in a separate database or schema.
@@ -1265,7 +1251,7 @@ Factory create = new Factory(connection, dialect, settings);]]>
When a user from My Book World logs in, you want them to access the MY_BOOK_WORLD schema using classes generated from DEV. This can be achieved with the
The query executed with a Factory equipped with the above mapping will in fact produce this SQL statement:
-Even if AUTHOR was generated from DEV.
@@ -1291,7 +1277,7 @@ create.selectFrom(AUTHOR).fetch(); Your development database may not be restricted to hold only one DEV schema. You may also have a LOG schema and a MASTER schema. Let's say the MASTER schema is shared among all customers, but each customer has their own LOG schema instance. Then you can enhance your RenderMapping like this (e.g. using an XML configuration file): -Note, you can load the above XML file like this:
-This will map generated classes from DEV to MY_BOOK_WORLD, from LOG to MY_BOOK_WORLD_LOG, but leave the MASTER schema alone. Whenever you want to change your mapping configuration, you will have to create a new Factory @@ -1322,40 +1308,40 @@ create.selectFrom(AUTHOR).fetch(); Another option to switch schema names is to use a default schema for the Factory's underlying Connection. Many RDBMS support a USE or SET SCHEMA command, which you can call like this:
-Queries generated from the above Factory will produce this kind of SQL statement:
-If you wish not to render any schema name at all, use the following Settings property for this:
-Not only schemata can be mapped, but also tables. If you are not the owner of the database your application connects to, you might need to install your schema with some sort of prefix to every table. In our examples, this might mean that you will have to map DEV.AUTHOR to something MY_BOOK_WORLD.MY_APP__AUTHOR, where MY_APP__ is a prefix applied to all of your tables. This can be achieved by creating the following mapping:
-The query executed with a Factory equipped with the above mapping will in fact produce this SQL statement:
-Table mapping and schema mapping can be applied independently, by specifying several MappedSchema entries in the above configuration. jOOQ will process them in order of appearance and map at first match. Note that you can always omit a MappedSchema's output value, in case of which, only the table mapping is applied. If you omit a MappedSchema's input value, the table mapping is applied to all schemata! @@ -1384,20 +1370,20 @@ create.selectFrom(AUTHOR).fetch();
Note that the manual's section about
There are a couple of subclasses for the general Factory. Each SQL dialect has its own dialect-specific factory. For instance, if you're only using the MySQL dialect, you can choose to create a new Factory using any of the following types:
-The advantage of using a dialect-specific Factory lies in the fact, that you have access to more proprietary RDMBS functionality. This may include:
@@ -1408,23 +1394,23 @@ MySQLFactory create = new MySQLFactory(connection);Another type of Factory subclasses are each generated schema's factories. If you generate your schema TEST, then you will have access to a TestFactory. By default, such a schema-specific Factory will not render the schema name.
-
jOOQ currently supports 6 types of SQL statements. All of these statements are constructed from a Factory instance with an optional
jOOQ ships with its own DSL (or Domain Specific Language) that simulates SQL in Java. This means, that you can write SQL statements almost as if Java natively supported it, just like .NET's C# does with LINQ to SQL.
@@ -1432,22 +1418,21 @@ MySQLFactory create = new MySQLFactory(connection); Here is an example to illustrate what that means: -
We'll see how the aliasing works later in the section about
Many other frameworks have similar APIs with similar feature sets. Yet, what makes jOOQ special is its informal
Unlike other, simpler frameworks that use "fluent APIs" or "method chaining", jOOQ's BNF-based interface hierarchy will not allow bad query syntax. The following will not compile, for instance:
-
Historically, jOOQ started out as an object-oriented SQL builder library like any other. This meant that all queries and their syntactic components were modeled as so-called
This query is equivalent to the one shown before using the DSL syntax. In fact, internally, the DSL API constructs precisely this QueryObject. Note, that you can always access the SelectQuery object to switch between DSL and non-DSL APIs:
-
Note, that for historic reasons, the DSL API mixes mutable and immutable behaviour with respect to the internal representation of the
Mutability may be removed in a future version of jOOQ.
-
When you don't just perform
Details about the various clauses of this query will be provided in subsequent sections
@@ -1594,14 +1578,14 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
A very similar, but limited API is available, if you want to select from single physical tables in order to retrieve
As you can see, there is no way to further restrict/project the selected fields. This just selects all known TableFields in the supplied Table, and it also binds <R extends Record> to your Table's associated Record. An example of such a Query would then be:
-The simple SELECT API is limited in the way that it does not support any of these clauses: @@ -1615,41 +1599,39 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
In most parts of this manual, it is assumed that you do not use the simple SELECT API. For more information about the simple SELECT API, see the manual's section about
The SELECT clause lets you project your own record types, referencing table fields, functions, arithmetic expressions, etc. The Factory provides several methods for expressing a SELECT clause:
-Some commonly used projections can be easily created using convenience methods:
-
See more details about functions and expressions in the manual's section about
The SQL FROM clause allows for specifying any number of
Read more about aliasing in the manual's section about
Note, in order to access the DbmsXplan package, you can use the
Read more about dual or dummy tables in the manual's section about
jOOQ supports many different types of standard SQL JOIN operations:
@@ -1743,7 +1721,7 @@ new Factory(SQLDialect.POSTGRES).selectOne().getSQL();]]> All of these JOIN methods can be called on
The two syntaxes will produce the same SQL statement. However, calling "join" on
In case of ambiguity, you can also supply field references for your foreign keys, or the generated foreign key reference to the onKey() method. @@ -1805,137 +1781,130 @@ JOIN BOOK ON BOOK.AUTHOR_ID = AUTHOR.ID]]> Most often, you will provide jOOQ with JOIN conditions in the JOIN .. ON clause. SQL supports a different means of specifying how two tables are to be joined. This is the JOIN .. USING clause. Instead of a condition, you supply a set of fields whose names are common to both tables to the left and right of a JOIN operation. This can be useful when your database schema has a high degree of relational normalisation. An example:
-In schemas with high degrees of normalisation, you may also choose to use NATURAL JOIN, which takes no JOIN arguments as it joins using all fields that are common to the table expressions to the left and to the right of the JOIN operator. An example:
-Oracle SQL ships with a special syntax available for OUTER JOIN clauses. According to the Oracle documentation about partitioned outer joins this can be used to fill gaps for simplified analytical calculations. jOOQ only supports putting the PARTITION BY clause to the right of the OUTER JOIN clause. The following example will create at least one record per AUTHOR and per existing value in BOOK.PUBLISHED_IN, regardless if an AUTHOR has actually published a book in that year.
-
The WHERE clause can be used for JOIN or filter predicates, in order to restrict the data returned by the
The above syntax is convenience provided by jOOQ, allowing you to connect the
You will find more information about creating
The Oracle database knows a very succinct syntax for creating hierarchical queries: the CONNECT BY clause, which is fully supported by jOOQ, including all related functions and pseudo-columns. A more or less formal definition of this clause is given here:
-An example for an iterative query, iterating through values between 1 and 5 is this:
-Here's a more complex example where you can recursively fetch directories in your database, and concatenate them to a path:
-The output might then look like this
-
Some of the supported functions and pseudo-columns are these (available from the
GROUP BY can be used to create unique groups of data, to form aggregations, to remove duplicates and for other reasons. It will transform your previously defined
As defined in the SQL standard, when grouping, you may no longer project any columns that are not a formal part of the GROUP BY clause, or
This will return an arbitrary title per author. jOOQ supports this syntax, as jOOQ is not doing any checks internally, about the consistence of tables/fields/functions that you provide it.
@@ -2028,68 +1994,64 @@ GROUP BY AUTHOR_ID]]>
jOOQ supports empty GROUP BY () clauses as well. This will result in
Some databases support the SQL standard grouping functions and some extensions thereof. See the manual's section about
The HAVING clause is commonly used to further restrict data resulting from a previously issued
According to the SQL standard, you may omit the GROUP BY clause and still issue a HAVING clause. This will implicitly GROUP BY (). jOOQ also supports this syntax. The following example selects one record, only if there are at least 4 books in the books table:
-Databases are allowed to return data in any arbitrary order, unless you explicitly declare that order in the ORDER BY clause. In jOOQ, this is straight-forward:
-
Any jOOQ
Note, how one() is used as a convenience short-cut for inline(1) @@ -2117,87 +2078,84 @@ ORDER BY 1 ASC, 2 DESC]]> A few databases support the SQL standard "null ordering" clause in sort specification lists, to define whether NULL values should come first or last in an ordered result.
-
If your database doesn't support this syntax, jOOQ simulates it using a
Using
But writing these things can become quite verbose. jOOQ supports a convenient syntax for specifying sort mappings. The same query can be written in jOOQ as such:
-More complex sort indirections can be provided using a Map:
-Of course, you can combine this feature with the previously discussed NULLS FIRST / NULLS LAST feature. So, if in fact these two books are the ones you like least, you can put all NULLS FIRST (all the other books):
-@@ -2209,23 +2167,23 @@ ORDER BY CASE TITLE
jOOQ also supports Oracle's SIBLINGS keyword to be used with ORDER BY clauses for
While being extremely useful for every application that does paging, or just to limit result sets to reasonable sizes, this clause is not yet part of any SQL standard (up until SQL:2008). Hence, there exist a variety of possible implementations in various SQL dialects, concerning this limit clause. jOOQ chose to implement the LIMIT .. OFFSET clause as understood and supported by MySQL, H2, HSQLDB, Postgres, and SQLite. Here is an example of how to apply limits with jOOQ:
-This will limit the result to 1 books starting with the 2nd book (starting at offset 0!). limit() is supported in all dialects, offset() in all but Sybase ASE, which has no reasonable means to simulate it. This is how jOOQ simulates the above query in various SQL dialects:
-As you can see, jOOQ will take care of the incredibly painful ROW_NUMBER() OVER() (or ROWNUM for Oracle) filtering in subselects for you, you'll just have to write limit(1).offset(2) in any dialect. @@ -2270,39 +2228,37 @@ AND ROWNUM_98843777 <= 3
As can be seen in the above example, writing correct SQL can be quite tricky, depending on the SQL dialect. For instance, with SQL Server, you cannot have an ORDER BY clause in a subquery, unless you also have a TOP clause. This is illustrated by the fact that jOOQ renders a TOP 100 PERCENT clause for you. The same applies to the fact that ROW_NUMBER() OVER() needs an ORDER BY windowing clause, even if you don't provide one to the jOOQ query. By default, jOOQ adds ordering by the first column of your projection.
-For inter-process synchronisation and other reasons, you may choose to use the SELECT .. FOR UPDATE clause to indicate to the database, that a set of cells or records should be locked by a given transaction for subsequent updates. With jOOQ, this can be achieved as such:
-The above example will produce a record-lock, locking the whole record for updates. Some databases also support cell-locks using FOR UPDATE OF ..
-Oracle goes a bit further and also allows to specify the actual locking behaviour. It features these additional clauses, which are all supported by jOOQ: @@ -2316,15 +2272,15 @@ FOR UPDATE OF TITLE]]> With jOOQ, you can use those Oracle extensions as such:
-The SQL standard specifies a FOR UPDATE clause to be applicable for cursors. Most databases interpret this as being applicable for all SELECT statements. An exception to this rule are the CUBRID and SQL Server databases, that do not allow for any FOR UPDATE clause in a regular SQL SELECT statement. jOOQ simulates the FOR UPDATE behaviour, by locking record by record with JDBC. JDBC allows for specifying the flags TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE for any statement, and then using ResultSet.updateXXX() methods to produce a cell-lock / row-lock. Here's a simplified example in JDBC:
-The main drawback of this approach is the fact that the database has to maintain a scrollable cursor, whose records are locked one by one. This can cause a major risk of deadlocks or race conditions if the JDBC driver can recover from the unsuccessful locking, if two Java threads execute the following statements:
-So use this technique with care, possibly only ever locking single rows! @@ -2361,12 +2317,12 @@ SELECT * FROM author ORDER BY id DESC;]]>
Note, that jOOQ also supports optimistic locking, if you're doing simple CRUD. This is documented in the section's manual about
SQL allows to perform set operations as understood in standard set theory on result sets. These operations include unions, intersections, subtractions. For two subselects to be combinable by such a set operator, each subselect must return a
@@ -2394,76 +2349,73 @@ create.selectFrom(BOOK).where(BOOK.ID.equal(5)));]]>
As previously mentioned in the manual's section about the
If you are closely coupling your application to an Oracle (or CUBRID) database, you might need to be able to pass hints of the form /*+HINT*/ with your SQL statements to the Oracle database. For example:
-This can be done in jOOQ using the .hint() clause in your SELECT statement:
-Note that you can pass any string in the .hint() clause. If you use that clause, the passed string will always be put in between the SELECT [DISTINCT] keywords and the actual projection list
-The INSERT statement is used to insert new records into a database table. Records can either be supplied using a VALUES() constructor, or a SELECT statement. jOOQ supports both types of INSERT statements. An example of an INSERT statement using a VALUES() constructor is given here:
-The SQL standard specifies that multiple rows can be supplied to the VALUES() constructor in an INSERT statement. Here's an example of a multi-record INSERT
-jOOQ tries to stay close to actual SQL. In detail, however, Java's expressiveness is limited. That's why the values() clause is repeated for every record in multi-record inserts. @@ -2472,30 +2424,29 @@ VALUES (100, 'Hermann', 'Hesse'), Some RDBMS do not support inserting several records in a single statement. In those cases, jOOQ simulates multi-record INSERTs using the following SQL:
-MySQL (and some other RDBMS) allow for using a non-SQL-standard, UPDATE-like syntax for INSERT statements. This is also supported in jOOQ, should you prefer that syntax. The above INSERT statement can also be expressed as follows:
-
As you can see, this syntax is a bit more verbose, but also more type-safe, as every field can be matched with its value. Internally, the two syntaxes are strictly equivalent.
@@ -2506,12 +2457,12 @@ SELECT 101, 'Alfred', 'Döblin' FROM DUAL;
The MySQL database supports a very convenient way to INSERT or UPDATE a record. This is a non-standard extension to the SQL syntax, which is supported by jOOQ and simulated in other RDBMS, where this is possible (i.e. if they support the SQL standard
The Postgres database has native support for an INSERT .. RETURNING clause. This is a very powerful concept that is simulated for all other dialects using JDBC's
Some databases have poor support for returning generated keys after INSERTs. In those cases, jOOQ might need to issue another
The UPDATE statement is used to modify one or several pre-existing records in a database table. UPDATE statements are only possible on single tables. Support for multi-table updates will be implemented in the near future. An example update query is given here:
-The DELETE statement physically removes records from a database table. DELETE statements are only possible on single tables. Support for multi-table deletes will be implemented in the near future. An example delete query is given here:
-The MERGE statement is one of the most advanced standardised SQL constructs, which is supported by DB2, HSQLDB, Oracle, SQL Server and Sybase (MySQL has the similar INSERT .. ON DUPLICATE KEY UPDATE construct)
@@ -2613,15 +2560,14 @@ create.insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) The point of the standard MERGE statement is to take a TARGET table, and merge (INSERT, UPDATE) data from a SOURCE table into it. DB2, Oracle, SQL Server and Sybase also allow for DELETING some data and for adding many additional clauses. With jOOQ {jooq-version}, only Oracle's MERGE extensions are supported. Here is an example: -The H2 database ships with a somewhat less powerful but a little more intuitive syntax for its own version of the MERGE statement. An example more or less equivalent to the previous one can be seen here:
-
This syntax can be fully simulated by jOOQ for all other databases that support the SQL standard. For more information about the H2 MERGE syntax, see the documentation here:
http://www.h2database.com/html/grammar.html#merge
The TRUNCATE statement is the only DDL statement supported by jOOQ so far. It is popular in many databases when you want to bypass constraints for table truncation. Databases may behave differently, when a truncated table is referenced by other tables. For instance, they may fail if records from a truncated table are referenced, even with ON DELETE CASCADE clauses in place. Please, consider your database manual to learn more about its TRUNCATE implementation.
@@ -2669,44 +2614,42 @@ VALUES ('John', 'Hitchcock') The TRUNCATE syntax is trivial: -TRUNCATE is not supported by Ingres and SQLite. jOOQ will execute a DELETE FROM AUTHOR statement instead.
-The following sections explain the various types of table expressions supported by jOOQ
-
Most of the times, when thinking about a
The above example shows how AUTHOR and BOOK tables are joined in a
See the manual's section about
The strength of jOOQ's
As you can see in the above example, calling as() on generated tables returns an object of the same type as the table. This means that the resulting object can be used to dereference fields from the aliased table. This is quite powerful in terms of having your Java compiler check the syntax of your SQL statements. If you remove a column from a table, dereferencing that column from that table alias will cause compilation errors. @@ -2755,17 +2697,17 @@ create.select()
TODO document this
-
The
SQL and relational algebra distinguish between at least the following JOIN types (upper-case: SQL, lower-case: relational algebra):
@@ -2783,7 +2725,7 @@ create.select()
jOOQ supports all of these JOIN types (except semi-join and anti-join) directly on any
Note that most of jOOQ's JOIN operations give way to a similar DSL API hierarchy as previously seen in the manual's section about the
A
If you are closely coupling your application to an Oracle database, you can take advantage of some Oracle-specific features, such as the PIVOT clause, used for statistical analyses. The formal syntax definition is as follows:
-
The PIVOT clause is available from the
There is one operation in relational algebra that is not given a lot of attention, because it is rarely used in real-world applications. It is the relational division, the opposite operation of the cross product (or, relational multiplication). The following is an approximate definition of a relational division:
-With jOOQ, you can simplify using relational divisions by using the following syntax:
-The above roughly translates to
-Or in plain text: Find those TEXT values in C whose ID's correspond to all ID's in B. Note that from the above SQL statement, it is immediately clear that proper indexing is of the essence. Be sure to have indexes on all columns referenced from the on(...) and returning(...) clauses. @@ -2949,13 +2888,13 @@ WHERE NOT EXISTS (
The SQL standard specifies how SQL databases should implement ARRAY and TABLE types, as well as CURSOR types. Put simply, a CURSOR is a pointer to any materialised
Note, in order to access the DbmsXplan package, you can use the
The SQL standard specifies that the
Note, that some databases (H2, MySQL) can normally do without "dual". However, there exist some corner-cases with complex nested SELECT statements, where this will cause syntax errors (or parser bugs). To stay on the safe side, jOOQ will always render "dual" in those dialects.
-
Column expressions can be used in various SQL clauses in order to refer to one or several columns. This chapter explains how to form various types of column expressions with jOOQ. A particular type of column expression is given in the section about
In general, it is up to you whether you want to use the "prefix" notation or the "postfix" notation to create new column expressions based on existing ones. The "SQL way" would be to use the "prefix notation", with functions created from the
Table columns are the most simple implementations of a
Table columns implement a more specific interface called
See the manual's section about
Just like
Here is how it's done with jOOQ:
-When you alias Fields like above, you can access those Fields' values using the alias name:
-jOOQ's source code generator tries to find the most accurate type mapping between your vendor-specific data types and a matching Java type. For instance, most VARCHAR, CHAR, CLOB types will map to String. Most BINARY, BYTEA, BLOB types will map to byte[]. NUMERIC types will default to java.math.BigDecimal, but can also be any of java.math.BigInteger, Long, Integer, Short, Byte, Double, Float.
@@ -3136,27 +3070,27 @@ System.out.println("Books : " + record.getValue("books")); Sometimes, this automatic mapping might not be what you needed, or jOOQ cannot know the type of a field. In those cases you would write SQL type CASTs like this: -in jOOQ, you can write something like that:
-The same thing can be achieved by casting a Field directly to String.class, as TEXT is the default data type in Postgres to map to Java's String
-The complete CAST API in Field consists of these three methods:
-
Your database can do the math for you. Arithmetic operations are implemented just like
In order to express a SQL query like this one:
-You can write something like this in jOOQ:
-jOOQ also supports the Oracle-style syntax for adding days to a Field<? extends java.util.Date>
-For more advanced datetime arithmetic, use the Factory's timestampDiff() and dateDiff() functions, as well as jOOQ's built-in SQL standard INTERVAL data type support: @@ -3219,31 +3151,30 @@ public class Factory {
The SQL standard defines the concatenation operator to be an infix operator, similar to the ones we've seen in the chapter about ||. Some other dialects do not support this operator, but expect a concat() function, instead. jOOQ renders the right operator / function, depending on your
There are a variety of general functions supported by jOOQ As discussed in the chapter about
Please refer to the
Math can be done efficiently in the database before returning results to your Java application. In addition to the
Please refer to the
Interestingly, bitwise functions and bitwise arithmetic is not very popular among SQL databases. Most databases only support a few bitwise operations, while others ship with the full set of operators. jOOQ's API includes most bitwise operations as listed below. In order to avoid ambiguities with
String formatting can be done efficiently in the database before returning results to your Java application. As discussed in the chapter about
Note that the SQL standard specifies that patterns should follow the XQuery standards. In the real world, the POSIX regular expression standard is the most used one, some use Java regular expressions, and only a few ones use Perl regular expressions. jOOQ does not make any assumptions about regular expression syntax. For cross-database compatibility, please read the relevant database manuals carefully, to learn about the appropriate syntax. Please refer to the
This is a list of date and time functions supported by jOOQ's
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. See the manual's section about
This is a list of system functions supported by jOOQ's
Aggregate functions work just like functions, even if they have a slightly different semantics. Here are some example aggregate functions from the
Here's an example, counting the number of books any author has written:
-
Aggregate functions have strong limitations about when they may be used and when not. For instance, you can use aggregate functions in scalar queries. Typically, this means you only select aggregate functions, no
Oracle and some other databases support "ordered aggregate functions". This means you can provide an ORDER BY clause to an aggregate function, which will be taken into consideration when aggregating. The best example for this is Oracle's LISTAGG() (also known as GROUP_CONCAT in other
The above query might yield:
-
Oracle allows for restricting aggregate functions using the KEEP() clause, which is supported by jOOQ. In Oracle, some aggregate functions (MIN, MAX, SUM, AVG, COUNT, VARIANCE, or STDDEV) can be restricted by this clause, hence
@@ -3533,12 +3461,12 @@ GROUP BY AUTHOR_ID
In those databases that support over() on it. See the manual's section about
Most major RDBMS support the concept of window functions. jOOQ knows of implementations in DB2, Oracle, Postgres, SQL Server, and Sybase SQL Anywhere, and supports most of their specific syntaxes. Note, that H2 and HSQLDB have implemented ROW_NUMBER() functions, without true windowing support.
over() method. See the chapter about
SQL distinguishes between various window function types (e.g. "ranking functions"). Depending on the function, SQL expects mandatory PARTITION BY or ORDER BY clauses within the OVER() clause. jOOQ does not enforce those rules for two reasons:
@@ -3583,7 +3511,7 @@ GROUP BY AUTHOR_ID
Here are some simple examples of window functions with jOOQ:
Window functions can be used for things like calculating a "running total". The following example fetches transactions and the running total for every transaction going back to the beginning of the transaction table (ordered by booked_at). Window functions are accessible from the previously seen over() method:
LISTAGG(). These functions have a window function / analytical function variant, as well. For example:
-
In the previous chapter about
User-defined aggregate functions also implement over(). This is supported by Oracle in particular. See the manual's section about
The SQL standard defines special functions that can be used in the
In English, the ROLLUP() grouping function provides N+1 groupings, when N is the number of arguments to the ROLLUP() function. Each grouping has an additional group field from the ROLLUP() argument field list. The results of the second query might look something like this:
CUBE() is different from ROLLUP() in the way that it doesn't just create N+1 groupings, it creates all 2^N possible combinations between all group fields in the CUBE() function argument list. Let's re-consider our second query from before:
The results would then hold:
-
@@ -3804,7 +3726,7 @@ ORDER BY 1 NULLS FIRST, 2 NULLS FIRST
jOOQ fully supports all of these functions, as well as the utility functions GROUPING() and GROUPING_ID(), used for identifying the grouping set ID of a record. The
MySQL and CUBRID don't know any grouping functions, but they support a WITH ROLLUP clause, that is equivalent to simple ROLLUP() grouping functions. jOOQ simulates ROLLUP() in MySQL and CUBRID, by rendering this WITH ROLLUP clause. The following two statements mean the same:
Some databases support user-defined functions, which can be embedded in any SQL statement, if you're using jOOQ's
The above function will be made available from a generated
Note that user-defined functions returning
Some databases support user-defined aggregate functions, which can then be used along with
The above OBJECT type is then available to function declarations as such:
-
jOOQ's
The CASE expression is part of the standard SQL syntax. While some RDBMS also offer an IF expression, or a DECODE function, you can always rely on the two types of CASE syntax:
-
In jOOQ, both syntaxes are supported (The second one is simulated in Derby, which only knows the first one). Unfortunately, both case and else are reserved words in Java. jOOQ chose to use decode() from the Oracle DECODE function, and otherwise(), which means the same as else.
@@ -3989,15 +3907,15 @@ create.decode().value(AUTHOR.FIRST_NAME)
A CASE expression can be used anywhere where you can place a
Oracle knows a more succinct, but maybe less readable DECODE() function with a variable number of arguments. This function roughly does the same as the second case expression syntax. jOOQ supports the DECODE() function and simulates it using CASE expressions in all dialects other than Oracle:
-
Sort indirection is often implemented with a CASE clause of a SELECT's ORDER BY clause. See the manual's section about the
Sequences implement the
So if you have a sequence like this in Oracle:
-
You can then use your
According to the SQL standard, row value expressions can have a degree of more than one. This is commonly used in the
@@ -4093,14 +4010,14 @@ public static RowN row(Object... values) { ... }]]>
The
Conditions or conditional expressions are widely used in SQL and in the jOOQ API. They can be used in
@@ -4137,22 +4054,21 @@ public static RowN row(Object... values) { ... }]]>
Note that jOOQ does not model these values as actual
With jOOQ, most
@@ -4168,48 +4084,47 @@ BOOK.TITLE.notEqual("Animal Farm")]]>
Conditions can also be connected using
In SQL, as in most other languages,
The above example shows that the number of parentheses in Java can quickly explode. Proper indentation may become crucial in making such code readable. In order to understand how jOOQ composes combined conditional expressions, let's assign component expressions first:
-
Here are all boolean operators on the
In SQL, comparison predicates are formed using common comparison operators:
@@ -4248,7 +4162,7 @@ not() // Invert a condition (synonym for Factory.not(Conditi Unfortunately, Java does not support operator overloading, hence these operators are also implemented as methods in jOOQ, like any other SQL syntax elements. The relevant parts of the
Note that every operator is represented by two methods. A verbose one (such as equal()) and a two-character one (such as eq()). Both methods are the same. You may choose either one, depending on your taste. The manual will always use the more verbose one.
@@ -4281,30 +4195,28 @@ ge or greaterOrEqual(Select
If the right-hand side of a
For the example, the right-hand side of the quantified comparison predicates were filled with argument lists. But it is easy to imagine that the source of values results from a
Typically, the
In SQL, you cannot compare NULL with any value using
Some databases support the DISTINCT predicate, which serves as a convenient, NULL-safe
If your database does not natively support the DISTINCT predicate, jOOQ simulates it with an equivalent
The BETWEEN predicate can be seen as syntactic sugar for a pair of
Note the inclusiveness of range boundaries in the definition of the BETWEEN predicate. Intuitively, this is supported in jOOQ as such:
-The SQL standard defines the SYMMETRIC keyword to be used along with BETWEEN to indicate that you do not care which bound of the range is larger than the other. A database system should simply swap range bounds, in case the first bound is greater than the second one. jOOQ supports this keyword as well, simulating it if necessary.
-The simulation is done trivially:
-LIKE predicates are popular for simple wildcard-enabled pattern matching. Supported wildcards in all SQL databases are:
@@ -4452,24 +4356,22 @@ BOOK.PUBLISHED_IN.notBetweenSymmetric(1940).and(1920)]]> With jOOQ, the LIKE predicate can be created from anyOften, your pattern may contain any of the wildcard characters "_" and "%", in case of which you may want to escape them. jOOQ does not automatically escape patterns in like() and notLike() methods. Instead, you can explicitly define an escape character as such:
-In the above predicate expressions, the exclamation mark character is passed as the escape character to escape wildcard characters "!_" and "!%", as well as to escape the escape character itself: "!!" @@ -4483,7 +4385,7 @@ BOOK.TITLE.notLike("%The !%-Sign Book%", '!')]]> In addition to the above, jOOQ provides a few convenience methods for common operations performed on strings using the LIKE predicate. Typical operations are "contains predicates", "starts with predicates", "ends with predicates", etc. Here is the full convenience API wrapping LIKE predicates:
-Note, that jOOQ escapes % and _ characters in value in some of the above predicate implementations. For simplicity, this has been omitted in this manual.
-
In SQL, apart from comparing a value against several values, the IN predicate can be used to create semi-joins or anti-joins. jOOQ knows the following methods on the
A sample IN predicate might look like this:
-
A good way to prevent this from happening is to use the
Slightly less intuitive, yet more powerful than the previously discussed
Note that in SQL, the projection of a subselect in an EXISTS predicate is irrelevant. To help you write queries like the above, you can use jOOQ's selectZero() or selectOne()
http://blog.jooq.org/2012/07/27/not-in-vs-not-exists-vs-left-join-is-null-mysql/
When comparing dates, the SQL standard allows for using a special OVERLAPS predicate, which checks whether two date ranges overlap each other. The following can be said:
-
jOOQ supports the OVERLAPS predicate on
This allows for expressing the above predicates as such:
-
Unlike the standard (or any database implementing the standard), jOOQ also supports the OVERLAPS predicate for comparing arbitrary
A DSL is a nice thing to have, it feels "fluent" and "natural", especially if it models a well-known language, such as SQL. But a DSL is always expressed in a host language (Java in this case), which was not made for exactly the same purposes as its hosted DSL. If it were, then jOOQ would be implemented on a compiler-level, similar to LINQ in .NET. But it's not, and so, the DSL is limited by language constraints of its host language. We have seen many functionalities where the DSL becomes a bit verbose. This can be especially true for:
@@ -4676,7 +4574,7 @@ row(Date.valueOf('2010-01-01'), new DayToSecond(2)).overlaps(Date.valueOf('2010-
Plain SQL API methods are usually overloaded in three ways. Let's look at the condition query part constructor:
Please refer to the
Apart from the general factory methods, plain SQL is also available in various other contexts. For instance, when adding a .where("a = b") clause to a query. Hence, there exist several convenience methods where plain SQL can be inserted usefully. This is an example displaying all various use-cases in one single query:
-@@ -4784,12 +4682,12 @@ create.select(LAST_NAME, COUNT1, COUNT2)
Bind values are used in SQL / JDBC for various reasons. Among the most obvious ones are:
@@ -4808,31 +4706,31 @@ create.select(LAST_NAME, COUNT1, COUNT2)The following sections explain how you can introduce bind values in jOOQ, and how you can control the way they are rendered and bound to SQL.
-JDBC only knows indexed bind values. A typical example for using bind values with JDBC is this:
-With dynamic SQL, keeping track of the number of question marks and their corresponding index may turn out to be hard. jOOQ abstracts this and lets you provide the bind value right where it is needed. A trivial example is this:
-
Note the using of
You can also extract specific bind values by index from a query, if you wish to modify their underlying value after creating a query. This can be achieved as such:
-
For more details about jOOQ's internals, see the manual's section about
Some SQL access abstractions that are built on top of JDBC, or some that bypass JDBC may support named parameters. jOOQ allows you to give names to your parameters as well, although those names are not rendered to SQL strings by default. Here is an example of how to create named parameters using the
The
In order to actually render named parameter names in generated SQL, use the
Sometimes, you may wish to avoid rendering bind variables while still using custom values in SQL. jOOQ refers to that as "inlined" bind values. When bind values are inlined, they render the actual value in SQL rather than a JDBC question mark. Bind value inlining can be achieved in two ways:
@@ -4939,7 +4836,7 @@ WHERE LAST_NAME = :lastName]]> In both cases, your inlined bind values will be properly escaped to avoid SQL syntax errors and SQL injection. Some examples: -
Special care needs to be taken when using
All methods in the jOOQ API that allow for plain (unescaped, untreated) SQL contain a warning message in their relevant Javadoc, to remind you of the risk of SQL injection in what is otherwise a SQL-injection-safe API.
-
A
Every
The following additional methods are inherited from a common
A simple example can be provided by checking out jOOQ's internal representation of a (simplified)
This is how jOOQ renders such a condition:
-
See the manual's sections about
As mentioned in the previous chapter about
And then, use the above factory to render pretty-printed SQL:
-
The section about
Every
Some additional methods are inherited from a common
This is how jOOQ binds values on such a condition:
-
See the manual's sections about
If a SQL clause is too complex to express with jOOQ, you can extend either one of the following types for use directly in a jOOQ query:
-
These classes are declared public and covered by jOOQ's integration tests. When you extend these classes, you will have to provide your own implementations for the
The above contract may be a bit tricky to understand at first. The best thing is to check out jOOQ source code and have a look at a couple of QueryParts, to see how it's done. Here's an example
If you don't need the integration of rather complex QueryParts into jOOQ, then you might be safer using simple
The above technique allows for creating rather complex SQL clauses that are currently not supported by jOOQ, without extending any of the
The only transient, non-serializable element in any jOOQ object is the
Another way of attaching QueryParts automatically, or rather providing them with a new
jOOQ-Scala is a maven module used for leveraging some advanced Scala features for those users that wish to use jOOQ with Scala.
@@ -5365,7 +5259,7 @@ create.attach(select);]]> The following depicts a trait which wraps all fields: -The following depicts a trait which wraps numeric fields:
-An example query using such overloaded operators would then look like this:
-This feature is still being experimented with. With Scala Macros, it might be possible to inline a true SQL dialect into the Scala syntax, backed by the jOOQ API. Stay tuned!
-
In a previous section of the manual, we've seen how jOOQ can be used to
The following sections of this manual will show how jOOQ is wrapping JDBC for SQL execution
-
Even if there are
Unlike JDBC, jOOQ has a lot of knowledge about a SQL query's structure and internals (see the manual's section about
Fetching is something that has been completely neglegted by JDBC and also by various other database abstraction libraries. Fetching is much more than just looping or listing records or mapped objects. There are so many ways you may want to fetch data from a database, it should be considered a first-class feature of any database abstraction API. Just to name a few, here are some of jOOQ's fetching modes:
@@ -5586,7 +5479,7 @@ Result
These means of fetching are also available from
These means of fetching are also available from
Note, that apart from the
jOOQ understands that SQL is much more expressive than Java, when it comes to the declarative typing of
When you use the
By default, jOOQ returns an
Note that most of these convenience methods are available both through
In a more functional operating mode, you might want to write callbacks that receive records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
See also the manual's section about the
In a more functional operating mode, you might want to write callbacks that map records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
See also the manual's section about the
Fetching data in records is fine as long as your application is not really layered, or as long as you're still writing code in the DAO layer. But if you have a more advanced application architecture, you may not want to allow for jOOQ artefacts to leak into other layers. You may choose to write POJOs (Plain Old Java Objects) as your primary DTOs (Data Transfer Objects), without any dependencies on jOOQ's
Just as with any other JPA implementation, you can put the
Please refer to the
Please refer to the
Please refer to the
Note: Because of your manual setting of ID = 10, jOOQ's store() method will asume that you want to insert a new record. See the manual's section about
jOOQ currently doesn't support more complex data structures, the way Hibernate/JPA attempt to map relational data onto POJOs. While future developments in this direction are not excluded, jOOQ claims that generic mapping strategies lead to an enormous additional complexity that only serves very few use cases. You are likely to find a solution using any of jOOQ's various More complex data structures
Unlike JDBC's
As a
Many databases support returning several result sets, or cursors, from single queries. An example for this is Sybase ASE's sp_help command:
-The correct (and verbose) way to do this with JDBC is as follows:
-
As previously discussed in the chapter about
Using generics, the resulting structure is immediately clear.
-
Some queries take very long to execute, yet they are not crucial for the continuation of the main program. For instance, you could be generating a complicated report in a Swing application, and while this report is being calculated in your database, you want to display a background progress bar, allowing the user to pursue some other work. This can be achived simply with jOOQ, by creating a
Note, that instead of letting jOOQ spawn a new thread, you can also provide jOOQ with your own
When interacting with legacy applications, you may prefer to have jOOQ return a
Instead of operating on a JDBC ResultSet holding an open resource from your database, you can also let jOOQ's
The inverse of the above is possible too. Maybe, a legacy part of your application produces JDBC
Apart from a few extra features (see the manual's section about
Such a converter can be used in many parts of the jOOQ API. Some examples have been illustrated in the manual's section about
jOOQ ships with a built-in default
jOOQ also allows for generated source code to reference your own custom converters, in order to permanently replace a
With JDBC, you have full control over your SQL statements. You can decide yourself, if you want to execute a static
@@ -6345,12 +6233,12 @@ inlined.select(val(1)).where(val(1).equal(1)).fetch();]]>
Note that you don't have to inline all your bind values at once. If you know that a bind value is not really a variable and should be inlined explicitly, you can do so by using
As previously discussed in the chapter about
The above technique can be quite useful when you want to reuse expensive database resources. This can be the case when your statement is executed very frequently and your database would take non-negligible time to soft-parse the prepared statement and generate a new statement / cursor resource. @@ -6387,7 +6275,7 @@ finally { This is also modeled in jOOQ. However, the difference to JDBC is that closing a statement is the default action, whereas keeping it open has to be configured explicitly. This is better than JDBC, because the default action should be the one that is used most often. Keeping open statements is rarely done in average applications. Here's an example of how to keep open PreparedStatements with jOOQ:
-The above example shows how a query can be executed twice against the same underlying PreparedStatement. Unlike in other execution scenarios, you must not forget to close this query now
-With JDBC, you can easily execute several statements at once using the addBatch() method. Essentially, there are two modes in JDBC
@@ -6422,7 +6310,7 @@ finally {In code, this looks like the following snippet:
-
Most databases support sequences of some sort, to provide you with unique values to be used for primary keys and other enumerations. If you're using jOOQ's
You can inline sequence references in jOOQ SQL statements. The following are examples of how to do that:
-
For more info about inlining sequence references in SQL statements, please refer to the manual's section about
Many RDBMS support the concept of "routines", usually calling them procedures and/or functions. These concepts have been around in programming languages for a while, also outside of databases. Famous languages distinguishing procedures from functions are:
@@ -6566,14 +6453,14 @@ create.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) If you're using jOOQ'sThe generated artefacts can then be used as follows:
-But you can also call the procedure using a generated convenience method in a global Routines class:
-
For more details about
The generated artefacts can then be used as follows:
-
For more info about inlining stored function references in SQL statements, please refer to the manual's section about
Oracle uses the concept of a PACKAGE to group several procedures/functions into a sort of namespace. The SQL 92 standard talks about "modules", to represent this concept, even if this is rarely implemented as such. This is reflected in jOOQ by the use of Java sub-packages in the
For more details about
Oracle UDTs can have object-oriented structures including member functions and procedures. With Oracle, you can do things like this:
-These member functions and procedures can simply be mapped to Java methods:
-
For more details about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's exporting functionality (see also the
The above query will result in an XML document looking like the following one:
-
The same result as an
See the XSD schema definition here, for a formal definition of the XML export format:
http://www.jooq.org/xsd/jooq-export-1.6.2.xsd
The above query will result in a CSV document looking like the following one:
-In addition to the standard behaviour, you can also specify a separator character, as well as a special string to represent NULL values (which cannot be represented in standard CSV):
-The above query will result in a JSON document looking like the following one:
-Note: This format has changed in jOOQ 2.6.0
-The above query will result in an HTML document looking like the following one
-The above query will result in a text document looking like the following one
-
A simple text representation can also be obtained by calling toString() on a Result object. See also the manual's section about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's importing functionality (see also exporting functionality). You can import data directly into a table from the formats described in the subsequent sections of this manual.
-
The below CSV data represents two author records that may have been exported previously, by jOOQ's
With jOOQ, you can load this data using various parameters from the loader API. A simple load may look like this:
-Here are various other examples:
-Any of the above configuration methods can be combined to achieve the type of load you need. Please refer to the API's Javadoc to learn about more details. Errors that occur during the load are reported by the execute method's result:
-This is not yet supported
-Your database application probably consists of 50% - 80% CRUD, whereas only the remaining 20% - 50% of querying is actual querying. Most often, you will operate on records of tables without using any advanced relational concepts. This is called CRUD for
@@ -6998,13 +6869,13 @@ Query query = error.query();]]> In normalised databases, every table has a primary key by which a tuple/record within that table can be uniquely identified. In simple cases, this is a (possibly auto-generated) number called ID. But in many cases, primary keys include several non-numeric columns. An important feature of such keys is the fact that in most databases, they are enforced using an index that allows for very fast random access to the table. A typical way to access / modify / delete a book is this: -
Normalised databases assume that a primary key is unique "forever", i.e. that a key, once inserted into a table, will never be changed or re-inserted after deletion. In order to use jOOQ's
For simplicity, the term "primary key" will be used in the sense of such a "main unique key" in this manual.
-
If you're using jOOQ's
See the manual's section about
Some remarks about storing: @@ -7086,11 +6957,11 @@ book2.store();]]> Deleting a record will remove it from the database. Here's how you delete records:
-@@ -7100,53 +6971,52 @@ book.delete();]]> In order to perform a refresh, use the following Java code:
-
CRUD operations can be combined with regular querying, if you select records from single database tables, as explained in the manual's section about
Many databases support the concept of IDENTITY values, or
If you're using jOOQ's
@@ -7155,8 +7025,8 @@ System.out.println(book.getId());]]>
These SQL dialects implement the standard very neatly.
-H2, MySQL, Postgres, SQL Server, Sybase ASE, Sybase SQL Anywhere @@ -7164,7 +7034,7 @@ id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1)]]>
These SQL dialects implement identites, but the DDL syntax doesn’t follow the standard
-Oracle @@ -7188,7 +7058,7 @@ id INTEGER NOT NULL IDENTITY]]>
Oracle does not know any identity columns at all. Instead, you will have to use a trigger and update the ID column yourself, using a custom sequence. Something along these lines:
-Note, that this approach can be employed in most databases supporting sequences and triggers! It is a lot more flexible than standard identities
-
Tables without UNIQUE keys are considered non-updatable by jOOQ, as jOOQ has no way of uniquely identifying such a record within the database. If you're using jOOQ's
Note, that some databases use internal rowid or object-id values to identify such records. jOOQ does not support these vendor-specific record meta-data.
-
jOOQ allows you to perform
If you're using jOOQ's
The MODIFIED column will contain a timestamp indicating the last modification timestamp for any book in the BOOK table. If you're using jOOQ and it's
As before, without the added TIMESTAMP column, optimistic locking is transparent to the API. @@ -7356,17 +7224,17 @@ book2.store();]]>
Note, for explicit pessimistic locking, please consider the manual's section about the
When inserting, updating, deleting a lot of records, you may wish to profit from JDBC batch operations, which can be performed by jOOQ. These are available through jOOQ's
Internally, jOOQ will render all the required SQL statements and execute them as a regular
If you're using jOOQ's
corresponds to the DAO's related generated POJO type
// getType();
-}]]>
Besides these base methods, generated DAO classes implement various useful fetch methods. An incomplete example is given here, for the BOOK table:
-Note that you can further subtype those pre-generated DAO classes, to add more useful DAO methods to them. Using such a DAO is simple:
-This is an eternal and religious debate. Pros and cons have been discussed time and again, and it still is a matter of taste, today. In this case, jOOQ clearly takes a side. jOOQ's exception strategy is simple: @@ -7502,12 +7368,12 @@ bookDao.delete(book);]]>
The following section about
The
Now, configure jOOQ's runtime to load your listener
-And log results any time with a snippet like this:
-This may result in the following log output:
-
Please read the
See also the manual's sections about
jOOQ logs all SQL queries and fetched result sets to its internal DEBUG logger, which is implemented as an
With the above configuration, let's fetch some data with jOOQ
-The above query may result in the following log output:
-Essentially, jOOQ will log @@ -7682,12 +7548,12 @@ Finishing : Total: 4.814ms, +3.375ms
If you wish to use your own logger (e.g. avoiding printing out sensitive data), you can deactivate jOOQ's logger using
Many users may have switched from higher-level abstractions such as Hibernate to jOOQ, because of Hibernate's hard-to-manage performance, when it comes to large database schemas and complex second-level caching strategies. jOOQ is not a lightweight database abstraction framework, and it comes with its own overhead. Please be sure to consider the following points:
@@ -7702,14 +7568,14 @@ Finishing : Total: 4.814ms, +3.375msDon't be put off by the above paragraphs. You should optimise wisely, i.e. only in places where you really need very high throughput to your database. jOOQ's overhead compared to plain JDBC is typically less than 1ms per query.
-
While optional, source code generation is one of jOOQ's main assets if you wish to increase developer productivity. jOOQ's code generator takes your database schema and reverse-engineers it into a set of Java classes modelling
The following chapters will show how to configure the code generator and how to generate various artefacts.
-There are three binaries available with jOOQ, to be downloaded from SourceForge or from Maven central:
@@ -7755,7 +7621,7 @@ Finishing : Total: 4.814ms, +3.375ms You need to tell jOOQ some things about your database connection. Here's an example of how to do it for an Oracle database -
There are also lots of advanced configuration parameters, which will be treated in the
@@ -7842,7 +7708,7 @@ Finishing : Total: 4.814ms, +3.375ms
Code generation works by calling this class with the above property file as argument.
Be sure that these elements are located on the classpath: @@ -7905,7 +7771,7 @@ Finishing : Total: 4.814ms, +3.375ms You can also use an ant task to generate your classes. As a rule of thumb, remove the dots "." and dashes "-" from the .properties file's property names to get the ant task's arguments:
-Note that when running code generation with ant's <java/> task, you may have to set fork="true":
-Using the official jOOQ-codegen-maven plugin, you can integrate source code generation in your Maven build process:
-
See the full example of a pom.xml including the jOOQ-codegen artefact here:
@@ -8012,7 +7878,7 @@ Finishing : Total: 4.814ms, +3.375ms
Before jOOQ 2.0.4, the code generator was configured using properties files. These files are still supported for source code generation, but their syntax won't be maintained any longer. If you wish to migrate to XML, you can migrate the file using this command on the command line
Using the migrate flag, jOOQ will read the properties file and output a corresponding XML file on system out @@ -8022,17 +7888,17 @@ Finishing : Total: 4.814ms, +3.375ms
Be sure, both jooq-{jooq-version}.jar and your generated package (see configuration) are located on your classpath. Once this is done, you can execute SQL statements with your generated classes.
-
In the
The following example shows how you can override the DefaultGeneratorStrategy to render table and column names the way they are defined in the database, rather than switching them to camel case:
-Within the <generator/> element, there are other configuration elements:
-Check out the some of the manual's "advanced" sections to find out more about the advanced configuration parameters. @@ -8229,7 +8095,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { Also, you can add some optional advanced configuration parameters for the generator:
-For increased convenience at the use-site, jOOQ generates "global" artefacts at the code generation root location, referencing tables, routines, sequences, etc. In detail, these global artefacts include the following:
@@ -8312,7 +8177,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { When referencing global artefacts from your client application, you would typically static import them as such: -
Every table in your database will generate a
@@ -8371,17 +8235,17 @@ create.insertInto(com.example.generated.Tables.MY_TABLE)
Table generation cannot be deactivated
-
Every table in your database will generate a
@@ -8442,17 +8306,17 @@ implements IBook {
Record generation can be deactivated using the records flag
-Every table in your database will generate a POJO implementation that looks like this:
-@@ -8505,24 +8369,24 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the pojos flag
-Every table in your database will generate an interface that looks like this:
-@@ -8537,18 +8401,18 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the interfaces flag
-
Every table in your database will generate a
DAO generation can be activated using the daos flag
-
Every sequence in your database will generate a
Sequence generation cannot be deactivated
-
Every procedure or function (routine) in your database will generate a
@@ -8645,17 +8509,17 @@ public class Book implements java.io.Serializable
Routine generation cannot be deactivated
-
Every UDT in your database will generate a
Besides the
UDT generation cannot be deactivated
-NOTE: This feature is deprecated in jOOQ 2.5.0 and will be removed as of jOOQ 3.0
@@ -8711,7 +8575,7 @@ public class Book implements java.io.Serializable As previously discussed, you can configure master data tables as follows: -The results of this will be a Java enum that looks similar to this:
-In the above example, you can see how the configured primary key is mapped to the id member, the configured literal column is mapped to the cd member and the configured description member is mapped to the description member and output as Javadoc. In other words, T_LANGUAGE is a table with 4 rows and at least three columns. @@ -8781,39 +8645,39 @@ public class Book implements java.io.Serializable The point of MasterDataTypes in jOOQ is that they behave exactly like true ENUM types. When the above T_LANGUAGE table is referenced by T_BOOK, instead of generating foreign key navigation methods and a LANGUAGE_ID Field<Integer>, a Field<TLanguage> is generated:
-Which can then be used in the BookRecord directly:
-You can use master data types when you're actually mapping master data to a Java enum. When the underlying table changes frequently, those updates will not be reflected by the statically generated code. Also, be aware that it will be difficult to perform actual JOIN operations on the underlying table with jOOQ, once the master data type is generated.
-
When using a custom type in jOOQ, you need to let jOOQ know about its associated
The above configuration will lead to AUTHOR.DATE_OF_BIRTH being generated like this:
-
This means that the bound type of <T> will be GregorianCalendar, wherever you reference DATE_OF_BIRTH. jOOQ will use your custom converter when binding variables and when fetching data from
We've seen previously in the chapter about
These chapters hold some information about tools to be used with jOOQ
-
The
Or when using programmatic settings:
-The in-process mode is useful for Swing applications or other, locally run Java programs accessing the database via jOOQ. In order to launch the jOOQ Console "in-process", specify the previously documented settings and launch the Console as follows:
-Only in the in-process mode, you can execute ad-hoc queries directly from the console, if you provide it with proper DatabaseDescriptor. These queries are executed from the Editor pane which features: @@ -9016,36 +8876,36 @@ catch (Exception ignore) {} In J2EE or other server/client environments, you may not be able to run the console in the same process as your application. You can then run the jOOQ Console in "headless" mode. In addition to the previously documented settings, you'll have to start a debugger server in your application process, that the console can connect to:
-Now start your application along with the debugger server and launch the console with this command:
-Depending on your distribution, you may have to manually add rsyntaxtextarea-1.5.0.jar and jOOQ artefacts on your classpath.
-These chapters hold some general jOOQ reference information
-Every RDMBS out there has its own little specialties. jOOQ considers those specialties as much as possible, while trying to standardise the behaviour in jOOQ. In order to increase the quality of jOOQ, some 70 unit tests are run for syntax and variable binding verification, as well as some 180 integration tests with an overall of around 1200 queries for any of these databases: @@ -9104,12 +8964,12 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
This section will soon contain a feature matrix, documenting what feature is available for which database.
-There is always a small mismatch between SQL data types and Java data types. This is for two reasons:
@@ -9120,21 +8980,21 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);This chapter should document the most important notes about SQL, JDBC and jOOQ data types.
-jOOQ currently doesn't explicitly support JDBC BLOB and CLOB data types. If you use any of these data types in your database, jOOQ will map them to byte[] and String instead. In simple cases (small data), this simplification is sufficient. In more sophisticated cases, you may have to bypass jOOQ, in order to deal with these data types and their respective resources. True support for LOBs is on the roadmap, though.
-
Some databases explicitly support unsigned integer data types. In most normal JDBC-based applications, they would just be mapped to their signed counterparts letting bit-wise shifting and tweaking to the user. jOOQ ships with a set of unsigned
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. SQL knows two different types of intervals:
@@ -9183,51 +9043,51 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);XML data types are currently not supported
-Geospacial data types are currently not supported
-Some databases support cursors returned from stored procedures. They are mapped to the following jOOQ data type:
-
In fact, such a cursor will be fetched immediately by jOOQ and wrapped in an
The SQL standard specifies ARRAY data types, that can be mapped to Java arrays as such:
-The above array type is supported by these SQL dialects: @@ -9242,23 +9102,23 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
Oracle has strongly-typed arrays and table types (as opposed to the previously seen anonymously typed arrays). These arrays are wrapped by
This chapter will soon contain an overview over jOOQ's API using a pseudo BNF notation.
-jOOQ lives in a very challenging ecosystem. The Java to SQL interface is still one of the most important system interfaces. Yet there are still a lot of open questions, best practices and no "true" standard has been established. This situation gave way to a lot of tools, APIs, utilities which essentially tackle the same problem domain as jOOQ. jOOQ has gotten great inspiration from pre-existing tools and this section should give them some credit. Here is a list of inspirational tools in alphabetical order:
@@ -9271,7 +9131,7 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);This manual is divided into six main sections:
@@ -88,12 +88,12 @@ -Java and SQL have come a long way. SQL is an "ancient", yet established and well-understood technology. Java is a legacy too, although its platform JVM allows for many new and contemporary languages built on top of it. Yet, after all these years, libraries dealing with the interface between SQL and Java have come and gone, leaving JPA to be a standard that is accepted only with doubts, short of any surviving options. @@ -149,18 +149,18 @@
SQL was never meant to be abstracted. To be confined in the narrow boundaries of heavy mappers, hiding the beauty and simplicity of relational data. SQL was never meant to be object-oriented. SQL was never meant to be anything other than... SQL!
-This section lists the various licenses that apply to different versions of jOOQ. Prior to version 3.2, jOOQ was shipped for free under the terms of the Apache Software License 2.0. With jOOQ 3.2, jOOQ became dual-licensed: Apache Software License 2.0 (for use with Open Source databases) and commercial (for use with commercial databases). Please contact sales@datageekery.com, should you have any questions regarding licensing.
Throughout the manual, the above trademarks are referenced without a formal ® (R) or ™ (TM) symbol. It is believed that referencing third-party trademarks in this manual or on the jOOQ website constitutes "fair use". Please contact us if you think that your trademark(s) are not properly attributed.
-These chapters contain a quick overview of how to get started with this manual and with jOOQ. While the subsequent chapters contain a lot of reference information, this chapter here just wraps up the essentials.
-This section helps you correctly interpret this manual in the context of jOOQ.
@@ -302,28 +302,21 @@ and Maintenance Agreement for more details: http://www.jooq.org/eula The following are code blocks: -These are useful to provide examples in code. Often, with jOOQ, it is even more useful to compare SQL code with its corresponding Java/jOOQ code. When this is done, the blocks are aligned side-by-side, with SQL usually being on the left, and Java usually being on the right:
-@@ -331,13 +324,11 @@ create.selectOne()]]>
-Your naming may differ, of course. For instance, you could name the "create" instance "db", instead. @@ -368,16 +359,16 @@ DSLContext create = DSL.using(connection, SQLDialect.ORACLE);]]>
jOOQ query examples run against the sample database. See the manual's section about
For the examples in this manual, the same database will always be referred to. It essentially consists of these entities created using the Oracle dialect
-More entities, types (e.g. UDT's, ARRAY types, ENUM types, etc), stored procedures and packages are introduced for specific examples
-jOOQ has originally been created as a library for complete abstraction of JDBC and all database interaction. Various best practices that are frequently encountered in pre-existing software products are applied to this library. This includes:
@@ -453,23 +444,23 @@ CREATE TABLE book_to_book_store (The following sections explain about various use cases for using jOOQ in your application.
-
This is the most simple of all use cases, allowing for construction of valid SQL for any database. In this use case, you will not use
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -481,23 +472,23 @@ String sql = create.select(fieldByName("BOOK","TITLE"), fieldByName("AUTHOR","FI
In addition to using jOOQ as a
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -509,30 +500,30 @@ String sql = create.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Instead of any tool mentioned in the previous chapters, you can also use jOOQ directly to execute your jOOQ-generated SQL statements. This will add a lot of convenience on top of the previously discussed API for typesafe SQL construction, when you can re-use the information from generated classes to fetch records and custom data types. An example is given here:
-jOOQ doesn't stop here, though! You can execute any SQL with jOOQ. In other words, you can use any other SQL building tool and run the SQL statements with jOOQ. An example is given here:
-
If you wish to use jOOQ as a SQL executor with (or without) code generation, the following sections of the manual will be of interest to you:
@@ -552,17 +543,17 @@ Result
This is probably the most complete use-case for jOOQ: Use all of jOOQ's features. Apart from jOOQ's fluent API for query construction, jOOQ can also help you execute everyday CRUD operations. An example is given here:
-If you wish to use all of jOOQ's features, the following sections of the manual will be of interest to you (including all sub-sections): @@ -586,12 +577,12 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
jOOQ isn't just a library that helps you
If you're a power user of your favourite, feature-rich database, jOOQ will help you access all of your database's vendor-specific features, such as OLAP features, stored procedures, user-defined types, vendor-specific SQL, functions, etc. Examples are given throughout this manual.
-Don't have time to read the full manual? Here are a couple of tutorials that will get you into the most essential parts of jOOQ as quick as possible.
-This manual section is intended for new users, to help them get a running application with jOOQ, quickly.
-
If you haven't already downloaded it, download jOOQ:
https://sourceforge.net/projects/jooq/files/Release/
@@ -640,7 +631,7 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
Alternatively, you can create a Maven dependency to download jOOQ artefacts:
-
Please refer to the manual's section about
If you don't have a MySQL instance up and running yet, get XAMPP now! XAMPP is a simple installation bundle for Apache, MySQL, PHP and Perl
-We're going to create a database called "guestbook" and a corresponding "posts" table. Connect to MySQL via your command line client and type the following:
-
In this step, we're going to use jOOQ's command line tools to generate classes that map to the Posts table we just created. More detailed information about how to set up the jOOQ code generator can be found here:
Replace the username with whatever user has the appropriate privileges to query the database meta data. You'll also want to look at the other values and replace as necessary. Here are the two interesting properties:
@@ -761,15 +751,15 @@ CREATE TABLE `posts` ( Once you have the JAR files and guestbook.xml in your temp directory, type this (use colons instead of semi-colons on UNIX/Linux systems): -Note the prefix slash before guestbook.xml. Even though it's in our working directory, we need to prepend a slash, as the configuration file is loaded from the classpath. Replace the filenames with your filenames. In this example, jOOQ {jooq-version} is being used. If everything has worked, you should see this in your console output:
-Let's just write a vanilla main class in the project containing the generated classes:
-This is pretty standard code for establishing a MySQL connection.
-Let's add a simple query:
-
First get an instance of
We then use jOOQ's DSL to return an instance of Result. We'll be using this result in the next step.
DSLContext so we can write a simple SELECT query. We pass an instance of the MySQL connection to DSL. Note that the DSLContext doesn't close the connection. We'll have to do that ourselves.
@@ -888,29 +876,29 @@ Result
After the line where we retrieve the results, let's iterate over the results and print out the data:
-The full program should now look like this:
-
jOOQ has grown to be a comprehensive SQL library. For more information, please consider the documentation:
http://www.jooq.org/learn.php
@@ -983,37 +970,37 @@ public class Main {
This tutorial is the courtesy of Ikai Lan. See the original source here:
http://ikaisays.com/2011/11/01/getting-started-with-jooq-a-tutorial/
Feel free to contribute a tutorial!
-Feel free to contribute a tutorial!
-Feel free to contribute a tutorial!
-As any other library, jOOQ can be easily used in Scala, taking advantage of the many Scala language features such as for example:
@@ -1036,7 +1023,7 @@ public class Main { A short example jOOQ application in Scala might look like this: -
For more details about jOOQ's Scala integration, please refer to the manual's section about
Dependencies are a big hassle in modern software. Many libraries depend on other, non-JDK library parts that come in different, incompatible versions, potentially causing trouble in your runtime environment. jOOQ has no external dependencies on any third-party libraries.
@@ -1097,12 +1084,12 @@ object Test { //In order to build jOOQ yourself, please download the sources from https://github.com/jOOQ/jOOQ and use Maven to build jOOQ, preferably in Eclipse. jOOQ requires Java 6+ to compile and run.
@@ -1126,12 +1113,12 @@ object Test { // -jOOQ follows the rules of semantic versioning according to http://semver.org quite strictly. Those rules impose a versioning scheme [X].[Y].[Z] that can be summarised as follows:
@@ -1159,14 +1146,14 @@ object Test { //jOOQ's DSL interfaces should not be implemented by client code! Extend only those extension points that are explicitly documented as "extendable" (e.g.
SQL is a declarative language that is hard to integrate into procedural, object-oriented, functional or any other type of programming languages. jOOQ's philosophy is to give SQL the credit it deserves and integrate SQL itself as an "internal domain specific language" directly into Java.
@@ -1176,7 +1163,7 @@ object Test { //
This section explains all about the various syntax elements involved with jOOQ's SQL building capabilities. For a complete overview of all syntax elements, please refer to the manual's section about
jOOQ exposes a lot of interfaces and hides most implementation facts from client code. The reasons for this are:
@@ -1203,21 +1190,20 @@ object Test { // With jOOQ 2.0, static factory methods have been introduced in order to make client code look more like SQL. Ideally, when working with jOOQ, you will simply static import all methods from the DSL class: -Note, that when working with Eclipse, you could also add the DSL to your favourites. This will allow to access functions even more fluently:
-There are a couple of subclasses for the general DSL. Each SQL dialect has its own dialect-specific DSL. For instance, if you're only using the MySQL dialect, you can choose to reference the MySQLDSL instead of the standard DSL:
@@ -1228,7 +1214,7 @@ DSL.concat(DSL.trim(FIRST_NAME), DSL.trim(LAST_NAME));
DSLContext references a
If you do not have a reference to a pre-existing Configuration object (e.g. created from DSL.using() methods will create one for you.
@@ -1285,32 +1271,31 @@ DSLContext create = DSL.using(connection, dialect);]]>
Wrapping a Configuration object, a DSLContext can construct
Note that you do not need to keep a reference to a DSLContext. You may as well inline your local variable, and fluently execute a SQL statement as such:
-
While jOOQ tries to represent the SQL standard as much as possible, many features are vendor-specific to a given database and to its "SQL dialect". jOOQ models this using the
Some parts of the jOOQ API are officially supported only by a given subset of the supported SQL dialects. For instance, the
jOOQ API methods which are not annotated with the
The aforementioned Support annotation does not only designate, which databases natively support a feature. It also indicates that a feature is simulated by jOOQ for some databases lacking this feature. An example of this is the
Nevertheless, the
jOOQ has a historic affinity to Oracle's SQL extensions. If something is supported in Oracle SQL, it has a high probability of making it into the jOOQ API
IS DISTINCT FROM predicate is supported by jOOQ in all dialects, as its semantics can be expressed with an equivalent
While you can use jOOQ for
Note that acquire() should always return the same Connection until this connection is returned via release()
In advanced use cases of integrating your application with jOOQ, you may want to put custom data into your
INSERT statements, when a given flag is set to true:
-
See the manual's section about ExecuteListener.
Configuration, in order for the listener to work:
-
Using the data() methods, you can store and retrieve custom data in your Configurations.
ExecuteListeners are a useful tool to...
See the manual's section about
The jOOQ Configuration allows for some optional configuration elements to be used by advanced users. The
For example, if you want to indicate to jOOQ, that it should inline all bind variables, and execute static
Subsequent sections of the manual contain some more in-depth explanations about these settings:
@@ -1545,12 +1530,12 @@ DSLContext create = DSL.using(connection, dialect, settings);]]> Please refer to the jOOQ runtime configuration XSD for more details:
You may wish to design your database in a way that you have several instances of your schema. This is useful when you want to cleanly separate data belonging to several customers / organisation units / branches / users and put each of those entities' data in a separate database or schema.
@@ -1570,7 +1555,7 @@ DSLContext create = DSL.using(connection, dialect, settings);]]>
When a user from My Book World logs in, you want them to access the MY_BOOK_WORLD schema using classes generated from DEV. This can be achieved with the
The query executed with a Configuration equipped with the above mapping will in fact produce this SQL statement:
-Even if AUTHOR was generated from DEV.
@@ -1596,7 +1581,7 @@ create.selectFrom(AUTHOR).fetch(); Your development database may not be restricted to hold only one DEV schema. You may also have a LOG schema and a MASTER schema. Let's say the MASTER schema is shared among all customers, but each customer has their own LOG schema instance. Then you can enhance your RenderMapping like this (e.g. using an XML configuration file): -Note, you can load the above XML file like this:
-This will map generated classes from DEV to MY_BOOK_WORLD, from LOG to MY_BOOK_WORLD_LOG, but leave the MASTER schema alone. Whenever you want to change your mapping configuration, you will have to create a new Configuration. @@ -1628,21 +1613,21 @@ create.selectFrom(AUTHOR).fetch();
-Not only schemata can be mapped, but also tables. If you are not the owner of the database your application connects to, you might need to install your schema with some sort of prefix to every table. In our examples, this might mean that you will have to map DEV.AUTHOR to something MY_BOOK_WORLD.MY_APP__AUTHOR, where MY_APP__ is a prefix applied to all of your tables. This can be achieved by creating the following mapping:
-The query executed with a Configuration equipped with the above mapping will in fact produce this SQL statement:
-Table mapping and schema mapping can be applied independently, by specifying several MappedSchema entries in the above configuration. jOOQ will process them in order of appearance and map at first match. Note that you can always omit a MappedSchema's output value, in case of which, only the table mapping is applied. If you omit a MappedSchema's input value, the table mapping is applied to all schemata! @@ -1671,23 +1656,23 @@ create.selectFrom(AUTHOR).fetch();
Note that the manual's section about
jOOQ currently supports 6 types of SQL statements. All of these statements are constructed from a DSLContext instance with an optional
jOOQ ships with its own DSL (or Domain Specific Language) that simulates SQL in Java. This means, that you can write SQL statements almost as if Java natively supported it, just like .NET's C# does with LINQ to SQL.
@@ -1695,22 +1680,21 @@ create.selectFrom(AUTHOR).fetch(); Here is an example to illustrate what that means: -
We'll see how the aliasing works later in the section about
Unlike other, simpler frameworks that use "fluent APIs" or "method chaining", jOOQ's BNF-based interface hierarchy will not allow bad query syntax. The following will not compile, for instance:
-
Historically, jOOQ started out as an object-oriented SQL builder library like any other. This meant that all queries and their syntactic components were modeled as so-called
This query is equivalent to the one shown before using the DSL syntax. In fact, internally, the DSL API constructs precisely this SelectQuery object. Note, that you can always access the SelectQuery object to switch between DSL and model APIs:
-
Note, that for historic reasons, the DSL API mixes mutable and immutable behaviour with respect to the internal representation of the
On the other hand, beware that you can always extract and modify QueryPart.
When you don't just perform
Details about the various clauses of this query will be provided in subsequent sections.
@@ -1875,14 +1858,14 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
A very similar, but limited API is available, if you want to select from single physical tables in order to retrieve SELECT statement with the DSL or DSLContext types:
As you can see, there is no way to further restrict/project the selected fields. This just selects all known TableFields in the supplied Table, and it also binds <R extends Record> to your Table's associated Record. An example of such a Query would then be:
-The "reduced" SELECT API is limited in the way that it skips DSL access to any of these clauses: @@ -1894,41 +1877,39 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
In most parts of this manual, it is assumed that you do not use the "reduced" SELECT API. For more information about the simple SELECT API, see the manual's section about
The SELECT clause lets you project your own record types, referencing table fields, functions, arithmetic expressions, etc. The DSL type provides several methods for expressing a SELECT clause:
-Some commonly used projections can be easily created using convenience methods:
-
See more details about functions and expressions in the manual's section about
jOOQ does not explicitly support the asterisk operator in projections. However, you can omit the projection as in these examples:
-
Since jOOQ 3.0, SELECT (and SELECT DISTINCT) API in both DSL and DSLContext. An extract from the DSL type:
Since the generic R type is bound to some SELECT statement can be assigned typesafely:
For more information about typesafe record types with degree up to {max-row-degree}, see the manual's section about
The SQL FROM clause allows for specifying any number of
Read more about aliasing in the manual's section about
Note, in order to access the DbmsXplan package, you can use the
Read more about dual or dummy tables in the manual's section about
jOOQ supports many different types of standard SQL JOIN operations:
@@ -2063,7 +2040,7 @@ DSL.using(SQLDialect.POSTGRES).selectOne().getSQL();]]> All of these JOIN methods can be called on
The two syntaxes will produce the same SQL statement. However, calling "join" on
In case of ambiguity, you can also supply field references for your foreign keys, or the generated foreign key reference to the onKey() method. @@ -2128,137 +2103,130 @@ JOIN BOOK ON BOOK.AUTHOR_ID = AUTHOR.ID]]> Most often, you will provide jOOQ with JOIN conditions in the JOIN .. ON clause. SQL supports a different means of specifying how two tables are to be joined. This is the JOIN .. USING clause. Instead of a condition, you supply a set of fields whose names are common to both tables to the left and right of a JOIN operation. This can be useful when your database schema has a high degree of relational normalisation. An example:
-In schemas with high degrees of normalisation, you may also choose to use NATURAL JOIN, which takes no JOIN arguments as it joins using all fields that are common to the table expressions to the left and to the right of the JOIN operator. An example:
-Oracle SQL ships with a special syntax available for OUTER JOIN clauses. According to the Oracle documentation about partitioned outer joins this can be used to fill gaps for simplified analytical calculations. jOOQ only supports putting the PARTITION BY clause to the right of the OUTER JOIN clause. The following example will create at least one record per AUTHOR and per existing value in BOOK.PUBLISHED_IN, regardless if an AUTHOR has actually published a book in that year.
-
The WHERE clause can be used for JOIN or filter predicates, in order to restrict the data returned by the
The above syntax is convenience provided by jOOQ, allowing you to connect the
You will find more information about creating
The Oracle database knows a very succinct syntax for creating hierarchical queries: the CONNECT BY clause, which is fully supported by jOOQ, including all related functions and pseudo-columns. A more or less formal definition of this clause is given here:
-An example for an iterative query, iterating through values between 1 and 5 is this:
-Here's a more complex example where you can recursively fetch directories in your database, and concatenate them to a path:
-The output might then look like this
-
Some of the supported functions and pseudo-columns are these (available from the
GROUP BY can be used to create unique groups of data, to form aggregations, to remove duplicates and for other reasons. It will transform your previously defined
The above example counts all books per author. @@ -2337,13 +2303,12 @@ GROUP BY AUTHOR_ID]]> MySQL has a peculiar way of not adhering to this standard behaviour. This is documented in the MySQL manual. In short, with MySQL, you can also project any other field that is not part of the GROUP BY clause. The projected values will just be arbitrary values from within the group. You cannot rely on any ordering. For example:
-
This will return an arbitrary title per author. jOOQ supports this syntax, as jOOQ is not doing any checks internally, about the consistence of tables/fields/functions that you provide it.
@@ -2354,68 +2319,64 @@ GROUP BY AUTHOR_ID]]>
jOOQ supports empty GROUP BY () clause as well. This will result in
Some databases support the SQL standard grouping functions and some extensions thereof. See the manual's section about
The HAVING clause is commonly used to further restrict data resulting from a previously issued
According to the SQL standard, you may omit the GROUP BY clause and still issue a HAVING clause. This will implicitly GROUP BY (). jOOQ also supports this syntax. The following example selects one record, only if there are at least 4 books in the books table:
-Databases are allowed to return data in any arbitrary order, unless you explicitly declare that order in the ORDER BY clause. In jOOQ, this is straight-forward:
-
Any jOOQ
Note, how one() is used as a convenience short-cut for inline(1)
@@ -2443,87 +2403,84 @@ ORDER BY 1 ASC, 2 DESC]]>
A few databases support the SQL standard "null ordering" clause in sort specification lists, to define whether NULL values should come first or last in an ordered result.
If your database doesn't support this syntax, jOOQ simulates it using a
Using
But writing these things can become quite verbose. jOOQ supports a convenient syntax for specifying sort mappings. The same query can be written in jOOQ as such:
-More complex sort indirections can be provided using a Map:
-Of course, you can combine this feature with the previously discussed NULLS FIRST / NULLS LAST feature. So, if in fact these two books are the ones you like least, you can put all NULLS FIRST (all the other books):
-@@ -2535,23 +2492,23 @@ ORDER BY CASE TITLE
jOOQ also supports Oracle's SIBLINGS keyword to be used with ORDER BY clauses for
While being extremely useful for every application that does paging, or just to limit result sets to reasonable sizes, this clause is not yet part of any SQL standard (up until SQL:2008). Hence, there exist a variety of possible implementations in various SQL dialects, concerning this limit clause. jOOQ chose to implement the LIMIT .. OFFSET clause as understood and supported by MySQL, H2, HSQLDB, Postgres, and SQLite. Here is an example of how to apply limits with jOOQ:
-This will limit the result to 1 books starting with the 2nd book (starting at offset 0!). limit() is supported in all dialects, offset() in all but Sybase ASE, which has no reasonable means to simulate it. This is how jOOQ simulates the above query in various SQL dialects:
-As you can see, jOOQ will take care of the incredibly painful ROW_NUMBER() OVER() (or ROWNUM for Oracle) filtering in subselects for you, you'll just have to write limit(1).offset(2) in any dialect. @@ -2596,39 +2553,37 @@ AND ROWNUM_98843777 <= 3
As can be seen in the above example, writing correct SQL can be quite tricky, depending on the SQL dialect. For instance, with SQL Server, you cannot have an ORDER BY clause in a subquery, unless you also have a TOP clause. This is illustrated by the fact that jOOQ renders a TOP 100 PERCENT clause for you. The same applies to the fact that ROW_NUMBER() OVER() needs an ORDER BY windowing clause, even if you don't provide one to the jOOQ query. By default, jOOQ adds ordering by the first column of your projection.
-For inter-process synchronisation and other reasons, you may choose to use the SELECT .. FOR UPDATE clause to indicate to the database, that a set of cells or records should be locked by a given transaction for subsequent updates. With jOOQ, this can be achieved as such:
-The above example will produce a record-lock, locking the whole record for updates. Some databases also support cell-locks using FOR UPDATE OF ..
-Oracle goes a bit further and also allows to specify the actual locking behaviour. It features these additional clauses, which are all supported by jOOQ: @@ -2642,15 +2597,15 @@ FOR UPDATE OF TITLE]]> With jOOQ, you can use those Oracle extensions as such:
-
The SQL standard specifies a FOR UPDATE clause to be applicable for cursors. Most databases interpret this as being applicable for all SELECT statements. An exception to this rule are the CUBRID and SQL Server databases, that do not allow for any FOR UPDATE clause in a regular SQL SELECT statement. jOOQ simulates the FOR UPDATE behaviour, by locking record by record with JDBC. JDBC allows for specifying the flags TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE for any statement, and then using ResultSet.updateXXX() methods to produce a cell-lock / row-lock. Here's a simplified example in JDBC:
The main drawback of this approach is the fact that the database has to maintain a scrollable cursor, whose records are locked one by one. This can cause a major risk of deadlocks or race conditions if the JDBC driver can recover from the unsuccessful locking, if two Java threads execute the following statements:
-So use this technique with care, possibly only ever locking single rows! @@ -2687,12 +2642,12 @@ SELECT * FROM author ORDER BY id DESC;]]>
Note, that jOOQ also supports optimistic locking, if you're doing simple CRUD. This is documented in the section's manual about
SQL allows to perform set operations as understood in standard set theory on result sets. These operations include unions, intersections, subtractions. For two subselects to be combinable by such a set operator, each subselect must return a
UNION removes all duplicate records resulting from this combination, UNION ALL leaves subselect results as they are. Typically, you should prefer UNION ALL over UNION, if you don't really need to remove duplicates. The following example shows how to use such a UNION operation in jOOQ.
-
@@ -2720,24 +2674,23 @@ create.selectFrom(BOOK).where(BOOK.ID.equal(5)));]]>
As previously mentioned in the manual's section about the
Two subselects that are combined by a set operator are required to be of the same degree and, in most databases, also of the same type. jOOQ 3.0's introduction of
If you are closely coupling your application to an Oracle (or CUBRID) database, you might need to be able to pass hints of the form /*+HINT*/ with your SQL statements to the Oracle database. For example:
This can be done in jOOQ using the .hint() clause in your SELECT statement:
Note that you can pass any string in the .hint() clause. If you use that clause, the passed string will always be put in between the SELECT [DISTINCT] keywords and the actual projection list. This can be useful in other databases too, such as MySQL, for instance:
SQL has a lexical and a logical order of SELECT clauses. The lexical order of SELECT clauses is inspired by the English language. As SQL statements are commands for the database, it is natural to express a statement in an imperative tense, such as "SELECT this and that!".
A LINQ example:
-A SLICK example:
-
While this looks like a good idea at first, it only complicates translation to more advanced SQL statements while impairing readability for those users that are used to writing SQL. jOOQ is designed to look just like SQL. This is specifically true for SLICK, which not only changed the SELECT clause order, but also heavily "integrated" SQL clauses with the Scala language.
@@ -2867,48 +2818,46 @@ val q = for {
For these reasons, the jOOQ DSL API is modelled in SQL's lexical order.
-
The INSERT statement is used to insert new records into a database table. Records can either be supplied using a VALUES() constructor, or a SELECT statement. jOOQ supports both types of INSERT statements. An example of an INSERT statement using a VALUES() constructor is given here:
Note that for explicit degrees up to {max-row-degree}, the VALUES() constructor provides additional typesafety. The following example illustrates this:
The SQL standard specifies that multiple rows can be supplied to the VALUES() constructor in an INSERT statement. Here's an example of a multi-record INSERT
-jOOQ tries to stay close to actual SQL. In detail, however, Java's expressiveness is limited. That's why the values() clause is repeated for every record in multi-record inserts. @@ -2917,30 +2866,29 @@ VALUES (100, 'Hermann', 'Hesse'), Some RDBMS do not support inserting several records in a single statement. In those cases, jOOQ simulates multi-record INSERTs using the following SQL:
-MySQL (and some other RDBMS) allow for using a non-SQL-standard, UPDATE-like syntax for INSERT statements. This is also supported in jOOQ, should you prefer that syntax. The above INSERT statement can also be expressed as follows:
-
As you can see, this syntax is a bit more verbose, but also more readable, as every field can be matched with its value. Internally, the two syntaxes are strictly equivalent.
@@ -2951,12 +2899,12 @@ SELECT 101, 'Alfred', 'Döblin' FROM DUAL;
The MySQL database supports a very convenient way to INSERT or UPDATE a record. This is a non-standard extension to the SQL syntax, which is supported by jOOQ and simulated in other RDBMS, where this is possible (i.e. if they support the SQL standard
The Postgres database has native support for an INSERT .. RETURNING clause. This is a very powerful concept that is simulated for all other dialects using JDBC's
Some databases have poor support for returning generated keys after INSERTs. In those cases, jOOQ might need to issue another
The UPDATE statement is used to modify one or several pre-existing records in a database table. UPDATE statements are only possible on single tables. Support for multi-table updates will be implemented in the near future. An example update query is given here:
-
Most databases allow for using scalar subselects in UPDATE statements in one way or another. jOOQ models this through a set(Field<T>, Select<? extends Record1<T>>) method in the UPDATE DSL API:
This can be particularly useful when using subselects:
-
The above row value expressions usages are completely typesafe.
@@ -3096,45 +3038,43 @@ create.insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
The Firebird and Postgres databases support a RETURNING clause on their UPDATE statements, similar as the RETURNING clause in
The UPDATE .. RETURNING clause is currently not simulated for other databases. Future versions might execute an additional
The DELETE statement physically removes records from a database table. DELETE statements are only possible on single tables. Support for multi-table deletes will be implemented in the near future. An example delete query is given here:
-The MERGE statement is one of the most advanced standardised SQL constructs, which is supported by DB2, HSQLDB, Oracle, SQL Server and Sybase (MySQL has the similar INSERT .. ON DUPLICATE KEY UPDATE construct)
@@ -3142,15 +3082,14 @@ RETURNING UPDATE_COUNT]]> The point of the standard MERGE statement is to take a TARGET table, and merge (INSERT, UPDATE) data from a SOURCE table into it. DB2, Oracle, SQL Server and Sybase also allow for DELETING some data and for adding many additional clauses. With jOOQ {jooq-version}, only Oracle's MERGE extensions are supported. Here is an example: -The H2 database ships with a somewhat less powerful but a little more intuitive syntax for its own version of the MERGE statement. An example more or less equivalent to the previous one can be seen here:
-
This syntax can be fully simulated by jOOQ for all other databases that support the SQL standard. For more information about the H2 MERGE syntax, see the documentation here:
@@ -3189,12 +3127,12 @@ VALUES ('John', 'Hitchcock')
Much like the MERGE statement's VALUES() clause provides typesafety for degrees up to {max-row-degree}, in both the standard syntax variant as well as the H2 variant.
The TRUNCATE statement is the only DDL statement supported by jOOQ so far. It is popular in many databases when you want to bypass constraints for table truncation. Databases may behave differently, when a truncated table is referenced by other tables. For instance, they may fail if records from a truncated table are referenced, even with ON DELETE CASCADE clauses in place. Please, consider your database manual to learn more about its TRUNCATE implementation.
@@ -3202,44 +3140,42 @@ VALUES ('John', 'Hitchcock') The TRUNCATE syntax is trivial: -TRUNCATE is not supported by Ingres and SQLite. jOOQ will execute a DELETE FROM AUTHOR statement instead.
-The following sections explain the various types of table expressions supported by jOOQ
-
Most of the times, when thinking about a
The above example shows how AUTHOR and BOOK tables are joined in a
See the manual's section about
The strength of jOOQ's
As you can see in the above example, calling as() on generated tables returns an object of the same type as the table. This means that the resulting object can be used to dereference fields from the aliased table. This is quite powerful in terms of having your Java compiler check the syntax of your SQL statements. If you remove a column from a table, dereferencing that column from that table alias will cause compilation errors.
@@ -3289,40 +3224,40 @@ create.select()
Only few table expressions provide the SQL syntax typesafety as shown above, where generated tables are used. Most tables, however, expose their fields through field() methods:
The SQL standard specifies how a table can be renamed / aliased in one go along with its columns. It references the term "derived column list" for the following syntax (as supported by Postgres, for instance):
-
This feature is useful in various use-cases where column names are not known in advance (but the table's degree is!). An example for this are
Only few databases really support such a syntax, but fortunately, jOOQ can simulate it easily using UNION ALL and an empty dummy record specifying the new column names. The two statements are equivalent:
In jOOQ, you would simply specify a varargs list of column aliases as such:
-
The
SQL and relational algebra distinguish between at least the following JOIN types (upper-case: SQL, lower-case: relational algebra):
@@ -3375,7 +3309,7 @@ create.select().from(values(
jOOQ supports all of these JOIN types (except semi-join and anti-join) directly on any
Note that most of jOOQ's JOIN operations give way to a similar DSL API hierarchy as previously seen in the manual's section about the
Some databases allow for expressing in-memory temporary tables using a VALUES() constructor. This constructor usually works the same way as the VALUES() clause known from the VALUES() table constructor, to create tables that can be used in a
Note, that it is usually quite useful to provide column aliases ("derived column lists") along with the table alias for the VALUES() constructor.
@@ -3430,7 +3363,7 @@ FROM VALUES(1, 'a'),
The above statement is simulated by jOOQ for those databases that do not support the VALUES() constructor, natively (actual simulations may vary):
A
If you are closely coupling your application to an Oracle database, you can take advantage of some Oracle-specific features, such as the PIVOT clause, used for statistical analyses. The formal syntax definition is as follows:
-
The PIVOT clause is available from the
There is one operation in relational algebra that is not given a lot of attention, because it is rarely used in real-world applications. It is the relational division, the opposite operation of the cross product (or, relational multiplication). The following is an approximate definition of a relational division:
-With jOOQ, you can simplify using relational divisions by using the following syntax:
-The above roughly translates to
-Or in plain text: Find those TEXT values in C whose ID's correspond to all ID's in B. Note that from the above SQL statement, it is immediately clear that proper indexing is of the essence. Be sure to have indexes on all columns referenced from the on(...) and returning(...) clauses. @@ -3587,13 +3516,13 @@ WHERE NOT EXISTS (
The SQL standard specifies how SQL databases should implement ARRAY and TABLE types, as well as CURSOR types. Put simply, a CURSOR is a pointer to any materialised
Note, in order to access the DbmsXplan package, you can use the
The SQL standard specifies that the
FROM clause, but renders the necessary "DUAL" table, if needed. The following program shows how jOOQ renders "DUAL" tables
-
Note, that some databases (H2, MySQL) can normally do without "DUAL". However, there exist some corner-cases with complex nested SELECT statements, where this will cause syntax errors (or parser bugs). To stay on the safe side, jOOQ will always render "dual" in those dialects.
Column expressions can be used in various SQL clauses in order to refer to one or several columns. This chapter explains how to form various types of column expressions with jOOQ. A particular type of column expression is given in the section about
In general, it is up to you whether you want to use the "prefix" notation or the "postfix" notation to create new column expressions based on existing ones. The "SQL way" would be to use the "prefix notation", with functions created from the
Table columns are the most simple implementations of a
Table columns implement a more specific interface called <R extends Record> record type.
@@ -3730,45 +3656,43 @@ ORDER BY BOOK.TITLE]]>
See the manual's section about
Just like
Here is how it's done with jOOQ:
-When you alias Fields like above, you can access those Fields' values using the alias name:
-
jOOQ's source code generator tries to find the most accurate type mapping between your vendor-specific data types and a matching Java type. For instance, most VARCHAR, CHAR, CLOB types will map to String. Most BINARY, BYTEA, BLOB types will map to byte[]. NUMERIC types will default to
CAST like this:
-in jOOQ, you can write something like that:
-
The same thing can be achieved by casting a Field directly to String.class, as TEXT is the default data type in Postgres to map to Java's String
The complete CAST API in
Your database can do the math for you. Arithmetic operations are implemented just like
In order to express a SQL query like this one:
-You can write something like this in jOOQ:
-jOOQ also supports the Oracle-style syntax for adding days to a Field<? extends java.util.Date>
-
For more advanced datetime arithmetic, use the DSL's timestampDiff() and dateDiff() functions, as well as jOOQ's built-in SQL standard INTERVAL data type support:
@@ -3859,31 +3781,30 @@ public class DSL {
INTERVAL YEAR TO MONTH: INTERVAL DAY TO SECOND:
The SQL standard defines the concatenation operator to be an infix operator, similar to the ones we've seen in the chapter about ||. Some other dialects do not support this operator, but expect a concat() function, instead. jOOQ renders the right operator / function, depending on your
There are a variety of general functions supported by jOOQ As discussed in the chapter about
Please refer to the
Math can be done efficiently in the database before returning results to your Java application. In addition to the
Please refer to the
Interestingly, bitwise functions and bitwise arithmetic is not very popular among SQL databases. Most databases only support a few bitwise operations, while others ship with the full set of operators. jOOQ's API includes most bitwise operations as listed below. In order to avoid ambiguities with
String formatting can be done efficiently in the database before returning results to your Java application. As discussed in the chapter about
REGEX_LIKE operator. Being an operator (and not a function), you should use the corresponding method on
Note that the SQL standard specifies that patterns should follow the XQuery standards. In the real world, the POSIX regular expression standard is the most used one, some use Java regular expressions, and only a few ones use Perl regular expressions. jOOQ does not make any assumptions about regular expression syntax. For cross-database compatibility, please read the relevant database manuals carefully, to learn about the appropriate syntax. Please refer to the
This is a list of date and time functions supported by jOOQ's
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. See the manual's section about
This is a list of system functions supported by jOOQ's
CURRENT_USER: Get current user.
Aggregate functions work just like functions, even if they have a slightly different semantics. Here are some example aggregate functions from the
Here's an example, counting the number of books any author has written:
-
Aggregate functions have strong limitations about when they may be used and when not. For instance, you can use aggregate functions in scalar queries. Typically, this means you only select aggregate functions, no
Oracle and some other databases support "ordered aggregate functions". This means you can provide an ORDER BY clause to an aggregate function, which will be taken into consideration when aggregating. The best example for this is Oracle's LISTAGG() (also known as GROUP_CONCAT in other
The above query might yield:
-
Oracle allows for restricting aggregate functions using the KEEP() clause, which is supported by jOOQ. In Oracle, some aggregate functions (MIN, MAX, SUM, AVG, COUNT, VARIANCE, or STDDEV) can be restricted by this clause, hence
@@ -4173,12 +4091,12 @@ GROUP BY AUTHOR_ID
In those databases that support over() on it. See the manual's section about
Most major RDBMS support the concept of window functions. jOOQ knows of implementations in DB2, Oracle, Postgres, SQL Server, and Sybase SQL Anywhere, and supports most of their specific syntaxes. Note, that H2 and HSQLDB have implemented ROW_NUMBER() functions, without true windowing support.
over() method. See the chapter about
SQL distinguishes between various window function types (e.g. "ranking functions"). Depending on the function, SQL expects mandatory PARTITION BY or ORDER BY clauses within the OVER() clause. jOOQ does not enforce those rules for two reasons:
@@ -4223,7 +4141,7 @@ GROUP BY AUTHOR_ID
Here are some simple examples of window functions with jOOQ:
Window functions can be used for things like calculating a "running total". The following example fetches transactions and the running total for every transaction going back to the beginning of the transaction table (ordered by booked_at). Window functions are accessible from the previously seen over() method:
LISTAGG(). These functions have a window function / analytical function variant, as well. For example:
-FIRST and LAST aggregate functions
In the previous chapter about FIRST and LAST aggregate functions". These functions have a window function / analytical function variant, as well. For example:
User-defined aggregate functions also implement over(). This is supported by Oracle in particular. See the manual's section about
The SQL standard defines special functions that can be used in the ROLLUP() for instance:
In English, the ROLLUP() grouping function provides N+1 groupings, when N is the number of arguments to the ROLLUP() function. Each grouping has an additional group field from the ROLLUP() argument field list. The results of the second query might look something like this:
CUBE() is different from ROLLUP() in the way that it doesn't just create N+1 groupings, it creates all 2^N possible combinations between all group fields in the CUBE() function argument list. Let's re-consider our second query from before:
The results would then hold:
-
@@ -4444,7 +4356,7 @@ ORDER BY 1 NULLS FIRST, 2 NULLS FIRST
jOOQ fully supports all of these functions, as well as the utility functions GROUPING() and GROUPING_ID(), used for identifying the grouping set ID of a record. The
MySQL and CUBRID don't know any grouping functions, but they support a WITH ROLLUP clause, that is equivalent to simple ROLLUP() grouping functions. jOOQ simulates ROLLUP() in MySQL and CUBRID, by rendering this WITH ROLLUP clause. The following two statements mean the same:
Some databases support user-defined functions, which can be embedded in any SQL statement, if you're using jOOQ's
The above function will be made available from a generated
Note that user-defined functions returning
Some databases support user-defined aggregate functions, which can then be used along with OBJECT type (the example was taken from the Oracle 11g documentation):
The above OBJECT type is then available to function declarations as such:
jOOQ's SECOND_MAX() aggregate function with jOOQ:
The CASE expression is part of the standard SQL syntax. While some RDBMS also offer an IF expression, or a DECODE function, you can always rely on the two types of CASE syntax:
In jOOQ, both syntaxes are supported (The second one is simulated in Derby, which only knows the first one). Unfortunately, both case and else are reserved words in Java. jOOQ chose to use decode() from the Oracle DECODE function, and otherwise(), which means the same as else.
@@ -4629,15 +4537,15 @@ create.decode().value(AUTHOR.FIRST_NAME)
A CASE expression can be used anywhere where you can place a SELECT the above expression, if you're selecting from AUTHOR:
Oracle knows a more succinct, but maybe less readable DECODE() function with a variable number of arguments. This function roughly does the same as the second case expression syntax. jOOQ supports the DECODE() function and simulates it using CASE expressions in all dialects other than Oracle:
Sort indirection is often implemented with a CASE clause of a SELECT's ORDER BY clause. See the manual's section about the
Sequences implement the
So if you have a sequence like this in Oracle:
-
You can then use your
According to the SQL standard, row value expressions can have a degree of more than one. This is commonly used in the VALUES row value constructor allows for providing a row value expression as a source for INSERT data. Row value expressions can appear in various other places, though. They are supported by jOOQ as records / rows. jOOQ's
@@ -4748,14 +4655,14 @@ public static RowN row(Object... values) { ... }]]>
jOOQ chose to explicitly support degrees up to {max-row-degree} to match Scala's typesafe tuple, function and product support. Unlike Scala, however, jOOQ also supports higher degrees without the additional typesafety.
-Conditions or conditional expressions are widely used in SQL and in the jOOQ API. They can be used in
@@ -4792,22 +4699,21 @@ public static RowN row(Object... values) { ... }]]>
Note that jOOQ does not model these values as actual
With jOOQ, most
@@ -4823,48 +4729,47 @@ BOOK.TITLE.notEqual("Animal Farm")]]>
Conditions can also be connected using
In SQL, as in most other languages, AND and OR binary operators, as well as the NOT unary operator, to form new conditional expressions. In jOOQ, this is modelled as such:
The above example shows that the number of parentheses in Java can quickly explode. Proper indentation may become crucial in making such code readable. In order to understand how jOOQ composes combined conditional expressions, let's assign component expressions first:
-
Here are all boolean operators on the
In SQL, comparison predicates are formed using common comparison operators:
@@ -4903,7 +4807,7 @@ not() // Invert a condition (synonym for DSL.not(Condition)] Unfortunately, Java does not support operator overloading, hence these operators are also implemented as methods in jOOQ, like any other SQL syntax elements. The relevant parts of the
Note that every operator is represented by two methods. A verbose one (such as equal()) and a two-character one (such as eq()). Both methods are the same. You may choose either one, depending on your taste. The manual will always use the more verbose one.
@@ -4931,25 +4835,24 @@ ge or greaterOrEqual(Select extends Record1
All variants of the
jOOQ supports all of the above row value expression comparison predicates, both with
If the right-hand side of a ALL, ANY, or SOME. Note that the SQL standard defines ANY and SOME to be equivalent. jOOQ settled for the more intuitive ANY and doesn't support SOME. Here are some examples, supported by jOOQ:
For the example, the right-hand side of the quantified comparison predicates were filled with argument lists. But it is easy to imagine that the source of values results from a ANY-quantified predicate. The following two expressions are equivalent:
Typically, the
In SQL, you cannot compare NULL with any value using NULL again, which is neither TRUE nor FALSE (see also the manual's section about NULL, use the NULL predicate as such:
The SQL NULL predicate also works well for NOT() operator! Here are some examples:
The SQL standard contains a nice truth table for the above rules:
-
In jOOQ, you would simply use the isNull() and isNotNull() methods on row value expressions. Again, as with the NULL predicate is simulated by jOOQ, if your database does not natively support it:
Some databases support the DISTINCT predicate, which serves as a convenient, NULL-safe DISTINCT predicate, the following truth table can be assumed:
NULL, which would lead to funny results. This is supported by jOOQ as such:
-
If your database does not natively support the DISTINCT predicate, jOOQ simulates it with an equivalent
The BETWEEN predicate can be seen as syntactic sugar for a pair of
Note the inclusiveness of range boundaries in the definition of the BETWEEN predicate. Intuitively, this is supported in jOOQ as such:
The SQL standard defines the SYMMETRIC keyword to be used along with BETWEEN to indicate that you do not care which bound of the range is larger than the other. A database system should simply swap range bounds, in case the first bound is greater than the second one. jOOQ supports this keyword as well, simulating it if necessary.
The simulation is done trivially:
-
The SQL BETWEEN predicate also works well for
The above can be factored out according to the rules listed in the manual's section about BETWEEN [SYMMETRIC] predicate and simulates it in all SQL dialects where necessary. An example is given here:
LIKE predicates are popular for simple wildcard-enabled pattern matching. Supported wildcards in all SQL databases are:
LIKE predicate can be created from any
Often, your pattern may contain any of the wildcard characters "_" and "%", in case of which you may want to escape them. jOOQ does not automatically escape patterns in like() and notLike() methods. Instead, you can explicitly define an escape character as such:
In the above predicate expressions, the exclamation mark character is passed as the escape character to escape wildcard characters "!_" and "!%", as well as to escape the escape character itself: "!!"
@@ -5289,7 +5173,7 @@ BOOK.TITLE.notLike("%The !%-Sign Book%", '!')]]>
In addition to the above, jOOQ provides a few convenience methods for common operations performed on strings using the LIKE predicate. Typical operations are "contains predicates", "starts with predicates", "ends with predicates", etc. Here is the full convenience API wrapping LIKE predicates:
Note, that jOOQ escapes % and _ characters in value in some of the above predicate implementations. For simplicity, this has been omitted in this manual.
In SQL, apart from comparing a value against several values, the IN predicate can be used to create semi-joins or anti-joins. jOOQ knows the following methods on the IN predicates:
A sample IN predicate might look like this:
NULL values in the right hand side of a NOT IN predicate, as the whole expression would evaluate to NULL, which is rarely desired. This can be shown informally using the following reasoning:
-
A good way to prevent this from happening is to use the NULL-value insensitive. See the manual's section about
The SQL IN predicate also works well for
jOOQ supports the IN predicate. Simulation of the IN predicate where row value expressions aren't well supported is currently only available for IN predicates that do not take a subselect as an IN predicate value. An example is given here:
Slightly less intuitive, yet more powerful than the previously discussed EXISTS predicate, that can be used to form semi-joins or anti-joins. With jOOQ, the EXISTS predicate can be formed in various ways:
EXISTS predicate can be seen here:
-
Note that in SQL, the projection of a subselect in an EXISTS predicate is irrelevant. To help you write queries like the above, you can use jOOQ's selectZero() or selectOne()
http://blog.jooq.org/2012/07/27/not-in-vs-not-exists-vs-left-join-is-null-mysql/
When comparing dates, the SQL standard allows for using a special OVERLAPS predicate, which checks whether two date ranges overlap each other. The following can be said:
jOOQ supports the OVERLAPS predicate on
This allows for expressing the above predicates as such:
-
Unlike the standard (or any database implementing the standard), jOOQ also supports the OVERLAPS predicate for comparing arbitrary (1, 3) OVERLAPS (2, 4) will yield true in jOOQ. This is simulated as such
A DSL is a nice thing to have, it feels "fluent" and "natural", especially if it models a well-known language, such as SQL. But a DSL is always expressed in a host language (Java in this case), which was not made for exactly the same purposes as its hosted DSL. If it were, then jOOQ would be implemented on a compiler-level, similar to LINQ in .NET. But it's not, and so, the DSL is limited by language constraints of its host language. We have seen many functionalities where the DSL becomes a bit verbose. This can be especially true for:
@@ -5502,7 +5380,7 @@ row(Date.valueOf('2010-01-01'), new DayToSecond(2)).overlaps(Date.valueOf('2010-
Plain SQL API methods are usually overloaded in three ways. Let's look at the condition query part constructor:
Please refer to the
Apart from the general factory methods, plain SQL is also available in various other contexts. For instance, when adding a .where("a = b") clause to a query. Hence, there exist several convenience methods where plain SQL can be inserted usefully. This is an example displaying all various use-cases in one single query:
@@ -5610,12 +5488,12 @@ create.select(LAST_NAME, COUNT1, COUNT2)
Bind values are used in SQL / JDBC for various reasons. Among the most obvious ones are:
@@ -5634,31 +5512,31 @@ create.select(LAST_NAME, COUNT1, COUNT2)The following sections explain how you can introduce bind values in jOOQ, and how you can control the way they are rendered and bound to SQL.
-JDBC only knows indexed bind values. A typical example for using bind values with JDBC is this:
-With dynamic SQL, keeping track of the number of question marks and their corresponding index may turn out to be hard. jOOQ abstracts this and lets you provide the bind value right where it is needed. A trivial example is this:
-
Note the using of
You can also extract specific bind values by index from a query, if you wish to modify their underlying value after creating a query. This can be achieved as such:
-
For more details about jOOQ's internals, see the manual's section about
Some SQL access abstractions that are built on top of JDBC, or some that bypass JDBC may support named parameters. jOOQ allows you to give names to your parameters as well, although those names are not rendered to SQL strings by default. Here is an example of how to create named parameters using the
The
In order to actually render named parameter names in generated SQL, use the
Sometimes, you may wish to avoid rendering bind variables while still using custom values in SQL. jOOQ refers to that as "inlined" bind values. When bind values are inlined, they render the actual value in SQL rather than a JDBC question mark. Bind value inlining can be achieved in two ways:
@@ -5765,7 +5642,7 @@ WHERE LAST_NAME = :lastName]]> In both cases, your inlined bind values will be properly escaped to avoid SQL syntax errors and SQL injection. Some examples: -
Special care needs to be taken when using
All methods in the jOOQ API that allow for plain (unescaped, untreated) SQL contain a warning message in their relevant Javadoc, to remind you of the risk of SQL injection in what is otherwise a SQL-injection-safe API.
-
A
Every
The following additional methods are inherited from a common
A simple example can be provided by checking out jOOQ's internal representation of a (simplified) AUTHOR.ID = BOOK.AUTHOR_ID condition here:
This is how jOOQ renders such a condition (simplified example):
-
See the manual's sections about
As mentioned in the previous chapter about
And then, use the above DSLContext to render pretty-printed SQL:
-
The section about
Every
Some additional methods are inherited from a common AUTHOR.ID = BOOK.AUTHOR_ID condition here:
This is how jOOQ binds values on such a condition:
-
See the manual's sections about
If a SQL clause is too complex to express with jOOQ, you can extend either one of the following types for use directly in a jOOQ query:
-
These classes are declared public and covered by jOOQ's integration tests. When you extend these classes, you will have to provide your own implementations for the
The above contract may be a bit tricky to understand at first. The best thing is to check out jOOQ source code and have a look at a couple of QueryParts, to see how it's done. Here's an example
If you don't need the integration of rather complex QueryParts into jOOQ, then you might be safer using simple
The above technique allows for creating rather complex SQL clauses that are currently not supported by jOOQ, without extending any of the
The only transient, non-serializable element in any jOOQ object is the
Another way of attaching QueryParts automatically, or rather providing them with a new
jOOQ-Scala is a maven module used for leveraging some advanced Scala features for those users that wish to use jOOQ with Scala.
@@ -6169,7 +6043,7 @@ create.attach(select);]]> The following depicts a trait which wraps all fields: -The following depicts a trait which wraps numeric fields:
-An example query using such overloaded operators would then look like this:
-This feature is still being experimented with. With Scala Macros, it might be possible to inline a true SQL dialect into the Scala syntax, backed by the jOOQ API. Stay tuned!
-
In a previous section of the manual, we've seen how jOOQ can be used to
The following sections of this manual will show how jOOQ is wrapping JDBC for SQL execution
-
Even if there are
Unlike JDBC, jOOQ has a lot of knowledge about a SQL query's structure and internals (see the manual's section about
Fetching is something that has been completely neglegted by JDBC and also by various other database abstraction libraries. Fetching is much more than just looping or listing records or mapped objects. There are so many ways you may want to fetch data from a database, it should be considered a first-class feature of any database abstraction API. Just to name a few, here are some of jOOQ's fetching modes:
@@ -6390,7 +6263,7 @@ Result
These means of fetching are also available from
These means of fetching are also available from
Note, that apart from the
jOOQ understands that SQL is much more expressive than Java, when it comes to the declarative typing of
When you use the
jOOQ's
jOOQ chose to explicitly support degrees up to {max-row-degree} to match Scala's typesafe tuple, function and product support. Unlike Scala, however, jOOQ also supports higher degrees without the additional typesafety.
-
By default, jOOQ returns an
Note that most of these convenience methods are available both through
In a more functional operating mode, you might want to write callbacks that receive records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
See also the manual's section about the
In a more functional operating mode, you might want to write callbacks that map records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
See also the manual's section about the
Fetching data in records is fine as long as your application is not really layered, or as long as you're still writing code in the DAO layer. But if you have a more advanced application architecture, you may not want to allow for jOOQ artefacts to leak into other layers. You may choose to write POJOs (Plain Old Java Objects) as your primary DTOs (Data Transfer Objects), without any dependencies on jOOQ's
Just as with any other JPA implementation, you can put the
Please refer to the
Please refer to the
Please refer to the
Note: Because of your manual setting of ID = 10, jOOQ's store() method will asume that you want to insert a new record. See the manual's section about
jOOQ currently doesn't support more complex data structures, the way Hibernate/JPA attempt to map relational data onto POJOs. While future developments in this direction are not excluded, jOOQ claims that generic mapping strategies lead to an enormous additional complexity that only serves very few use cases. You are likely to find a solution using any of jOOQ's various More complex data structures
Unlike JDBC's
As a
Many databases support returning several result sets, or cursors, from single queries. An example for this is Sybase ASE's sp_help command:
-The correct (and verbose) way to do this with JDBC is as follows:
-
As previously discussed in the chapter about
Using generics, the resulting structure is immediately clear.
-
Some queries take very long to execute, yet they are not crucial for the continuation of the main program. For instance, you could be generating a complicated report in a Swing application, and while this report is being calculated in your database, you want to display a background progress bar, allowing the user to pursue some other work. This can be achived simply with jOOQ, by creating a
Note, that instead of letting jOOQ spawn a new thread, you can also provide jOOQ with your own
When interacting with legacy applications, you may prefer to have jOOQ return a
Instead of operating on a JDBC ResultSet holding an open resource from your database, you can also let jOOQ's
The inverse of the above is possible too. Maybe, a legacy part of your application produces JDBC
You can also tighten the interaction with jOOQ's data type system and
If supplied, the additional information is used to override the information obtained from the
Apart from a few extra features (
Such a converter can be used in many parts of the jOOQ API. Some examples have been illustrated in the manual's section about
jOOQ ships with a built-in default
jOOQ also allows for generated source code to reference your own custom converters, in order to permanently replace a
SQL result tables are not optimal in terms of used memory as they are not designed to represent hierarchical data as produced by JOIN operations. Specifically, FOREIGN KEY values may repeat themselves unnecessarily:
Now, if you have millions of records with only few distinct values for AUTHOR_ID, you may not want to hold references to distinct (but equal)
You can specify as many fields as you want for interning. The above has the following effect: @@ -7199,14 +7070,14 @@ Result> r1 = create.select(BOOK.ID, BOOK.AUTHOR_ID, BOOK.TITLE)
Note, that jOOQ will not use interned data for identity comparisons: string1 == string2. Interning is used only to reduce the memory footprint of
With JDBC, you have full control over your SQL statements. You can decide yourself, if you want to execute a static
@@ -7248,12 +7118,12 @@ inlined.select(val(1)).where(val(1).equal(1)).fetch();]]>
Note that you don't have to inline all your bind values at once. If you know that a bind value is not really a variable and should be inlined explicitly, you can do so by using
As previously discussed in the chapter about
The above technique can be quite useful when you want to reuse expensive database resources. This can be the case when your statement is executed very frequently and your database would take non-negligible time to soft-parse the prepared statement and generate a new statement / cursor resource. @@ -7290,7 +7160,7 @@ finally { This is also modeled in jOOQ. However, the difference to JDBC is that closing a statement is the default action, whereas keeping it open has to be configured explicitly. This is better than JDBC, because the default action should be the one that is used most often. Keeping open statements is rarely done in average applications. Here's an example of how to keep open PreparedStatements with jOOQ:
-The above example shows how a query can be executed twice against the same underlying PreparedStatement. Unlike in other execution scenarios, you must not forget to close this query now
-With JDBC, you can easily execute several statements at once using the addBatch() method. Essentially, there are two modes in JDBC
@@ -7325,7 +7195,7 @@ finally {In code, this looks like the following snippet:
-
When creating a batch execution with a single query and multiple bind values, you will still have to provide jOOQ with dummy bind values for the original query. In the above example, these are set to null. For subsequent calls to bind(), there will be no type safety provided by jOOQ.
Most databases support sequences of some sort, to provide you with unique values to be used for primary keys and other enumerations. If you're using jOOQ's
You can inline sequence references in jOOQ SQL statements. The following are examples of how to do that:
-
For more info about inlining sequence references in SQL statements, please refer to the manual's section about
Many RDBMS support the concept of "routines", usually calling them procedures and/or functions. These concepts have been around in programming languages for a while, also outside of databases. Famous languages distinguishing procedures from functions are:
@@ -7477,14 +7347,14 @@ create.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) If you're using jOOQ'sThe generated artefacts can then be used as follows:
-But you can also call the procedure using a generated convenience method in a global Routines class:
-
For more details about
The generated artefacts can then be used as follows:
-
For more info about inlining stored function references in SQL statements, please refer to the manual's section about
Oracle uses the concept of a PACKAGE to group several procedures/functions into a sort of namespace. The SQL 92 standard talks about "modules", to represent this concept, even if this is rarely implemented as such. This is reflected in jOOQ by the use of Java sub-packages in the
For more details about
Oracle UDTs can have object-oriented structures including member functions and procedures. With Oracle, you can do things like this:
-These member functions and procedures can simply be mapped to Java methods:
-
For more details about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's exporting functionality (see also the
The above query will result in an XML document looking like the following one:
-
The same result as an
See the XSD schema definition here, for a formal definition of the XML export format:
http://www.jooq.org/xsd/jooq-export-{export-xsd-version}.xsd
The above query will result in a CSV document looking like the following one:
-In addition to the standard behaviour, you can also specify a separator character, as well as a special string to represent NULL values (which cannot be represented in standard CSV):
-The above query will result in a JSON document looking like the following one:
-Note: This format has changed in jOOQ 2.6.0
-The above query will result in an HTML document looking like the following one
-The above query will result in a text document looking like the following one
-
A simple text representation can also be obtained by calling toString() on a Result object. See also the manual's section about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's importing functionality (see also exporting functionality). You can import data directly into a table from the formats described in the subsequent sections of this manual.
-
The below CSV data represents two author records that may have been exported previously, by jOOQ's
With jOOQ, you can load this data using various parameters from the loader API. A simple load may look like this:
-Here are various other examples:
-Any of the above configuration methods can be combined to achieve the type of load you need. Please refer to the API's Javadoc to learn about more details. Errors that occur during the load are reported by the execute method's result:
-This is not yet supported
-Your database application probably consists of 50% - 80% CRUD, whereas only the remaining 20% - 50% of querying is actual querying. Most often, you will operate on records of tables without using any advanced relational concepts. This is called CRUD for
@@ -7909,35 +7763,35 @@ Query query = error.query();]]> In normalised databases, every table has a primary key by which a tuple/record within that table can be uniquely identified. In simple cases, this is a (possibly auto-generated) number called ID. But in many cases, primary keys include several non-numeric columns. An important feature of such keys is the fact that in most databases, they are enforced using an index that allows for very fast random access to the table. A typical way to access / modify / delete a book is this: -
Normalised databases assume that a primary key is unique "forever", i.e. that a key, once inserted into a table, will never be changed or re-inserted after deletion. In order to use jOOQ's
If you're using jOOQ's
See the manual's section about
Some remarks about storing: @@ -7984,11 +7838,11 @@ book2.store();]]> Deleting a record will remove it from the database. Here's how you delete records:
-@@ -7998,31 +7852,30 @@ book.delete();]]> In order to perform a refresh, use the following Java code:
-
CRUD operations can be combined with regular querying, if you select records from single database tables, as explained in the manual's section about selectFrom() method from the
All of jOOQ's
The purpose of the above information is for jOOQ's
Many databases support the concept of IDENTITY values, or
If you're using jOOQ's
@@ -8071,8 +7924,8 @@ System.out.println(book.getId());]]>
These SQL dialects implement the standard very neatly.
-H2, MySQL, Postgres, SQL Server, Sybase ASE, Sybase SQL Anywhere @@ -8080,7 +7933,7 @@ id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1)]]>
These SQL dialects implement identites, but the DDL syntax doesn’t follow the standard
-Oracle @@ -8104,7 +7957,7 @@ id INTEGER NOT NULL IDENTITY]]>
Oracle does not know any identity columns at all. Instead, you will have to use a trigger and update the ID column yourself, using a custom sequence. Something along these lines:
-Note, that this approach can be employed in most databases supporting sequences and triggers! It is a lot more flexible than standard identities
-
Tables without a PRIMARY KEY are considered non-updatable by jOOQ, as jOOQ has no way of uniquely identifying such a record within the database. If you're using jOOQ's
Note, that some databases use internal rowid or object-id values to identify such records. jOOQ does not support these vendor-specific record meta-data.
-
jOOQ allows you to perform
If you're using jOOQ's
The MODIFIED column will contain a timestamp indicating the last modification timestamp for any book in the BOOK table. If you're using jOOQ and it's
As before, without the added TIMESTAMP column, optimistic locking is transparent to the API. @@ -8247,17 +8099,17 @@ book2.store();]]>
Note, for explicit pessimistic locking, please consider the manual's section about the
When inserting, updating, deleting a lot of records, you may wish to profit from JDBC batch operations, which can be performed by jOOQ. These are available through jOOQ's
Internally, jOOQ will render all the required SQL statements and execute them as a regular
If you're using jOOQ's
corresponds to the DAO's related generated POJO type
// getType();
-}]]>
Besides these base methods, generated DAO classes implement various useful fetch methods. An incomplete example is given here, for the BOOK table:
-Note that you can further subtype those pre-generated DAO classes, to add more useful DAO methods to them. Using such a DAO is simple:
-This is an eternal and religious debate. Pros and cons have been discussed time and again, and it still is a matter of taste, today. In this case, jOOQ clearly takes a side. jOOQ's exception strategy is simple: @@ -8393,12 +8243,12 @@ bookDao.delete(book);]]>
The following section about
The
Now, configure jOOQ's runtime to load your listener
-And log results any time with a snippet like this:
-This may result in the following log output:
-
Please read the
See also the manual's sections about
Since jOOQ 3.0, a simple wrapping API has been added to wrap JDBC's rather awkward
jOOQ logs all SQL queries and fetched result sets to its internal DEBUG logger, which is implemented as an
With the above configuration, let's fetch some data with jOOQ
-The above query may result in the following log output:
-Essentially, jOOQ will log @@ -8583,12 +8433,12 @@ Finishing : Total: 4.814ms, +3.375ms
If you wish to use your own logger (e.g. avoiding printing out sensitive data), you can deactivate jOOQ's logger using
Many users may have switched from higher-level abstractions such as Hibernate to jOOQ, because of Hibernate's difficult-to-manage performance, when it comes to large database schemas and complex second-level caching strategies. However, jOOQ itself is not a lightweight database abstraction framework, and it comes with its own overhead. Please be sure to consider the following points:
@@ -8603,14 +8453,14 @@ Finishing : Total: 4.814ms, +3.375msDon't be put off by the above paragraphs. You should optimise wisely, i.e. only in places where you really need very high throughput to your database. jOOQ's overhead compared to plain JDBC is typically less than 1ms per query.
-
While optional, source code generation is one of jOOQ's main assets if you wish to increase developer productivity. jOOQ's code generator takes your database schema and reverse-engineers it into a set of Java classes modelling
The following chapters will show how to configure the code generator and how to generate various artefacts.
-There are three binaries available with jOOQ, to be downloaded from SourceForge or from Maven central:
@@ -8656,7 +8506,7 @@ Finishing : Total: 4.814ms, +3.375ms You need to tell jOOQ some things about your database connection. Here's an example of how to do it for an Oracle database -
There are also lots of advanced configuration parameters, which will be treated in the
@@ -8742,7 +8592,7 @@ Finishing : Total: 4.814ms, +3.375ms
Code generation works by calling this class with the above property file as argument.
Be sure that these elements are located on the classpath: @@ -8810,20 +8660,20 @@ Finishing : Total: 4.814ms, +3.375ms When running code generation with ant's <java/> task, you may have to set fork="true":
-Using the official jOOQ-codegen-maven plugin, you can integrate source code generation in your Maven build process:
-
See the full example of a pom.xml including the jOOQ-codegen artefact here:
@@ -8887,17 +8737,17 @@ Finishing : Total: 4.814ms, +3.375ms
Be sure, both jooq-{jooq-version}.jar and your generated package (see configuration) are located on your classpath. Once this is done, you can execute SQL statements with your generated classes.
-
In the
The following example shows how you can override the DefaultGeneratorStrategy to render table and column names the way they are defined in the database, rather than switching them to camel case:
-More examples can be found here: @@ -9036,7 +8886,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { Within the <generator/> element, there are other configuration elements:
-Check out the some of the manual's "advanced" sections to find out more about the advanced configuration parameters. @@ -9098,7 +8948,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { Also, you can add some optional advanced configuration parameters for the generator:
-@@ -9179,12 +9029,12 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy {
daos = true, then jOOQ will set pojos = trueimmutablePojos = true, then jOOQ will set pojos = trueFor increased convenience at the use-site, jOOQ generates "global" artefacts at the code generation root location, referencing tables, routines, sequences, etc. In detail, these global artefacts include the following:
@@ -9201,7 +9051,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { When referencing global artefacts from your client application, you would typically static import them as such: -
Every table in your database will generate a
@@ -9260,17 +9109,17 @@ create.insertInto(com.example.generated.Tables.MY_TABLE)
Table generation cannot be deactivated
-
Every table in your database will generate a
@@ -9331,17 +9180,17 @@ implements IBook {
Record generation can be deactivated using the records flag
-Every table in your database will generate a POJO implementation that looks like this:
-@@ -9394,24 +9243,24 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the pojos flag
-Every table in your database will generate an interface that looks like this:
-@@ -9426,18 +9275,18 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the interfaces flag
-
Every table in your database will generate a
DAO generation can be activated using the daos flag
-
Every sequence in your database will generate a
Sequence generation cannot be deactivated
-
Every procedure or function (routine) in your database will generate a
@@ -9534,17 +9383,17 @@ public class Book implements java.io.Serializable
Routine generation cannot be deactivated
-
Every UDT in your database will generate a
Besides the
UDT generation cannot be deactivated
-
When using a custom type in jOOQ, you need to let jOOQ know about its associated
The above configuration will lead to AUTHOR.DATE_OF_BIRTH being generated like this:
-
This means that the bound type of <T> will be GregorianCalendar, wherever you reference DATE_OF_BIRTH. jOOQ will use your custom converter when binding variables and when fetching data from
We've seen previously in the chapter about
Databases can become very large in real-world applications. This is not a problem for jOOQ's code generator, but it can be for the Java compiler. jOOQ generates some classes for
<globalObjectReferences/>These chapters hold some information about tools to be used with jOOQ
-When writing unit tests for your data access layer, you have probably used some generic mocking tool offered by popular providers like Mockito, jmock, mockrunner, or even DBUnit. With jOOQ, you can take advantage of the built-in JDBC mock API that allows you to simulate a database on the JDBC level for precisely those SQL/JDBC use cases supported by jOOQ.
@@ -9728,7 +9573,7 @@ create.selectFrom(AUTHOR) This work is greatly simplified, when using jOOQ's own mock API. Theorg.jooq.tools.jdbc package contains all the essential implementations for both JDBC 4.0 and 4.1, which are needed to mock JDBC for jOOQ. In order to write mock tests, provide the jOOQ
As you can see, the configuration setup is simple. Now, the MockDataProvider acts as your single point of contact with JDBC / jOOQ. It unifies any of these execution modes, transparently:
@@ -9760,7 +9605,7 @@ ResultMockDataProvider:
Essentially, the
See the
The
The in-process mode is useful for Swing applications or other, locally run Java programs accessing the database via jOOQ. In order to launch the jOOQ Console "in-process", specify the previously documented settings and launch the Console as follows:
-Only in the in-process mode, you can execute ad-hoc queries directly from the console, if you provide it with proper DatabaseDescriptor. These queries are executed from the Editor pane which features: @@ -9930,36 +9775,36 @@ catch (Exception ignore) {} In J2EE or other server/client environments, you may not be able to run the console in the same process as your application. You can then run the jOOQ Console in "headless" mode. In addition to the previously documented settings, you'll have to start a debugger server in your application process, that the console can connect to:
-Now start your application along with the debugger server and launch the console with this command:
-Depending on your distribution, you may have to manually add rsyntaxtextarea-2.0.2.jar and jOOQ artefacts on your classpath.
-These chapters hold some general jOOQ reference information
-Every RDMBS out there has its own little specialties. jOOQ considers those specialties as much as possible, while trying to standardise the behaviour in jOOQ. In order to increase the quality of jOOQ, some 70 unit tests are run for syntax and variable binding verification, as well as some 180 integration tests with an overall of around 1200 queries for any of these databases: @@ -10018,12 +9863,12 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
This section will soon contain a feature matrix, documenting what feature is available for which database.
-There is always a small mismatch between SQL data types and Java data types. This is for two reasons:
@@ -10034,21 +9879,21 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);This chapter should document the most important notes about SQL, JDBC and jOOQ data types.
-jOOQ currently doesn't explicitly support JDBC BLOB and CLOB data types. If you use any of these data types in your database, jOOQ will map them to byte[] and String instead. In simple cases (small data), this simplification is sufficient. In more sophisticated cases, you may have to bypass jOOQ, in order to deal with these data types and their respective resources. True support for LOBs is on the roadmap, though.
-
Some databases explicitly support unsigned integer data types. In most normal JDBC-based applications, they would just be mapped to their signed counterparts letting bit-wise shifting and tweaking to the user. jOOQ ships with a set of unsigned
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. SQL knows two different types of intervals:
@@ -10097,51 +9942,51 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);XML data types are currently not supported
-Geospacial data types are currently not supported
-Some databases support cursors returned from stored procedures. They are mapped to the following jOOQ data type:
-
In fact, such a cursor will be fetched immediately by jOOQ and wrapped in an
The SQL standard specifies ARRAY data types, that can be mapped to Java arrays as such:
-The above array type is supported by these SQL dialects: @@ -10156,23 +10001,23 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
Oracle has strongly-typed arrays and table types (as opposed to the previously seen anonymously typed arrays). These arrays are wrapped by
This chapter will soon contain an overview over jOOQ's API using a pseudo BNF notation.
-jOOQ is running some of your most mission-critical logic: the interface layer between your Java / Scala application and the database. You have probably chosen jOOQ for any of the following reasons:
@@ -10212,7 +10057,7 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT); jOOQ is used in jOOQ-meta as a proof of concept. This includes complex queries such as the following Postgres query -These rather complex queries show that the jOOQ API is fit for advanced SQL use-cases, compared to the rather simple, often unrealistic queries in the integration test suite.
@@ -10275,12 +10120,12 @@ for (Record record : create().select(Keeping things DRY leads to longer stack traces, but in turn, also increases the relevance of highly reusable code-blocks. Chances that some parts of the jOOQ code base slips by integration test coverage decrease significantly.
-This section is for all users of jOOQ 2.x who wish to upgrade to the next major release. In the next sub-sections, the most important changes are explained. Some code hints are also added to help you fix compilation errors.
@@ -10304,13 +10149,13 @@ for (Record record : create().select( Some hints related to row value expressions: -
@@ -10336,7 +10181,7 @@ Result> result = create.select(BOOK.TITLE, BOOK.ID).from(BOOK).fetch();]]>
-
Field.equalAny(...) and similar methods have been removed in favour of Field.equal(any(...)). This greatly simplified the Field API. An example:
-
@@ -10372,7 +10217,7 @@ Condition condition = BOOK.ID.equal(subselect);]]>
GroupField has been introduced as a DSL marker interface to denote fields that can be passed to GROUP BY clauses. This includes all org.jooq.Field types. However, fields obtained from ROLLUP(), CUBE(), and GROUPING SETS() functions no longer implement Field. Instead, they only implement GroupField. An example:
@@ -10398,14 +10243,14 @@ Field> field2c = DSL.one(); // OK]]> Here is an example how to check if a field has a given value, without applying SQL's ternary NULL logic:
-@@ -10426,14 +10271,14 @@ Condition condition3 = BOOK.TITLE.isNotDistinctFrom(possiblyNull);]]> In order to allow for simpler connection / data source management, jOOQ externalised connection handling in a new ConnectionProvider type. The previous two connection modes are maintained backwards-compatibly (JDBC standalone connection mode, pooled DataSource mode). Other connection modes can be injected using:
-These are some side-effects of the above change @@ -10483,7 +10328,7 @@ Condition condition3 = BOOK.TITLE.isNotDistinctFrom(possiblyNull);]]>
Table.getPrimaryKey() != null check.USE statement support has been removed from jOOQ. Its behaviour was ill-defined, while it didn't work the same way (or didn't work at all) in some databases.These are useful to provide examples in code. Often, with jOOQ, it is even more useful to compare SQL code with its corresponding Java/jOOQ code. When this is done, the blocks are aligned side-by-side, with SQL usually being on the left, and Java usually being on the right:
-@@ -331,13 +324,11 @@ create.selectOne()]]>
-Your naming may differ, of course. For instance, you could name the "create" instance "db", instead. @@ -368,16 +359,16 @@ DSLContext create = DSL.using(connection, SQLDialect.ORACLE);]]>
jOOQ query examples run against the sample database. See the manual's section about
For the examples in this manual, the same database will always be referred to. It essentially consists of these entities created using the Oracle dialect
-More entities, types (e.g. UDT's, ARRAY types, ENUM types, etc), stored procedures and packages are introduced for specific examples
-jOOQ has originally been created as a library for complete abstraction of JDBC and all database interaction. Various best practices that are frequently encountered in pre-existing software products are applied to this library. This includes:
@@ -453,23 +444,23 @@ CREATE TABLE book_to_book_store (The following sections explain about various use cases for using jOOQ in your application.
-
This is the most simple of all use cases, allowing for construction of valid SQL for any database. In this use case, you will not use
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -481,23 +472,23 @@ String sql = create.select(fieldByName("BOOK","TITLE"), fieldByName("AUTHOR","FI
In addition to using jOOQ as a
The SQL string that you can generate as such can then be executed using JDBC directly, using Spring's JdbcTemplate, using Apache DbUtils and many other tools. @@ -509,30 +500,30 @@ String sql = create.select(BOOK.TITLE, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Instead of any tool mentioned in the previous chapters, you can also use jOOQ directly to execute your jOOQ-generated SQL statements. This will add a lot of convenience on top of the previously discussed API for typesafe SQL construction, when you can re-use the information from generated classes to fetch records and custom data types. An example is given here:
-jOOQ doesn't stop here, though! You can execute any SQL with jOOQ. In other words, you can use any other SQL building tool and run the SQL statements with jOOQ. An example is given here:
-
If you wish to use jOOQ as a SQL executor with (or without) code generation, the following sections of the manual will be of interest to you:
@@ -552,17 +543,17 @@ Result
This is probably the most complete use-case for jOOQ: Use all of jOOQ's features. Apart from jOOQ's fluent API for query construction, jOOQ can also help you execute everyday CRUD operations. An example is given here:
-If you wish to use all of jOOQ's features, the following sections of the manual will be of interest to you (including all sub-sections): @@ -586,12 +577,12 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
jOOQ isn't just a library that helps you
If you're a power user of your favourite, feature-rich database, jOOQ will help you access all of your database's vendor-specific features, such as OLAP features, stored procedures, user-defined types, vendor-specific SQL, functions, etc. Examples are given throughout this manual.
-Don't have time to read the full manual? Here are a couple of tutorials that will get you into the most essential parts of jOOQ as quick as possible.
-This manual section is intended for new users, to help them get a running application with jOOQ, quickly.
-
If you haven't already downloaded it, download jOOQ:
https://sourceforge.net/projects/jooq/files/Release/
@@ -640,7 +631,7 @@ for (AuthorRecord author : create.fetch(AUTHOR)) {
Alternatively, you can create a Maven dependency to download jOOQ artefacts:
-
Please refer to the manual's section about
If you don't have a MySQL instance up and running yet, get XAMPP now! XAMPP is a simple installation bundle for Apache, MySQL, PHP and Perl
-We're going to create a database called "guestbook" and a corresponding "posts" table. Connect to MySQL via your command line client and type the following:
-
In this step, we're going to use jOOQ's command line tools to generate classes that map to the Posts table we just created. More detailed information about how to set up the jOOQ code generator can be found here:
Replace the username with whatever user has the appropriate privileges to query the database meta data. You'll also want to look at the other values and replace as necessary. Here are the two interesting properties:
@@ -761,15 +751,15 @@ CREATE TABLE `posts` ( Once you have the JAR files and guestbook.xml in your temp directory, type this (use colons instead of semi-colons on UNIX/Linux systems): -Note the prefix slash before guestbook.xml. Even though it's in our working directory, we need to prepend a slash, as the configuration file is loaded from the classpath. Replace the filenames with your filenames. In this example, jOOQ {jooq-version} is being used. If everything has worked, you should see this in your console output:
-Let's just write a vanilla main class in the project containing the generated classes:
-This is pretty standard code for establishing a MySQL connection.
-Let's add a simple query:
-
First get an instance of
We then use jOOQ's DSL to return an instance of Result. We'll be using this result in the next step.
DSLContext so we can write a simple SELECT query. We pass an instance of the MySQL connection to DSL. Note that the DSLContext doesn't close the connection. We'll have to do that ourselves.
@@ -888,29 +876,29 @@ Result
After the line where we retrieve the results, let's iterate over the results and print out the data:
-The full program should now look like this:
-
jOOQ has grown to be a comprehensive SQL library. For more information, please consider the documentation:
http://www.jooq.org/learn.php
@@ -983,21 +970,21 @@ public class Main {
This tutorial is the courtesy of Ikai Lan. See the original source here:
http://ikaisays.com/2011/11/01/getting-started-with-jooq-a-tutorial/
Feel free to contribute a tutorial!
-jOOQ and Spring are easy to integrate. In this example, we shall integrate:
@@ -1016,7 +1003,7 @@ public class Main { For this example, we'll create the following Maven dependencies -The above dependencies are configured together using a Spring Beans configuration:
-
The following simple program shows how you can now easily obtain a
The above example shows how Spring's TransactionAwareDataSourceProxy discovers that a jOOQ query is not executed in a transactional context. @@ -1149,7 +1136,7 @@ public class Test { The following simple program shows how Spring's TransactionAwareDataSourceProxy will discover that several jOOQ queries are executed in the context of an explicitly created transaction
-Of course, in an actual productive setup, you are more likely to use Spring's AOP features to declare transactions and transactional behaviour on service methods, instead of explicitly starting and committing / rollbacking transactions. There are many other transaction models that you can choose to use with jOOQ.
-Feel free to contribute a tutorial!
-As any other library, jOOQ can be easily used in Scala, taking advantage of the many Scala language features such as for example:
@@ -1221,7 +1208,7 @@ public class Test { A short example jOOQ application in Scala might look like this: -
For more details about jOOQ's Scala integration, please refer to the manual's section about
Dependencies are a big hassle in modern software. Many libraries depend on other, non-JDK library parts that come in different, incompatible versions, potentially causing trouble in your runtime environment. jOOQ has no external dependencies on any third-party libraries.
@@ -1282,12 +1269,12 @@ object Test { //In order to build jOOQ yourself, please download the sources from https://github.com/jOOQ/jOOQ and use Maven to build jOOQ, preferably in Eclipse. jOOQ requires Java 6+ to compile and run.
@@ -1311,12 +1298,12 @@ object Test { // -jOOQ follows the rules of semantic versioning according to http://semver.org quite strictly. Those rules impose a versioning scheme [X].[Y].[Z] that can be summarised as follows:
@@ -1344,14 +1331,14 @@ object Test { //jOOQ's DSL interfaces should not be implemented by client code! Extend only those extension points that are explicitly documented as "extendable" (e.g.
SQL is a declarative language that is hard to integrate into procedural, object-oriented, functional or any other type of programming languages. jOOQ's philosophy is to give SQL the credit it deserves and integrate SQL itself as an "internal domain specific language" directly into Java.
@@ -1361,7 +1348,7 @@ object Test { //
This section explains all about the various syntax elements involved with jOOQ's SQL building capabilities. For a complete overview of all syntax elements, please refer to the manual's sections about
jOOQ exposes a lot of interfaces and hides most implementation facts from client code. The reasons for this are:
@@ -1388,21 +1375,20 @@ object Test { // With jOOQ 2.0, static factory methods have been introduced in order to make client code look more like SQL. Ideally, when working with jOOQ, you will simply static import all methods from the DSL class: -Note, that when working with Eclipse, you could also add the DSL to your favourites. This will allow to access functions even more fluently:
-There are a couple of subclasses for the general DSL. Each SQL dialect has its own dialect-specific DSL. For instance, if you're only using the MySQL dialect, you can choose to reference the MySQLDSL instead of the standard DSL:
@@ -1413,7 +1399,7 @@ DSL.concat(DSL.trim(FIRST_NAME), DSL.trim(LAST_NAME));
DSLContext references a
If you do not have a reference to a pre-existing Configuration object (e.g. created from DSL.using() methods will create one for you.
@@ -1471,32 +1457,31 @@ DSLContext create = DSL.using(connection, dialect);]]>
Wrapping a Configuration object, a DSLContext can construct
Note that you do not need to keep a reference to a DSLContext. You may as well inline your local variable, and fluently execute a SQL statement as such:
-
While jOOQ tries to represent the SQL standard as much as possible, many features are vendor-specific to a given database and to its "SQL dialect". jOOQ models this using the
Some parts of the jOOQ API are officially supported only by a given subset of the supported SQL dialects. For instance, the
jOOQ API methods which are not annotated with the
The aforementioned Support annotation does not only designate, which databases natively support a feature. It also indicates that a feature is simulated by jOOQ for some databases lacking this feature. An example of this is the
Nevertheless, the
jOOQ has a historic affinity to Oracle's SQL extensions. If something is supported in Oracle SQL, it has a high probability of making it into the jOOQ API
IS DISTINCT FROM predicate is supported by jOOQ in all dialects, as its semantics can be expressed with an equivalent
In jOOQ 3.1, the notion of a SQLDialect.family() was introduced, in order to group several similar
In the above list, SQLSERVER is both a dialect and a family of three dialects. This distinction is used internally by jOOQ to distinguish whether to use the ROW_NUMBER() OVER() (SQL Server 2008).
While you can use jOOQ for
Note that acquire() should always return the same Connection until this connection is returned via release()
In advanced use cases of integrating your application with jOOQ, you may want to put custom data into your
INSERT statements, when a given flag is set to true:
-
See the manual's section about ExecuteListener.
Configuration, in order for the listener to work:
-
Using the data() methods, you can store and retrieve custom data in your Configurations.
ExecuteListeners are a useful tool to...
See the manual's section about
The jOOQ Configuration allows for some optional configuration elements to be used by advanced users. The
For example, if you want to indicate to jOOQ, that it should inline all bind variables, and execute static
Subsequent sections of the manual contain some more in-depth explanations about these settings:
@@ -1750,12 +1735,12 @@ DSLContext create = DSL.using(connection, dialect, settings);]]> Please refer to the jOOQ runtime configuration XSD for more details:
You may wish to design your database in a way that you have several instances of your schema. This is useful when you want to cleanly separate data belonging to several customers / organisation units / branches / users and put each of those entities' data in a separate database or schema.
@@ -1775,7 +1760,7 @@ DSLContext create = DSL.using(connection, dialect, settings);]]>
When a user from My Book World logs in, you want them to access the MY_BOOK_WORLD schema using classes generated from DEV. This can be achieved with the
The query executed with a Configuration equipped with the above mapping will in fact produce this SQL statement:
-Even if AUTHOR was generated from DEV.
@@ -1801,7 +1786,7 @@ create.selectFrom(AUTHOR).fetch(); Your development database may not be restricted to hold only one DEV schema. You may also have a LOG schema and a MASTER schema. Let's say the MASTER schema is shared among all customers, but each customer has their own LOG schema instance. Then you can enhance your RenderMapping like this (e.g. using an XML configuration file): -Note, you can load the above XML file like this:
-This will map generated classes from DEV to MY_BOOK_WORLD, from LOG to MY_BOOK_WORLD_LOG, but leave the MASTER schema alone. Whenever you want to change your mapping configuration, you will have to create a new Configuration. @@ -1833,21 +1818,21 @@ create.selectFrom(AUTHOR).fetch();
-Not only schemata can be mapped, but also tables. If you are not the owner of the database your application connects to, you might need to install your schema with some sort of prefix to every table. In our examples, this might mean that you will have to map DEV.AUTHOR to something MY_BOOK_WORLD.MY_APP__AUTHOR, where MY_APP__ is a prefix applied to all of your tables. This can be achieved by creating the following mapping:
-The query executed with a Configuration equipped with the above mapping will in fact produce this SQL statement:
-Table mapping and schema mapping can be applied independently, by specifying several MappedSchema entries in the above configuration. jOOQ will process them in order of appearance and map at first match. Note that you can always omit a MappedSchema's output value, in case of which, only the table mapping is applied. If you omit a MappedSchema's input value, the table mapping is applied to all schemata! @@ -1876,23 +1861,23 @@ create.selectFrom(AUTHOR).fetch();
Note that the manual's section about
jOOQ currently supports 6 types of SQL statements. All of these statements are constructed from a DSLContext instance with an optional
jOOQ ships with its own DSL (or Domain Specific Language) that simulates SQL in Java. This means, that you can write SQL statements almost as if Java natively supported it, just like .NET's C# does with LINQ to SQL.
@@ -1900,22 +1885,21 @@ create.selectFrom(AUTHOR).fetch(); Here is an example to illustrate what that means: -
We'll see how the aliasing works later in the section about
Unlike other, simpler frameworks that use "fluent APIs" or "method chaining", jOOQ's BNF-based interface hierarchy will not allow bad query syntax. The following will not compile, for instance:
-
Historically, jOOQ started out as an object-oriented SQL builder library like any other. This meant that all queries and their syntactic components were modeled as so-called
This query is equivalent to the one shown before using the DSL syntax. In fact, internally, the DSL API constructs precisely this SelectQuery object. Note, that you can always access the SelectQuery object to switch between DSL and model APIs:
-
Note, that for historic reasons, the DSL API mixes mutable and immutable behaviour with respect to the internal representation of the
On the other hand, beware that you can always extract and modify QueryPart.
When you don't just perform
Details about the various clauses of this query will be provided in subsequent sections.
@@ -2080,14 +2063,14 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
A very similar, but limited API is available, if you want to select from single physical tables in order to retrieve SELECT statement with the DSL or DSLContext types:
As you can see, there is no way to further restrict/project the selected fields. This just selects all known TableFields in the supplied Table, and it also binds <R extends Record> to your Table's associated Record. An example of such a Query would then be:
-The "reduced" SELECT API is limited in the way that it skips DSL access to any of these clauses: @@ -2099,41 +2082,39 @@ create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
In most parts of this manual, it is assumed that you do not use the "reduced" SELECT API. For more information about the simple SELECT API, see the manual's section about
The SELECT clause lets you project your own record types, referencing table fields, functions, arithmetic expressions, etc. The DSL type provides several methods for expressing a SELECT clause:
-Some commonly used projections can be easily created using convenience methods:
-
See more details about functions and expressions in the manual's section about
jOOQ does not explicitly support the asterisk operator in projections. However, you can omit the projection as in these examples:
-
Since jOOQ 3.0, SELECT (and SELECT DISTINCT) API in both DSL and DSLContext. An extract from the DSL type:
Since the generic R type is bound to some SELECT statement can be assigned typesafely:
For more information about typesafe record types with degree up to {max-row-degree}, see the manual's section about
The SQL FROM clause allows for specifying any number of
Read more about aliasing in the manual's section about
Note, in order to access the DbmsXplan package, you can use the
Read more about dual or dummy tables in the manual's section about
jOOQ supports many different types of standard SQL JOIN operations:
@@ -2268,7 +2245,7 @@ DSL.using(SQLDialect.POSTGRES).selectOne().getSQL();]]> All of these JOIN methods can be called on
The two syntaxes will produce the same SQL statement. However, calling "join" on
In case of ambiguity, you can also supply field references for your foreign keys, or the generated foreign key reference to the onKey() method. @@ -2333,137 +2308,130 @@ JOIN BOOK ON BOOK.AUTHOR_ID = AUTHOR.ID]]> Most often, you will provide jOOQ with JOIN conditions in the JOIN .. ON clause. SQL supports a different means of specifying how two tables are to be joined. This is the JOIN .. USING clause. Instead of a condition, you supply a set of fields whose names are common to both tables to the left and right of a JOIN operation. This can be useful when your database schema has a high degree of relational normalisation. An example:
-In schemas with high degrees of normalisation, you may also choose to use NATURAL JOIN, which takes no JOIN arguments as it joins using all fields that are common to the table expressions to the left and to the right of the JOIN operator. An example:
-Oracle SQL ships with a special syntax available for OUTER JOIN clauses. According to the Oracle documentation about partitioned outer joins this can be used to fill gaps for simplified analytical calculations. jOOQ only supports putting the PARTITION BY clause to the right of the OUTER JOIN clause. The following example will create at least one record per AUTHOR and per existing value in BOOK.PUBLISHED_IN, regardless if an AUTHOR has actually published a book in that year.
-
The WHERE clause can be used for JOIN or filter predicates, in order to restrict the data returned by the
The above syntax is convenience provided by jOOQ, allowing you to connect the
You will find more information about creating
The Oracle database knows a very succinct syntax for creating hierarchical queries: the CONNECT BY clause, which is fully supported by jOOQ, including all related functions and pseudo-columns. A more or less formal definition of this clause is given here:
-An example for an iterative query, iterating through values between 1 and 5 is this:
-Here's a more complex example where you can recursively fetch directories in your database, and concatenate them to a path:
-The output might then look like this
-
Some of the supported functions and pseudo-columns are these (available from the
GROUP BY can be used to create unique groups of data, to form aggregations, to remove duplicates and for other reasons. It will transform your previously defined
The above example counts all books per author. @@ -2542,13 +2508,12 @@ GROUP BY AUTHOR_ID]]> MySQL has a peculiar way of not adhering to this standard behaviour. This is documented in the MySQL manual. In short, with MySQL, you can also project any other field that is not part of the GROUP BY clause. The projected values will just be arbitrary values from within the group. You cannot rely on any ordering. For example:
-
This will return an arbitrary title per author. jOOQ supports this syntax, as jOOQ is not doing any checks internally, about the consistence of tables/fields/functions that you provide it.
@@ -2559,68 +2524,64 @@ GROUP BY AUTHOR_ID]]>
jOOQ supports empty GROUP BY () clause as well. This will result in
Some databases support the SQL standard grouping functions and some extensions thereof. See the manual's section about
The HAVING clause is commonly used to further restrict data resulting from a previously issued
According to the SQL standard, you may omit the GROUP BY clause and still issue a HAVING clause. This will implicitly GROUP BY (). jOOQ also supports this syntax. The following example selects one record, only if there are at least 4 books in the books table:
-Databases are allowed to return data in any arbitrary order, unless you explicitly declare that order in the ORDER BY clause. In jOOQ, this is straight-forward:
-
Any jOOQ
Note, how one() is used as a convenience short-cut for inline(1)
@@ -2648,87 +2608,84 @@ ORDER BY 1 ASC, 2 DESC]]>
A few databases support the SQL standard "null ordering" clause in sort specification lists, to define whether NULL values should come first or last in an ordered result.
If your database doesn't support this syntax, jOOQ simulates it using a
Using
But writing these things can become quite verbose. jOOQ supports a convenient syntax for specifying sort mappings. The same query can be written in jOOQ as such:
-More complex sort indirections can be provided using a Map:
-Of course, you can combine this feature with the previously discussed NULLS FIRST / NULLS LAST feature. So, if in fact these two books are the ones you like least, you can put all NULLS FIRST (all the other books):
-@@ -2740,23 +2697,23 @@ ORDER BY CASE TITLE
jOOQ also supports Oracle's SIBLINGS keyword to be used with ORDER BY clauses for
While being extremely useful for every application that does paging, or just to limit result sets to reasonable sizes, this clause is not yet part of any SQL standard (up until SQL:2008). Hence, there exist a variety of possible implementations in various SQL dialects, concerning this limit clause. jOOQ chose to implement the LIMIT .. OFFSET clause as understood and supported by MySQL, H2, HSQLDB, Postgres, and SQLite. Here is an example of how to apply limits with jOOQ:
-This will limit the result to 1 books starting with the 2nd book (starting at offset 0!). limit() is supported in all dialects, offset() in all but Sybase ASE, which has no reasonable means to simulate it. This is how jOOQ simulates the above query in various SQL dialects:
-As you can see, jOOQ will take care of the incredibly painful ROW_NUMBER() OVER() (or ROWNUM for Oracle) filtering in subselects for you, you'll just have to write limit(1).offset(2) in any dialect. @@ -2801,39 +2758,37 @@ AND ROWNUM_98843777 <= 3
As can be seen in the above example, writing correct SQL can be quite tricky, depending on the SQL dialect. For instance, with SQL Server, you cannot have an ORDER BY clause in a subquery, unless you also have a TOP clause. This is illustrated by the fact that jOOQ renders a TOP 100 PERCENT clause for you. The same applies to the fact that ROW_NUMBER() OVER() needs an ORDER BY windowing clause, even if you don't provide one to the jOOQ query. By default, jOOQ adds ordering by the first column of your projection.
-For inter-process synchronisation and other reasons, you may choose to use the SELECT .. FOR UPDATE clause to indicate to the database, that a set of cells or records should be locked by a given transaction for subsequent updates. With jOOQ, this can be achieved as such:
-The above example will produce a record-lock, locking the whole record for updates. Some databases also support cell-locks using FOR UPDATE OF ..
-Oracle goes a bit further and also allows to specify the actual locking behaviour. It features these additional clauses, which are all supported by jOOQ: @@ -2847,15 +2802,15 @@ FOR UPDATE OF TITLE]]> With jOOQ, you can use those Oracle extensions as such:
-
The SQL standard specifies a FOR UPDATE clause to be applicable for cursors. Most databases interpret this as being applicable for all SELECT statements. An exception to this rule are the CUBRID and SQL Server databases, that do not allow for any FOR UPDATE clause in a regular SQL SELECT statement. jOOQ simulates the FOR UPDATE behaviour, by locking record by record with JDBC. JDBC allows for specifying the flags TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE for any statement, and then using ResultSet.updateXXX() methods to produce a cell-lock / row-lock. Here's a simplified example in JDBC:
The main drawback of this approach is the fact that the database has to maintain a scrollable cursor, whose records are locked one by one. This can cause a major risk of deadlocks or race conditions if the JDBC driver can recover from the unsuccessful locking, if two Java threads execute the following statements:
-So use this technique with care, possibly only ever locking single rows! @@ -2892,12 +2847,12 @@ SELECT * FROM author ORDER BY id DESC;]]>
Note, that jOOQ also supports optimistic locking, if you're doing simple CRUD. This is documented in the section's manual about
SQL allows to perform set operations as understood in standard set theory on result sets. These operations include unions, intersections, subtractions. For two subselects to be combinable by such a set operator, each subselect must return a
UNION removes all duplicate records resulting from this combination, UNION ALL leaves subselect results as they are. Typically, you should prefer UNION ALL over UNION, if you don't really need to remove duplicates. The following example shows how to use such a UNION operation in jOOQ.
-
@@ -2925,24 +2879,23 @@ create.selectFrom(BOOK).where(BOOK.ID.equal(5)));]]>
As previously mentioned in the manual's section about the
Two subselects that are combined by a set operator are required to be of the same degree and, in most databases, also of the same type. jOOQ 3.0's introduction of
If you are closely coupling your application to an Oracle (or CUBRID) database, you might need to be able to pass hints of the form /*+HINT*/ with your SQL statements to the Oracle database. For example:
This can be done in jOOQ using the .hint() clause in your SELECT statement:
Note that you can pass any string in the .hint() clause. If you use that clause, the passed string will always be put in between the SELECT [DISTINCT] keywords and the actual projection list. This can be useful in other databases too, such as MySQL, for instance:
SQL has a lexical and a logical order of SELECT clauses. The lexical order of SELECT clauses is inspired by the English language. As SQL statements are commands for the database, it is natural to express a statement in an imperative tense, such as "SELECT this and that!".
A LINQ example:
-A SLICK example:
-
While this looks like a good idea at first, it only complicates translation to more advanced SQL statements while impairing readability for those users that are used to writing SQL. jOOQ is designed to look just like SQL. This is specifically true for SLICK, which not only changed the SELECT clause order, but also heavily "integrated" SQL clauses with the Scala language.
@@ -3072,48 +3023,46 @@ val q = for {
For these reasons, the jOOQ DSL API is modelled in SQL's lexical order.
-
The INSERT statement is used to insert new records into a database table. Records can either be supplied using a VALUES() constructor, or a SELECT statement. jOOQ supports both types of INSERT statements. An example of an INSERT statement using a VALUES() constructor is given here:
Note that for explicit degrees up to {max-row-degree}, the VALUES() constructor provides additional typesafety. The following example illustrates this:
The SQL standard specifies that multiple rows can be supplied to the VALUES() constructor in an INSERT statement. Here's an example of a multi-record INSERT
-jOOQ tries to stay close to actual SQL. In detail, however, Java's expressiveness is limited. That's why the values() clause is repeated for every record in multi-record inserts. @@ -3122,30 +3071,29 @@ VALUES (100, 'Hermann', 'Hesse'), Some RDBMS do not support inserting several records in a single statement. In those cases, jOOQ simulates multi-record INSERTs using the following SQL:
-MySQL (and some other RDBMS) allow for using a non-SQL-standard, UPDATE-like syntax for INSERT statements. This is also supported in jOOQ, should you prefer that syntax. The above INSERT statement can also be expressed as follows:
-
As you can see, this syntax is a bit more verbose, but also more readable, as every field can be matched with its value. Internally, the two syntaxes are strictly equivalent.
@@ -3156,12 +3104,12 @@ SELECT 101, 'Alfred', 'Döblin' FROM DUAL;
The MySQL database supports a very convenient way to INSERT or UPDATE a record. This is a non-standard extension to the SQL syntax, which is supported by jOOQ and simulated in other RDBMS, where this is possible (i.e. if they support the SQL standard
The Postgres database has native support for an INSERT .. RETURNING clause. This is a very powerful concept that is simulated for all other dialects using JDBC's
Some databases have poor support for returning generated keys after INSERTs. In those cases, jOOQ might need to issue another
The UPDATE statement is used to modify one or several pre-existing records in a database table. UPDATE statements are only possible on single tables. Support for multi-table updates will be implemented in the near future. An example update query is given here:
-
Most databases allow for using scalar subselects in UPDATE statements in one way or another. jOOQ models this through a set(Field<T>, Select<? extends Record1<T>>) method in the UPDATE DSL API:
This can be particularly useful when using subselects:
-
The above row value expressions usages are completely typesafe.
@@ -3301,45 +3243,43 @@ create.insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
The Firebird and Postgres databases support a RETURNING clause on their UPDATE statements, similar as the RETURNING clause in
The UPDATE .. RETURNING clause is currently not simulated for other databases. Future versions might execute an additional
The DELETE statement physically removes records from a database table. DELETE statements are only possible on single tables. Support for multi-table deletes will be implemented in the near future. An example delete query is given here:
-The MERGE statement is one of the most advanced standardised SQL constructs, which is supported by DB2, HSQLDB, Oracle, SQL Server and Sybase (MySQL has the similar INSERT .. ON DUPLICATE KEY UPDATE construct)
@@ -3347,15 +3287,14 @@ RETURNING UPDATE_COUNT]]> The point of the standard MERGE statement is to take a TARGET table, and merge (INSERT, UPDATE) data from a SOURCE table into it. DB2, Oracle, SQL Server and Sybase also allow for DELETING some data and for adding many additional clauses. With jOOQ {jooq-version}, only Oracle's MERGE extensions are supported. Here is an example: -The H2 database ships with a somewhat less powerful but a little more intuitive syntax for its own version of the MERGE statement. An example more or less equivalent to the previous one can be seen here:
-
This syntax can be fully simulated by jOOQ for all other databases that support the SQL standard. For more information about the H2 MERGE syntax, see the documentation here:
@@ -3394,12 +3332,12 @@ VALUES ('John', 'Hitchcock')
Much like the MERGE statement's VALUES() clause provides typesafety for degrees up to {max-row-degree}, in both the standard syntax variant as well as the H2 variant.
The TRUNCATE statement is the only DDL statement supported by jOOQ so far. It is popular in many databases when you want to bypass constraints for table truncation. Databases may behave differently, when a truncated table is referenced by other tables. For instance, they may fail if records from a truncated table are referenced, even with ON DELETE CASCADE clauses in place. Please, consider your database manual to learn more about its TRUNCATE implementation.
@@ -3407,44 +3345,42 @@ VALUES ('John', 'Hitchcock') The TRUNCATE syntax is trivial: -TRUNCATE is not supported by Ingres and SQLite. jOOQ will execute a DELETE FROM AUTHOR statement instead.
-The following sections explain the various types of table expressions supported by jOOQ
-
Most of the times, when thinking about a
The above example shows how AUTHOR and BOOK tables are joined in a
See the manual's section about
The strength of jOOQ's
As you can see in the above example, calling as() on generated tables returns an object of the same type as the table. This means that the resulting object can be used to dereference fields from the aliased table. This is quite powerful in terms of having your Java compiler check the syntax of your SQL statements. If you remove a column from a table, dereferencing that column from that table alias will cause compilation errors.
@@ -3494,40 +3429,40 @@ create.select()
Only few table expressions provide the SQL syntax typesafety as shown above, where generated tables are used. Most tables, however, expose their fields through field() methods:
The SQL standard specifies how a table can be renamed / aliased in one go along with its columns. It references the term "derived column list" for the following syntax (as supported by Postgres, for instance):
-
This feature is useful in various use-cases where column names are not known in advance (but the table's degree is!). An example for this are
Only few databases really support such a syntax, but fortunately, jOOQ can simulate it easily using UNION ALL and an empty dummy record specifying the new column names. The two statements are equivalent:
In jOOQ, you would simply specify a varargs list of column aliases as such:
-
The
SQL and relational algebra distinguish between at least the following JOIN types (upper-case: SQL, lower-case: relational algebra):
@@ -3580,7 +3514,7 @@ create.select().from(values(
jOOQ supports all of these JOIN types (except semi-join and anti-join) directly on any
Note that most of jOOQ's JOIN operations give way to a similar DSL API hierarchy as previously seen in the manual's section about the
Some databases allow for expressing in-memory temporary tables using a VALUES() constructor. This constructor usually works the same way as the VALUES() clause known from the VALUES() table constructor, to create tables that can be used in a
Note, that it is usually quite useful to provide column aliases ("derived column lists") along with the table alias for the VALUES() constructor.
@@ -3635,7 +3568,7 @@ FROM VALUES(1, 'a'),
The above statement is simulated by jOOQ for those databases that do not support the VALUES() constructor, natively (actual simulations may vary):
A
If you are closely coupling your application to an Oracle database, you can take advantage of some Oracle-specific features, such as the PIVOT clause, used for statistical analyses. The formal syntax definition is as follows:
-
The PIVOT clause is available from the
There is one operation in relational algebra that is not given a lot of attention, because it is rarely used in real-world applications. It is the relational division, the opposite operation of the cross product (or, relational multiplication). The following is an approximate definition of a relational division:
-With jOOQ, you can simplify using relational divisions by using the following syntax:
-The above roughly translates to
-Or in plain text: Find those TEXT values in C whose ID's correspond to all ID's in B. Note that from the above SQL statement, it is immediately clear that proper indexing is of the essence. Be sure to have indexes on all columns referenced from the on(...) and returning(...) clauses. @@ -3792,13 +3721,13 @@ WHERE NOT EXISTS (
The SQL standard specifies how SQL databases should implement ARRAY and TABLE types, as well as CURSOR types. Put simply, a CURSOR is a pointer to any materialised
Note, in order to access the DbmsXplan package, you can use the
The SQL standard specifies that the
FROM clause, but renders the necessary "DUAL" table, if needed. The following program shows how jOOQ renders "DUAL" tables
-
Note, that some databases (H2, MySQL) can normally do without "DUAL". However, there exist some corner-cases with complex nested SELECT statements, where this will cause syntax errors (or parser bugs). To stay on the safe side, jOOQ will always render "dual" in those dialects.
Column expressions can be used in various SQL clauses in order to refer to one or several columns. This chapter explains how to form various types of column expressions with jOOQ. A particular type of column expression is given in the section about
In general, it is up to you whether you want to use the "prefix" notation or the "postfix" notation to create new column expressions based on existing ones. The "SQL way" would be to use the "prefix notation", with functions created from the
Table columns are the most simple implementations of a
Table columns implement a more specific interface called <R extends Record> record type.
@@ -3935,45 +3861,43 @@ ORDER BY BOOK.TITLE]]>
See the manual's section about
Just like
Here is how it's done with jOOQ:
-When you alias Fields like above, you can access those Fields' values using the alias name:
-
jOOQ's source code generator tries to find the most accurate type mapping between your vendor-specific data types and a matching Java type. For instance, most VARCHAR, CHAR, CLOB types will map to String. Most BINARY, BYTEA, BLOB types will map to byte[]. NUMERIC types will default to
CAST like this:
-in jOOQ, you can write something like that:
-
The same thing can be achieved by casting a Field directly to String.class, as TEXT is the default data type in Postgres to map to Java's String
The complete CAST API in
A slightly different use case than
In the above example,
This technique is better than performing unsafe or rawtype casting in Java, if you cannot access the "right" field type from any given expression.
field1 will be treated by jOOQ as a Field<String>, binding the numeric literal 1 as a VARCHAR value. The same applies to field2, whose string literal "1" will be bound as an INTEGER value.
@@ -4042,40 +3965,39 @@ Field
Your database can do the math for you. Arithmetic operations are implemented just like
In order to express a SQL query like this one:
-You can write something like this in jOOQ:
-jOOQ also supports the Oracle-style syntax for adding days to a Field<? extends java.util.Date>
-
For more advanced datetime arithmetic, use the DSL's timestampDiff() and dateDiff() functions, as well as jOOQ's built-in SQL standard INTERVAL data type support:
@@ -4084,31 +4006,30 @@ FieldINTERVAL YEAR TO MONTH: INTERVAL DAY TO SECOND:
The SQL standard defines the concatenation operator to be an infix operator, similar to the ones we've seen in the chapter about ||. Some other dialects do not support this operator, but expect a concat() function, instead. jOOQ renders the right operator / function, depending on your
There are a variety of general functions supported by jOOQ As discussed in the chapter about
Please refer to the
Math can be done efficiently in the database before returning results to your Java application. In addition to the
Please refer to the
Interestingly, bitwise functions and bitwise arithmetic is not very popular among SQL databases. Most databases only support a few bitwise operations, while others ship with the full set of operators. jOOQ's API includes most bitwise operations as listed below. In order to avoid ambiguities with
String formatting can be done efficiently in the database before returning results to your Java application. As discussed in the chapter about
REGEX_LIKE operator. Being an operator (and not a function), you should use the corresponding method on
Note that the SQL standard specifies that patterns should follow the XQuery standards. In the real world, the POSIX regular expression standard is the most used one, some use Java regular expressions, and only a few ones use Perl regular expressions. jOOQ does not make any assumptions about regular expression syntax. For cross-database compatibility, please read the relevant database manuals carefully, to learn about the appropriate syntax. Please refer to the
This is a list of date and time functions supported by jOOQ's
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. See the manual's section about
This is a list of system functions supported by jOOQ's
CURRENT_USER: Get current user.
Aggregate functions work just like functions, even if they have a slightly different semantics. Here are some example aggregate functions from the
Here's an example, counting the number of books any author has written:
-
Aggregate functions have strong limitations about when they may be used and when not. For instance, you can use aggregate functions in scalar queries. Typically, this means you only select aggregate functions, no
Oracle and some other databases support "ordered aggregate functions". This means you can provide an ORDER BY clause to an aggregate function, which will be taken into consideration when aggregating. The best example for this is Oracle's LISTAGG() (also known as GROUP_CONCAT in other
The above query might yield:
-
Oracle allows for restricting aggregate functions using the KEEP() clause, which is supported by jOOQ. In Oracle, some aggregate functions (MIN, MAX, SUM, AVG, COUNT, VARIANCE, or STDDEV) can be restricted by this clause, hence
@@ -4398,12 +4316,12 @@ GROUP BY AUTHOR_ID
In those databases that support over() on it. See the manual's section about
Most major RDBMS support the concept of window functions. jOOQ knows of implementations in DB2, Oracle, Postgres, SQL Server, and Sybase SQL Anywhere, and supports most of their specific syntaxes. Note, that H2 and HSQLDB have implemented ROW_NUMBER() functions, without true windowing support.
over() method. See the chapter about
SQL distinguishes between various window function types (e.g. "ranking functions"). Depending on the function, SQL expects mandatory PARTITION BY or ORDER BY clauses within the OVER() clause. jOOQ does not enforce those rules for two reasons:
@@ -4448,7 +4366,7 @@ GROUP BY AUTHOR_ID
Here are some simple examples of window functions with jOOQ:
Window functions can be used for things like calculating a "running total". The following example fetches transactions and the running total for every transaction going back to the beginning of the transaction table (ordered by booked_at). Window functions are accessible from the previously seen over() method:
LISTAGG(). These functions have a window function / analytical function variant, as well. For example:
-FIRST and LAST aggregate functions
In the previous chapter about FIRST and LAST aggregate functions". These functions have a window function / analytical function variant, as well. For example:
User-defined aggregate functions also implement over(). This is supported by Oracle in particular. See the manual's section about
The SQL standard defines special functions that can be used in the ROLLUP() for instance:
In English, the ROLLUP() grouping function provides N+1 groupings, when N is the number of arguments to the ROLLUP() function. Each grouping has an additional group field from the ROLLUP() argument field list. The results of the second query might look something like this:
CUBE() is different from ROLLUP() in the way that it doesn't just create N+1 groupings, it creates all 2^N possible combinations between all group fields in the CUBE() function argument list. Let's re-consider our second query from before:
The results would then hold:
-
@@ -4669,7 +4581,7 @@ ORDER BY 1 NULLS FIRST, 2 NULLS FIRST
jOOQ fully supports all of these functions, as well as the utility functions GROUPING() and GROUPING_ID(), used for identifying the grouping set ID of a record. The
MySQL and CUBRID don't know any grouping functions, but they support a WITH ROLLUP clause, that is equivalent to simple ROLLUP() grouping functions. jOOQ simulates ROLLUP() in MySQL and CUBRID, by rendering this WITH ROLLUP clause. The following two statements mean the same:
Some databases support user-defined functions, which can be embedded in any SQL statement, if you're using jOOQ's
The above function will be made available from a generated
Note that user-defined functions returning
Some databases support user-defined aggregate functions, which can then be used along with OBJECT type (the example was taken from the Oracle 11g documentation):
The above OBJECT type is then available to function declarations as such:
jOOQ's SECOND_MAX() aggregate function with jOOQ:
The CASE expression is part of the standard SQL syntax. While some RDBMS also offer an IF expression, or a DECODE function, you can always rely on the two types of CASE syntax:
In jOOQ, both syntaxes are supported (The second one is simulated in Derby, which only knows the first one). Unfortunately, both case and else are reserved words in Java. jOOQ chose to use decode() from the Oracle DECODE function, and otherwise(), which means the same as else.
@@ -4854,15 +4762,15 @@ create.decode().value(AUTHOR.FIRST_NAME)
A CASE expression can be used anywhere where you can place a SELECT the above expression, if you're selecting from AUTHOR:
Oracle knows a more succinct, but maybe less readable DECODE() function with a variable number of arguments. This function roughly does the same as the second case expression syntax. jOOQ supports the DECODE() function and simulates it using CASE expressions in all dialects other than Oracle:
Sort indirection is often implemented with a CASE clause of a SELECT's ORDER BY clause. See the manual's section about the
Sequences implement the
So if you have a sequence like this in Oracle:
-
You can then use your
According to the SQL standard, row value expressions can have a degree of more than one. This is commonly used in the VALUES row value constructor allows for providing a row value expression as a source for INSERT data. Row value expressions can appear in various other places, though. They are supported by jOOQ as records / rows. jOOQ's
@@ -4973,14 +4880,14 @@ public static RowN row(Object... values) { ... }]]>
jOOQ chose to explicitly support degrees up to {max-row-degree} to match Scala's typesafe tuple, function and product support. Unlike Scala, however, jOOQ also supports higher degrees without the additional typesafety.
-Conditions or conditional expressions are widely used in SQL and in the jOOQ API. They can be used in
@@ -5017,22 +4924,21 @@ public static RowN row(Object... values) { ... }]]>
Note that jOOQ does not model these values as actual
With jOOQ, most
@@ -5048,48 +4954,47 @@ BOOK.TITLE.notEqual("Animal Farm")]]>
Conditions can also be connected using
In SQL, as in most other languages, AND and OR binary operators, as well as the NOT unary operator, to form new conditional expressions. In jOOQ, this is modelled as such:
The above example shows that the number of parentheses in Java can quickly explode. Proper indentation may become crucial in making such code readable. In order to understand how jOOQ composes combined conditional expressions, let's assign component expressions first:
-
Here are all boolean operators on the
In SQL, comparison predicates are formed using common comparison operators:
@@ -5128,7 +5032,7 @@ not() // Invert a condition (synonym for DSL.not(Condition)] Unfortunately, Java does not support operator overloading, hence these operators are also implemented as methods in jOOQ, like any other SQL syntax elements. The relevant parts of the
Note that every operator is represented by two methods. A verbose one (such as equal()) and a two-character one (such as eq()). Both methods are the same. You may choose either one, depending on your taste. The manual will always use the more verbose one.
@@ -5156,25 +5060,24 @@ ge or greaterOrEqual(Select extends Record1
All variants of the
jOOQ supports all of the above row value expression comparison predicates, both with
If the right-hand side of a ALL, ANY, or SOME. Note that the SQL standard defines ANY and SOME to be equivalent. jOOQ settled for the more intuitive ANY and doesn't support SOME. Here are some examples, supported by jOOQ:
For the example, the right-hand side of the quantified comparison predicates were filled with argument lists. But it is easy to imagine that the source of values results from a ANY-quantified predicate. The following two expressions are equivalent:
Typically, the
In SQL, you cannot compare NULL with any value using NULL again, which is neither TRUE nor FALSE (see also the manual's section about NULL, use the NULL predicate as such:
The SQL NULL predicate also works well for NOT() operator! Here are some examples:
The SQL standard contains a nice truth table for the above rules:
-
In jOOQ, you would simply use the isNull() and isNotNull() methods on row value expressions. Again, as with the NULL predicate is simulated by jOOQ, if your database does not natively support it:
Some databases support the DISTINCT predicate, which serves as a convenient, NULL-safe DISTINCT predicate, the following truth table can be assumed:
NULL, which would lead to funny results. This is supported by jOOQ as such:
-
If your database does not natively support the DISTINCT predicate, jOOQ simulates it with an equivalent
The BETWEEN predicate can be seen as syntactic sugar for a pair of
Note the inclusiveness of range boundaries in the definition of the BETWEEN predicate. Intuitively, this is supported in jOOQ as such:
The SQL standard defines the SYMMETRIC keyword to be used along with BETWEEN to indicate that you do not care which bound of the range is larger than the other. A database system should simply swap range bounds, in case the first bound is greater than the second one. jOOQ supports this keyword as well, simulating it if necessary.
The simulation is done trivially:
-
The SQL BETWEEN predicate also works well for
The above can be factored out according to the rules listed in the manual's section about BETWEEN [SYMMETRIC] predicate and simulates it in all SQL dialects where necessary. An example is given here:
LIKE predicates are popular for simple wildcard-enabled pattern matching. Supported wildcards in all SQL databases are:
LIKE predicate can be created from any
Often, your pattern may contain any of the wildcard characters "_" and "%", in case of which you may want to escape them. jOOQ does not automatically escape patterns in like() and notLike() methods. Instead, you can explicitly define an escape character as such:
In the above predicate expressions, the exclamation mark character is passed as the escape character to escape wildcard characters "!_" and "!%", as well as to escape the escape character itself: "!!"
@@ -5514,7 +5398,7 @@ BOOK.TITLE.notLike("%The !%-Sign Book%", '!')]]>
In addition to the above, jOOQ provides a few convenience methods for common operations performed on strings using the LIKE predicate. Typical operations are "contains predicates", "starts with predicates", "ends with predicates", etc. Here is the full convenience API wrapping LIKE predicates:
Note, that jOOQ escapes % and _ characters in value in some of the above predicate implementations. For simplicity, this has been omitted in this manual.
In SQL, apart from comparing a value against several values, the IN predicate can be used to create semi-joins or anti-joins. jOOQ knows the following methods on the IN predicates:
A sample IN predicate might look like this:
NULL values in the right hand side of a NOT IN predicate, as the whole expression would evaluate to NULL, which is rarely desired. This can be shown informally using the following reasoning:
-
A good way to prevent this from happening is to use the NULL-value insensitive. See the manual's section about
The SQL IN predicate also works well for
jOOQ supports the IN predicate. Simulation of the IN predicate where row value expressions aren't well supported is currently only available for IN predicates that do not take a subselect as an IN predicate value. An example is given here:
Slightly less intuitive, yet more powerful than the previously discussed EXISTS predicate, that can be used to form semi-joins or anti-joins. With jOOQ, the EXISTS predicate can be formed in various ways:
EXISTS predicate can be seen here:
-
Note that in SQL, the projection of a subselect in an EXISTS predicate is irrelevant. To help you write queries like the above, you can use jOOQ's selectZero() or selectOne()
http://blog.jooq.org/2012/07/27/not-in-vs-not-exists-vs-left-join-is-null-mysql/
When comparing dates, the SQL standard allows for using a special OVERLAPS predicate, which checks whether two date ranges overlap each other. The following can be said:
jOOQ supports the OVERLAPS predicate on
This allows for expressing the above predicates as such:
-
Unlike the standard (or any database implementing the standard), jOOQ also supports the OVERLAPS predicate for comparing arbitrary (1, 3) OVERLAPS (2, 4) will yield true in jOOQ. This is simulated as such
A DSL is a nice thing to have, it feels "fluent" and "natural", especially if it models a well-known language, such as SQL. But a DSL is always expressed in a host language (Java in this case), which was not made for exactly the same purposes as its hosted DSL. If it were, then jOOQ would be implemented on a compiler-level, similar to LINQ in .NET. But it's not, and so, the DSL is limited by language constraints of its host language. We have seen many functionalities where the DSL becomes a bit verbose. This can be especially true for:
@@ -5727,7 +5605,7 @@ row(Date.valueOf('2010-01-01'), new DayToSecond(2)).overlaps(Date.valueOf('2010-
Plain SQL API methods are usually overloaded in three ways. Let's look at the condition query part constructor:
Please refer to the
Apart from the general factory methods, plain SQL is also available in various other contexts. For instance, when adding a .where("a = b") clause to a query. Hence, there exist several convenience methods where plain SQL can be inserted usefully. This is an example displaying all various use-cases in one single query:
@@ -5835,12 +5713,12 @@ create.select(LAST_NAME, COUNT1, COUNT2)
Bind values are used in SQL / JDBC for various reasons. Among the most obvious ones are:
@@ -5859,31 +5737,31 @@ create.select(LAST_NAME, COUNT1, COUNT2)The following sections explain how you can introduce bind values in jOOQ, and how you can control the way they are rendered and bound to SQL.
-JDBC only knows indexed bind values. A typical example for using bind values with JDBC is this:
-With dynamic SQL, keeping track of the number of question marks and their corresponding index may turn out to be hard. jOOQ abstracts this and lets you provide the bind value right where it is needed. A trivial example is this:
-
Note the using of
You can also extract specific bind values by index from a query, if you wish to modify their underlying value after creating a query. This can be achieved as such:
-
For more details about jOOQ's internals, see the manual's section about
Some SQL access abstractions that are built on top of JDBC, or some that bypass JDBC may support named parameters. jOOQ allows you to give names to your parameters as well, although those names are not rendered to SQL strings by default. Here is an example of how to create named parameters using the
The
In order to actually render named parameter names in generated SQL, use the
Sometimes, you may wish to avoid rendering bind variables while still using custom values in SQL. jOOQ refers to that as "inlined" bind values. When bind values are inlined, they render the actual value in SQL rather than a JDBC question mark. Bind value inlining can be achieved in two ways:
@@ -5990,7 +5867,7 @@ WHERE LAST_NAME = :lastName]]> In both cases, your inlined bind values will be properly escaped to avoid SQL syntax errors and SQL injection. Some examples: -
Special care needs to be taken when using
All methods in the jOOQ API that allow for plain (unescaped, untreated) SQL contain a warning message in their relevant Javadoc, to remind you of the risk of SQL injection in what is otherwise a SQL-injection-safe API.
-
A
Every
The following additional methods are inherited from a common
A simple example can be provided by checking out jOOQ's internal representation of a (simplified) AUTHOR.ID = BOOK.AUTHOR_ID condition here:
This is how jOOQ renders such a condition (simplified example):
-
See the manual's sections about
As mentioned in the previous chapter about
And then, use the above DSLContext to render pretty-printed SQL:
-
The section about
Every
Some additional methods are inherited from a common AUTHOR.ID = BOOK.AUTHOR_ID condition here:
This is how jOOQ binds values on such a condition:
-
See the manual's sections about
If a SQL clause is too complex to express with jOOQ, you can extend either one of the following types for use directly in a jOOQ query:
-
These classes are declared public and covered by jOOQ's integration tests. When you extend these classes, you will have to provide your own implementations for the
The above contract may be a bit tricky to understand at first. The best thing is to check out jOOQ source code and have a look at a couple of QueryParts, to see how it's done. Here's an example
If you don't need the integration of rather complex QueryParts into jOOQ, then you might be safer using simple
The above technique allows for creating rather complex SQL clauses that are currently not supported by jOOQ, without extending any of the
The only transient, non-serializable element in any jOOQ object is the
Another way of attaching QueryParts automatically, or rather providing them with a new
jOOQ-Scala is a maven module used for leveraging some advanced Scala features for those users that wish to use jOOQ with Scala.
@@ -6394,7 +6268,7 @@ create.attach(select);]]> The following depicts a trait which wraps all fields: -The following depicts a trait which wraps numeric fields:
-An example query using such overloaded operators would then look like this:
-This feature is still being experimented with. With Scala Macros, it might be possible to inline a true SQL dialect into the Scala syntax, backed by the jOOQ API. Stay tuned!
-
In a previous section of the manual, we've seen how jOOQ can be used to
The following sections of this manual will show how jOOQ is wrapping JDBC for SQL execution
-
Even if there are
Unlike JDBC, jOOQ has a lot of knowledge about a SQL query's structure and internals (see the manual's section about
Fetching is something that has been completely neglegted by JDBC and also by various other database abstraction libraries. Fetching is much more than just looping or listing records or mapped objects. There are so many ways you may want to fetch data from a database, it should be considered a first-class feature of any database abstraction API. Just to name a few, here are some of jOOQ's fetching modes:
@@ -6616,7 +6489,7 @@ Result
These means of fetching are also available from
These means of fetching are also available from
Note, that apart from the
jOOQ understands that SQL is much more expressive than Java, when it comes to the declarative typing of
When you use the
jOOQ's
jOOQ chose to explicitly support degrees up to {max-row-degree} to match Scala's typesafe tuple, function and product support. Unlike Scala, however, jOOQ also supports higher degrees without the additional typesafety.
-
By default, jOOQ returns an
Note that most of these convenience methods are available both through
In a more functional operating mode, you might want to write callbacks that receive records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
See also the manual's section about the
In a more functional operating mode, you might want to write callbacks that map records from your select statement results in order to do some processing. This is a common data access pattern in Spring's JdbcTemplate, and it is also available in jOOQ. With jOOQ, you can implement your own
Your custom RecordMapper types can be used automatically through jOOQ's
See also the manual's section about the
Fetching data in records is fine as long as your application is not really layered, or as long as you're still writing code in the DAO layer. But if you have a more advanced application architecture, you may not want to allow for jOOQ artefacts to leak into other layers. You may choose to write POJOs (Plain Old Java Objects) as your primary DTOs (Data Transfer Objects), without any dependencies on jOOQ's
Just as with any other JPA implementation, you can put the
Please refer to the
Please refer to the
Please refer to the
Note: Because of your manual setting of ID = 10, jOOQ's store() method will asume that you want to insert a new record. See the manual's section about
jOOQ currently doesn't support more complex data structures, the way Hibernate/JPA attempt to map relational data onto POJOs. While future developments in this direction are not excluded, jOOQ claims that generic mapping strategies lead to an enormous additional complexity that only serves very few use cases. You are likely to find a solution using any of jOOQ's various More complex data structures
In the previous sections we have seen how to create RecordMapper every time you need one. For this, you can provide jOOQ's
The above is a very simple example showing that you will have complete flexibility in how to override jOOQ's record to POJO mapping mechanisms. @@ -7121,17 +6994,17 @@ bookDao.delete(book);]]>
If you're looking into a generic, third-party mapping utility, have a look at ModelMapper, or Orika Mapper, which can both be easily integrated with jOOQ.
-
Unlike JDBC's
As a
Many databases support returning several result sets, or cursors, from single queries. An example for this is Sybase ASE's sp_help command:
-The correct (and verbose) way to do this with JDBC is as follows:
-
As previously discussed in the chapter about
Using generics, the resulting structure is immediately clear.
-
Some queries take very long to execute, yet they are not crucial for the continuation of the main program. For instance, you could be generating a complicated report in a Swing application, and while this report is being calculated in your database, you want to display a background progress bar, allowing the user to pursue some other work. This can be achived simply with jOOQ, by creating a
Note, that instead of letting jOOQ spawn a new thread, you can also provide jOOQ with your own
When interacting with legacy applications, you may prefer to have jOOQ return a
Instead of operating on a JDBC ResultSet holding an open resource from your database, you can also let jOOQ's
The inverse of the above is possible too. Maybe, a legacy part of your application produces JDBC
You can also tighten the interaction with jOOQ's data type system and
If supplied, the additional information is used to override the information obtained from the
Apart from a few extra features (
Such a converter can be used in many parts of the jOOQ API. Some examples have been illustrated in the manual's section about
jOOQ ships with a built-in default
jOOQ also allows for generated source code to reference your own custom converters, in order to permanently replace a
SQL result tables are not optimal in terms of used memory as they are not designed to represent hierarchical data as produced by JOIN operations. Specifically, FOREIGN KEY values may repeat themselves unnecessarily:
Now, if you have millions of records with only few distinct values for AUTHOR_ID, you may not want to hold references to distinct (but equal)
You can specify as many fields as you want for interning. The above has the following effect: @@ -7479,14 +7350,14 @@ Result> r1 = create.select(BOOK.ID, BOOK.AUTHOR_ID, BOOK.TITLE)
Note, that jOOQ will not use interned data for identity comparisons: string1 == string2. Interning is used only to reduce the memory footprint of
With JDBC, you have full control over your SQL statements. You can decide yourself, if you want to execute a static
@@ -7528,12 +7398,12 @@ inlined.select(val(1)).where(val(1).equal(1)).fetch();]]>
Note that you don't have to inline all your bind values at once. If you know that a bind value is not really a variable and should be inlined explicitly, you can do so by using
As previously discussed in the chapter about
The above technique can be quite useful when you want to reuse expensive database resources. This can be the case when your statement is executed very frequently and your database would take non-negligible time to soft-parse the prepared statement and generate a new statement / cursor resource. @@ -7570,7 +7440,7 @@ finally { This is also modeled in jOOQ. However, the difference to JDBC is that closing a statement is the default action, whereas keeping it open has to be configured explicitly. This is better than JDBC, because the default action should be the one that is used most often. Keeping open statements is rarely done in average applications. Here's an example of how to keep open PreparedStatements with jOOQ:
-The above example shows how a query can be executed twice against the same underlying PreparedStatement. Unlike in other execution scenarios, you must not forget to close this query now
-JDBC knows a couple of execution flags and modes, which can be set through the jOOQ API as well. jOOQ essentially supports these flags and execution modes:
-An example of why you might want to manually set a ResultSet's concurrency flag to something non-default is given here:
-
In the above example, your custom Record from the JDBC ResultSet. With the concurrency being set to ResultSet.CONCUR_UPDATABLE, you can now modify the database cursor through the standard JDBC ResultSet API.
With JDBC, you can easily execute several statements at once using the addBatch() method. Essentially, there are two modes in JDBC
@@ -7683,7 +7551,7 @@ DSL.using(new DefaultConfiguration()In code, this looks like the following snippet:
-
When creating a batch execution with a single query and multiple bind values, you will still have to provide jOOQ with dummy bind values for the original query. In the above example, these are set to null. For subsequent calls to bind(), there will be no type safety provided by jOOQ.
Most databases support sequences of some sort, to provide you with unique values to be used for primary keys and other enumerations. If you're using jOOQ's
You can inline sequence references in jOOQ SQL statements. The following are examples of how to do that:
-
For more info about inlining sequence references in SQL statements, please refer to the manual's section about
Many RDBMS support the concept of "routines", usually calling them procedures and/or functions. These concepts have been around in programming languages for a while, also outside of databases. Famous languages distinguishing procedures from functions are:
@@ -7835,14 +7703,14 @@ create.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) If you're using jOOQ'sThe generated artefacts can then be used as follows:
-But you can also call the procedure using a generated convenience method in a global Routines class:
-
For more details about
The generated artefacts can then be used as follows:
-
For more info about inlining stored function references in SQL statements, please refer to the manual's section about
Oracle uses the concept of a PACKAGE to group several procedures/functions into a sort of namespace. The SQL 92 standard talks about "modules", to represent this concept, even if this is rarely implemented as such. This is reflected in jOOQ by the use of Java sub-packages in the
For more details about
Oracle UDTs can have object-oriented structures including member functions and procedures. With Oracle, you can do things like this:
-These member functions and procedures can simply be mapped to Java methods:
-
For more details about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's exporting functionality (see also the
The above query will result in an XML document looking like the following one:
-
The same result as an
See the XSD schema definition here, for a formal definition of the XML export format:
http://www.jooq.org/xsd/jooq-export-{export-xsd-version}.xsd
The above query will result in a CSV document looking like the following one:
-In addition to the standard behaviour, you can also specify a separator character, as well as a special string to represent NULL values (which cannot be represented in standard CSV):
-The above query will result in a JSON document looking like the following one:
-Note: This format has changed in jOOQ 2.6.0
-The above query will result in an HTML document looking like the following one
-The above query will result in a text document looking like the following one
-
A simple text representation can also be obtained by calling toString() on a Result object. See also the manual's section about
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's importing functionality (see also exporting functionality). You can import data directly into a table from the formats described in the subsequent sections of this manual.
-
The below CSV data represents two author records that may have been exported previously, by jOOQ's
With jOOQ, you can load this data using various parameters from the loader API. A simple load may look like this:
-Here are various other examples:
-Any of the above configuration methods can be combined to achieve the type of load you need. Please refer to the API's Javadoc to learn about more details. Errors that occur during the load are reported by the execute method's result:
-This is not yet supported
-Your database application probably consists of 50% - 80% CRUD, whereas only the remaining 20% - 50% of querying is actual querying. Most often, you will operate on records of tables without using any advanced relational concepts. This is called CRUD for
@@ -8267,35 +8119,35 @@ Query query = error.query();]]> In normalised databases, every table has a primary key by which a tuple/record within that table can be uniquely identified. In simple cases, this is a (possibly auto-generated) number called ID. But in many cases, primary keys include several non-numeric columns. An important feature of such keys is the fact that in most databases, they are enforced using an index that allows for very fast random access to the table. A typical way to access / modify / delete a book is this: -
Normalised databases assume that a primary key is unique "forever", i.e. that a key, once inserted into a table, will never be changed or re-inserted after deletion. In order to use jOOQ's
If you're using jOOQ's
See the manual's section about
Some remarks about storing: @@ -8342,11 +8194,11 @@ book2.store();]]> Deleting a record will remove it from the database. Here's how you delete records:
-@@ -8356,31 +8208,30 @@ book.delete();]]> In order to perform a refresh, use the following Java code:
-
CRUD operations can be combined with regular querying, if you select records from single database tables, as explained in the manual's section about selectFrom() method from the
All of jOOQ's
The purpose of the above information is for jOOQ's
Many databases support the concept of IDENTITY values, or
If you're using jOOQ's
@@ -8429,8 +8280,8 @@ System.out.println(book.getId());]]>
These SQL dialects implement the standard very neatly.
-H2, MySQL, Postgres, SQL Server, Sybase ASE, Sybase SQL Anywhere @@ -8438,7 +8289,7 @@ id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1)]]>
These SQL dialects implement identites, but the DDL syntax doesn’t follow the standard
-Oracle @@ -8462,7 +8313,7 @@ id INTEGER NOT NULL IDENTITY]]>
Oracle does not know any identity columns at all. Instead, you will have to use a trigger and update the ID column yourself, using a custom sequence. Something along these lines:
-Note, that this approach can be employed in most databases supporting sequences and triggers! It is a lot more flexible than standard identities
-
Tables without a PRIMARY KEY are considered non-updatable by jOOQ, as jOOQ has no way of uniquely identifying such a record within the database. If you're using jOOQ's
Note, that some databases use internal rowid or object-id values to identify such records. jOOQ does not support these vendor-specific record meta-data.
-
jOOQ allows you to perform
If you're using jOOQ's
The MODIFIED column will contain a timestamp indicating the last modification timestamp for any book in the BOOK table. If you're using jOOQ and it's
As before, without the added TIMESTAMP column, optimistic locking is transparent to the API. @@ -8605,17 +8455,17 @@ book2.store();]]>
Note, for explicit pessimistic locking, please consider the manual's section about the
When inserting, updating, deleting a lot of records, you may wish to profit from JDBC batch operations, which can be performed by jOOQ. These are available through jOOQ's
Internally, jOOQ will render all the required SQL statements and execute them as a regular
If you're using jOOQ's
corresponds to the DAO's related generated POJO type
// getType();
-}]]>
Besides these base methods, generated DAO classes implement various useful fetch methods. An incomplete example is given here, for the BOOK table:
-Note that you can further subtype those pre-generated DAO classes, to add more useful DAO methods to them. Using such a DAO is simple:
-This is an eternal and religious debate. Pros and cons have been discussed time and again, and it still is a matter of taste, today. In this case, jOOQ clearly takes a side. jOOQ's exception strategy is simple: @@ -8751,12 +8599,12 @@ bookDao.delete(book);]]>
The following section about
The
Now, configure jOOQ's runtime to load your listener
-And log results any time with a snippet like this:
-This may result in the following log output:
-
Please read the
See also the manual's sections about
Since jOOQ 3.0, a simple wrapping API has been added to wrap JDBC's rather awkward
jOOQ logs all SQL queries and fetched result sets to its internal DEBUG logger, which is implemented as an
With the above configuration, let's fetch some data with jOOQ
-The above query may result in the following log output:
-Essentially, jOOQ will log @@ -8941,12 +8789,12 @@ Finishing : Total: 4.814ms, +3.375ms
If you wish to use your own logger (e.g. avoiding printing out sensitive data), you can deactivate jOOQ's logger using
Many users may have switched from higher-level abstractions such as Hibernate to jOOQ, because of Hibernate's difficult-to-manage performance, when it comes to large database schemas and complex second-level caching strategies. However, jOOQ itself is not a lightweight database abstraction framework, and it comes with its own overhead. Please be sure to consider the following points:
@@ -8961,14 +8809,14 @@ Finishing : Total: 4.814ms, +3.375msDon't be put off by the above paragraphs. You should optimise wisely, i.e. only in places where you really need very high throughput to your database. jOOQ's overhead compared to plain JDBC is typically less than 1ms per query.
-
While optional, source code generation is one of jOOQ's main assets if you wish to increase developer productivity. jOOQ's code generator takes your database schema and reverse-engineers it into a set of Java classes modelling
The following chapters will show how to configure the code generator and how to generate various artefacts.
-There are three binaries available with jOOQ, to be downloaded from SourceForge or from Maven central:
@@ -9014,7 +8862,7 @@ Finishing : Total: 4.814ms, +3.375ms You need to tell jOOQ some things about your database connection. Here's an example of how to do it for an Oracle database -
There are also lots of advanced configuration parameters, which will be treated in the
@@ -9100,7 +8948,7 @@ Finishing : Total: 4.814ms, +3.375ms
Code generation works by calling this class with the above property file as argument.
Be sure that these elements are located on the classpath: @@ -9168,20 +9016,20 @@ Finishing : Total: 4.814ms, +3.375ms When running code generation with ant's <java/> task, you may have to set fork="true":
-Using the official jOOQ-codegen-maven plugin, you can integrate source code generation in your Maven build process:
-
See the full example of a pom.xml including the jOOQ-codegen artefact here:
@@ -9245,17 +9093,17 @@ Finishing : Total: 4.814ms, +3.375ms
Be sure, both jooq-{jooq-version}.jar and your generated package (see configuration) are located on your classpath. Once this is done, you can execute SQL statements with your generated classes.
-
In the
The following example shows how you can override the DefaultGeneratorStrategy to render table and column names the way they are defined in the database, rather than switching them to camel case:
-More examples can be found here: @@ -9394,7 +9242,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { Within the <generator/> element, there are other configuration elements:
-Check out the some of the manual's "advanced" sections to find out more about the advanced configuration parameters. @@ -9456,7 +9304,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { Also, you can add some optional advanced configuration parameters for the generator:
-@@ -9537,12 +9385,12 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy {
daos = true, then jOOQ will set pojos = trueimmutablePojos = true, then jOOQ will set pojos = trueFor increased convenience at the use-site, jOOQ generates "global" artefacts at the code generation root location, referencing tables, routines, sequences, etc. In detail, these global artefacts include the following:
@@ -9559,7 +9407,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy { When referencing global artefacts from your client application, you would typically static import them as such: -
Every table in your database will generate a
@@ -9618,17 +9465,17 @@ create.insertInto(com.example.generated.Tables.MY_TABLE)
Table generation cannot be deactivated
-
Every table in your database will generate a
@@ -9689,17 +9536,17 @@ implements IBook {
Record generation can be deactivated using the records flag
-Every table in your database will generate a POJO implementation that looks like this:
-@@ -9752,24 +9599,24 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the pojos flag
-Every table in your database will generate an interface that looks like this:
-@@ -9784,18 +9631,18 @@ public class Book implements java.io.Serializable
POJO generation can be activated using the interfaces flag
-
Every table in your database will generate a
DAO generation can be activated using the daos flag
-
Every sequence in your database will generate a
Sequence generation cannot be deactivated
-
Every procedure or function (routine) in your database will generate a
@@ -9892,17 +9739,17 @@ public class Book implements java.io.Serializable
Routine generation cannot be deactivated
-
Every UDT in your database will generate a
Besides the
UDT generation cannot be deactivated
-
Sometimes, the actual database data type does not match the SQL data type that you would like to use in Java. This is often the case for ill-supported SQL data types, such as BOOLEAN or UUID. jOOQ's code generator allows you to apply simple data type rewriting. The following configuration will rewrite IS_VALID columns in all tables to be of type BOOLEAN.
See the section about
When using a custom type in jOOQ, you need to let jOOQ know about its associated
The above configuration will lead to AUTHOR.DATE_OF_BIRTH being generated like this:
-
This means that the bound type of <T> will be GregorianCalendar, wherever you reference DATE_OF_BIRTH. jOOQ will use your custom converter when binding variables and when fetching data from
We've seen previously in the chapter about
Databases can become very large in real-world applications. This is not a problem for jOOQ's code generator, but it can be for the Java compiler. jOOQ generates some classes for
<globalObjectReferences/>These chapters hold some information about tools to be used with jOOQ
-When writing unit tests for your data access layer, you have probably used some generic mocking tool offered by popular providers like Mockito, jmock, mockrunner, or even DBUnit. With jOOQ, you can take advantage of the built-in JDBC mock API that allows you to simulate a database on the JDBC level for precisely those SQL/JDBC use cases supported by jOOQ.
@@ -10117,7 +9960,7 @@ create.selectFrom(AUTHOR) This work is greatly simplified, when using jOOQ's own mock API. Theorg.jooq.tools.jdbc package contains all the essential implementations for both JDBC 4.0 and 4.1, which are needed to mock JDBC for jOOQ. In order to write mock tests, provide the jOOQ
As you can see, the configuration setup is simple. Now, the MockDataProvider acts as your single point of contact with JDBC / jOOQ. It unifies any of these execution modes, transparently:
@@ -10149,7 +9992,7 @@ ResultMockDataProvider:
Essentially, the
See the
The
The in-process mode is useful for Swing applications or other, locally run Java programs accessing the database via jOOQ. In order to launch the jOOQ Console "in-process", specify the previously documented settings and launch the Console as follows:
-Only in the in-process mode, you can execute ad-hoc queries directly from the console, if you provide it with proper DatabaseDescriptor. These queries are executed from the Editor pane which features: @@ -10319,36 +10162,36 @@ catch (Exception ignore) {} In J2EE or other server/client environments, you may not be able to run the console in the same process as your application. You can then run the jOOQ Console in "headless" mode. In addition to the previously documented settings, you'll have to start a debugger server in your application process, that the console can connect to:
-Now start your application along with the debugger server and launch the console with this command:
-Depending on your distribution, you may have to manually add rsyntaxtextarea-2.0.2.jar and jOOQ artefacts on your classpath.
-These chapters hold some general jOOQ reference information
-Every RDMBS out there has its own little specialties. jOOQ considers those specialties as much as possible, while trying to standardise the behaviour in jOOQ. In order to increase the quality of jOOQ, some 70 unit tests are run for syntax and variable binding verification, as well as some 180 integration tests with an overall of around 1200 queries for any of these databases: @@ -10407,12 +10250,12 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
This section will soon contain a feature matrix, documenting what feature is available for which database.
-There is always a small mismatch between SQL data types and Java data types. This is for two reasons:
@@ -10423,21 +10266,21 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);This chapter should document the most important notes about SQL, JDBC and jOOQ data types.
-jOOQ currently doesn't explicitly support JDBC BLOB and CLOB data types. If you use any of these data types in your database, jOOQ will map them to byte[] and String instead. In simple cases (small data), this simplification is sufficient. In more sophisticated cases, you may have to bypass jOOQ, in order to deal with these data types and their respective resources. True support for LOBs is on the roadmap, though.
-
Some databases explicitly support unsigned integer data types. In most normal JDBC-based applications, they would just be mapped to their signed counterparts letting bit-wise shifting and tweaking to the user. jOOQ ships with a set of unsigned
jOOQ fills a gap opened by JDBC, which neglects an important SQL data type as defined by the SQL standards: INTERVAL types. SQL knows two different types of intervals:
@@ -10486,51 +10329,51 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);XML data types are currently not supported
-Geospacial data types are currently not supported
-Some databases support cursors returned from stored procedures. They are mapped to the following jOOQ data type:
-
In fact, such a cursor will be fetched immediately by jOOQ and wrapped in an
The SQL standard specifies ARRAY data types, that can be mapped to Java arrays as such:
-The above array type is supported by these SQL dialects: @@ -10545,14 +10388,14 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
Oracle has strongly-typed arrays and table types (as opposed to the previously seen anonymously typed arrays). These arrays are wrapped by
jOOQ takes SQL as an external domain-specific language and maps it onto Java, creating an internal domain-specific language. Internal DSLs cannot 100% implement their external language counter parts, as they have to adhere to the syntax rules of their host or target language (i.e. Java). This section explains the various problems and workarounds encountered and implemented in jOOQ.
@@ -10562,19 +10405,17 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT); SQL syntax does not always need keywords to form expressions. The clause takes various argument assignments:
-
The above example also shows missing operator overloading capabilities, where "=" is replaced by "," in jOOQ. Another example are
In this case, ROW is an actual (optional) SQL keyword, implemented by at least PostgreSQL.
@@ -10585,27 +10426,25 @@ SERVER = new RemoteDebuggerServer(DEBUGGER_PORT);
As most languages, SQL does not attribute any meaning to whitespace. However, whitespace is important when forming "composed" keywords, i.e. SQL clauses composed of several keywords. jOOQ follows standard Java method naming conventions to map SQL keywords (case-insensitive) to Java methods (case-sensitive, camel-cased). Some examples:
Future versions of jOOQ may use all-uppercased method names in addition to the camel-cased ones (to prevent collisions with Java keywords):
-@@ -10620,10 +10459,9 @@ WHEN_MATCHED_THEN_UPDATE() jOOQ omits some of those keywords when it is too tedious to write them in Java.
-
The above example omits THEN and END keywords in Java. Future versions of jOOQ may comprise a more complete DSL, including such keywords again though, to provide a more 1:1 match for the SQL language.
@@ -10634,23 +10472,21 @@ WHEN_MATCHED_THEN_UPDATE()
Some SQL constructs are hard to map to Java, but they are also not really necessary. SQL often expects syntactic parentheses where they wouldn't really be needed, or where they feel slightly inconsistent with the rest of the SQL language.
The parentheses used for the WITHIN GROUP (..) and OVER (..) clauses are required in SQL but do not seem to add any immediate value. In some cases, jOOQ omits them, although the above might be optionally re-phrased in the future to form a more SQLesque experience:
@@ -10666,12 +10502,11 @@ WHEN_MATCHED_THEN_UPDATE() jOOQ replaces those keywords by "synonyms":
-There is more future collision potential with: @@ -10694,70 +10529,67 @@ pivot(..).on(..).in(..) Most SQL operators have to be mapped to descriptive method names in Java, as Java does not allow operator overloading:
-
For those users using
This is less of a syntactic SQL feature than a semantic one. In SQL, objects can be referenced before (i.e. "lexicographically before") they are declared. This is particularly true for
A more sophisticated example are common table expressions (CTE), which are currently not supported by jOOQ:
-
Common table expressions define a "derived column list", just like t.a from t.
This chapter will soon contain an overview over jOOQ's API using a pseudo BNF notation.
-jOOQ is running some of your most mission-critical logic: the interface layer between your Java / Scala application and the database. You have probably chosen jOOQ for any of the following reasons:
@@ -10797,7 +10629,7 @@ FROM t jOOQ is used in jOOQ-meta as a proof of concept. This includes complex queries such as the following Postgres query -These rather complex queries show that the jOOQ API is fit for advanced SQL use-cases, compared to the rather simple, often unrealistic queries in the integration test suite.
@@ -10860,12 +10692,12 @@ for (Record record : create().select(Keeping things DRY leads to longer stack traces, but in turn, also increases the relevance of highly reusable code-blocks. Chances that some parts of the jOOQ code base slips by integration test coverage decrease significantly.
-This section is for all users of jOOQ 2.x who wish to upgrade to the next major release. In the next sub-sections, the most important changes are explained. Some code hints are also added to help you fix compilation errors.
@@ -10889,13 +10721,13 @@ for (Record record : create().select( Some hints related to row value expressions: -
@@ -10921,7 +10753,7 @@ Result> result = create.select(BOOK.TITLE, BOOK.ID).from(BOOK).fetch();]]>
-
Field.equalAny(...) and similar methods have been removed in favour of Field.equal(any(...)). This greatly simplified the Field API. An example:
-
@@ -10957,7 +10789,7 @@ Condition condition = BOOK.ID.equal(subselect);]]>
GroupField has been introduced as a DSL marker interface to denote fields that can be passed to GROUP BY clauses. This includes all org.jooq.Field types. However, fields obtained from ROLLUP(), CUBE(), and GROUPING SETS() functions no longer implement Field. Instead, they only implement GroupField. An example:
@@ -10983,14 +10815,14 @@ Field> field2c = DSL.one(); // OK]]> Here is an example how to check if a field has a given value, without applying SQL's ternary NULL logic:
-@@ -11011,14 +10843,14 @@ Condition condition3 = BOOK.TITLE.isNotDistinctFrom(possiblyNull);]]> In order to allow for simpler connection / data source management, jOOQ externalised connection handling in a new ConnectionProvider type. The previous two connection modes are maintained backwards-compatibly (JDBC standalone connection mode, pooled DataSource mode). Other connection modes can be injected using:
-These are some side-effects of the above change @@ -11068,7 +10900,7 @@ Condition condition3 = BOOK.TITLE.isNotDistinctFrom(possiblyNull);]]>
Table.getPrimaryKey() != null check.USE statement support has been removed from jOOQ. Its behaviour was ill-defined, while it didn't work the same way (or didn't work at all) in some databases.There are also lots of advanced configuration parameters, which will be @@ -1627,7 +1616,7 @@ public void bind(BindContext context) throws DataAccessException;
Code generation works by calling this class with the above property file as argument.
-Be sure that these elements are located on the classpath:
Note that when running code generation with ant's <java/> task, you may have to set fork="true":
-Using the official jOOQ-codegen-maven plugin, you can integrate source code generation in your Maven build process:
-See the full example of a pom.xml including the jOOQ-codegen artefact here: https://github.com/jOOQ/jOOQ/blob/master/jOOQ-codegen-maven-example/pom.xml
@@ -1790,7 +1779,7 @@ public void bind(BindContext context) throws DataAccessException; won't be maintained any longer. If you wish to migrate to XML, you can migrate the file using this command on the command line -Using the migrate flag, jOOQ will read the properties file and output a corresponding XML file on system out @@ -1800,13 +1789,13 @@ public void bind(BindContext context) throws DataAccessException;
Be sure, both jOOQ.jar and your generated package (see configuration) are located on your classpath. Once this is done, you can execute SQL statements with your generated classes.
- +
In the
The following example shows how you can override the @@ -1836,7 +1825,7 @@ public void bind(BindContext context) throws DataAccessException; camel case:
-Within the <generator/> element, there are other configuration elements:
-Check out the some of the manual's "advanced" sections @@ -2003,7 +1992,7 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy {
Also, you can add some optional advanced configuration parameters for the generator:
-As of jOOQ 1.5, the top-level generated object is the @@ -2094,15 +2082,14 @@ public class AsInDatabaseStrategy extends DefaultGeneratorStrategy {
The schema can be used to dynamically discover generate database artefacts. Tables, sequences, and other items are accessible from the schema. For example:
-The most important generated artefacts are @@ -2125,7 +2112,7 @@ public final java.util.List<org.jooq.Table<?>> getTables();
If you use the @@ -2153,7 +2140,7 @@ public final java.util.List<org.jooq.Table<?>> getTables(); extends Record>'s associated Record type <R>. In the case of the above TAuthor Table, this will be a TAuthorRecord.
-The above could be your custom POJO or a POJO generated @@ -2222,20 +2209,20 @@ public class MyAuthor { you can now let jOOQ fetch records "into" your custom type:
-Read the javadoc for Record.into() for more details.
-This is one of the most important reasons why you should consider jOOQ. Read also my @@ -2254,14 +2241,14 @@ public class MyAuthor {
Let's say you have these stored procedures and functions in your Oracle database
-jOOQ will essentially generate two artefacts for every procedure/function:
Let's see what these things look like, in Java. The classes (simplified for the example):
-An example invocation of such a stored procedure might look like this:
-The above configuration is a @@ -2322,7 +2309,7 @@ assertEquals(BigDecimal.ONE, p.getResult()); If you use the generated convenience methods, however, things are much simpler, still:
-An sample invocation, equivalent to the previous example:
-These member functions and procedures can simply be mapped to Java methods:
-For more details about UDT's see the Manual's section on
In recent years, most RDBMS have started to implement some support for @@ -2474,7 +2461,7 @@ assertNotNull(author.getLastName());
In Oracle, you would define UDTs like this:
-These types could then be used in tables and/or stored procedures like such:
-
Standard JDBC UDT support encourages JDBC-driver developers to implement
@@ -2514,7 +2501,7 @@ CREATE OR REPLACE PROCEDURE p_check_address (address IN OUT u_address_type); and
Now, when you interact with entities or procedures that hold UDT's, that's very simple as well. Here is an example:
-A similar thing can be achieved when interacting with the example stored procedure:
-An example usage of ARRAYs is given here for the Postgres dialect
-When generating source code from the above entities, these artefacts will be created in Java:
-In Oracle, a VARRAY type is something slightly different than in @@ -2610,7 +2597,7 @@ public final class Functions { generated objects from those types as well. The example above would read like this in Oracle:
-Note that it becomes clear immediately, that a mapping from
U_STRING_ARRAY to String[] is obvious. But a mapping from String[] to
@@ -2628,7 +2615,7 @@ RETURN u_string_array
Some examples:
-The above Postgres ENUM type will be generated as
-Intuitively, the generated classes for the T_BOOK table in Postgres would look like this:
-Note that jOOQ allows you to simulate ENUM types where this makes
sense in your data model. See the section on
Sequences implement the
So if you have a sequence like this in Oracle:
-This is what jOOQ will generate:
-Which you can use in a select statement as such:
-Or directly fetch currval() and nextval() from the sequence using the Factory:
-jOOQ ships with its own DSL (or Domain Specific Language) that @@ -2748,7 +2734,7 @@ BigInteger nextval = create.nextval(Sequences.S_AUTHOR_ID); just like .NET's C# does with LINQ to SQL.
Here is an example to show you what that means. When you want to write a query like this in SQL:
-
You couldn't come much closer to SQL itself in Java, without re-writing the compiler.
We'll see how the aliasing works later in the section about
When you don't just perform CRUD (i.e. SELECT * FROM your_table WHERE ID = ?), you're usually generating new types using custom projections. With jOOQ, this is @@ -2785,7 +2770,7 @@ create.select() some extensions, is provided by a query like this:
-So that's daily business. How to do it with jOOQ: When you first create a SELECT statement using the Factory's select() methods
-jOOQ will return an "intermediary" type to you, representing the @@ -2826,11 +2811,11 @@ create.select(T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME, count()); clauses. Let's say you do decide to add a FROM clause, then you can use this method for instance:
-After adding the table-like structures (mostly just Tables) to
select from, you may optionally choose to add a JOIN clause, as the
@@ -2839,7 +2824,7 @@ create.select(T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME, count())
Now, if you do add a JOIN clause, you have to specify the JOIN .. ON
condition before you can add more clauses. That's not an optional step
@@ -2860,7 +2845,7 @@ create.select(T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME, count())
See the section about
@@ -2888,13 +2873,13 @@ create.select(T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME, count())
re-iterate and add another JOIN clause, just like in SQL. Or we go on
to the next step, adding conditions in the
Now the returned type
Let's assume we have that method parseDate() creating a
and the HAVING clause:
-and the ORDER BY clause. Some RDBMS support NULLS FIRST and NULLS LAST extensions to the ORDER BY clause. If this is not supported by the RDBMS, then the behaviour is simulated with an additional CASE WHEN ... IS NULL THEN 1 ELSE 0 END clause.
-and finally the LIMIT clause. Most dialects have a means of limiting the number of result records (except Oracle). Some even support having @@ -2962,7 +2947,7 @@ create.select(T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME, count()) clause, jOOQ will simulate the LIMIT clause using nested selects and filtering on ROWNUM (for Oracle), or on ROW_NUMBER() (for DB2 and SQL Server):
-In the final step, there are some proprietary extensions available only in some RDBMS. One of those extensions are the FOR UPDATE (supported in most RDBMS) and FOR SHARE clauses (supported only in MySQL and Postgres):
-
Now the most relevant super-type of the object we have just created is
@@ -3004,7 +2989,7 @@ create.select(T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME, count())
section about the
As you can see, there is no way to further restrict/project the selected fields. This just selects all known TableFields in the supplied Table, and it also binds <R extends Record> to your Table's associated Record. An example of such a Query would then be:
-
In the relational data model,
@@ -3055,7 +3039,7 @@ Record fetchOne();
the
The
In your average application, you will typically have 3-4 SQL queries
that have quite a long list of predicates (and possibly JOINs), such
@@ -3130,7 +3114,7 @@ TableOnConditionStep onKey(ForeignKey, ?> key);]]>
which is typically a participant of a
condition, and the
The above example describes the essence of boolean logic in jOOQ. As soon as you have a Condition object, you can connect that to other @@ -3155,7 +3139,7 @@ TableOnConditionStep onKey(ForeignKey, ?> key);]]> cases. Here are some important API elements in the Field interface:
-As you see in the partially displayed API above, you can compare a
Field either with other Fields, with constant values (which is a
@@ -3206,26 +3190,25 @@ TableOnConditionStep onKey(ForeignKey, ?> key);]]>
Combining the API of Field and Condition you can express complex predicates like this:
-Just write:
-A typical example of what you might want to do in SQL is this:
-In this example, we are aliasing Tables, calling them a and b. @@ -3237,36 +3220,36 @@ T_BOOK.TYPE_CODE.in(create.select(T_TYPES.CODE).from(T_TYPES)).and(T_BOOK.LANGUA Here is how you can create Table aliases in jOOQ:
-Now, if you want to reference any fields from those Tables, you may not use the original T_BOOK or T_AUTHOR meta-model objects anymore. Instead, you have to get the fields from the new book and author Table aliases:
-So this is how the above SQL statement would read in jOOQ:
-Fields can also be aliased independently from Tables. Most often, this is done when using functions or aggregate operators. Here is an example:
-Here is how it's done with jOOQ:
-When you alias Fields like above, you can access those Fields' values using the alias name:
-In addition to a list of constant values, the IN operator in @@ -3317,7 +3299,7 @@ System.out.println("Books : " + record.getValue("books")); course, this is possible with a plain JOIN as well, but let's say we want to use the IN operator. Then you have two possibilities:
-The EXISTS operator is rather independent and can stand any place where there may be a new condition:
@@ -3361,8 +3342,8 @@ create.select(T_BOOK.getFields())This is reflected by the fact that an EXISTS clause is usually created directly from the Factory:
-When you create such a Condition, it can then be connected to any
other condition using AND, OR operators (see also the manual's section
@@ -3371,70 +3352,68 @@ Condition notExists(Select<?> query);
convenience methods, where they might be useful. For instance in the
Or in the
Or in the
An example of how to use it is quickly given. Get all authors that haven't written any books:
-If you can ensure that a nested SELECT will only return one Record with one Field, then you can test for equality. This is how it is done in SQL:
-More examples like the above can be guessed from the
Instead, you have to do this (or something similar). For jOOQ, this is an excellent example, combining various SQL features into a single statement. Here's how to do it:
-You'll notice how some verbosity seems inevitable when you combine nested SELECT statements with aliasing.
@@ -3481,7 +3459,7 @@ create.select(nested.getFields()) really hard way? :-) But then again, maybe you want to take advantage of Oracle Scalar Subquery Caching -The
Hence, this is how you can write a simple UNION with jOOQ:
-In some SQL dialects, you can arbitrarily nest UNIONs to several @@ -3549,7 +3525,7 @@ create.select(T_BOOK.TITLE) errors in the aforementioned dialects.
An example of advanced UNION usage is the following statement in jOOQ:
-This example does not seem surprising, when you have read the previous chapters about @@ -3565,14 +3541,14 @@ create.select(union.getField(T_BOOK.TITLE)) But when you check out the rendered SQL:
-You can see that jOOQ takes care of many syntax pitfalls, when you're not used to the various dialects' unique requirements. The @@ -3581,14 +3557,14 @@ ORDER BY alias_38173.AUTHOR_ID DESC
It is no problem either for you to create SQL statements with several unions. Just write:
-@@ -3600,7 +3576,7 @@ part1.union(part2).union(part3).union(part4); Let's say, you want to find the employees with the highest salary in every department in Postgres syntax:
-There is a subtle difference between the above two queries.
In SQL, every UNION subselect is in fact a
jOOQ allows you to access native functions from your RDBMS. jOOQ follows two strategies:
@@ -3653,7 +3628,7 @@ create.selectFrom(EMP).where(DEPT.equal("R&D")These are just a few functions in the Factory, so you get the idea:
-Aggregate functions work just like functions, even if they have a slightly different semantics. Here are some examples from Factory:
-A typical example of how to use an aggregate operator is when generating the next key on insertion of an ID. When you want to achieve something like this
-See also the section about
The full power of your database's vendor-specific extensions can hardly @@ -3794,21 +3766,21 @@ GROUP BY AUTHOR_ID to that function. Typically, if you have this type of function in your database:
-Then convenience methods like these are generated:
-Let's say, you have a T_PERSON table with persons' names in it, and you want to know whether there exists an author with precisely that name, you can reuse the above stored function in a SQL query:
-The notion of a stored procedure is implemented in most RDBMS by the @@ -3832,13 +3803,13 @@ create.select(T_PERSON.NAME) it may well have OUT parameters. Since there is not a standard way how to embed stored procedures in SQL, they cannot be integrated in jOOQ's DSL either.
-Your database can do the math for you. Most arithmetic operations are @@ -3850,19 +3821,18 @@ create.select(T_PERSON.NAME) similar limitations as far as type restrictions are concerned. You can use any of these operators:
-In order to express a SQL query like this one:
-You can write something like this in jOOQ:
-jOOQ also supports the Oracle-style syntax for adding days to a Field<? extends java.util.Date>
-For more advanced datetime arithmetic, use the Factory's timestampDiff() and dateDiff() functions, @@ -3877,30 +3847,29 @@ create.select(T_PERSON.NAME)
This is not really an arithmetic expression, but it's still an expression with operators: The string concatenation. jOOQ provides you with the Field's concat() method:
-The CASE clause is part of the standard SQL syntax. While some RDBMS also offer an IF clause, or a DECODE function, you can always rely on the two types of CASE syntax:
-In jOOQ, both syntaxes are supported (although, Derby only knows the @@ -3936,36 +3904,35 @@ create.decode().value(T_AUTHOR.FIRST_NAME)
A CASE clause can be used anywhere where you can place a Field. For instance, you can SELECT the above expression, if you're selecting from T_AUTHOR:
-Sort indirection is often implemented with a CASE clause of a SELECT's ORDER BY clause. In SQL, this reads:
-This will order your authors such that all 'Paulo' come first, then all 'George' and everyone else last (depending on your RDBMS' handling of NULL values in sorting). This is a very common task, such that jOOQ simplifies its use:
-jOOQ's source code generator tries to find the most accurate type
mapping between your vendor-specific data types and a matching Java
@@ -3978,17 +3945,17 @@ ORDER BY CASE FIRST_NAME WHEN 'Paulo' THEN 1
in jOOQ, you can write something like that:
-The same thing can be achieved by casting a Field directly to String.class, as TEXT is the default data type in Postgres to map to Java's String
-The complete CAST API in Field consists of these three methods:
-A DSL is a nice thing to have, it feels "fluent" and "natural", especially if it models a well-known language, such as SQL. But a DSL @@ -4040,7 +4006,7 @@ public class Factory {
To construct artefacts wrapping plain SQL, you should use any of these methods from the Factory class:
-Apart from the general factory methods, plain SQL is useful also in various other contexts. For instance, when adding a .where("a = b") clause to a query. Hence, there exist several convenience methods where plain SQL can be inserted usefully. This is an example displaying all various use-cases in one single query:
-There are some important things to keep in mind when using plain SQL:
@@ -4125,7 +4091,7 @@ create.select(LAST_NAME, COUNT1, COUNT2)This section covers some advanced topics and features that don't fit into any other section.
-Only MySQL and Postgres databases support true ENUM types natively. Some other RDBMS allow you to map the concept of an ENUM data type to @@ -4158,7 +4124,7 @@ create.select(LAST_NAME, COUNT1, COUNT2)
As previously discussed in the
The results of this will be a Java enum that looks similar to this:
-In the above example, you can see how the configured primary key is mapped to the id member, the configured literal column is mapped to @@ -4228,20 +4194,20 @@ create.select(LAST_NAME, COUNT1, COUNT2) LANGUAGE_ID Field<Integer>, a Field<TLanguage> is generated:
-Which can then be used in the TBookRecord directly:
-You can use master data types when you're actually mapping master @@ -4250,13 +4216,13 @@ create.select(LAST_NAME, COUNT1, COUNT2) Also, be aware that it will be difficult to perform actual JOIN operations on the underlying table with jOOQ, once the master data type is generated.
-When using a custom type in jOOQ, you need to let jOOQ know about @@ -4273,7 +4239,7 @@ create.select(LAST_NAME, COUNT1, COUNT2) a Converter like this:
-Such a Converter can now be used in various places of the jOOQ API, especially when reading data from the database:
-@@ -4332,7 +4298,7 @@ create.select(T_AUTHOR.DATE_OF_BIRTH) fields that start with DATE_OF_
-The above configuration will lead to T_AUTHOR.DATE_OF_BIRTH being generated like this:
-
This means that the bound of <T> will be GregorianCalendar,
@@ -4377,11 +4343,11 @@ create.select(T_AUTHOR.DATE_OF_BIRTH)
Read more about advanced code generation configuration in
@@ -4398,13 +4364,13 @@ create.selectFrom(T_AUTHOR)
you may choose to extend
You may wish to design your database in a way that you have several instances of your schema. This is useful when you want to cleanly @@ -4431,7 +4397,7 @@ create.selectFrom(T_AUTHOR) class, that you can equip your Factory's settings with. Take the following example:
-The query executed with a Factory equipped with the above mapping will in fact produce this SQL statement:
-Even if T_AUTHOR was generated from DEV.
Note, you can load the above XML file like this:
-This will map generated classes from DEV to MY_BOOK_WORLD, from LOG to MY_BOOK_WORLD_LOG, but leave the MASTER schema alone. Whenever you @@ -4485,29 +4451,29 @@ create.selectFrom(T_AUTHOR).fetch(); the Factory's underlying Connection. Many RDBMS support a USE or SET SCHEMA command, which you can call like this:
-Queries generated from the above Factory will produce this kind of SQL statement:
-If you wish not to render any schema name at all, use the following Settings property for this:
-Not only schemata can be mapped, but also tables. If you are not the @@ -4518,7 +4484,7 @@ create.selectFrom(T_AUTHOR).fetch(); applied to all of your tables. This can be achieved by creating the following mapping:
-The query executed with a Factory equipped with the above mapping will in fact produce this SQL statement:
-Table mapping and schema mapping can be applied independently, by specifying several MappedSchema entries @@ -4550,7 +4516,7 @@ create.selectFrom(T_AUTHOR).fetch(); dedicated developer databases, and a common integration database. In the code generation configuration, you would then write.
-
See the manual's section about
The
Now, configure jOOQ's runtime to load your listener
-And log results any time with a snippet like this:
-This may result in the following log output:
-Please read the ExecuteListener Javadoc @@ -4689,18 +4655,18 @@ for (ExecuteType type : ExecuteType.values()) { in the Factory's settings. When using XML settings:
-Or when using programmatic settings:
-@@ -4710,7 +4676,7 @@ Factory factory = new Factory(connection, dialect, settings);]]> previously documented settings and launch the Console as follows:
-Only in the in-process mode, you can execute ad-hoc queries directly @@ -4778,74 +4744,74 @@ catch (Exception ignore) {} connect to:
-Now start your application along with the debugger server and launch the console with this command:
-Depending on your distribution, you may have to manually add rsyntaxtextarea-1.5.0.jar and jOOQ artefacts on your classpath.
-If you are closely coupling your application to an Oracle (or CUBRID) database, you might need to be able to pass hints of the form /*+HINT*/ with your SQL statements to the Oracle database. For example:
-This can be done in jOOQ using the .hint() clause in your SELECT statement:
-Note that you can pass any string in the .hint() clause. If you use that clause, the passed string will always be put in between the SELECT [DISTINCT] keywords and the actual projection list
-If you are closely coupling your application to an Oracle (or CUBRID) database, you can take advantage of some Oracle-specific features, such as the CONNECT BY clause, used for hierarchical queries. The formal syntax definition is as follows:
-This can be done in jOOQ using the .connectBy(Condition) clauses in your SELECT statement:
-Here's a more complex example where you can recursively fetch directories in your database, and concatenate them to a path:
-The output might then look like this
-If you are closely coupling your application to an Oracle database, you can take advantage of some Oracle-specific features, such as the PIVOT clause, used for statistical analyses. The formal syntax definition is as follows:
-The PIVOT clause is available from the @@ -4891,13 +4856,13 @@ create.select(create.rownum()) PIVOT clause will be added later. Also, jOOQ may simulate PIVOT for other dialects in the future.
-There is one operation in relational algebra that is not given @@ -4907,12 +4872,12 @@ create.select(create.rownum()) The following is an approximate definition of a relational division:
-@@ -4920,11 +4885,11 @@ B = C ÷ A by using the following syntax:
-The above roughly translates to
-Or in plain text: Find those TEXT values in C @@ -4956,13 +4921,13 @@ WHERE NOT EXISTS ( http://www.simple-talk.com/sql/t-sql-programming/divided-we-stand-the-sql-of-relational-division/ -
If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's exporting @@ -4971,11 +4936,11 @@ WHERE NOT EXISTS (
Export your results as XML:
-The above query will result in an XML document looking like the following one:
-Export your results as CSV:
-The above query will result in a CSV document looking like the following one:
-Export your results as JSON:
-The above query will result in a JSON document looking like the following one:
-Export your results as HTML:
-The above query will result in an HTML document looking like the following one:
-Export your results as text:
-The above query will result in a text document looking like the following one:
-If you are using jOOQ for scripting purposes or in a slim, unlayered application server, you might be interested in using jOOQ's importing @@ -5073,24 +5037,24 @@ String text = create.selectFrom(T_BOOK).fetch().format(); exported previously, by jOOQ's exporting functionality, and then modified in Microsoft Excel or any other spreadsheet tool:
-With jOOQ, you can load this data using various parameters from the loader API. A simple load may look like this:
-Here are various other examples:
-Any of the above configuration methods can be combined to achieve the type of load you need. Please refer to the API's Javadoc to learn about more details. Errors that occur during the load are reported by the execute method's result:
-This will be implemented soon...
-With JDBC, you can easily execute several statements at once using the addBatch() method. Essentially, there are two modes in JDBC
@@ -5182,7 +5146,7 @@ Query query = error.query();]]>In code, this looks like the following snippet:
-jOOQ supports executing queries in batch mode as follows:
-