Skip to content

Commit

Permalink
♻️ java instead of xml in jooq codegen #198
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Feb 3, 2024
1 parent 39b32c9 commit 1064438
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 8 deletions.
2 changes: 1 addition & 1 deletion observe/docs
2 changes: 1 addition & 1 deletion observe/meepo
Submodule meepo updated 0 files
2 changes: 1 addition & 1 deletion observe/mirana
Submodule mirana updated 0 files
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import org.jooq.meta.jaxb.Configuration;
import org.jooq.meta.jaxb.Database;
import org.jooq.meta.jaxb.ForcedType;
import org.jooq.meta.jaxb.Generate;
import org.jooq.meta.jaxb.Generator;
import org.jooq.meta.jaxb.Jdbc;
import org.jooq.meta.jaxb.Logging;
import org.jooq.meta.jaxb.Strategy;
import org.jooq.meta.jaxb.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -48,7 +54,6 @@
*/
public class WingsCodeGenerator {

public static final String JOOQ_XML = "/wings-flywave/jooq-codegen-faceless.xml";
private static final Logger log = LoggerFactory.getLogger(WingsCodeGenerator.class);

/**
Expand All @@ -59,7 +64,8 @@ public class WingsCodeGenerator {
* @param suffix Add suffixes to DefaultCatalog, DefaultSchema and Global to distinguish generation.
*/

@SuppressWarnings("resource") public static void generate(Configuration conf, boolean incremental, String suffix) {
@SuppressWarnings("resource")
public static void generate(Configuration conf, boolean incremental, String suffix) {
if (conf == null) {
conf = config();
}
Expand Down Expand Up @@ -101,9 +107,88 @@ public static Builder builder(Configuration conf) {
return new Builder(conf == null ? config() : conf);
}

/**
* the default wings mysql configuration
*/
@NotNull
public static Configuration config() {
return config(WingsCodeGenerator.class.getResourceAsStream(JOOQ_XML));
Configuration conf = new Configuration();
conf.withLogging(Logging.INFO);
conf.withJdbc(new Jdbc()
.withDriver("com.mysql.cj.jdbc.Driver")
.withUrl("!!your-config-here!!")
.withUser("!!your-config-here!!")
.withPassword("!!your-config-here!!"));
conf.withGenerator(new Generator()
.withName(WingsJavaGenerator.class.getName())
.withTarget(new Target()
.withPackageName("!!your-config-here!!")
.withDirectory("!!your-config-here!!"))
.withDatabase(new Database()
.withInputSchema("!!your-config-here!!")
.withOutputCatalogToDefault(true)
.withOutputSchemaToDefault(true)
.withIncludes(".*")
.withExcludes("""
spring.* # Spring table
|.*__[a-z]* # journal table
|.*\\$[a-z]* # journal table
|sys_commit_journal # jdbc handled
|sys_light_sequence # jdbc handled
|sys_schema_journal # jdbc handled
|sys_schema_version # jdbc handled
""")
.withSchemaVersionProvider("SELECT MAX(revision) FROM sys_schema_version WHERE apply_dt > '1000-01-01'")
.withIncludeTables(true)
.withIncludeRoutines(false)
.withIncludePackages(false)
.withIncludePackageRoutines(false)
.withIncludePackageUDTs(false)
.withIncludePackageConstants(false)
.withIncludeUDTs(false)
.withIncludeSequences(false)
.withIncludePrimaryKeys(true)
.withIncludeUniqueKeys(false)
.withIncludeForeignKeys(false)
.withIncludeCheckConstraints(false)
.withIncludeIndexes(false)
.withForcedTypes(
new ForcedType().withName("BOOLEAN").withIncludeTypes("TINYINT(\\(1\\))?"),
new ForcedType().withName("INTEGER").withIncludeTypes("TINYINT[2-9()]*")
)
)
.withGenerate(new Generate()
.withComments(false)
.withEmptyCatalogs(true)
.withEmptySchemas(true)
.withIndexes(false)
// JavaEE / JakartaEE
.withSpringAnnotations(true)
.withJpaAnnotations(false)

.withValidationAnnotations(false)
.withJavaTimeTypes(true)
.withKeys(false)
.withInterfaces(true)
.withDaos(true)
.withPojos(true)
.withPojosEqualsAndHashCode(true)
.withPojosToString(true)
// Don't open it. or the JournalAware will not work well. column editing is good for setter assignment.
.withFluentSetters(false)

.withGlobalCatalogReferences(false)
.withGlobalSchemaReferences(false)
.withGlobalTableReferences(true)
.withGlobalSequenceReferences(false)
.withGlobalUDTReferences(false)
.withGlobalRoutineReferences(false)
.withGlobalQueueReferences(false)
.withGlobalLinkReferences(false)
)
.withStrategy(new Strategy().withName(WingsJavaStrategy.class.getName()))
);
return conf;
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pro.fessional.wings.faceless.jooqgen;

import lombok.extern.slf4j.Slf4j;
import org.jooq.meta.jaxb.Configuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* @author trydofor
* @since 2024-02-01
*/
@Slf4j
class WingsCodeGeneratorTest {

@Test
void config() {
Configuration c1 = WingsCodeGenerator.config();
Configuration c2 = WingsCodeGenerator.config(WingsCodeGeneratorTest.class.getResourceAsStream("/wings-flywave/jooq-codegen-faceless.xml"));
String s1 = c1.toString();
String s2 = c2.toString();
log.debug("java config={}", s1);
log.debug("xml config={}", s2);
Assertions.assertEquals(format(s1), format(s2));
}

String format(String str){
return str.replaceAll("\\s+", "").replace("<","\n<");
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.17.0.xsd">
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.18.0.xsd">
<logging>INFO</logging>
<jdbc>
<driver>!!your-config-here!!</driver>
<driver>com.mysql.cj.jdbc.Driver</driver>
<url>!!your-config-here!!</url>
<user>!!your-config-here!!</user>
<password>!!your-config-here!!</password>
Expand Down

0 comments on commit 1064438

Please sign in to comment.