Skip to content

Commit

Permalink
separate null and optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed Apr 27, 2024
1 parent c4c6a87 commit 6401475
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
7 changes: 3 additions & 4 deletions dbGen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ type ColumnMapping struct {
}

type ParamMapping struct {
MappedName string `mapstructure:"MappedName"`
MappedType string `mapstructure:"MappedType"`

// this is string because it can be empty
MappedName string `mapstructure:"MappedName"`
MappedType string `mapstructure:"MappedType"`
IsNullable null.Bool `mapstructure:"IsNullable"`
IsOptional null.Bool `mapstructure:"IsOptional"`
}

type Mapping struct {
Expand Down
9 changes: 6 additions & 3 deletions dbGen/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type DbParameter struct {
Mode string `db:"parameter_mode"` // IN/OUT
UDTName string `db:"udt_name"` // User defined type
IsNullable bool `db:"is_nullable"`
IsOptional bool `db:"is_optional"`
}

const (
Expand Down Expand Up @@ -138,17 +139,19 @@ func addParamsToRoutine(conn *common.DbConn, routine *DbRoutine) error {
parameter_name::text,
parameter_mode::text,
udt_name::text,
parameter_default is not null as is_nullable
false as is_nullable,
parameter_default is not null as is_optional
from information_schema.parameters
where specific_schema = $1
and specific_name = $2
union
select c.ordinal_position::int, c.column_name::text, 'OUT', c.udt_name::text, c.is_nullable = 'YES'
select c.ordinal_position::int, c.column_name::text, 'OUT', c.udt_name::text, c.is_nullable = 'YES',true
from information_schema.columns c
where c.table_name = $3
and c.table_schema = coalesce($4, 'public')
union
select a.ordinal_position::int, a.attribute_name::text, 'OUT', a.attribute_udt_name::text, is_nullable = 'YES'
select a.ordinal_position::int, a.attribute_name::text, 'OUT', a.attribute_udt_name::text, is_nullable = 'YES',true
from information_schema.attributes a
where a.udt_name = $3
and a.udt_schema = coalesce($4, 'public')
Expand Down
11 changes: 11 additions & 0 deletions dbGen/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type effectiveParamMapping struct {
name string
typeMapping mapping
isNullable bool
isOptional bool
}

// TODO make configurable
Expand Down Expand Up @@ -139,6 +140,7 @@ func mapModel(routine DbRoutine, globalTypeMappings *map[string]mapping, routine
Position: column.OrdinalPosition - positionOffset,
MapperFunction: columnMapping.typeMapping.mappedFunction,
Nullable: columnMapping.isNullable,
Optional: columnMapping.isOptional,
}

properties = append(properties, property)
Expand Down Expand Up @@ -178,6 +180,7 @@ func mapParameters(attributes []DbParameter, typeMappings *map[string]mapping, r
Position: parameter.OrdinalPosition - positionOffset,
MapperFunction: "",
Nullable: effectiveMapping.isNullable,
Optional: effectiveMapping.isOptional,
}

properties[i] = *property
Expand Down Expand Up @@ -253,13 +256,16 @@ func getColumnMapping(param DbParameter, routineMapping *RoutineMapping, globalM
name: name,
typeMapping: *typeMapping,
isNullable: isNullable,
// no column has to be selected => column is always optional
isOptional: true,
}, nil

}

func getParamMapping(param DbParameter, routineMapping *RoutineMapping, globalMappings *map[string]mapping, config *Config) (*effectiveParamMapping, error) {
name := param.Name
isNullable := param.IsNullable
isOptional := param.IsOptional
var typeMapping *mapping = nil
var err error = nil

Expand All @@ -273,6 +279,10 @@ func getParamMapping(param DbParameter, routineMapping *RoutineMapping, globalMa
isNullable = explicitMapping.IsNullable.Bool
}

if explicitMapping.IsOptional.Valid {
isOptional = explicitMapping.IsOptional.Bool
}

if explicitMapping.MappedType != "" {
typeMapping, err = handleTypeMappingOverride(explicitMapping.MappedType, "", config)
if err != nil {
Expand All @@ -293,6 +303,7 @@ func getParamMapping(param DbParameter, routineMapping *RoutineMapping, globalMa
name: name,
typeMapping: *typeMapping,
isNullable: isNullable,
isOptional: isOptional,
}, nil

}
Expand Down
1 change: 1 addition & 0 deletions dbGen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Property struct {
Position int
MapperFunction string
Nullable bool // This can be unreliable
Optional bool // only used in Params
}

type Routine struct {
Expand Down

0 comments on commit 6401475

Please sign in to comment.