SQL builder for Xorm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.