Recently, using Xorm, and using SQL Builder to build SQL queries, I didn't expect the original code to be used after the upgrade.
0x00 Code
sql, args, _ := builder.Select("*"). From("user"). Where(builder.Eq{"uid"1}). ToSQL()res, err := orm.QueryString(sql, args...)
0x01 vs.
The discovery of Xorm in 0.6.3 and 0.6.4 has been changed,
0.6.3
0.6.4
So, the first parameter was removed, changed to all variable parameters, then the wit of the args..., changed to args.
0x02 New Error
Did not expect to compile yes, run the Times wrong, prompt
Sql:converting argument $ type:unsupported Type []interface {}, a slice of interface
That is, a type error. Continue tracing the code and discover that there is a SQL-generated function in Session_query.go, with the following code:
func (session *Session) genQuerySQL(sqlorArgs ...interface{}) (string, []interfaceerror) { iflen0 { return sqlorArgs[0].(string), sqlorArgs[1nil } //省略}
Because Sqlorargs is slice, and builder. Tosql's args is also slice, then sqlorargs[1:] and create a new slice, let the last return of slice into two yuan slice, so there is the above type error.
0X03 Solutions
Think about it, actually I think the previous version of the function signature is better, two parameters, one is responsible for accepting SQL statements, one is responsible for receiving SQL variables. Issue to the author, perhaps the author has a better solution.
Here's my temporary workaround:
func (Session *session) Genquerysql (Sqlorargs ... interface {}) (string , []interface {}, error ) {if len (Sqlorargs) > 0 {if len (sqlorargs) = = 2 && reflect. TypeOf (Sqlorargs[1 ]). Kind () = = reflect. Slice {return sqlorargs[0 ]. ( string ), Sqlorargs[1 ]. ([]interface {}), nil } return Sqlora Rgs[0 ]. (string ), Sqlorargs[1 :], nil } //omit }
Xorm SQL Builder