Skip to content

Commit 87b89ef

Browse files
committed
If a primitive column is nullable, handle correctly
1 parent 2c6a780 commit 87b89ef

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

examples/src/main/resources/ondeck/postgresql/query/checking_account.sql

Whitespace-only changes.

examples/src/main/resources/ondeck/postgresql/schema/0004_checking_account.sql

Whitespace-only changes.

internal/core/gen.go

+38-13
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ func jdbcSet(t ktType, idx int, name string) string {
9696
if t.IsUUID() {
9797
return fmt.Sprintf("stmt.setObject(%d, %s)", idx, name)
9898
}
99-
return fmt.Sprintf("stmt.set%s(%d, %s)", t.Name, idx, name)
99+
if t.IsNull && t.PrimitiveType != "" {
100+
return fmt.Sprintf("if (%[3]s != null) stmt.set%[1]s(%[2]d, %[3]s) else stmt.setNull(%[2]d, Types.%[4]s)", t.Name, idx, name, t.PrimitiveType)
101+
} else {
102+
return fmt.Sprintf("stmt.set%s(%d, %s)", t.Name, idx, name)
103+
}
100104
}
101105

102106
type Params struct {
@@ -320,12 +324,13 @@ func BuildDataClasses(conf Config, req *plugin.GenerateRequest) []Struct {
320324
}
321325

322326
type ktType struct {
323-
Name string
324-
IsEnum bool
325-
IsArray bool
326-
IsNull bool
327-
DataType string
328-
Engine string
327+
Name string
328+
IsEnum bool
329+
IsArray bool
330+
IsNull bool
331+
PrimitiveType string
332+
DataType string
333+
Engine string
329334
}
330335

331336
func (t ktType) String() string {
@@ -374,12 +379,13 @@ func (t ktType) IsBigDecimal() bool {
374379
func makeType(req *plugin.GenerateRequest, col *plugin.Column) ktType {
375380
typ, isEnum := ktInnerType(req, col)
376381
return ktType{
377-
Name: typ,
378-
IsEnum: isEnum,
379-
IsArray: col.IsArray,
380-
IsNull: !col.NotNull,
381-
DataType: sdk.DataType(col.Type),
382-
Engine: req.Settings.Engine,
382+
Name: typ,
383+
IsEnum: isEnum,
384+
IsArray: col.IsArray,
385+
IsNull: !col.NotNull,
386+
PrimitiveType: ktPrimitiveType(typ),
387+
DataType: sdk.DataType(col.Type),
388+
Engine: req.Settings.Engine,
383389
}
384390
}
385391

@@ -395,6 +401,25 @@ func ktInnerType(req *plugin.GenerateRequest, col *plugin.Column) (string, bool)
395401
}
396402
}
397403

404+
func ktPrimitiveType(t string) string {
405+
switch t {
406+
case "Int":
407+
return "INTEGER"
408+
case "Double":
409+
return "DOUBLE"
410+
case "Long":
411+
return "BIGINT"
412+
case "Short":
413+
return "SMALLINT"
414+
case "Float":
415+
return "REAL"
416+
case "Boolean":
417+
return "BOOLEAN"
418+
default:
419+
return ""
420+
}
421+
}
422+
398423
type goColumn struct {
399424
id int
400425
*plugin.Column

0 commit comments

Comments
 (0)