Browse Source

bug fixed for table name detect on insert a slice

xormplus 9 years ago
parent
commit
998849c99e
4 changed files with 19 additions and 7 deletions
  1. 11 2
      engine.go
  2. 6 3
      session.go
  3. 1 1
      statement.go
  4. 1 1
      xorm.go

+ 11 - 2
engine.go

@@ -410,12 +410,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
@@ -911,6 +916,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()

+ 6 - 3
session.go

@@ -2262,9 +2262,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
 	}

+ 1 - 1
statement.go

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

+ 1 - 1
xorm.go

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