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

Implement import/export feature #9

Merged
merged 2 commits into from
Dec 29, 2023
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
72 changes: 69 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Modify your pom.xml as bellow and adjust necessary parameters:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<version>2.2.224</version>
</dependency>
</dependencies>
</plugin>
Expand Down Expand Up @@ -75,7 +75,8 @@ Optionally if you need to attach generated code to your source code add this:
</plugin>
```

Configuration params:
### Configuration params

- __jdbcUrl__
- __jdbcClassName__
- __jdbcUser__
Expand All @@ -85,17 +86,82 @@ Configuration params:
- __catalog__ can be blank, mostly it's database
- __tableNamePattern__ supports %_, can be blank, selects all tables
- __types__ one of TABLE, VIEW, SYSTEM_TABLE, GLOBAL_TEMPORARY, LOCAL_TEMPORARY, ALIAS, SYNONYM, can be blank
- __exportFile__ file to export metadata, which later can be imported from that file, instead of fetching from DB
- __importFile__ metadata file previously exported with exportFile, which can be used as source of metadata
- __baseDir__ where to output generated source, can be ${project.baseDir}
- __targetFolder__ where to put sources under baseDir, can be target/generated-sources
- __targetPackage__ what package should be used for generated classes
- __ext__ extension for generated files, defaults to .java
- __dateImpl__ what java date implementation should be used: UTIL_DATE or LOCAL_DATE
- __includeGenerationInfo__ should info about generation be included? Defaults to false

You can customize generation template, by providing __templates__ list.
You can customize generation template, by providing __templates__ list:
```xml
<templates>
<template>some-template.mustache</template>
</templates>
```

Currently built-in templates are: __pojo.mustache__, __spring-data.mustache__.
You can provide your own template:

```xml
<templates>
<template>${project.baseUri}/my-custom.mustache</template>
</templates>
```


You can provide multiple executions with different id's to select from different schemas, providing different templates or some other config options.

Check https://github.com/alberlau/DB2Code/tree/master/java-pojo-generator-mojo-example and see example usage.

To see what is exposed into model, check: https://github.com/alberlau/DB2Code/tree/master/core/src/main/java/org/db2code/rawmodel classes
along with adapter class: https://github.com/alberlau/DB2Code/blob/master/java-pojo-generator/src/main/java/org/db2code/generator/java/pojo/adapter/JavaClassAdapter.java

### Use case: export/import from metadata file

To exportMetadata, add to configuration/extractionParameters exportFile

```xml
<execution>
<id>exportMetadata</id>
<goals>
<goal>generatePojo</goal>
</goals>
<configuration>
<includeGenerationInfo>true</includeGenerationInfo>
<jdbcUrl>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM '${project.basedir}/init.sql'</jdbcUrl>
<jdbcClassName>org.h2.Driver</jdbcClassName>
<extractionParameters>
<item>
<schemaPattern>TEST_SCHEMA</schemaPattern>
<catalog>TEST</catalog>
<exportFile>${basedir}/target/classes/TEST_SCHEMA.json</exportFile>
</item>
</extractionParameters>
</configuration>
</execution>
```
Above can be added under separate maven profile.

Then metadata file can be imported with such execution:
```xml
<execution>
<id>importFromExportedMetadata</id>
<goals>
<goal>generatePojo</goal>
</goals>
<configuration>
<includeGenerationInfo>true</includeGenerationInfo>
<extractionParameters>
<item>
<importFile>${basedir}/target/classes/TEST_SCHEMA.json</importFile>
</item>
</extractionParameters>
<baseDir>${project.basedir}</baseDir>
<targetFolder>target/generated-sources</targetFolder>
<targetPackage>com.mypkg</targetPackage>
</configuration>
</execution>
```
4 changes: 0 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,23 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/db2code/MetadataExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.db2code.extractors.ColumnExtractor;
import org.db2code.extractors.ExtractionParameters;
import org.db2code.extractors.DatabaseExtractionParameters;
import org.db2code.extractors.TableExtractor;
import org.db2code.rawmodel.RawColumn;
import org.db2code.rawmodel.RawDatabaseMetadata;
Expand All @@ -19,7 +19,7 @@ public MetadataExtractor(ConnectionProvider connectionProvider) {
this.connectionProvider = connectionProvider;
}

public RawDatabaseMetadata extract(ExtractionParameters extractionParameters) {
public RawDatabaseMetadata extract(DatabaseExtractionParameters extractionParameters) {
DatabaseMetaData databaseMetaData;
try {
databaseMetaData = connectionProvider.getConnection().getMetaData();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.db2code.convert;

public class JavaPropertyConverter {
public final class JavaPropertyConverter {

private JavaPropertyConverter() {
// Utility class
}

public static String camelCaseFromSnakeCaseInitCap(String columnName) {
return camelCaseFromSnakeCase(columnName, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.apache.commons.beanutils.PropertyUtils;
import org.db2code.rawmodel.AbstractRawItem;

public abstract class AbstractExtractor {
public abstract class AbstractExtractor<T extends ExtractionParameters> {
protected void setProperty(Object object, Object mdValue, String propName) {
try {
PropertyUtils.setProperty(object, propName, mdValue);
Expand All @@ -16,5 +16,5 @@ protected void setProperty(Object object, Object mdValue, String propName) {
}

public abstract List<? extends AbstractRawItem> extract(
DatabaseMetaData databaseMetaData, ExtractionParameters params);
DatabaseMetaData databaseMetaData, T params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import org.db2code.md.ColumnMetadata;
import org.db2code.rawmodel.RawColumn;

public class ColumnExtractor extends AbstractExtractor {
public List<RawColumn> extract(DatabaseMetaData databaseMetaData, ExtractionParameters params) {
public class ColumnExtractor extends AbstractExtractor<DatabaseExtractionParameters> {
public List<RawColumn> extract(
DatabaseMetaData databaseMetaData, DatabaseExtractionParameters params) {
try {
return _extract(databaseMetaData, params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private List<RawColumn> _extract(DatabaseMetaData databaseMetaData, ExtractionParameters params)
private List<RawColumn> _extract(
DatabaseMetaData databaseMetaData, DatabaseExtractionParameters params)
throws SQLException {
List<RawColumn> results = new ArrayList<>();
try (ResultSet columns =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.db2code.extractors;

import lombok.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DatabaseExtractionParameters implements ExtractionParameters {
private String catalog;
private String schemaPattern;
private String tableNamePattern;
private String[] types;
private String exportFile;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
package org.db2code.extractors;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExtractionParameters {
private String catalog;
private String schemaPattern;
private String tableNamePattern;
private String[] types;
}
public interface ExtractionParameters {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.db2code.extractors;

import lombok.Data;
import lombok.RequiredArgsConstructor;

@Data
@RequiredArgsConstructor
public class MetadataFileExtractionParameters implements ExtractionParameters {
private final String metadataFile;
}
8 changes: 5 additions & 3 deletions core/src/main/java/org/db2code/extractors/TableExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
import org.db2code.rawmodel.RawTable;

@Slf4j
public class TableExtractor extends AbstractExtractor {
public List<RawTable> extract(DatabaseMetaData databaseMetaData, ExtractionParameters params) {
public class TableExtractor extends AbstractExtractor<DatabaseExtractionParameters> {
public List<RawTable> extract(
DatabaseMetaData databaseMetaData, DatabaseExtractionParameters params) {
try {
return _extract(databaseMetaData, params);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

private List<RawTable> _extract(DatabaseMetaData databaseMetaData, ExtractionParameters params)
private List<RawTable> _extract(
DatabaseMetaData databaseMetaData, DatabaseExtractionParameters params)
throws SQLException {
List<RawTable> results = new ArrayList<>();
try (ResultSet tables =
Expand Down
14 changes: 7 additions & 7 deletions core/src/test/java/org/db2code/MetadataExtractorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Objects;
import org.db2code.extractors.ExtractionParameters;
import org.db2code.extractors.DatabaseExtractionParameters;
import org.db2code.rawmodel.RawDatabaseMetadata;
import org.h2.util.IOUtils;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -46,9 +46,8 @@ public static void beforeAll() throws IOException, SQLException {

@Test
public void genericTest() throws IOException {
ExtractionParameters extractionParameters = new ExtractionParameters();
extractionParameters.setSchemaPattern("TEST_SCHEMA");
extractionParameters.setCatalog("TEST");
DatabaseExtractionParameters extractionParameters =
new DatabaseExtractionParameters("TEST", "TEST_SCHEMA", null, null, null);
RawDatabaseMetadata metadata = metadataExtractor.extract(extractionParameters);

JsonNode expected =
Expand All @@ -61,7 +60,8 @@ public void genericTest() throws IOException {

@Test
public void noFiltersTest() {
RawDatabaseMetadata metadata = metadataExtractor.extract(new ExtractionParameters());
RawDatabaseMetadata metadata =
metadataExtractor.extract(new DatabaseExtractionParameters());
Assertions.assertTrue(
metadata.getTables().size() > 3,
"Tables more than we defined. Should include system tables");
Expand All @@ -75,8 +75,8 @@ public void noFiltersTest() {

@Test
public void tableFilterTest() throws IOException {
ExtractionParameters extractionParameters = new ExtractionParameters();
extractionParameters.setTableNamePattern("TEST_TABLE_1");
DatabaseExtractionParameters extractionParameters =
DatabaseExtractionParameters.builder().tableNamePattern("TEST_TABLE_1").build();
RawDatabaseMetadata metadata = metadataExtractor.extract(extractionParameters);

JsonNode expected =
Expand Down
38 changes: 36 additions & 2 deletions java-pojo-generator-mojo-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -54,7 +53,6 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.1</version>
</dependency>
</dependencies>
<build>
Expand All @@ -64,6 +62,42 @@
<artifactId>java-pojo-generator-mojo</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>exportMetadata</id>
<goals>
<goal>generatePojo</goal>
</goals>
<configuration>
<includeGenerationInfo>true</includeGenerationInfo>
<jdbcUrl>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM '${project.basedir}/init.sql'</jdbcUrl>
<jdbcClassName>org.h2.Driver</jdbcClassName>
<extractionParameters>
<item>
<schemaPattern>TEST_SCHEMA</schemaPattern>
<catalog>TEST</catalog>
<exportFile>${basedir}/target/classes/TEST_SCHEMA.json</exportFile>
</item>
</extractionParameters>
</configuration>
</execution>
<execution>
<id>importFromExportedMetadata</id>
<goals>
<goal>generatePojo</goal>
</goals>
<configuration>
<includeGenerationInfo>true</includeGenerationInfo>
<extractionParameters>
<item>
<importFile>${basedir}/target/classes/TEST_SCHEMA.json</importFile>
</item>
</extractionParameters>
<baseDir>${project.basedir}</baseDir>
<targetFolder>target/generated-sources</targetFolder>
<targetPackage>from_imported_metadata.testpkg</targetPackage>
<!-- Default pojo.mustache is invoked -->
</configuration>
</execution>
<execution>
<id>pojoExecution</id>
<goals>
Expand Down
4 changes: 4 additions & 0 deletions java-pojo-generator-mojo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
<version>3.9.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.db2code.generator.java.pojo.mojo;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Item {
private String catalog;
private String schemaPattern;
private String tableNamePattern;
private String[] types;
private String importFile;
private String exportFile;
}
Loading