Skip to content

Commit

Permalink
add jooq module with default maven codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienpuissant committed Nov 14, 2024
1 parent c63bb4c commit 42678fa
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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"))
Expand All @@ -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(
"""
<jdbc>
<driver>%s</driver>
<url>%s</url>
<user>%s</user>
<password>%s</password>
</jdbc>
<generator>
<database>
<name>%s</name>
<includes>.*</includes>
<inputSchema>%s</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
""",
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";
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tech.jhipster.lite.module.domain.jooqplugin;

//shared enum ??

public enum DatabaseType {
POSTGRESQL,
MYSQL,
MARIADB,
MSSQL,
}
Original file line number Diff line number Diff line change
@@ -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(
"""
<jdbc>
<url>%s</url>
<user>%s</user>
<password>%s</password>
</jdbc>
<generator>
<database>
<name>%s</name>
<includes>.*</includes>
<inputSchema>%s</inputSchema>
</database>
<target>
<packageName>org.jooq.codegen</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
""",
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@UnitTest
@ExtendWith(MockitoExtension.class)
public class JooqModuleFactoryTest {
class JooqModuleFactoryTest {

@Mock
private DockerImages dockerImages;
Expand Down

0 comments on commit 42678fa

Please sign in to comment.