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 { if tb, ok := v.Interface().(TableName); ok {
return tb.TableName() 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 { if tb, ok := v.Addr().Interface().(TableName); ok {
return tb.TableName() 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 // DumpAll dump database all table structs and data to w with specify db type
@ -906,6 +911,10 @@ type TableName interface {
TableName() string TableName() string
} }
var (
tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
)
func (engine *Engine) mapType(v reflect.Value) *core.Table { func (engine *Engine) mapType(v reflect.Value) *core.Table {
t := v.Type() t := v.Type()
table := engine.newTable() 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") return 0, errors.New("needs a pointer to a slice")
} }
bean := sliceValue.Index(0).Interface() if sliceValue.Len() <= 0 {
elementValue := rValue(bean) return 0, errors.New("could not insert a empty slice")
session.Statement.setRefValue(elementValue) }
session.Statement.setRefValue(sliceValue.Index(0))
if len(session.Statement.TableName()) <= 0 { if len(session.Statement.TableName()) <= 0 {
return 0, ErrTableNotFound 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) { 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) statement.tableName = statement.Engine.tbName(v)
} }

View File

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