Available in versions: Dev (3.19) | Latest (3.18) | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11 | 3.10 | 3.9
Matcher strategies
Applies to ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
Using custom matcher strategies
In the previous section, we have seen how to override generator strategies programmatically. In this chapter, we'll see how such strategies can be configured in the XML or Maven code generator configuration. Instead of specifying a strategy name, you can also specify a <matchers/>
element as such:
-
NOTE: All regular expressions that match object identifiers try to match identifiers first by unqualified name (
org.jooq.meta.Definition.getName()
), then by qualified name (org.jooq.meta.Definition.getQualifiedName()
). - NOTE: There had been an incompatible change between jOOQ 3.2 and jOOQ 3.3 in the configuration of these matcher strategies. See #3217 for details.
<!-- These properties can be added directly to the generator element: --> <generator> <strategy> <matchers> <!-- Specify 0..n schema matchers to provide a strategy for naming objects created from schemas. --> <schemas> <schema> <!-- Match unqualified or qualified schema names. If left empty, this matcher applies to all schemas. --> <expression>MY_SCHEMA</expression> <!-- These elements influence the naming of a generated org.jooq.Schema object. --> <schemaClass> see below MatcherRule specification </schemaClass> <schemaIdentifier> see below MatcherRule specification </schemaIdentifier> <schemaImplements>com.example.MyOptionalCustomInterface</schemaImplements> </schema> </schemas> <!-- Specify 0..n table matchers to provide a strategy for naming objects created from tables. --> <tables> <table> <!-- Match unqualified or qualified table names. If left empty, this matcher applies to all tables. --> <expression>MY_TABLE</expression> <!-- These elements influence the naming of a generated org.jooq.Table object. --> <tableClass> see below MatcherRule specification </tableClass> <tableIdentifier> see below MatcherRule specification </tableIdentifier> <tableImplements>com.example.MyOptionalCustomInterface</tableImplements> <!-- These elements influence the naming of a generated org.jooq.Record object. --> <recordClass> see below MatcherRule specification </recordClass> <recordImplements>com.example.MyOptionalCustomInterface</recordImplements> <!-- These elements influence the naming of a generated interface, implemented by generated org.jooq.Record objects and by generated POJOs. --> <interfaceClass> see below MatcherRule specification </interfaceClass> <interfaceImplements>com.example.MyOptionalCustomInterface</interfaceImplements> <!-- These elements influence the naming of a generated org.jooq.DAO object. --> <daoClass> see below MatcherRule specification </daoClass> <daoImplements>com.example.MyOptionalCustomInterface</daoImplements> <!-- These elements influence the naming of a generated POJO object. --> <pojoClass> see below MatcherRule specification </pojoClass> <pojoExtends>com.example.MyOptionalCustomBaseClass</pojoExtends> <pojoImplements>com.example.MyOptionalCustomInterface</pojoImplements> </table> </tables> <!-- Specify 0..n field matchers to provide a strategy for naming objects created from fields. --> <fields> <field> <!-- Match unqualified or qualified field names. If left empty, this matcher applies to all fields. --> <expression>MY_FIELD</expression> <!-- These elements influence the naming of a generated org.jooq.Field object. --> <fieldIdentifier> see below MatcherRule specification </fieldIdentifier> <fieldMember> see below MatcherRule specification </fieldMember> <fieldSetter> see below MatcherRule specification </fieldSetter> <fieldGetter> see below MatcherRule specification </fieldGetter> </field> </fields> <!-- Specify 0..n routine matchers to provide a strategy for naming objects created from routines. --> <routines> <routine> <!-- Match unqualified or qualified routine names. If left empty, this matcher applies to all routines. --> <expression>MY_ROUTINE</expression> <!-- These elements influence the naming of a generated org.jooq.Routine object. --> <routineClass> see below MatcherRule specification </routineClass> <routineMethod> see below MatcherRule specification </routineMethod> <routineImplements>com.example.MyOptionalCustomInterface</routineImplements> </routine> </routines> <!-- Specify 0..n sequence matchers to provide a strategy for naming objects created from sequences. --> <sequences> <sequence> <!-- Match unqualified or qualified sequence names. If left empty, this matcher applies to all sequences. --> <expression>MY_SEQUENCE</expression> <!-- These elements influence the naming of the generated Sequences class. --> <sequenceIdentifier> see below MatcherRule specification </sequenceIdentifier> </sequence> </sequences> <!-- Specify 0..n enum matchers to provide a strategy for naming objects created from enums. --> <enums> <enum> <!-- Match unqualified or qualified enum names. If left empty, this matcher applies to all enums. --> <expression>MY_ENUM</expression> <!-- These elements influence the naming of a generated org.jooq.EnumType object. --> <enumClass> see below MatcherRule specification </enumClass> <enumImplements>com.example.MyOptionalCustomInterface</enumImplements> </enum> </enums> </matchers> </strategy> </generator>
The above example used references to "MatcherRule", which is an XSD type that looks like this:
<schemaClass> <!-- The optional transform element lets you apply a name transformation algorithm to transform the actual database name into a more convenient form. Possible values are: - AS_IS : Leave the database name as it is : MY_name => MY_name - LOWER : Transform the database name into lower case : MY_name => my_name - LOWER_FIRST_LETTER : Transform the first letter into lower case : MY_name => mY_name - UPPER : Transform the database name into upper case : MY_name => MY_NAME - UPPER_FIRST_LETTER : Transform the first letter into upper case : my_NAME => My_NAME - CAMEL : Transform the database name into camel case : MY_name => myName - PASCAL : Transform the database name into pascal case : MY_name => MyName --> <transform>CAMEL</transform> <!-- The mandatory expression element lets you specify a replacement expression to be used when replacing the matcher's regular expression. You can use indexed variables $0, $1, $2. --> <expression>PREFIX_$0_SUFFIX</expression> </schemaClass>
Some examples
The following example shows a matcher strategy that adds a "T_"
prefix to all table classes and to table identifiers:
<generator> <strategy> <matchers> <tables> <table> <!-- Expression is omitted. This will make this rule apply to all tables --> <tableIdentifier> <transform>UPPER</transform> <expression>T_$0</expression> </tableIdentifier> <tableClass> <transform>PASCAL</transform> <expression>T_$0</expression> </tableClass> </table> </tables> </matchers> </strategy> </generator>
The following example shows a matcher strategy that renames BOOK table identifiers (or table identifiers containing BOOK) into BROCHURE (or tables containing BROCHURE):
<generator> <strategy> <matchers> <tables> <table> <expression>^(.*?)_BOOK_(.*)$</expression> <tableIdentifier> <transform>UPPER</transform> <expression>$1_BROCHURE_$2</expression> </tableIdentifier> </table> </tables> </matchers> </strategy> </generator>
For more information about each XML tag, please refer to the https://www.jooq.org/xsd/jooq-codegen-3.12.0.xsd XSD file.
Feedback
Do you have any feedback about this page? We'd love to hear it!