bug fixed for table name detect on insert a slice
This commit is contained in:
parent
c6c7056840
commit
db48d70fd0
13
engine.go
13
engine.go
|
@ -405,12 +405,17 @@ func (engine *Engine) tbName(v reflect.Value) string {
|
|||
if tb, ok := v.Interface().(TableName); ok {
|
||||
return tb.TableName()
|
||||
}
|
||||
if v.CanAddr() {
|
||||
|
||||
if v.Type().Kind() == reflect.Ptr {
|
||||
if tb, ok := reflect.Indirect(v).Interface().(TableName); ok {
|
||||
return tb.TableName()
|
||||
}
|
||||
} else if v.CanAddr() {
|
||||
if tb, ok := v.Addr().Interface().(TableName); ok {
|
||||
return tb.TableName()
|
||||
}
|
||||
}
|
||||
return engine.TableMapper.Obj2Table(v.Type().Name())
|
||||
return engine.TableMapper.Obj2Table(reflect.Indirect(v).Type().Name())
|
||||
}
|
||||
|
||||
// DumpAll dump database all table structs and data to w with specify db type
|
||||
|
@ -906,6 +911,10 @@ type TableName interface {
|
|||
TableName() string
|
||||
}
|
||||
|
||||
var (
|
||||
tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
|
||||
)
|
||||
|
||||
func (engine *Engine) mapType(v reflect.Value) *core.Table {
|
||||
t := v.Type()
|
||||
table := engine.newTable()
|
||||
|
|
|
@ -2241,9 +2241,12 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
|
|||
return 0, errors.New("needs a pointer to a slice")
|
||||
}
|
||||
|
||||
bean := sliceValue.Index(0).Interface()
|
||||
elementValue := rValue(bean)
|
||||
session.Statement.setRefValue(elementValue)
|
||||
if sliceValue.Len() <= 0 {
|
||||
return 0, errors.New("could not insert a empty slice")
|
||||
}
|
||||
|
||||
session.Statement.setRefValue(sliceValue.Index(0))
|
||||
|
||||
if len(session.Statement.TableName()) <= 0 {
|
||||
return 0, ErrTableNotFound
|
||||
}
|
||||
|
|
|
@ -193,7 +193,7 @@ func (statement *Statement) Or(querystring string, args ...interface{}) *Stateme
|
|||
}
|
||||
|
||||
func (statement *Statement) setRefValue(v reflect.Value) {
|
||||
statement.RefTable = statement.Engine.autoMapType(v)
|
||||
statement.RefTable = statement.Engine.autoMapType(reflect.Indirect(v))
|
||||
statement.tableName = statement.Engine.tbName(v)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue