From a183ed8c8c581f371f841e43ab863829c987d387 Mon Sep 17 00:00:00 2001 From: ichizero <30754019+ichizero@users.noreply.github.com> Date: Mon, 29 Jan 2024 03:19:38 +0900 Subject: [PATCH 1/3] fix: To returns null instead of zero-value, use getObject instead --- internal/core/gen.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/core/gen.go b/internal/core/gen.go index 5a7ac0a..1cb0c8a 100644 --- a/internal/core/gen.go +++ b/internal/core/gen.go @@ -166,17 +166,16 @@ func jdbcGet(t ktType, idx int) string { if t.IsInstant() { return fmt.Sprintf(`results.getTimestamp(%d).toInstant()`, idx) } - if t.IsUUID() { - var nullCast string - if t.IsNull { - nullCast = "?" - } - return fmt.Sprintf(`results.getObject(%d) as%s %s`, idx, nullCast, t.Name) - } if t.IsBigDecimal() { return fmt.Sprintf(`results.getBigDecimal(%d)`, idx) } - return fmt.Sprintf(`results.get%s(%d)`, t.Name, idx) + + var nullCast string + if t.IsNull { + nullCast = "?" + } + + return fmt.Sprintf(`results.getObject(%d) as%s %s`, idx, nullCast, t.Name) } func (v QueryValue) ResultSet() string { From 4cdb1d140b0c6a0bce2749cd56b6440374a3f239 Mon Sep 17 00:00:00 2001 From: ichizero <30754019+ichizero@users.noreply.github.com> Date: Mon, 29 Jan 2024 03:23:21 +0900 Subject: [PATCH 2/3] fix: Cast to nullable type instead of using the safe cast In case of type mismatch, ClassCastException should be throwned instead of null being returned. --- internal/core/gen.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/core/gen.go b/internal/core/gen.go index 1cb0c8a..dcfd0a9 100644 --- a/internal/core/gen.go +++ b/internal/core/gen.go @@ -175,7 +175,7 @@ func jdbcGet(t ktType, idx int) string { nullCast = "?" } - return fmt.Sprintf(`results.getObject(%d) as%s %s`, idx, nullCast, t.Name) + return fmt.Sprintf(`results.getObject(%d) as %s%s`, idx, t.Name, nullCast) } func (v QueryValue) ResultSet() string { From 02f6476e63dfcd7f41065b5b5202329e93c19155 Mon Sep 17 00:00:00 2001 From: ichizero <30754019+ichizero@users.noreply.github.com> Date: Mon, 29 Jan 2024 03:40:26 +0900 Subject: [PATCH 3/3] fix: Treat BigDecimal same as other standard lib types --- internal/core/gen.go | 7 ------- internal/core/imports.go | 6 ++++++ internal/core/postgresql_type.go | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/core/gen.go b/internal/core/gen.go index dcfd0a9..b754b26 100644 --- a/internal/core/gen.go +++ b/internal/core/gen.go @@ -166,9 +166,6 @@ func jdbcGet(t ktType, idx int) string { if t.IsInstant() { return fmt.Sprintf(`results.getTimestamp(%d).toInstant()`, idx) } - if t.IsBigDecimal() { - return fmt.Sprintf(`results.getBigDecimal(%d)`, idx) - } var nullCast string if t.IsNull { @@ -366,10 +363,6 @@ func (t ktType) IsUUID() bool { return t.Name == "UUID" } -func (t ktType) IsBigDecimal() bool { - return t.Name == "java.math.BigDecimal" -} - func makeType(req *plugin.GenerateRequest, col *plugin.Column) ktType { typ, isEnum := ktInnerType(req, col) return ktType{ diff --git a/internal/core/imports.go b/internal/core/imports.go index 7fdcb88..79fa99e 100644 --- a/internal/core/imports.go +++ b/internal/core/imports.go @@ -86,6 +86,9 @@ func (i *Importer) modelImports() [][]string { if i.usesType("UUID") { std["java.util.UUID"] = struct{}{} } + if i.usesType("BigDecimal") { + std["java.math.BigDecimal"] = struct{}{} + } stds := make([]string, 0, len(std)) for s := range std { @@ -120,6 +123,9 @@ func stdImports(uses func(name string) bool) map[string]struct{} { if uses("UUID") { std["java.util.UUID"] = struct{}{} } + if uses("BigDecimal") { + std["java.math.BigDecimal"] = struct{}{} + } return std } diff --git a/internal/core/postgresql_type.go b/internal/core/postgresql_type.go index ce53e19..a033890 100644 --- a/internal/core/postgresql_type.go +++ b/internal/core/postgresql_type.go @@ -36,7 +36,7 @@ func postgresType(req *plugin.GenerateRequest, col *plugin.Column) (string, bool return "Float", false case "pg_catalog.numeric": - return "java.math.BigDecimal", false + return "BigDecimal", false case "bool", "pg_catalog.bool": return "Boolean", false