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 support for generation info #5

Merged
merged 1 commit into from
Dec 21, 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
1 change: 1 addition & 0 deletions java-pojo-generator-mojo-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class PojoMojo extends AbstractMojo {
@Parameter private String ext;

@Parameter private DateImpl dateImpl;
@Parameter private boolean includeGenerationInfo;

public void execute() {
if (isWindows()) {
Expand Down Expand Up @@ -66,7 +67,8 @@ public void execute() {
this.targetFolder,
this.baseDir,
ext,
dateImpl));
dateImpl,
includeGenerationInfo));
}

public static boolean isWindows() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ExecutorParams {
private final String baseDir;
private final String ext;
private final DateImpl dateImpl;
private final boolean includeGenerationInfo;

public ExecutorParams(
Collection<ExtractionParameters> extractionParameters,
Expand All @@ -20,14 +21,16 @@ public ExecutorParams(
String targetFolder,
String baseDir,
String ext,
DateImpl dateImpl) {
DateImpl dateImpl,
boolean includeGenerationInfo) {
this.extractionParameters = extractionParameters;
this.templates = templates;
this.targetPackage = targetPackage;
this.targetFolder = targetFolder;
this.baseDir = baseDir;
this.ext = ext;
this.dateImpl = dateImpl;
this.includeGenerationInfo = includeGenerationInfo;
}

public Collection<ExtractionParameters> getExtractionParameters() {
Expand Down Expand Up @@ -57,4 +60,8 @@ public String getExt() {
public DateImpl getDateImpl() {
return dateImpl;
}

public boolean isIncludeGenerationInfo() {
return includeGenerationInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public void execute(ExecutorParams params) {
dateImpl = DateImpl.UTIL_DATE;
}

new JavaDatabaseAdapter(metadata, params.getTargetPackage(), dateImpl)
new JavaDatabaseAdapter(
metadata,
params.getTargetPackage(),
dateImpl,
params.isIncludeGenerationInfo())
.getClasses()
.forEach(
javaClass ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.db2code.generator.java.pojo.adapter;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collection;
import java.util.stream.Collectors;
import org.db2code.convert.JavaPropertyConverter;
Expand All @@ -9,11 +11,17 @@ public class JavaClassAdapter {
private final RawTable rawTable;
private final String targetPackage;
private final DateImpl dateImpl;
private final boolean includeGenerationInfo;

public JavaClassAdapter(RawTable rawTable, String targetPackage, DateImpl dateImpl) {
public JavaClassAdapter(
RawTable rawTable,
String targetPackage,
DateImpl dateImpl,
boolean includeGenerationInfo) {
this.rawTable = rawTable;
this.targetPackage = targetPackage;
this.dateImpl = dateImpl;
this.includeGenerationInfo = includeGenerationInfo;
}

public String getClassName() {
Expand All @@ -33,4 +41,13 @@ public Collection<JavaPropertyAdapter> getProperties() {
.map(rawColumn -> new JavaPropertyAdapter(rawTable, rawColumn, dateImpl))
.collect(Collectors.toList());
}

public String getGenerationInfo() {
if (includeGenerationInfo) {
return "Generated by DB2Code at: "
+ LocalDateTime.now(ZoneId.systemDefault()).toString();
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@ public class JavaDatabaseAdapter {
private final RawDatabaseMetadata rawDatabaseMetadata;
private final String targetPackage;
private final DateImpl dateImpl;
private final boolean includeGenerationInfo;

public JavaDatabaseAdapter(
RawDatabaseMetadata rawDatabaseMetadata, String targetPackage, DateImpl dateImpl) {
RawDatabaseMetadata rawDatabaseMetadata,
String targetPackage,
DateImpl dateImpl,
boolean includeGenerationInfo) {
this.rawDatabaseMetadata = rawDatabaseMetadata;
this.targetPackage = targetPackage;
this.dateImpl = dateImpl;
this.includeGenerationInfo = includeGenerationInfo;
}

public Collection<JavaClassAdapter> getClasses() {
return rawDatabaseMetadata.getTables().stream()
.map((RawTable rawTable) -> new JavaClassAdapter(rawTable, targetPackage, dateImpl))
.map(
(RawTable rawTable) ->
new JavaClassAdapter(
rawTable, targetPackage, dateImpl, includeGenerationInfo))
.collect(Collectors.toList());
}
}
4 changes: 3 additions & 1 deletion java-pojo-generator/src/main/resources/pojo.mustache
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package {{package}};

/** {{rawTable.remarks}}
{{rawTable.tableCat}}.{{rawTable.tableSchem}}.{{rawTable.tableName}}
{{rawTable.tableCat}}.{{rawTable.tableSchem}}.{{rawTable.tableName}}{{#generationInfo}}

{{generationInfo}}{{/generationInfo}}
*/
public class {{className}} {
{{#properties}}
Expand Down
4 changes: 3 additions & 1 deletion java-pojo-generator/src/main/resources/spring-data.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

/** {{rawTable.remarks}}
{{rawTable.tableCat}}.{{rawTable.tableSchem}}.{{rawTable.tableName}}
{{rawTable.tableCat}}.{{rawTable.tableSchem}}.{{rawTable.tableName}}{{#generationInfo}}

{{generationInfo}}{{/generationInfo}}
*/
@Table("{{rawTable.tableName}}")
public class {{className}} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void test() throws IOException {
TARGET_FOLDER,
dir,
null,
DateImpl.UTIL_DATE));
DateImpl.UTIL_DATE,
false));

Arrays.stream(
Objects.requireNonNull(
Expand Down