Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add adamGenerateJooqMetamodel #13

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ gradle-plugin-test/gradle/
gradle-plugin-test/gradlew*
gradle.properties
local.properties
bin/
.settings/
.project
.classpath
1 change: 1 addition & 0 deletions gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sourceCompatibility = 21
dependencies {
implementation gradleApi()
implementation project(':core')
implementation group: 'org.jooq', name: 'jooq-codegen', version: '3.19.11'
}

apply from: "${rootProject.projectDir}/common.gradle"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
package ch.ergon.adam.gradleplugin;

import ch.ergon.adam.gradleplugin.tasks.*;
import javax.annotation.Nonnull;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;

import javax.annotation.Nonnull;
import ch.ergon.adam.gradleplugin.tasks.CleanDbTask;
import ch.ergon.adam.gradleplugin.tasks.ExportGitHistoryTask;
import ch.ergon.adam.gradleplugin.tasks.ExportMigrationScriptsTask;
import ch.ergon.adam.gradleplugin.tasks.ExportTargetVersionTask;
import ch.ergon.adam.gradleplugin.tasks.GenerateJooqMetamodelTask;
import ch.ergon.adam.gradleplugin.tasks.MigrateDBTask;

public class AdamPlugin implements Plugin<Project> {

public static final String ADAM_EXTENSION = "adam";
public static final String ADAM_EXTENSION = "adam";

@Override
public void apply(@Nonnull Project project) {

@Override
public void apply(@Nonnull Project project) {
project.getExtensions().create(ADAM_EXTENSION, AdamExtension.class, project);

project.getExtensions().create(ADAM_EXTENSION, AdamExtension.class, project);
project.getTasks().create("adamExportMigrationScripts", ExportMigrationScriptsTask.class)
.setGroup(ADAM_EXTENSION);

project.getTasks().create("adamExportMigrationScripts", ExportMigrationScriptsTask.class).setGroup(ADAM_EXTENSION);
project.getTasks().create("adamExportGitHistory", ExportGitHistoryTask.class).setGroup(ADAM_EXTENSION);

project.getTasks().create("adamExportGitHistory", ExportGitHistoryTask.class).setGroup(ADAM_EXTENSION);
project.getTasks().create("adamExportTargetVersion", ExportTargetVersionTask.class).setGroup(ADAM_EXTENSION);

project.getTasks().create("adamExportTargetVersion", ExportTargetVersionTask.class).setGroup(ADAM_EXTENSION);
project.getTasks().create("adamGenerateJooqMetamodel", GenerateJooqMetamodelTask.class)
.setGroup(ADAM_EXTENSION);

Task adamExportTask = project.getTasks().create("adamExport", Task.class);
adamExportTask.setGroup(ADAM_EXTENSION);
adamExportTask.dependsOn("adamExportTargetVersion", "adamExportGitHistory", "adamExportMigrationScripts");
Task adamExportTask = project.getTasks().create("adamExport", Task.class);
adamExportTask.setGroup(ADAM_EXTENSION);
adamExportTask.dependsOn("adamExportTargetVersion", "adamExportGitHistory", "adamExportMigrationScripts");

Task migrateDbTask = project.getTasks().create("adamMigrateDb", MigrateDBTask.class);
migrateDbTask.setGroup(ADAM_EXTENSION);
migrateDbTask.dependsOn(adamExportTask);
Task migrateDbTask = project.getTasks().create("adamMigrateDb", MigrateDBTask.class);
migrateDbTask.setGroup(ADAM_EXTENSION);
migrateDbTask.dependsOn(adamExportTask);

project.getTasks().create("adamCleanDb", CleanDbTask.class).setGroup(ADAM_EXTENSION);
}
project.getTasks().create("adamCleanDb", CleanDbTask.class).setGroup(ADAM_EXTENSION);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ch.ergon.adam.gradleplugin.tasks;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.gradle.api.DefaultTask;
import org.gradle.api.file.Directory;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;

import ch.ergon.adam.gradleplugin.util.AdamJooqMetamodelGenerator;

public class GenerateJooqMetamodelTask extends DefaultTask {

@Input
@Optional
private String jooqConfig;

@Input
private String packageName;

@Input
private String outputPath;

@Input
private String source;

@TaskAction
public void generateJooqMetamodel() throws Exception {
Path output = getProject().getLayout().getProjectDirectory().dir(outputPath).getAsFile().toPath();
AdamJooqMetamodelGenerator generator = new AdamJooqMetamodelGenerator(packageName, output, getSource(),
jooqConfig);
generator.run();
}

public String getJooqConfig() {
return jooqConfig;
}

public void setJooqConfig(String jooqConfig) {
this.jooqConfig = jooqConfig;
}

public String getPackageName() {
return packageName;
}

public void setPackageName(String packageName) {
this.packageName = packageName;
}

public String getOutputPath() {
return outputPath;
}

public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package ch.ergon.adam.gradleplugin.util;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.jooq.meta.AbstractDatabase;
import org.jooq.meta.ArrayDefinition;
import org.jooq.meta.CatalogDefinition;
import org.jooq.meta.DefaultEnumDefinition;
import org.jooq.meta.DefaultRelations;
import org.jooq.meta.DomainDefinition;
import org.jooq.meta.EnumDefinition;
import org.jooq.meta.PackageDefinition;
import org.jooq.meta.RoutineDefinition;
import org.jooq.meta.SchemaDefinition;
import org.jooq.meta.SequenceDefinition;
import org.jooq.meta.TableDefinition;
import org.jooq.meta.UDTDefinition;
import org.jooq.meta.XMLSchemaCollectionDefinition;

import ch.ergon.adam.core.db.SourceAndSinkFactory;
import ch.ergon.adam.core.db.schema.Field;
import ch.ergon.adam.core.db.schema.ForeignKey;
import ch.ergon.adam.core.db.schema.Index;
import ch.ergon.adam.core.db.schema.Schema;
import ch.ergon.adam.core.db.schema.Table;

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

@Override
protected DSLContext create0() {
return DSL.using(getConnection());
}

@Override
protected void loadPrimaryKeys(DefaultRelations r) throws SQLException {
handleIndexes(i -> i.isPrimary(), i -> {
TableDefinition td = getTable(i.getTable());
String name = i.getName();
for (Field field : i.getFields()) {
r.addPrimaryKey(name, td, td.getColumn(field.getName()));
}
});
}

@Override
protected void loadUniqueKeys(DefaultRelations r) throws SQLException {
handleIndexes(i -> i.isUnique() && !i.isPrimary(), i -> {
TableDefinition td = getTable(i.getTable());
String name = i.getName();
for (Field field : i.getFields()) {
r.addUniqueKey(name, td, td.getColumn(field.getName()));
}
});
}

private void handleIndexes(Predicate<Index> filter, Consumer<Index> consumer) {
for (Table table : schema.getTables()) {
TableDefinition td = getTable(table);
if (td == null) {
// table is excluded from build
continue;
}
table.getIndexes().stream().filter(filter).forEach(consumer);
}

}

@Override
protected void loadForeignKeys(DefaultRelations r) throws SQLException {
for (Table table : schema.getTables()) {
TableDefinition td = getTable(table);
if (td == null) {
// table is excluded from build
continue;
}
for (ForeignKey fkey : table.getForeignKeys()) {
TableDefinition target = getTable(fkey.getTargetIndex().getTable());
if (target != null) {
r.addForeignKey(fkey.getName(), td, td.getColumn(fkey.getField().getName()),
fkey.getTargetIndex().getName(), target);
}
}
}
}

@Override
protected void loadCheckConstraints(DefaultRelations r) throws SQLException {
// not supported
}

@Override
protected List<CatalogDefinition> getCatalogs0() throws SQLException {
ensureSchema();
return mutableList(new CatalogDefinition(this, "", ""));
}

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

@Override
protected List<SequenceDefinition> getSequences0() throws SQLException {
// not supported
return List.of();
}

@Override
protected List<TableDefinition> getTables0() throws SQLException {
return schema.getTables().stream().map(t -> (TableDefinition) new AdamTableDefinition(schemaDefinition, t))
.toList();
}

@Override
protected List<RoutineDefinition> getRoutines0() throws SQLException {
// not supported
return List.of();
}

@Override
protected List<PackageDefinition> getPackages0() throws SQLException {
// not supported
return List.of();
}

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
return schema.getEnums().stream().map(e -> {
DefaultEnumDefinition definition = new DefaultEnumDefinition(schemaDefinition, e.getName(), null);
definition.addLiterals(e.getValues());
return (EnumDefinition) definition;
}).toList();
}

@Override
protected List<DomainDefinition> getDomains0() throws SQLException {
// not supported
return List.of();
}

@Override
protected List<XMLSchemaCollectionDefinition> getXMLSchemaCollections0() throws SQLException {
// not supported
return List.of();
}

@Override
protected List<UDTDefinition> getUDTs0() throws SQLException {
// not supported
return List.of();
}

@Override
protected List<ArrayDefinition> getArrays0() throws SQLException {
// not supported
return List.of();
}

private TableDefinition getTable(Table table) {
return getTable(schemaDefinition, table.getName());
}

private <T> List<T> mutableList(T value) {
List<T> list = new ArrayList<>();
list.add(value);
return list;
}

private void ensureSchema() {
if (schema == null) {
String source = (String) getProperties().get(SOURCE_PROPERTY);
schema = SourceAndSinkFactory.getInstance().getSource(source).getSchema();
schemaDefinition = new SchemaDefinition(this, "", null);
}
}
}
Loading
Loading