【问题标题】:How can I dynamically exclude objects from code generation in jOOQ如何在 jOOQ 中从代码生成中动态排除对象
【发布时间】:2022-08-24 21:37:07
【问题描述】:

jOOQ 的代码生成器支持<includes><excludes> 元素通过使用静态正则表达式在代码生成中包含和排除对象。例如:

<configuration>
  <generator>
    <database>
      <includes>.*</includes>
      <excludes>
           UNUSED_TABLE                # This table (unqualified name) should not be generated
         | PREFIX_.*                   # Objects with a given prefix should not be generated
         | SECRET_SCHEMA\\.SECRET_TABLE # This table (qualified name) should not be generated
         | SECRET_ROUTINE              # This routine (unqualified name) ...
      </excludes>
    </database>
  </generator>
</configuration>

As documented in the manual。这些正则表达式是静态的。有没有办法动态包含或排除对象,例如基于表格的某些属性?例如,我想排除在 PostgreSQL 数据库中生成的所有视图。

这一直是其他论坛的常见问题,现在有一个答案,which is why I\'m documenting it here

    标签: java sql code-generation jooq


    【解决方案1】:

    从 jOOQ 3.17 和 #6489 开始,现在可以使用 SQL 查询动态构建包含/排除表达式!

    此查询将所有不是视图的表生成为正则表达式:

    select table_schema || '\.' || table_name
    from information_schema.tables
    where table_type != 'VIEW';
    

    结果包括:

    information_schema\.sql_features
    information_schema\.sql_implementation_info
    information_schema\.sql_parts
    information_schema\.sql_sizing
    ...
    

    现在可以将此查询单独添加到配置中,也可以添加到其他正则表达式中:

    <configuration>
      <generator>
        <database>
          <includes>.*</includes>
          <excludes>static_exclusions</excludes>
          <excludesSql>
            select table_schema || '\.' || table_name
            from information_schema.tables
            where table_type != 'VIEW'
          </excludesSql>
        </database>
      </generator>
    </configuration>
    

    该查询显然是特定于供应商的。如果正在使用不同的 RDBMS(例如 Oracle),则需要调整查询,例如查询SYS.ALL_TABLES

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 2019-06-23
      • 2016-09-13
      • 1970-01-01
      • 2012-03-22
      • 2014-09-09
      • 2017-12-03
      • 2017-08-13
      • 2020-01-23
      相关资源
      最近更新 更多