statement.SetTable() 传入 string 类型会崩溃(因为内部没有处理string类型) #1821

Closed
opened 2020-11-02 02:58:43 +00:00 by MadX · 2 comments

v1.0.5
代码如下:

// Table tempororily set table name, the parameter could be a string or a pointer of struct
func (statement *Statement) SetTable(tableNameOrBean interface{}) error {
	v := rValue(tableNameOrBean)
	t := v.Type()
	if t.Kind() == reflect.Struct {
		var err error
		statement.RefTable, err = statement.tagParser.ParseWithCache(v)
		if err != nil {
			return err
		}
	}

	statement.AltTableName = dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tableNameOrBean, true)
	return nil
}
v1.0.5 代码如下: ``` // Table tempororily set table name, the parameter could be a string or a pointer of struct func (statement *Statement) SetTable(tableNameOrBean interface{}) error { v := rValue(tableNameOrBean) t := v.Type() if t.Kind() == reflect.Struct { var err error statement.RefTable, err = statement.tagParser.ParseWithCache(v) if err != nil { return err } } statement.AltTableName = dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tableNameOrBean, true) return nil } ```
Owner

Hi, Could you paste your example codes here? I think you should not use this method directly.

Hi, Could you paste your example codes here? I think you should not use this method directly.
Author

Hi, Could you paste your example codes here? I think you should not use this method directly.

TestCode:

func main() {
	testInsertValue()
}

type Test3Table struct {
	ID  int32  `xorm:"bigint autoincr pk id"`
	UID string `xorm:"varchar(64) uid"`
}

func (s *Test3Table) TableName() string {
	return "test_3_t"
}

func testInsertValue() {
	dbXorm, err := xorm.NewEngine("mysql", dbUrl)
	if err != nil {
		fmt.Println("open db err:", err.Error())
		return
	}
	dbXorm.SetLogLevel(log.LOG_DEBUG)
	dbXorm.Logger().ShowSQL(true)

	tb := &Test3Table{UID: "test_uid"}
	dbXorm.Sync2(tb)
	fmt.Println("sync succ")

	value := make(map[string]interface{})
	value["uid"] = "test_uid2"
	_, err = dbXorm.Table(tb.TableName()).Update(value, tb)
	if err != nil {
		fmt.Println("update err:", err.Error())
		return
	}

	fmt.Println("update map succ!")
}
> Hi, Could you paste your example codes here? I think you should not use this method directly. TestCode: ``` func main() { testInsertValue() } type Test3Table struct { ID int32 `xorm:"bigint autoincr pk id"` UID string `xorm:"varchar(64) uid"` } func (s *Test3Table) TableName() string { return "test_3_t" } func testInsertValue() { dbXorm, err := xorm.NewEngine("mysql", dbUrl) if err != nil { fmt.Println("open db err:", err.Error()) return } dbXorm.SetLogLevel(log.LOG_DEBUG) dbXorm.Logger().ShowSQL(true) tb := &Test3Table{UID: "test_uid"} dbXorm.Sync2(tb) fmt.Println("sync succ") value := make(map[string]interface{}) value["uid"] = "test_uid2" _, err = dbXorm.Table(tb.TableName()).Update(value, tb) if err != nil { fmt.Println("update err:", err.Error()) return } fmt.Println("update map succ!") } ```
lunny added the
kind
bug
label 2020-11-03 06:36:37 +00:00
lunny added this to the 1.0.6 milestone 2020-11-03 06:36:41 +00:00
lunny referenced this issue from a commit 2021-01-05 02:55:37 +00:00
lunny closed this issue 2021-01-05 02:55:38 +00:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1821
No description provided.