Skip to content

Commit

Permalink
fix: dumper will not try to sort by non-orderable columns like JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Stumble committed Jan 30, 2023
1 parent 080d332 commit 1961eea
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions internal/codegen/golang/dumploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,41 @@ func (d DumpLoader) FieldDBNames() string {
return strings.Join(fields, ",")
}

func (d DumpLoader) DumpSortByFields() string {
if d.MainStruct == nil {
panic("no MainStruct in DumpLoader")
}
var fields []string
for _, f := range d.MainStruct.Fields {
switch f.Type {
// TODO(yumin):
// best-effort sorting for now. Once we pass table indexes to codegen
// we can just use index information.
case "int", "int16", "int32", "int64", "float32", "float64", "string", "bool", "time.Time":
fields = append(fields, f.DBName)
case "*int", "*int16", "*int32", "*int64", "*float32", "*float64", "*string", "*bool", "*time.Time":
fields = append(fields, f.DBName)
default:
continue
}
}
return strings.Join(fields, ",")
}

func (d DumpLoader) ParamList() string {
if d.MainStruct == nil {
panic("no MainStruct in DumpLoader")
}
var vals []string
for i := range d.MainStruct.Fields {
vals = append(vals, fmt.Sprintf("$%d", i + 1))
vals = append(vals, fmt.Sprintf("$%d", i+1))
}
return strings.Join(vals, ",")
}

func (d DumpLoader) DumpSQL() string {
return fmt.Sprintf("SELECT %s FROM %s ORDER BY %s ASC;",
d.FieldDBNames(), d.MainStruct.Table.Name, d.FieldDBNames())
d.FieldDBNames(), d.MainStruct.Table.Name, d.DumpSortByFields())
}

func (d DumpLoader) LoadSQL() string {
Expand Down

0 comments on commit 1961eea

Please sign in to comment.