Skip to content

Commit

Permalink
Merge pull request #14 from ergon/feature/pojo-generation-metamodel
Browse files Browse the repository at this point in the history
Feature/pojo generation metamodel
  • Loading branch information
jmatj authored Dec 3, 2024
2 parents 5a2d564 + 81d1de2 commit 3818e67
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,59 @@
import java.nio.file.Path;

import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.Configuration;
import org.jooq.meta.jaxb.Database;
import org.jooq.meta.jaxb.Generate;
import org.jooq.meta.jaxb.Generator;
import org.jooq.meta.jaxb.Property;
import org.jooq.meta.jaxb.Strategy;
import org.jooq.meta.jaxb.Target;
import org.jooq.meta.jaxb.*;

public class AdamJooqMetamodelGenerator {

private String packageName;
private Path outputPath;
private String source;
private String jooqConfig;

public AdamJooqMetamodelGenerator(String packageName, Path outputPath, String source, String jooqConfig) {
this.packageName = packageName;
this.outputPath = outputPath;
this.source = source;
this.jooqConfig = jooqConfig;
}

public void run() throws Exception {
Configuration configuration;
if (jooqConfig != null) {
configuration = GenerationTool.load(new ByteArrayInputStream(jooqConfig.getBytes("UTF-8")));
} else {
configuration = buildConfiguration();
}
GenerationTool.generate(configuration);
}

private Configuration buildConfiguration() {
Configuration configuration;
configuration = new Configuration();

Generator generator = new Generator();
Database database = new Database();
database.setName(AdamDatabase.class.getName());
database.getProperties().add(new Property().withKey(AdamDatabase.SOURCE_PROPERTY).withValue(source));
generator.setDatabase(database);

Strategy strategy = new Strategy();
strategy.setName(TableSuffixGeneratorStrategy.class.getName());
generator.setStrategy(strategy);

Generate generate = new Generate();
generate.setGlobalObjectReferences(true);
generate.setJavaTimeTypes(true);
generator.setGenerate(generate);

Target target = new Target();
target.setPackageName(packageName);
target.setDirectory(outputPath.toString());
generator.setTarget(target);
configuration.setGenerator(generator);
return configuration;
}
private final String packageName;
private final Path outputPath;
private final String source;
private final String jooqConfig;

public AdamJooqMetamodelGenerator(String packageName, Path outputPath, String source, String jooqConfig) {
this.packageName = packageName;
this.outputPath = outputPath;
this.source = source;
this.jooqConfig = jooqConfig;
}

public void run() throws Exception {
Configuration configuration;
if (jooqConfig != null) {
configuration = GenerationTool.load(new ByteArrayInputStream(jooqConfig.getBytes("UTF-8")));
} else {
configuration = buildConfiguration();
}
GenerationTool.generate(configuration);
}

private Configuration buildConfiguration() {
Configuration configuration;
configuration = new Configuration();

Generator generator = new Generator();
Database database = new Database();
database.setName(AdamDatabase.class.getName());
database.getProperties().add(new Property().withKey(AdamDatabase.SOURCE_PROPERTY).withValue(source));
generator.setDatabase(database);

Strategy strategy = new Strategy();
strategy.setName(TableSuffixGeneratorStrategy.class.getName());
generator.setStrategy(strategy);

Generate generate = new Generate();
generate.setGlobalObjectReferences(true);
generate.setJavaTimeTypes(true);
generate.setPojos(true);
generate.setFluentSetters(true);
generator.setGenerate(generate);

Target target = new Target();
target.setPackageName(packageName);
target.setDirectory(outputPath.toString());
generator.setTarget(target);
configuration.setGenerator(generator);
return configuration;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;

import org.jooq.Name;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.meta.AbstractTableDefinition;
import org.jooq.meta.ColumnDefinition;
Expand All @@ -19,7 +20,7 @@

public class AdamTableDefinition extends AbstractTableDefinition {

private Table table;
private final Table table;

public AdamTableDefinition(SchemaDefinition schema, Table table) {
super(schema, table.getName(), null);
Expand All @@ -37,11 +38,9 @@ protected List<ColumnDefinition> getElements0() throws SQLException {
}

private DataTypeDefinition dataTypeDefinition(Field field) {
DataTypeDefinition dataType = new DefaultDataTypeDefinition(getDatabase(), getSchema(),
typeName(field.getDataType()), field.getLength(), field.getPrecision(), field.getScale(),
field.isNullable(), field.getDefaultValue(), (Name) null);

return dataType;
return new DefaultDataTypeDefinition(getDatabase(), getSchema(),
typeName(field.getDataType()), field.getLength(), field.getPrecision(), field.getScale(),
field.isNullable(), field.getDefaultValue(), getUserType(field));
}

private String typeName(DataType type) {
Expand Down Expand Up @@ -97,4 +96,10 @@ private boolean isIdentity(Field field) {
return field.isSequence();
}

private Name getUserType(Field field) {
if (field.getDbEnum() != null) {
return DSL.name(field.getDbEnum().getName());
}
return null;
}
}
5 changes: 5 additions & 0 deletions integration-test-db/src/main/resources/adam/schema/enums.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: test_status
values:
- CANCELLED
- ORDERED
- SHIPPED
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fields:
- name: "col5"
dataType: "VARCHAR"
nullable: true
- name: "col6"
dataType: ENUM
enumName: test_status
nullable: true
foreignKeys: []
indexes:
- name: "test_table_col1_key"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@

public class GenerateJooqMetamodelTest {

private static Path tempFolder;

@BeforeAll
public static void setupTempFolder() throws IOException {
tempFolder = Files.createTempDirectory("MetamodelTest");
tempFolder.toFile().deleteOnExit();
}

@Test
public void testMetamodelGeneration() {
AdamJooqMetamodelGenerator generator = new AdamJooqMetamodelGenerator("test", tempFolder,
"yml-classpath://" + DEFAULT_SCHEMA_PACKAGE, null);
try {
generator.run();
Path generated = tempFolder.resolve("test").resolve("tables").resolve("TestTableTable.java");
assertTrue(generated.toFile().exists(), "Metamodel for test table must be generated");
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
private static Path tempFolder;

@BeforeAll
public static void setupTempFolder() throws IOException {
tempFolder = Files.createTempDirectory("MetamodelTest");
tempFolder.toFile().deleteOnExit();
}

@Test
public void testMetamodelGeneration() {
AdamJooqMetamodelGenerator generator = new AdamJooqMetamodelGenerator("test", tempFolder,
"yml-classpath://" + DEFAULT_SCHEMA_PACKAGE, null);
try {
generator.run();
Path tableFilePath = tempFolder.resolve("test").resolve("tables").resolve("TestTableTable.java");
assertTrue(tableFilePath.toFile().exists(), "Metamodel for test table must be generated");

Path pojoFilePath = tempFolder.resolve("test").resolve("tables").resolve("pojos").resolve("TestTable.java");
assertTrue(pojoFilePath.toFile().exists(), "Metamodel for test table pojo must be generated");

Path enumFilePath = tempFolder.resolve("test").resolve("enums").resolve("TestStatus.java");
assertTrue(enumFilePath.toFile().exists(), "Metamodel for enums must be generated");
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
}

0 comments on commit 3818e67

Please sign in to comment.