Skip to content

Commit

Permalink
add schema as an input param for metamodel generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatj committed Dec 4, 2024
1 parent b941fb8 commit f0f3f16
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

public class GenerateJooqMetamodelTask extends DefaultTask {

@Input
@Optional
private String schemaName;

@Input
@Optional
private String jooqConfig;
Expand All @@ -26,11 +30,19 @@ public class GenerateJooqMetamodelTask extends DefaultTask {
@TaskAction
public void generateJooqMetamodel() throws Exception {
Path output = getProject().getLayout().getProjectDirectory().dir(outputPath).getAsFile().toPath();
AdamJooqMetamodelGenerator generator = new AdamJooqMetamodelGenerator(packageName, output, getSource(),
AdamJooqMetamodelGenerator generator = new AdamJooqMetamodelGenerator(schemaName, packageName, output, getSource(),
jooqConfig);
generator.run();
}

public String getSchemaName() {
return schemaName;
}

public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}

public String getJooqConfig() {
return jooqConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

public class AdamDatabase extends AbstractDatabase {
public static final String SOURCE_PROPERTY = "source";
public static final String SCHEMA_PROPERTY = "schema";
private Schema schema;
private SchemaDefinition schemaDefinition;

Expand Down Expand Up @@ -89,13 +90,13 @@ protected void loadCheckConstraints(DefaultRelations r) throws SQLException {
@Override
protected List<CatalogDefinition> getCatalogs0() throws SQLException {
ensureSchema();
return mutableList(new CatalogDefinition(this, "", ""));
return mutableList(schemaDefinition.getCatalog());
}

@Override
protected List<SchemaDefinition> getSchemata0() throws SQLException {
ensureSchema();
return mutableList(new SchemaDefinition(this, "", null));
return mutableList(schemaDefinition);
}

@Override
Expand Down Expand Up @@ -186,9 +187,10 @@ private <T> List<T> mutableList(T value) {

private void ensureSchema() {
if (schema == null) {
String source = (String) getProperties().get(SOURCE_PROPERTY);
String source = getProperties().getProperty(SOURCE_PROPERTY);
String schemaName = getProperties().getProperty(SCHEMA_PROPERTY, "");
schema = SourceAndSinkFactory.getInstance().getSource(source).getSchema();
schemaDefinition = new SchemaDefinition(this, "", null);
schemaDefinition = new SchemaDefinition(this, schemaName, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import java.io.ByteArrayInputStream;
import java.nio.file.Path;

import com.google.common.base.Strings;
import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.*;

public class AdamJooqMetamodelGenerator {

private final String schemaName;
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) {
public AdamJooqMetamodelGenerator(String schemaName, String packageName, Path outputPath, String source, String jooqConfig) {
this.schemaName = schemaName;
this.packageName = packageName;
this.outputPath = outputPath;
this.source = source;
Expand All @@ -38,6 +41,9 @@ private Configuration buildConfiguration() {
Database database = new Database();
database.setName(AdamDatabase.class.getName());
database.getProperties().add(new Property().withKey(AdamDatabase.SOURCE_PROPERTY).withValue(source));
if (!Strings.isNullOrEmpty(schemaName)) {
database.getProperties().add(new Property().withKey(AdamDatabase.SCHEMA_PROPERTY).withValue(schemaName));
}
generator.setDatabase(database);

Strategy strategy = new Strategy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private boolean isIdentity(Field field) {

private Name getUserType(Field field) {
if (field.getDbEnum() != null) {
return DSL.name(field.getDbEnum().getName());
return DSL.name(getSchema().getName(), field.getDbEnum().getName());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void setupTempFolder() throws IOException {

@Test
public void testMetamodelGeneration() {
AdamJooqMetamodelGenerator generator = new AdamJooqMetamodelGenerator("test", tempFolder,
AdamJooqMetamodelGenerator generator = new AdamJooqMetamodelGenerator("test-schema", "test", tempFolder,
"yml-classpath://" + DEFAULT_SCHEMA_PACKAGE, null);
try {
generator.run();
Expand All @@ -45,4 +45,25 @@ public void testMetamodelGeneration() {
fail(e.getMessage());
}
}

@Test
public void testSchemaGeneration() {
AdamJooqMetamodelGenerator defaultSchemaGenerator = new AdamJooqMetamodelGenerator(null, "test", tempFolder,
"yml-classpath://" + DEFAULT_SCHEMA_PACKAGE, null);

AdamJooqMetamodelGenerator generator = new AdamJooqMetamodelGenerator("test-schema", "test", tempFolder,
"yml-classpath://" + DEFAULT_SCHEMA_PACKAGE, null);
try {
defaultSchemaGenerator.run();
Path defaultSchemaPath = tempFolder.resolve("test").resolve("DefaultSchema.java");
assertTrue(defaultSchemaPath.toFile().exists(), "Metamodel for default schema must be generated");

generator.run();
Path testSchemaPath = tempFolder.resolve("test").resolve("TestSchema.java");
assertTrue(testSchemaPath.toFile().exists(), "Metamodel for test schema must be generated");
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
}

0 comments on commit f0f3f16

Please sign in to comment.