Fix two issues with dumptables (#1903)

There are two issues with #1872 which have become apparent
after testing on Gitea.

1. Ensure structs which are have before processors run correctly
2. Ensure structs extending other structs work
3. Ensure that numerical enums become numeric

Signed-off-by: Andrew Thornton <art27@cantab.net>

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1903
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Andrew Thornton <art27@cantab.net>
Co-committed-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
Andrew Thornton 2021-05-08 12:27:22 +08:00 committed by Lunny Xiao
parent 912c2524f8
commit 210c30a7dd
1 changed files with 14 additions and 7 deletions

View File

@ -496,7 +496,7 @@ func formatColumnValue(dstDialect dialects.Dialect, d interface{}, col *schemas.
} }
return fmt.Sprintf("%v", strconv.FormatBool(v)) return fmt.Sprintf("%v", strconv.FormatBool(v))
} }
return fmt.Sprintf("%v", d) return fmt.Sprintf("%d", d)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if col.SQLType.Name == schemas.Bool { if col.SQLType.Name == schemas.Bool {
v := reflect.ValueOf(d).Uint() > 0 v := reflect.ValueOf(d).Uint() > 0
@ -508,7 +508,7 @@ func formatColumnValue(dstDialect dialects.Dialect, d interface{}, col *schemas.
} }
return fmt.Sprintf("%v", strconv.FormatBool(v)) return fmt.Sprintf("%v", strconv.FormatBool(v))
} }
return fmt.Sprintf("%v", d) return fmt.Sprintf("%d", d)
default: default:
return fmt.Sprintf("%v", d) return fmt.Sprintf("%v", d)
} }
@ -554,7 +554,7 @@ func (engine *Engine) dumpTables(tables []*schemas.Table, w io.Writer, tp ...sch
for i, table := range tables { for i, table := range tables {
dstTable := table dstTable := table
if table.Type != nil { if table.Type != nil {
dstTable, err = dstTableCache.Parse(reflect.New(table.Type)) dstTable, err = dstTableCache.Parse(reflect.New(table.Type).Elem())
if err != nil { if err != nil {
engine.logger.Errorf("Unable to infer table for %s in new dialect. Error: %v", table.Name) engine.logger.Errorf("Unable to infer table for %s in new dialect. Error: %v", table.Name)
dstTable = table dstTable = table
@ -610,7 +610,8 @@ func (engine *Engine) dumpTables(tables []*schemas.Table, w io.Writer, tp ...sch
sess := engine.NewSession() sess := engine.NewSession()
defer sess.Close() defer sess.Close()
for rows.Next() { for rows.Next() {
bean := reflect.New(table.Type) beanValue := reflect.New(table.Type)
bean := beanValue.Interface()
fields, err := rows.Columns() fields, err := rows.Columns()
if err != nil { if err != nil {
return err return err
@ -620,8 +621,8 @@ func (engine *Engine) dumpTables(tables []*schemas.Table, w io.Writer, tp ...sch
return err return err
} }
dataStruct := utils.ReflectValue(bean.Interface()) dataStruct := utils.ReflectValue(bean)
_, err = sess.slice2Bean(scanResults, fields, bean.Interface(), &dataStruct, table) _, err = sess.slice2Bean(scanResults, fields, bean, &dataStruct, table)
if err != nil { if err != nil {
return err return err
} }
@ -637,7 +638,13 @@ func (engine *Engine) dumpTables(tables []*schemas.Table, w io.Writer, tp ...sch
if col == nil { if col == nil {
return errors.New("unknown column error") return errors.New("unknown column error")
} }
temp += "," + formatColumnValue(dstDialect, bean.Elem().FieldByName(col.FieldName).Interface(), col)
fields := strings.Split(col.FieldName, ".")
field := dataStruct
for _, fieldName := range fields {
field = field.FieldByName(fieldName)
}
temp += "," + formatColumnValue(dstDialect, field.Interface(), col)
} }
_, err = io.WriteString(w, temp[1:]+");\n") _, err = io.WriteString(w, temp[1:]+");\n")
if err != nil { if err != nil {