The above is just simple method-chaining, without any sophisticated formal language definition. Most often in method-chaining contexts, the order of method calls is irrelevant to the API and may be chosen freely (i.e. you can call “withAnotherThing()” first, then “withSomethingElse()”. Instead, I mean a full-fledged domain specific language, formally defined using BNF notation (or anything equivalent).
new Builder().withSomething(x) .withSomethingElse(y) .withSomething(z) .withAnotherThing(xx);
jOOQ’s fluent APIToday, I’d like to give some insight about how jOOQ can be formally expressed as a true domain specific language using BNF notation. jOOQ in itself has actually evolved to become a SQL dialect of its own. It knows most of the standard DML SQL statements and clauses, as well as many vendor-specific ones. Let’s have a look at jOOQ 2.0’s understanding of a SELECT statement:
Or in the much more readable graphical representation:
SELECT ::= ( ( 'select' | 'selectDistinct' ) FIELD* | 'selectOne' | 'selectZero' | 'selectCount' ) ( 'select' FIELD* )* ( 'hint' SQL )* ( 'from' ( TABLE+ | SQL ) ( ( 'join' | 'leftOuterJoin' | 'rightOuterJoin' | 'fullOuterJoin' ) ( TABLE | SQL ) ( 'on' ( CONDITION+ | SQL ) MORE_CONDITIONS? | 'using' FIELD+ ) | ( 'crossJoin' | 'naturalJoin' | 'naturalLeftOuterJoin' | 'naturalRightOuterJoin' ) ( TABLE | SQL ) )* )? ( ( 'where' ( CONDITION+ | SQL ) | ( 'whereExists' | 'whereNotExists' ) SELECT ) MORE_CONDITIONS? )? ( ( 'connectBy' | 'connectByNoCycle' ) ( CONDITION | SQL ) ( 'and' ( CONDITION | SQL ) )* ( 'startWith' ( CONDITION | SQL ) )? )? ( 'groupBy' GROUP_FIELD+ )? ( 'having' ( CONDITION+ | SQL ) MORE_CONDITIONS? )? ( 'orderBy' ( FIELD+ | SORT_FIELD+ | INT+ ) )? ( ( 'limit' INT ( 'offset' INT | INT )? ) | ( ( 'forUpdate' ( 'of' ( FIELD+ | TABLE+ ) )? ( 'wait' INT | 'noWait' | 'skipLocked' )? ) | 'forShare' ) )? ( ( 'union' | 'unionAll' | 'except' | 'intersect' ) SELECT )*
- http://www.jooq.org/sql-as-seen-by-jooq.svg (graphical representation)
- http://www.jooq.org/sql-as-seen-by-jooq.txt (text representation)