Skip to content

Commit

Permalink
Merge pull request #195
Browse files Browse the repository at this point in the history
Add support for BigDecimal type
  • Loading branch information
jklingsporn authored Jan 4, 2022
2 parents 90557de + dee6b1e commit f626abf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.jooq.tools.JooqLogger;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.time.*;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -299,6 +301,8 @@ private void generateFromJson(TableDefinition table, JavaWriter out, GeneratorSt
jsonValueExtractor = "json::getInstant";
}else if(isJavaTimeType(columnType)){
jsonValueExtractor = String.format("key -> {String s = json.getString(key); return s==null?null:%s.parse(s);}",columnType);
}else if(isType(columnType, BigDecimal.class)){
jsonValueExtractor = String.format("key -> {String s = json.getString(key); return s==null?null:new %s(s);}",columnType);
}else if(isEnum(table, column)) {
//if this is an enum from the database (no converter) it has getLiteral defined
if(column.getType().getConverter() == null){
Expand Down Expand Up @@ -347,14 +351,14 @@ protected Class<?> tryGetPgConverterFromType(String columnType, String converter
try {
Class<?> converterClazz = Class.forName(converter);
if(PgConverter.class.isAssignableFrom(converterClazz)){
PgConverter<?,?,?> converterInstance = (PgConverter<?, ?, ?>) converterClazz.newInstance();
PgConverter<?,?,?> converterInstance = (PgConverter<?, ?, ?>) converterClazz.getDeclaredConstructor().newInstance();
return converterInstance.rowConverter().fromType();
}
return null;
} catch (ClassNotFoundException e) {
logger.info(String.format("'%s' to map '%s' could not be accessed from code generator.",converter,columnType));
return null;
} catch (IllegalAccessException | InstantiationException e) {
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
logger.info(String.format("'%s' to map '%s' could not be instantiated from code generator.",converter,columnType));
return null;
}
Expand Down Expand Up @@ -384,7 +388,7 @@ private void generateToJson(TableDefinition table, JavaWriter out, GeneratorStra
}
}else if(isAllowedJsonType(column, columnType)){
out.tab(2).println("json.put(\"%s\",%s());", getJsonKeyName(column),getter);
}else if(isJavaTimeType(columnType)){
}else if(isJavaTimeType(columnType) || isStringSerializableType(columnType)){
out.tab(2).println("json.put(\"%s\",%s()==null?null:%s().toString());", getJsonKeyName(column),getter,getter);
}else if(isCollectionType(columnType)){
out.tab(2).println("json.put(\"%s\",%s()==null?null: new io.vertx.core.json.JsonArray(%s()));", getJsonKeyName(column),getter,getter);
Expand Down Expand Up @@ -426,6 +430,10 @@ private boolean isJavaTimeType(String columnType){
|| isType(columnType, LocalDate.class);
}

private boolean isStringSerializableType(String columnType) {
return isType(columnType, BigDecimal.class);
}

private boolean isCollectionType(String columnType){
return isType(columnType,Collection.class) || isType(columnType,List.class) || isType(columnType,Set.class);
}
Expand Down Expand Up @@ -540,11 +548,11 @@ public String getKeyType(UniqueKeyDefinition key, JavaWriter out){
tType = getJavaType(keyColumns.get(0).getType(),out);
}
else if (keyColumns.size() <= Constants.MAX_ROW_DEGREE) {
String generics = "";
StringBuilder generics = new StringBuilder();
String separator = "";

for (ColumnDefinition column : keyColumns) {
generics += separator + (getJavaType(column.getType(),out));
generics.append(separator).append(getJavaType(column.getType(), out));
separator = ", ";
}

Expand Down Expand Up @@ -607,11 +615,11 @@ private void generateDAO(UniqueKeyDefinition key, TableDefinition table, VertxJa
tType = getJavaType(keyColumns.get(0).getType(),out);
}
else if (keyColumns.size() <= Constants.MAX_ROW_DEGREE) {
String generics = "";
StringBuilder generics = new StringBuilder();
String separator = "";

for (ColumnDefinition column : keyColumns) {
generics += separator + out.ref(getJavaType(column.getType(),out));
generics.append(separator).append(out.ref(getJavaType(column.getType(), out)));
separator = ", ";
}

Expand Down Expand Up @@ -664,16 +672,16 @@ else if (keyColumns.size() <= Constants.MAX_ROW_DEGREE) {

// [#2574] This should be replaced by a call to a method on the target table's Key type
else {
String params = "";
StringBuilder params = new StringBuilder();
String separator = "";

for (ColumnDefinition column : keyColumns) {
params += separator + "object." + getStrategy().getJavaGetterName(column, GeneratorStrategy.Mode.POJO) + "()";
params.append(separator).append("object.").append(getStrategy().getJavaGetterName(column, GeneratorStrategy.Mode.POJO)).append("()");

separator = ", ";
}

out.tab(2).println("return compositeKeyRecord(%s);", params);
out.tab(2).println("return compositeKeyRecord(%s);", params.toString());
}

out.tab(1).println("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void setupDatabase() throws Exception {
" \"someHugeNumber\" BIGINT NULL,\n" +
" \"someSmallNumber\" SMALLINT NULL,\n" +
" \"someRegularNumber\" INTEGER NULL,\n" +
" \"someDecimal\" DECIMAL NULL,\n" +
" \"someDouble\" DOUBLE PRECISION NULL,\n" +
" \"someEnum\" \"someEnum\" DEFAULT 'FOO' ,\n" +
" \"someJsonObject\" VARCHAR(45) NULL,\n" +
Expand Down

0 comments on commit f626abf

Please sign in to comment.