All versions: 3.11 | 3.10 | 3.9 | 3.8 | 3.7 | Development versions: 3.12 | Unsupported versions: 3.6 | 3.5 | 3.4 | 3.3 | 3.2

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 Issue #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> --> MatcherRule </schemaClass>
          <schemaIdentifier> --> MatcherRule </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> --> MatcherRule </tableClass>
          <tableIdentifier> --> MatcherRule </tableIdentifier>
          <tableImplements>com.example.MyOptionalCustomInterface</tableImplements>
        
          <!-- These elements influence the naming of a generated org.jooq.Record object. -->
          <recordClass> --> MatcherRule </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> --> MatcherRule </interfaceClass>
          <interfaceImplements>com.example.MyOptionalCustomInterface</interfaceImplements>
        
          <!-- These elements influence the naming of a generated org.jooq.DAO object. -->
          <daoClass> --> MatcherRule </daoClass>
          <daoImplements>com.example.MyOptionalCustomInterface</daoImplements>
        
          <!-- These elements influence the naming of a generated POJO object.  -->
          <pojoClass> --> MatcherRule </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> --> MatcherRule </fieldIdentifier>
          <fieldMember> --> MatcherRule </fieldMember>
          <fieldSetter> --> MatcherRule </fieldSetter>
          <fieldGetter> --> MatcherRule </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> --> MatcherRule </routineClass>
          <routineMethod> --> MatcherRule </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> --> MatcherRule </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> --> MatcherRule </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
       - UPPER  : Transform the database name 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 http://www.jooq.org/xsd/jooq-codegen-3.11.0.xsd XSD file.

The jOOQ Logo