diff --git a/jOOQ-website/img/navigation-item-active.png b/jOOQ-website/img/navigation-item-active.png
new file mode 100644
index 0000000000..3c4966b080
Binary files /dev/null and b/jOOQ-website/img/navigation-item-active.png differ
diff --git a/jOOQ-website/index.php b/jOOQ-website/index.php
index 2e720f0d91..07b8798782 100644
--- a/jOOQ-website/index.php
+++ b/jOOQ-website/index.php
@@ -9,49 +9,37 @@ function getSlogan() {
SQL was never meant to be object-oriented. SQL was never meant to be
anything other than... SQL!";
}
+function getActiveMenu() {
+ return "home";
+}
function printContent() {
?>
What does jOOQ code look like?It's simple. With the jOOQ DSL, SQL looks almost as if it were -natively supported by Java. +natively supported by Java. For instance, get all books published in 2011, ordered by title
jOOQ also supports more complex SQL statements. get all authors' + first and last names, and the number of books they've written in + German, if they have written more than five books in German in the last + three years (from 2011), and sort those authors by last names limiting + results to the second and third row, then lock first and last names + columns for update
What's jOOQ+What is jOOQ?jOOQ stands for Java Object Oriented Querying. It combines these essential features: -
How does jOOQ help you?
jOOQ's strategy for supporting vendor-specific functions+jOOQ allows you to access native functions from your RDBMS. jOOQ + follows two strategies: +
Functions+These are just a few functions in the Field interface, so you get the idea: + ++Field<String> rpad(Field<? extends Number> length); +Field<String> rpad(int length); +Field<String> rpad(Field<? extends Number> length, Field<String> c); +Field<String> rpad(int length, char c); +Field<String> lpad(Field<? extends Number> length); +Field<String> lpad(int length); +Field<String> lpad(Field<? extends Number> length, Field<String> c); +Field<String> lpad(int length, char c); +Field<String> replace(Field<String> search); +Field<String> replace(String search); +Field<String> replace(Field<String> search, Field<String> replace); +Field<String> replace(String search, String replace); +Field<Integer> position(String search); +Field<Integer> position(Field<String> search);+ + Aggregate operators+Aggregate operators work just like functions, even if they have a + slightly different semantics. Some of them are also placed in the + Field interface. Others in the Factory. Here are some examples from + Field: + ++Field<Integer> count(); +Field<Integer> countDistinct(); +Field<T> max(); +Field<T> min(); +Field<BigDecimal> sum(); +Field<BigDecimal> avg();+ + 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 + Arithmetic operations + + +Window functions+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. 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) + + +
jOOQ will return an "intermediary" type to you, representing the SELECT statement about to be created (by the way, check out the - section on aggregate operators + section on aggregate operators to learn more about the COUNT(*) function). This type is the org.jooq.SelectFromStep. diff --git a/jOOQ-website/manual/DSL/SQL/index.php b/jOOQ-website/manual/DSL/SQL/index.php index 9fb9697c20..b821c77ad4 100644 --- a/jOOQ-website/manual/DSL/SQL/index.php +++ b/jOOQ-website/manual/DSL/SQL/index.php @@ -6,6 +6,9 @@ require '../../../frame.php'; function printH1() { print "When it's just much easier: Plain SQL"; } +function getActiveMenu() { + return "manual"; +} function getSlogan() { return ""; } diff --git a/jOOQ-website/manual/DSL/UNION/index.php b/jOOQ-website/manual/DSL/UNION/index.php index 38596419c4..076a8f5acf 100644 --- a/jOOQ-website/manual/DSL/UNION/index.php +++ b/jOOQ-website/manual/DSL/UNION/index.php @@ -6,19 +6,161 @@ require '../../../frame.php'; function printH1() { print "UNION and other set operations"; } +function getActiveMenu() { + return "manual"; +} function getSlogan() { - return ""; + return "Unions, differences and intersections are vital set operations taken from set theory."; } function printContent() { global $root; ?> jOOQ's set operation API+The + org.jooq.Select API directly supports the UNION + syntax for all types of Select as discussed in the manual's section about + Queries and Query subtypes. + It consists of these methods: + +
+public interface Select<R extends Record> {
+ Select<R> union(Select<R> select);
+ Select<R> unionAll(Select<R> select);
+ Select<R> except(Select<R> select);
+ Select<R> intersect(Select<R> select);
+}
+
+ Hence, this is how you can write a simple UNION with jOOQ: +
Nested UNIONs+In some SQL dialects, you can arbitrarily nest UNIONs to several + levels. Be aware, though, that SQLite, Derby and MySQL have serious + syntax limitations. jOOQ tries to render correct UNION SQL statements, + but unfortunately, you can create situations that will cause syntax + errors in the aforementioned dialects. + +An example of advanced UNION usage is the following statement in jOOQ: ++// Create a UNION of several types of books +Select<?> union = + create.select(TBook.TITLE, TBook.AUTHOR_ID).from(T_BOOK).where(TBook.PUBLISHED_IN.greaterThan(1945)).union( + create.select(TBook.TITLE, TBook.AUTHOR_ID).from(T_BOOK).where(TBook.AUTHOR_ID.equal(1))); + +// Now, re-use the above UNION and order it by author +create.select(union.getField(TBook.TITLE)) + .from(union) + .orderBy(union.getField(TBook.AUTHOR_ID).descending());+ + This example does not seem surprising, when you have read the + previous chapters about + nested SELECT statements. + But when you check + out the rendered SQL: + ++-- alias_38173 is an example of a generated alias, +-- generated by jOOQ for union queries +SELECT alias_38173.TITLE FROM ( + SELECT T_BOOK.TITLE, T_BOOK.AUTHOR_ID FROM T_BOOK WHERE T_BOOK.PUBLISHED_IN > 1945 + UNION + SELECT T_BOOK.TITLE, T_BOOK.AUTHOR_ID FROM T_BOOK WHERE T_BOOK.AUTHOR_ID = 1 +) alias_38173 +ORDER BY alias_38173.AUTHOR_ID DESC+ + You can see that jOOQ takes care of many syntax pitfalls, when + you're not used to the various dialects' unique requirements. The + above automatic aliasing was added in order to be compliant with + MySQL's requirements about aliasing nested selects. + +Several UNIONs+It is no problem either for you to create SQL statements with several unions. Just write: ++Select<?> part1; +Select<?> part2; +Select<?> part3; +Select<?> part4; + +// [...] + +part1.union(part2).union(part3).union(part4);+ + UNION and the ORDER BY clause++ Strictly speaking, in SQL, you cannot order a subselect that is part + of a UNION operation. You can only order the whole set. In set theory, + or relational algebra, it wouldn't make sense to order subselects + anyway, as a set operation cannot guarantee order correctness. Often, + you still want to do it, because you apply a LIMIT to every subselect. + 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 + nested SELECT, wrapped in parentheses. + In this example, the notion of "nested SELECT" and "subselect" are slightly + different. +
jOOQ's set operation API+The
+ Hence, this is how you can write a simple UNION with jOOQ: +Nested UNIONs+In some SQL dialects, you can arbitrarily nest UNIONs to several + levels. Be aware, though, that SQLite, Derby and MySQL have serious + syntax limitations. jOOQ tries to render correct UNION SQL statements, + but unfortunately, you can create situations that will cause syntax + 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
+ You can see that jOOQ takes care of many syntax pitfalls, when + you're not used to the various dialects' unique requirements. The + above automatic aliasing was added in order to be compliant with + MySQL's requirements about aliasing nested selects. + +Several UNIONs+It is no problem either for you to create SQL statements with several unions. Just write: +UNION and the ORDER BY clause++ Strictly speaking, in SQL, you cannot order a subselect that is part + of a UNION operation. You can only order the whole set. In set theory, + or relational algebra, it wouldn't make sense to order subselects + anyway, as a set operation cannot guarantee order correctness. Often, + you still want to do it, because you apply a LIMIT to every subselect. + 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's strategy for supporting vendor-specific functions+jOOQ allows you to access native functions from your RDBMS. jOOQ + follows two strategies: +
Functions+These are just a few functions in the Field interface, so you get the idea: + +Aggregate operators+Aggregate operators work just like functions, even if they have a + slightly different semantics. Some of them are also placed in the + Field interface. Others in the Factory. Here are some examples from + Field: + +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
+ Window functions+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. 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) + + + |