bug fixed for table name detect on insert a slice

This commit is contained in:
Lunny Xiao 2016-08-22 12:56:40 +08:00
parent c6c7056840
commit db48d70fd0
5 changed files with 20 additions and 8 deletions

View File

@ -1 +1 @@
xorm v0.5.5.0728
xorm v0.5.5.0822

View File

@ -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()

View File

@ -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
}

View File

@ -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)
}

View File

@ -17,7 +17,7 @@ import (
const (
// Version show the xorm's version
Version string = "0.5.5.0728"
Version string = "0.5.5.0822"
)
func regDrvsNDialects() bool {