我有一个特定Postgre模式,它收集所有表定义的类型,就像Status(name, description)这里的值可能是OPEN, Open item status,CLOSED, Closed item status等
Status(name, description)
OPEN, Open item status
CLOSED, Closed item status
我们需要获取所有这些表并基于它们生成枚举,以便以后在我们的应用程序中使用它。因此,这些枚举应如下所示:
enum Status { OPEN("Open item status"), CLOSED("Closed item status") ... }
我们决定使用看起来很有趣的JOOQ,但是我们找不到文档/示例来创建使用默认Java生成器行为以及自定义枚举生成扩展的自定义生成器。
基于此帖子,使用JOOQ从表生成枚举类,它带来了一些想法,但仍不清楚该怎么做才能实现答案所陈述的内容。
更新:为了让我的自定义生成器可以被jooq-codegen- maven插件选择,我创建了一个单独的项目,并将其jar添加为父项目的依赖项。我创建了一个类MyGenerator,并将其从扩展JavaGenerator。为了做到这一点,org.jooq.codegen.JavaGenerator我必须在maven依赖项下面添加(不加入spring- boot-starter-jooq):
jooq-codegen- maven
MyGenerator
JavaGenerator
org.jooq.codegen.JavaGenerator
spring- boot-starter-jooq
<dependency> <groupId>org.jooq</groupId> <artifactId>jooq-codegen-maven</artifactId> <version>3.11.2</version> </dependency>
完成此操作并从JavaGenerator继承后,根据发布的答案,我应该将其挂接到generate(SchemaDefinition)方法中,但这不是受保护的范围,因此我想这里缺少一些内容。
generate(SchemaDefinition)
您知道还是可以提供一个示例来描述如何为枚举编写这种代码生成器?我需要JOOQ像往常一样为特定模式生成代码,但除此之外,我还需要查询另一个仅包含那些“枚举”表的特定“枚举”模式。
我知道这是一个老问题,但是我发布了答案,因为它可能对其他人有用。
我不得不面对同样的需求,这很难实现,因此在这里您可以找到实现从enums架构生成枚举的代码。
enums
该代码是用groovy实现的,但是对于Java来说却非常相似。
首先也是 非常重要的一点 ,我必须为我的枚举生成器创建一个单独的项目,因为它将作为要使用它的项目的依赖项。这是 必需的, 因为生成代码的项目必须在编译时运行枚举生成器,因此实现此目的的方法是将枚举生成器添加为依赖项。
package com.ctg.jooqgenerator.jooq import org.jooq.codegen.JavaGenerator import org.jooq.codegen.JavaWriter import org.jooq.meta.Database import org.jooq.meta.SchemaDefinition import org.jooq.meta.TableDefinition import org.slf4j.Logger import org.slf4j.LoggerFactory import java.sql.ResultSet class EnumGenerator extends JavaGenerator { private static final String ENUMS_SCHEMA = "enums" private static final Logger log = LoggerFactory.getLogger(EnumGenerator.class) @Override void generateSchema(SchemaDefinition schema) { // Apply custom logic only for `enums` schema. Others schema has regular generation if (schema.name != ENUMS_SCHEMA) { super.generateSchema(schema) return } log.info("Generating enums") log.info("----------------------------------------------------------") Database db = schema.database db.getTables(schema).each { TableDefinition table -> // Prepare enum name from snake_case to CamelCase String enumName = table.name.replaceAll('_([a-z])') { it[1].capitalize() }.capitalize() JavaWriter out = newJavaWriter(new File(getFile(schema).getParentFile(), "${enumName}.java")) log.info("Generating enum: {}.java [input={}, output={}]", enumName, table.name, enumName) printPackage(out, schema) out.println("public enum $enumName {") ResultSet rs = db.connection.prepareStatement("SELECT * FROM ${schema}.\"${table.name}\"").executeQuery() while (rs.next()) { String name = rs.getString('name'), description = rs.getString('description'), s = rs.isLast() ? ";" : "," // Generate enum entry out.tab(1).println("$name(\"$description\")$s") } out.println(""" | private final String description; | | private $enumName(String description) { | this.description = description; | } |} """.stripMargin()) closeJavaWriter(out) } log.info("----------------------------------------------------------") super.generateSchema(schema) } }
将转换为枚举的表如下所示:
-- Table name `account_role` will be translated into `AccountRole` CREATE TABLE enums.account_role ( "name" varchar(100) NOT NULL, description varchar(255) NOT NULL, CONSTRAINT account_role_name_key UNIQUE (name) ); -- Table entries will be translated into enum entries INSERT INTO enums.account_role ("name",description) VALUES ('BILLING','Role for contact/address that will be a Billing contact/address'), ('PAYMENT','Role for contact/address that will be a Payment contact/address'), ('SERVICE','Role for contact/address that will be a Service contact/address'), ('SOLD_TO','Role for contact/address that will be a SoldTo contact/address') ;
此数据定义将导致以下自动生成的枚举AccountRole.java:
/* * This file is generated by jOOQ. */ package com.congerotechnology.ctgcommon.jooq.enums; public enum AccountRole { BILLING("Role for contact/address that will be a Billing contact/address"), PAYMENT("Role for contact/address that will be a Payment contact/address"), SERVICE("Role for contact/address that will be a Service contact/address"), SOLD_TO("Role for contact/address that will be a SoldTo contact/address"); private final String description; private AccountRole(String description) { this.description = description; } }
然后在将使用此枚举生成器的主项目上,我在上设置了以下maven代码pom.xml:
pom.xml
<dependencies> ... <!-- JOOQ custom generator --> <dependency> <groupId>com.ctg</groupId> <artifactId>ctg-jooq-generator</artifactId> <version>0.0.1</version> </dependency> ... </dependencies> <build> ... <plugins> <!-- JOOQ code generation --> <plugin> <groupId>org.jooq</groupId> <artifactId>jooq-codegen-maven</artifactId> <version>${jooq.version}</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <jdbc> <driver>org.postgresql.Driver</driver> <url>jdbc:postgresql://${env.DB_URL}</url> <user>${env.DB_USER}</user> <password>${env.DB_PASSWORD}</password> </jdbc> <generator> <name>com.ctg.ctgjooqgenerator.jooq.EnumGenerator</name> <database> <name>org.jooq.meta.postgres.PostgresDatabase</name> <includes>.*</includes> <excludes /> <dateAsTimestamp>true</dateAsTimestamp> <inputSchema>enums</inputSchema> </database> <generate> <deprecated>false</deprecated> <instanceFields>true</instanceFields> </generate> <target> <packageName>com.ctg.ctgcommon.jooq.enums</packageName> <directory>target/generated-sources/jooq-postgres</directory> </target> </generator> </configuration> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${postgresql.version}</version> </dependency> </dependencies> </plugin> </plugins>