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 LocalDate #4

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
10 changes: 4 additions & 6 deletions java-pojo-generator-mojo-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
<goal>generatePojo</goal>
</goals>
<configuration>
<jdbcUrl>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM '${project.basedir}/init.sql'
</jdbcUrl>
<jdbcUrl>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM '${project.basedir}/init.sql'</jdbcUrl>
<jdbcClassName>org.h2.Driver</jdbcClassName>
<extractionParameters>
<item>
Expand All @@ -91,8 +90,8 @@
<goal>generatePojo</goal>
</goals>
<configuration>
<jdbcUrl>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM '${project.basedir}/init.sql'
</jdbcUrl>
<jdbcUrl>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM '${project.basedir}/init.sql'</jdbcUrl>
<dateImpl>LOCAL_DATE</dateImpl>
<jdbcClassName>org.h2.Driver</jdbcClassName>
<extractionParameters>
<item>
Expand All @@ -114,8 +113,7 @@
<goal>generatePojo</goal>
</goals>
<configuration>
<jdbcUrl>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM '${project.basedir}/init.sql'
</jdbcUrl>
<jdbcUrl>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM '${project.basedir}/init.sql'</jdbcUrl>
<jdbcClassName>org.h2.Driver</jdbcClassName>
<extractionParameters>
<item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -27,10 +25,8 @@ public void testSaveAndFind() {
TestTable1 entity = new TestTable1();
entity.setTestVarchar("Test Name");
entity.setTestNumeric(BigDecimal.ONE);
entity.setTestDate(
Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()));
entity.setTestDatetime(
Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()));
entity.setTestDate(LocalDate.now());
entity.setTestDatetime(LocalDate.now().atStartOfDay());
TestTable1 savedEntity = repository.save(entity);

assertThat(savedEntity).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.util.Collections;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -15,6 +13,7 @@
import org.db2code.generator.java.pojo.ExecutorParams;
import org.db2code.generator.java.pojo.Generator;
import org.db2code.generator.java.pojo.GeneratorExecutor;
import org.db2code.generator.java.pojo.adapter.DateImpl;

@Mojo(name = "generatePojo", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class PojoMojo extends AbstractMojo {
Expand All @@ -38,7 +37,9 @@ public class PojoMojo extends AbstractMojo {

@Parameter private String ext;

public void execute() throws MojoExecutionException, MojoFailureException {
@Parameter private DateImpl dateImpl;

public void execute() {
if (isWindows()) {
jdbcUrl = jdbcUrl.replace("\\", "\\\\");
}
Expand All @@ -64,7 +65,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
this.targetPackage,
this.targetFolder,
this.baseDir,
ext));
ext,
dateImpl));
}

public static boolean isWindows() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;
import org.db2code.extractors.ExtractionParameters;
import org.db2code.generator.java.pojo.adapter.DateImpl;

public class ExecutorParams {
private final Collection<ExtractionParameters> extractionParameters;
Expand All @@ -10,20 +11,23 @@ public class ExecutorParams {
private final String targetFolder;
private final String baseDir;
private final String ext;
private final DateImpl dateImpl;

public ExecutorParams(
Collection<ExtractionParameters> extractionParameters,
Collection<String> templates,
String targetPackage,
String targetFolder,
String baseDir,
String ext) {
String ext,
DateImpl dateImpl) {
this.extractionParameters = extractionParameters;
this.templates = templates;
this.targetPackage = targetPackage;
this.targetFolder = targetFolder;
this.baseDir = baseDir;
this.ext = ext;
this.dateImpl = dateImpl;
}

public Collection<ExtractionParameters> getExtractionParameters() {
Expand All @@ -49,4 +53,8 @@ public Collection<String> getTemplates() {
public String getExt() {
return ext;
}

public DateImpl getDateImpl() {
return dateImpl;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.db2code.generator.java.pojo;

import org.db2code.MetadataExtractor;
import org.db2code.generator.java.pojo.adapter.DateImpl;
import org.db2code.generator.java.pojo.adapter.JavaClassAdapter;
import org.db2code.generator.java.pojo.adapter.JavaDatabaseAdapter;
import org.db2code.rawmodel.RawDatabaseMetadata;
Expand All @@ -24,7 +25,12 @@ public void execute(ExecutorParams params) {
param -> {
RawDatabaseMetadata metadata = metadataExtractor.extract(param);

new JavaDatabaseAdapter(metadata, params.getTargetPackage())
DateImpl dateImpl = params.getDateImpl();
if (dateImpl == null) {
dateImpl = DateImpl.UTIL_DATE;
}

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

public enum DateImpl {
UTIL_DATE,
LOCAL_DATE
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
public class JavaClassAdapter {
private final RawTable rawTable;
private final String targetPackage;
private final DateImpl dateImpl;

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

public String getClassName() {
Expand All @@ -28,7 +30,7 @@ public String getPackage() {

public Collection<JavaPropertyAdapter> getProperties() {
return rawTable.getColumns().stream()
.map(rawColumn -> new JavaPropertyAdapter(rawTable, rawColumn))
.map(rawColumn -> new JavaPropertyAdapter(rawTable, rawColumn, dateImpl))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
public class JavaDatabaseAdapter {
private final RawDatabaseMetadata rawDatabaseMetadata;
private final String targetPackage;
private final DateImpl dateImpl;

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

public Collection<JavaClassAdapter> getClasses() {
return rawDatabaseMetadata.getTables().stream()
.map((RawTable rawTable) -> new JavaClassAdapter(rawTable, targetPackage))
.map((RawTable rawTable) -> new JavaClassAdapter(rawTable, targetPackage, dateImpl))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.db2code.generator.java.pojo.adapter;

import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.db2code.convert.JavaPropertyConverter;
Expand All @@ -14,16 +15,20 @@ public class JavaPropertyAdapter {
private final String JAVA_CHAR = "Char";
private final String JAVA_STRING = "String";
private final String JAVA_DATE = "java.util.Date";
private final String JAVA_LOCAL_DATE = "java.time.LocalDate";
private final String JAVA_LOCAL_DATE_TIME = "java.time.LocalDateTime";
private final String JAVA_BYTE_ARRAY = "byte[]";
private final String JAVA_CHAR_ARRAY = "char[]";
private final String JAVA_OBJECT = "Object";
private final String JAVA_BOOLEAN = "Boolean";
private final RawTable rawTable;
private final Set<String> primaryKeyColumns;
private final DateImpl dateImpl;

public JavaPropertyAdapter(RawTable rawTable, RawColumn rawColumn) {
public JavaPropertyAdapter(RawTable rawTable, RawColumn rawColumn, DateImpl dateImpl) {
this.rawTable = rawTable;
this.rawColumn = rawColumn;
this.dateImpl = dateImpl;
this.primaryKeyColumns =
rawTable.getPrimaryKey().stream()
.map(RawTable.RawPrimaryKey::getColumnName)
Expand Down Expand Up @@ -53,7 +58,7 @@ public Boolean getIsNullable() {
}

public Integer getSize() {
if (getJavaType() == JAVA_STRING) {
if (Objects.equals(getJavaType(), JAVA_STRING)) {
return rawColumn.getColumnSize();
} else {
return null;
Expand Down Expand Up @@ -84,11 +89,12 @@ public String getJavaType() {
case -16:
return JAVA_STRING;
case 91:
return resolveDateImpl();
case 92:
case 93:
case 2013:
case 2014:
return JAVA_DATE;
return resolveTimeImpl();
case -2:
case -3:
case -4:
Expand Down Expand Up @@ -117,4 +123,20 @@ public String getJavaType() {
+ rawColumn.getTableName());
}
}

private String resolveDateImpl() {
if (dateImpl == DateImpl.UTIL_DATE) {
return JAVA_DATE;
} else {
return JAVA_LOCAL_DATE;
}
}

private String resolveTimeImpl() {
if (dateImpl == DateImpl.UTIL_DATE) {
return JAVA_DATE;
} else {
return JAVA_LOCAL_DATE_TIME;
}
}
}
8 changes: 5 additions & 3 deletions java-pojo-generator/src/main/resources/spring-data.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public class {{className}} {
/** {{rawColumn.remarks}}
{{rawColumn.columnName}} {{rawColumn.typeName}}({{rawColumn.columnSize}}) Nullable={{rawColumn.isNullable}}
*/
{{^isNullable}}@jakarta.validation.constraints.NotNull{{/isNullable}}{{#isNullable}}
{{/isNullable}}{{#size}}@jakarta.validation.constraints.Size(max = {{size}}){{/size}}
@Column("{{rawColumn.columnName}}") {{#isId}}@Id{{/isId}}
@Column("{{rawColumn.columnName}}") {{#isId}}@Id{{/isId}}{{^isNullable}}

@jakarta.validation.constraints.NotNull{{/isNullable}}{{#size}}

@jakarta.validation.constraints.Size(max = {{size}}){{/size}}
private {{javaType}} {{propertyName}};
{{/properties}}
{{#properties}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.apache.commons.io.IOUtils;
import org.db2code.MetadataExtractor;
import org.db2code.extractors.ExtractionParameters;
import org.db2code.generator.java.pojo.adapter.DateImpl;
import org.db2code.rawmodel.RawDatabaseMetadata;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -69,7 +70,8 @@ public void test() throws IOException {
TESTPKG,
TARGET_FOLDER,
dir,
null));
null,
DateImpl.UTIL_DATE));

Arrays.stream(
Objects.requireNonNull(
Expand Down