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 11, 2024
1 parent 708479d commit de6b01b
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package tech.jhipster.lite.generator.server.springboot.database.jooq.domain;

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 jooqGenerationPackage;
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);
Assert.notNull("jooqGenerationPackage", builder.jooqGenerationPackage);

this.database = builder.database;
this.databaseUrl = builder.databaseUrl;
this.user = builder.user;
this.inputSchema = builder.inputSchema;
this.jooqGenerationPackage = builder.jooqGenerationPackage;
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>%s</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
""",
databaseUrl,
user,
password,
databaseJooqName(),
inputSchema,
jooqGenerationPackage
);
}

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 {
JooqModuleCodegenConfigurationJooqGenerationPackageBuilder inputSchema(String inputSchema);
}

public interface JooqModuleCodegenConfigurationJooqGenerationPackageBuilder {
JooqModuleCodegenConfigurationBuilder jooqGenerationPackage(String jooqGenerationPackage);
}

public interface JooqModuleCodegenConfigurationPasswordBuilder {
JooqModuleCodegenConfiguration password(String password);
}

public static final class JooqModuleCodegenConfigurationBuilder
implements
JooqModuleCodegenConfigurationDatabaseBuilder,
JooqModuleCodegenConfigurationDatabaseUrlBuilder,
JooqModuleCodegenConfigurationUserBuilder,
JooqModuleCodegenConfigurationInputSchemaBuilder,
JooqModuleCodegenConfigurationJooqGenerationPackageBuilder,
JooqModuleCodegenConfigurationPasswordBuilder {

private DatabaseType database;
private String databaseUrl;
private String user;
private String inputSchema;
private String jooqGenerationPackage;
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 JooqModuleCodegenConfigurationJooqGenerationPackageBuilder inputSchema(String inputSchema) {
this.inputSchema = inputSchema;

return this;
}

@Override
public JooqModuleCodegenConfigurationBuilder jooqGenerationPackage(String jooqGenerationPackage) {
this.jooqGenerationPackage = jooqGenerationPackage;

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
@@ -1,6 +1,7 @@
package tech.jhipster.lite.generator.server.springboot.database.jooq.domain;

import static tech.jhipster.lite.generator.server.springboot.database.jooq.domain.CommonModuleBuilder.commonModuleBuilder;
import static tech.jhipster.lite.generator.server.springboot.database.jooq.domain.DatabaseType.*;
import static tech.jhipster.lite.module.domain.JHipsterModule.*;
import static tech.jhipster.lite.module.domain.JHipsterModule.lineBeforeText;

Expand All @@ -16,8 +17,13 @@

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_PACKAGE = "mysql";
private static final String MYSQL_GROUP_ID = "com.mysql";
private static final String MYSQL_ARTIFACT_ID = "mysql-connector-j";

Expand All @@ -37,7 +43,7 @@ public JHipsterModule buildPostgresql(JHipsterModuleProperties properties) {
//@formatter:off
return commonModuleBuilder(
properties,
DatabaseType.POSTGRESQL,
POSTGRESQL,
dockerImage,
documentationTitle("Postgresql"),
artifactId("postgresql")
Expand All @@ -53,11 +59,18 @@ 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.builder()
.database(POSTGRESQL)
.databaseUrl("jdbc:postgresql://localhost:5432/" + properties.projectBaseName().name())
.user(properties.projectBaseName().name())
.inputSchema("public")
.jooqGenerationPackage(properties.basePackage().basePackage())
.build()
.getConfiguration())
.build())
.and()
.springMainProperties()
Expand All @@ -84,7 +97,7 @@ public JHipsterModule buildMariaDB(JHipsterModuleProperties properties) {
//@formatter:off
return commonModuleBuilder(
properties,
DatabaseType.MARIADB,
MARIADB,
dockerImages.get("mariadb"),
documentationTitle("MariaDB"),
artifactId("mariadb")
Expand All @@ -96,11 +109,18 @@ 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.builder()
.database(MARIADB)
.databaseUrl("jdbc:mariadb://localhost:3306/" + properties.projectBaseName().name())
.user("root")
.inputSchema(properties.projectBaseName().name())
.jooqGenerationPackage(properties.basePackage().basePackage())
.build()
.getConfiguration())
.build())
.and()
.springMainProperties()
Expand All @@ -116,21 +136,28 @@ public JHipsterModule buildMySQL(JHipsterModuleProperties properties) {
//@formatter:off
return commonModuleBuilder(
properties,
DatabaseType.MYSQL,
dockerImages.get(MYSQL),
MYSQL,
dockerImages.get(MYSQL_PACKAGE),
documentationTitle("MySQL"),
artifactId(MYSQL)
artifactId(MYSQL_PACKAGE)
)
.javaDependencies()
.addDependency(javaDependency().groupId(MYSQL_GROUP_ID).artifactId(MYSQL_ARTIFACT_ID).scope(JavaDependencyScope.RUNTIME).build())
.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.builder()
.database(MYSQL)
.databaseUrl("jdbc:mysql://localhost:3306/" + properties.projectBaseName().name())
.user("root")
.inputSchema(properties.projectBaseName().name())
.jooqGenerationPackage(properties.basePackage().basePackage())
.build()
.getConfiguration())
.build())
.and()
.springMainProperties()
Expand All @@ -149,7 +176,7 @@ public JHipsterModule buildMsSQL(JHipsterModuleProperties properties) {
//@formatter:off
return commonModuleBuilder(
properties,
DatabaseType.MSSQL,
MSSQL,
dockerImage,
documentationTitle("MsSQL"),
artifactId("mssqlserver")
Expand All @@ -166,11 +193,18 @@ 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.builder()
.database(MSSQL)
.databaseUrl("jdbc:sqlserver://localhost:1433;database=" + properties.projectBaseName().name() + ";trustServerCertificate=true")
.user("SA")
.inputSchema("model")
.jooqGenerationPackage(properties.basePackage().basePackage())
.password(MSSQL_PASSWORD)
.getConfiguration())
.build())
.and()
.springMainProperties()
Expand All @@ -179,7 +213,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 +224,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 +240,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";
};
}
}
Loading

0 comments on commit de6b01b

Please sign in to comment.