如何将生成的代码限制为数据库中的表?

How do I restrict the generated code to tables in a database?

我目前正在评估 jooq。我对它很陌生。我想使用 jooq 访问 SQL Server 2008 R2 数据库。当我生成 类 时,codegen 工具开始为来自该服务器上所有数据库的对象生成代码,这不是我想要的。

我在配置文件中使用什么设置来限制该工具只能使用一个数据库?我检查了 documentation 并没有那么明显。好吧,为了限制 tables 我想我可以使用 <includes></includes><excludes></excludes>.

我能否使用相同的标签,通过使用包含三个部分 [database].[schema].[table] 的完全限定 table 名称,将工具限制为仅在某些数据库中的对象?

其他一些评论:

谢谢

您当前问题/评论的答案:

Could I use the same tags to restrict the tool to objects only in certain databases by using fully qualified table names with three parts [database].[schema].[table]?

是的,<includes/><excludes/> 元素匹配限定和非限定名称,因此您可以排除 database.schema.table(省略括号)。

It would be nice if the tool allowed specifying inclusion/exclusion rules per object type i.e. table, view, procedures , functions etc.

的确,这在路线图上:#5263

place classes for tables/views/procedures in separate packages, if possible, instead of lumping them all together.

您可以使用生成器策略覆盖当前行为:

One of databases had a schema \ and the code generated was invalid. Just make sure \ are generated to \ in strings.

是的,<includes/><excludes/> 元素(与代码生成器配置中的许多其他元素一样)采用 Java 具有所有关联语义的正则表达式。

更好地解决您的实际问题:

在 jOOQ 3.9 中,您可以在代码生成器中使用 catalog / schema mapping feature。有不同的配置方式:

只有一个输入目录(SQL 服务器调用数据库的标准名称)

这是最简单的入门配置。它将在单个数据库中生成所有内容:

<configuration>
  <generator>
    <database>
      <inputCatalog>database</inputCatalog>
      ...

您可以进一步将生成的输出减少到该数据库中的单个模式,例如:

<configuration>
  <generator>
    <database>
      <inputCatalog>database</inputCatalog>
      <inputSchema>schema</inputSchema>
      ...

多输入目录

在更复杂的设置中(或随着项目的增长),更合适的方法是明确列出所有目录和模式:

<configuration>
  <generator>
    <database>
      <catalogs>

        <!-- This configuration generates everything inside of that catalog -->
        <catalog>
          <inputCatalog>database1</inputCatalog>
        </catalog>

        <!-- This configuration generates only some schemas inside of the catalog -->
        <catalog>
          <inputCatalog>database2</inputCatalog>
          <schemata>
            <schema>
              <inputSchema>schema1</inputSchema>
            </schema>
            <schema>
              <inputSchema>schema2</inputSchema>
            </schema>
          </schemata>
        </catalog>
      ...

背景:

默认情况下,jOOQ 代码生成器始终生成它可以看到的所有内容。有两种方法可以明确限制:

  1. The code generator's catalog / schema mapping feature
  2. The standard <includes/> and <excludes/> regular expressions(适用于所有对象)

在 jOOQ 3.8 中,添加了对多个目录(在 SQL 服务器:数据库中)的代码生成支持,但没有添加通过目录映射功能限制它们的支持。这在 jOOQ 3.9 中得到了纠正,当时添加了目录映射 (#4794)。

#4794 之前,唯一的解决方法是使用 <excludes/> 从不需要的目录中排除所有内容,这导致 jOOQ 3.8 代码生成器仅生成空目录。

See also this discussion on the jOOQ User Group