From cd0a5457b8855b5848c389d64726589ae8b272e6 Mon Sep 17 00:00:00 2001 From: fabienpuissant Date: Sun, 20 Oct 2024 21:39:46 +0200 Subject: [PATCH] add jooq module with default maven codegen --- .../jooq/domain/JooqModuleFactory.java | 120 ++++++--------- .../lite/module/domain/JHipsterModule.java | 5 + .../domain/jooqplugin/DatabaseType.java | 10 ++ .../JooqModuleCodegenConfiguration.java | 140 ++++++++++++++++++ .../jooq/domain/JooqModuleFactoryTest.java | 2 +- 5 files changed, 203 insertions(+), 74 deletions(-) create mode 100644 src/main/java/tech/jhipster/lite/module/domain/jooqplugin/DatabaseType.java create mode 100644 src/main/java/tech/jhipster/lite/module/domain/jooqplugin/JooqModuleCodegenConfiguration.java diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactory.java index 777d2989b6b..e5c4cd550de 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactory.java @@ -16,6 +16,11 @@ public class JooqModuleFactory { + public static final String GENERATE = "generate"; + public static final String JOOQ_CODEGEN = "jooq-codegen"; + public static final String JOOQ_CODEGEN_MAVEN = "jooq-codegen-maven"; + public static final String ORG_JOOQ = "org.jooq"; + public static final String MSSQL_PASSWORD = "yourStrong(!)Password"; public static final String ORG_POSTGRESQL = "org.postgresql"; private static final String MYSQL = "mysql"; private static final String MYSQL_GROUP_ID = "com.mysql"; @@ -53,11 +58,17 @@ public JHipsterModule buildPostgresql(JHipsterModuleProperties properties) { .and() .mavenPlugins() .plugin(mavenPlugin() - .groupId("org.jooq") - .artifactId("jooq-codegen-maven") + .groupId(ORG_JOOQ) + .artifactId(JOOQ_CODEGEN_MAVEN) .versionSlug("jooq") - .addExecution(pluginExecution().goals("generate").id("jooq-codegen").phase(MavenBuildPhase.GENERATE_RESOURCES)) - .configuration(jooqCodegenPluginConfiguration("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name(), DatabaseType.POSTGRESQL, properties.projectBaseName().name(), "", "public")) + .addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES)) + .configuration(jooqModuleCodegenConfiguration() + .database(tech.jhipster.lite.module.domain.jooqplugin.DatabaseType.POSTGRESQL) + .databaseUrl("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name()) + .user(properties.projectBaseName().name()) + .inputSchema("public") + .build() + .getConfiguration()) .build()) .and() .springMainProperties() @@ -96,11 +107,17 @@ public JHipsterModule buildMariaDB(JHipsterModuleProperties properties) { .and() .mavenPlugins() .plugin(mavenPlugin() - .groupId("org.jooq") - .artifactId("jooq-codegen-maven") + .groupId(ORG_JOOQ) + .artifactId(JOOQ_CODEGEN_MAVEN) .versionSlug("jooq") - .addExecution(pluginExecution().goals("generate").id("jooq-codegen").phase(MavenBuildPhase.GENERATE_RESOURCES)) - .configuration(jooqCodegenPluginConfiguration("jdbc:mariadb://localhost:3306/" + properties.projectBaseName().name(), DatabaseType.MARIADB, "root", "", properties.projectBaseName().name())) + .addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES)) + .configuration(jooqModuleCodegenConfiguration() + .database(tech.jhipster.lite.module.domain.jooqplugin.DatabaseType.MARIADB) + .databaseUrl("jdbc:mariadb://localhost:3306/" + properties.projectBaseName().name()) + .user("root") + .inputSchema("properties.projectBaseName().name()") + .build() + .getConfiguration()) .build()) .and() .springMainProperties() @@ -126,11 +143,17 @@ public JHipsterModule buildMySQL(JHipsterModuleProperties properties) { .and() .mavenPlugins() .plugin(mavenPlugin() - .groupId("org.jooq") - .artifactId("jooq-codegen-maven") + .groupId(ORG_JOOQ) + .artifactId(JOOQ_CODEGEN_MAVEN) .versionSlug("jooq") - .addExecution(pluginExecution().goals("generate").id("jooq-codegen").phase(MavenBuildPhase.GENERATE_RESOURCES)) - .configuration(jooqCodegenPluginConfiguration("jdbc:mysql://localhost:3306/" + properties.projectBaseName().name(), DatabaseType.MYSQL, "root", "", properties.projectBaseName().name())) + .addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES)) + .configuration(jooqModuleCodegenConfiguration() + .database(tech.jhipster.lite.module.domain.jooqplugin.DatabaseType.MYSQL) + .databaseUrl("jdbc:mysql://localhost:3306/" + properties.projectBaseName().name()) + .user("root") + .inputSchema("properties.projectBaseName().name()") + .build() + .getConfiguration()) .build()) .and() .springMainProperties() @@ -166,11 +189,17 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { .and() .mavenPlugins() .plugin(mavenPlugin() - .groupId("org.jooq") - .artifactId("jooq-codegen-maven") + .groupId(ORG_JOOQ) + .artifactId(JOOQ_CODEGEN_MAVEN) .versionSlug("jooq") - .addExecution(pluginExecution().goals("generate").id("jooq-codegen").phase(MavenBuildPhase.GENERATE_RESOURCES)) - .configuration(jooqCodegenPluginConfiguration("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true", DatabaseType.MSSQL, "SA", "yourStrong(!)Password", "model")) + .addExecution(pluginExecution().goals(GENERATE).id(JOOQ_CODEGEN).phase(MavenBuildPhase.GENERATE_RESOURCES)) + .configuration(jooqModuleCodegenConfiguration() + .database(tech.jhipster.lite.module.domain.jooqplugin.DatabaseType.MSSQL) + .databaseUrl("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true") + .user("SA") + .inputSchema("model") + .password(MSSQL_PASSWORD) + .getConfiguration()) .build()) .and() .springMainProperties() @@ -179,7 +208,7 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { propertyValue("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true") ) .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("SA")) - .set(propertyKey("spring.datasource.password"), propertyValue("yourStrong(!)Password")) + .set(propertyKey("spring.datasource.password"), propertyValue(MSSQL_PASSWORD)) .set(propertyKey(SPRING_DATASOURCE_DRIVER_CLASS_NAME), propertyValue("com.microsoft.sqlserver.jdbc.SQLServerDriver")) .and() .springTestProperties() @@ -190,7 +219,7 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { ) ) .set(propertyKey(SPRING_DATASOURCE_USERNAME), propertyValue("SA")) - .set(propertyKey("spring.datasource.password"), propertyValue("yourStrong(!)Password")) + .set(propertyKey("spring.datasource.password"), propertyValue(MSSQL_PASSWORD)) .and() .mandatoryReplacements() .in(path("src/test/java").append(properties.basePackage().path()).append("IntegrationTest.java")) @@ -206,59 +235,4 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) { .build(); //@formatter:on } - - //TODO Create an API for MavenPluginConfiguration with a builder - private String jooqCodegenPluginConfiguration( - String databaseUrl, - DatabaseType databaseType, - String user, - String password, - String inputSchema - ) { - return String.format( - """ - - %s - %s - %s - %s - - - - %s - .* - %s - - - org.jooq.codegen - target/generated-sources/jooq - - - """, - databaseDriver(databaseType), - databaseUrl, - user, - password, - databaseJooqName(databaseType), - inputSchema - ); - } - - private static String databaseJooqName(DatabaseType databaseType) { - return switch (databaseType) { - case POSTGRESQL -> "org.jooq.meta.postgres.PostgresDatabase"; - case MYSQL -> "org.jooq.meta.mysql.MySQLDatabase"; - case MARIADB -> "org.jooq.meta.mariadb.MariaDBDatabase"; - case MSSQL -> "org.jooq.meta.sqlserver.SQLServerDatabase"; - }; - } - - private static String databaseDriver(DatabaseType databaseType) { - return switch (databaseType) { - case POSTGRESQL -> "org.postgresql.Driver"; - case MARIADB -> "org.mariadb.jdbc.Driver"; - case MYSQL -> "com.mysql.jdbc.Driver"; - case MSSQL -> "com.microsoft.sqlserver.jdbc.SQLServerDriver"; - }; - } } diff --git a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java index 868add69bfb..0d4f881c663 100644 --- a/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java +++ b/src/main/java/tech/jhipster/lite/module/domain/JHipsterModule.java @@ -37,6 +37,7 @@ import tech.jhipster.lite.module.domain.javaproperties.*; import tech.jhipster.lite.module.domain.javaproperties.JHipsterModuleSpringFactories.JHipsterModuleSpringFactoriesBuilder; import tech.jhipster.lite.module.domain.javaproperties.JHipsterModuleSpringProperties.JHipsterModuleSpringPropertiesBuilder; +import tech.jhipster.lite.module.domain.jooqplugin.JooqModuleCodegenConfiguration; import tech.jhipster.lite.module.domain.mavenplugin.*; import tech.jhipster.lite.module.domain.mavenplugin.JHipsterModuleMavenPlugins.JHipsterModuleMavenPluginsBuilder; import tech.jhipster.lite.module.domain.mavenplugin.MavenPlugin.MavenPluginGroupIdBuilder; @@ -190,6 +191,10 @@ public static JavaDependencyGroupIdBuilder javaDependency() { return JavaDependency.builder(); } + public static JooqModuleCodegenConfiguration.JooqModuleCodegenConfigurationBuilder jooqModuleCodegenConfiguration() { + return JooqModuleCodegenConfiguration.builder(); + } + public static MavenBuildExtensionGroupIdBuilder mavenBuildExtension() { return MavenBuildExtension.builder(); } diff --git a/src/main/java/tech/jhipster/lite/module/domain/jooqplugin/DatabaseType.java b/src/main/java/tech/jhipster/lite/module/domain/jooqplugin/DatabaseType.java new file mode 100644 index 00000000000..1484212f297 --- /dev/null +++ b/src/main/java/tech/jhipster/lite/module/domain/jooqplugin/DatabaseType.java @@ -0,0 +1,10 @@ +package tech.jhipster.lite.module.domain.jooqplugin; + +//shared enum ?? + +public enum DatabaseType { + POSTGRESQL, + MYSQL, + MARIADB, + MSSQL, +} diff --git a/src/main/java/tech/jhipster/lite/module/domain/jooqplugin/JooqModuleCodegenConfiguration.java b/src/main/java/tech/jhipster/lite/module/domain/jooqplugin/JooqModuleCodegenConfiguration.java new file mode 100644 index 00000000000..bc18291c9dc --- /dev/null +++ b/src/main/java/tech/jhipster/lite/module/domain/jooqplugin/JooqModuleCodegenConfiguration.java @@ -0,0 +1,140 @@ +package tech.jhipster.lite.module.domain.jooqplugin; + +import tech.jhipster.lite.shared.error.domain.Assert; + +public final class JooqModuleCodegenConfiguration { + + private final DatabaseType database; + private final String databaseUrl; + private final String user; + private final String inputSchema; + private final String password; + + private JooqModuleCodegenConfiguration(JooqModuleCodegenConfigurationBuilder builder) { + Assert.notNull("database", builder.database); + Assert.notNull("databaseUrl", builder.databaseUrl); + Assert.notNull("user", builder.user); + Assert.notNull("inputSchema", builder.inputSchema); + + this.database = builder.database; + this.databaseUrl = builder.databaseUrl; + this.user = builder.user; + this.inputSchema = builder.inputSchema; + this.password = builder.password == null ? "" : builder.password; + } + + public String getConfiguration() { + return String.format( + """ + + %s + %s + %s + + + + %s + .* + %s + + + org.jooq.codegen + target/generated-sources/jooq + + + """, + databaseUrl, + user, + password, + databaseJooqName(), + inputSchema + ); + } + + private String databaseJooqName() { + return switch (database) { + case POSTGRESQL -> "org.jooq.meta.postgres.PostgresDatabase"; + case MYSQL -> "org.jooq.meta.mysql.MySQLDatabase"; + case MARIADB -> "org.jooq.meta.mariadb.MariaDBDatabase"; + case MSSQL -> "org.jooq.meta.sqlserver.SQLServerDatabase"; + }; + } + + public static JooqModuleCodegenConfigurationBuilder builder() { + return new JooqModuleCodegenConfigurationBuilder(); + } + + public interface JooqModuleCodegenConfigurationDatabaseBuilder { + JooqModuleCodegenConfigurationDatabaseUrlBuilder database(DatabaseType database); + } + + public interface JooqModuleCodegenConfigurationDatabaseUrlBuilder { + JooqModuleCodegenConfigurationUserBuilder databaseUrl(String databaseUrl); + } + + public interface JooqModuleCodegenConfigurationUserBuilder { + JooqModuleCodegenConfigurationInputSchemaBuilder user(String user); + } + + public interface JooqModuleCodegenConfigurationInputSchemaBuilder { + JooqModuleCodegenConfigurationBuilder inputSchema(String inputSchema); + } + + public interface JooqModuleCodegenConfigurationPasswordBuilder { + JooqModuleCodegenConfiguration password(String password); + } + + public static final class JooqModuleCodegenConfigurationBuilder + implements + JooqModuleCodegenConfigurationDatabaseBuilder, + JooqModuleCodegenConfigurationDatabaseUrlBuilder, + JooqModuleCodegenConfigurationUserBuilder, + JooqModuleCodegenConfigurationInputSchemaBuilder, + JooqModuleCodegenConfigurationPasswordBuilder { + + private DatabaseType database; + private String databaseUrl; + private String user; + private String inputSchema; + private String password; + + @Override + public JooqModuleCodegenConfigurationDatabaseUrlBuilder database(DatabaseType database) { + this.database = database; + + return this; + } + + @Override + public JooqModuleCodegenConfigurationUserBuilder databaseUrl(String databaseUrl) { + this.databaseUrl = databaseUrl; + + return this; + } + + @Override + public JooqModuleCodegenConfigurationInputSchemaBuilder user(String user) { + this.user = user; + + return this; + } + + @Override + public JooqModuleCodegenConfigurationBuilder inputSchema(String inputSchema) { + this.inputSchema = inputSchema; + + return this; + } + + @Override + public JooqModuleCodegenConfiguration password(String password) { + this.password = password; + + return build(); + } + + public JooqModuleCodegenConfiguration build() { + return new JooqModuleCodegenConfiguration(this); + } + } +} diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactoryTest.java index 258ea11b624..0773df34c68 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/database/jooq/domain/JooqModuleFactoryTest.java @@ -19,7 +19,7 @@ @UnitTest @ExtendWith(MockitoExtension.class) -public class JooqModuleFactoryTest { +class JooqModuleFactoryTest { @Mock private DockerImages dockerImages;