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

fix: Use ResultSet.getObject to respect nullable columns of the basic SQL data types #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 6 additions & 14 deletions internal/core/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,13 @@ 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)

var nullCast string
if t.IsNull {
nullCast = "?"
}
return fmt.Sprintf(`results.get%s(%d)`, t.Name, idx)

return fmt.Sprintf(`results.getObject(%d) as %s%s`, idx, t.Name, nullCast)
}

func (v QueryValue) ResultSet() string {
Expand Down Expand Up @@ -367,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{
Expand Down
6 changes: 6 additions & 0 deletions internal/core/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down