合成枚举
适用于 ❌ 开源版 ✅ Express 版 ✅ 专业版 ✅ 企业版
jOOQ 的代码生成器支持从各种枚举文字源生成合成枚举,包括
- 文字的显式列表
- 任意 1 列投影 SQL 查询的结果
- 形式为
CHECK (col IN ('a', 'b'))
的简单CHECK
约束,或任何其他可以简化为 IN 谓词 的非规范形式,使用 基于模式的转换。
特别是,基于 CHECK
约束的配置非常强大!
enum TStatus implements EnumType { A, B, C; // [...] }
CREATE TABLE t ( status VARCHAR(10), CHECK (status IN ('A', 'B', 'C')) )
配置示例
XML(独立和 Maven)
编程方式
Gradle (Kotlin)
Gradle (Groovy)
Gradle(第三方)
<configuration> <generator> <database> <syntheticObjects> <enums> <enum> <!-- Optional name of the enum type in the schema. If left empty, the generated name is concatenated as TABLE_COLUMN --> <name>EnumName</name> <!-- Optional regular expression matching all tables that have this enum type. --> <tables>SCHEMA\.TABLE</tables> <!-- Regular expression matching all fields that have this enum type. --> <fields>STATUS</fields> <!-- An optional, defining comment of the enum --> <comment>An enumeration for the TABLE.STATUS column.</comment> <!-- The following are mutually exclusive settings. If none are provided, and the synthetic enum is named, then it will just reference a previously defined synthetic or actual enum type by name. --> <!-- Specify enum literals explicitly --> <literals> <literal>A</literal> <literal>B</literal> </literals> <!-- Specify a query that returns the distinct enum literals --> <literalSql>SELECT DISTINCT status FROM schema.table</literalSql> <!-- Fetch distinct enum literals from the column's content. This is the same as specifying literalSql to be SELECT DISTINCT matched_column FROM matched_table --> <literalsFromColumnContent>true</literalsFromColumnContent> <!-- The list of literals is parsed from the applicable CHECK constraints for the matched column, if possible. --> <literalsFromCheckConstraints>true</literalsFromCheckConstraints> </enum> </enums> </syntheticObjects> </database> </generator> </configuration>
有关更多详细信息,请参阅配置 XSD、独立代码生成和maven 代码生成。
new org.jooq.meta.jaxb.Configuration() .withGenerator(new Generator() .withDatabase(new Database() .withSyntheticObjects(new SyntheticObjectsType() .withEnums( new SyntheticEnumType() // Optional name of the enum type in the schema. If left empty, the generated name // is concatenated as TABLE_COLUMN .withName("EnumName") // Optional regular expression matching all tables that have this enum type. .withTables("SCHEMA\\.TABLE") // Regular expression matching all fields that have this enum type. .withFields("STATUS") // An optional, defining comment of the enum .withComment("An enumeration for the TABLE.STATUS column.") // The following are mutually exclusive settings. If none are provided, and the // synthetic enum is named, then it will just reference a previously defined // synthetic or actual enum type by name. .withLiterals( "A", "B" ) // Specify a query that returns the distinct enum literals .withLiteralSql("SELECT DISTINCT status FROM schema.table") // Fetch distinct enum literals from the column's content. // This is the same as specifying literalSql to be // // SELECT DISTINCT matched_column FROM matched_table .withLiteralsFromColumnContent(true) // The list of literals is parsed from the applicable CHECK constraints // for the matched column, if possible. .withLiteralsFromCheckConstraints(true) ) ) ) )
import org.jooq.meta.jaxb.* configuration { generator { database { syntheticObjects { enums { enum_ { // Optional name of the enum type in the schema. If left empty, the generated name // is concatenated as TABLE_COLUMN name = "EnumName" // Optional regular expression matching all tables that have this enum type. tables = "SCHEMA\\.TABLE" // Regular expression matching all fields that have this enum type. fields = "STATUS" // An optional, defining comment of the enum comment = "An enumeration for the TABLE.STATUS column." // The following are mutually exclusive settings. If none are provided, and the // synthetic enum is named, then it will just reference a previously defined // synthetic or actual enum type by name. literals { literal = "A" literal = "B" } // Specify a query that returns the distinct enum literals literalSql = "SELECT DISTINCT status FROM schema.table" // Fetch distinct enum literals from the column's content. // This is the same as specifying literalSql to be // // SELECT DISTINCT matched_column FROM matched_table isLiteralsFromColumnContent = true // The list of literals is parsed from the applicable CHECK constraints // for the matched column, if possible. isLiteralsFromCheckConstraints = true } } } } } }
有关更多详细信息,请参阅配置 XSD和gradle 代码生成。
configuration { generator { database { syntheticObjects { enums { enum_ { // Optional name of the enum type in the schema. If left empty, the generated name // is concatenated as TABLE_COLUMN name = "EnumName" // Optional regular expression matching all tables that have this enum type. tables = "SCHEMA\\.TABLE" // Regular expression matching all fields that have this enum type. fields = "STATUS" // An optional, defining comment of the enum comment = "An enumeration for the TABLE.STATUS column." // The following are mutually exclusive settings. If none are provided, and the // synthetic enum is named, then it will just reference a previously defined // synthetic or actual enum type by name. literals { literal = "A" literal = "B" } // Specify a query that returns the distinct enum literals literalSql = "SELECT DISTINCT status FROM schema.table" // Fetch distinct enum literals from the column's content. // This is the same as specifying literalSql to be // // SELECT DISTINCT matched_column FROM matched_table literalsFromColumnContent = true // The list of literals is parsed from the applicable CHECK constraints // for the matched column, if possible. literalsFromCheckConstraints = true } } } } } }
有关更多详细信息,请参阅配置 XSD和gradle 代码生成。
// The jOOQ-codegen-gradle plugin has been introduced in version 3.19. // Please use the official plugin instead of the third party plugin that was recommended before.
一如既往,当使用正则表达式时,它们是具有默认标志的正则表达式。
反馈
您对此页面有任何反馈吗? 我们很乐意听取您的意见!