简介
Squirrel不是ORM
,它可以帮助你通过Go Method
的方式构建SQL
如何使用
import sq "github.com/Masterminds/squirrel"
users := sq.Select("*").
From("user u").
LeftJoin("user_info ui").
Where("userId = 1000")
sql, _, _ := users.ToSql()
fmt.Printf("sql: %s\n", sql)
常用方法
type QueryUser struct {
ID uint
Name string
}
func (q QueryUser) Query()
type User struct {
ID uint
}
func (u User) TableName() string {
return "user"
}
func (u User) sqlSB(s string) sq.SelectBuilder {
return sq.Select(s).From(fmt.Sprintf("%s u", User{}.TableName()))
}
func (u User) QueryById(id int) (sql string) {
sql, _, _ = u.sqlSB("*").Where("id = ?", id).ToSql()
return
}
func main() {
fmt.Printf("sql: %s\n", User{}.QueryById(1000))
}