简介


Squirrel不是ORM,它可以帮助你通过Go Method的方式构建SQL

如何使用


  1. import sq "github.com/Masterminds/squirrel"
  1. users := sq.Select("*").
  2. From("user u").
  3. LeftJoin("user_info ui").
  4. Where("userId = 1000")
  5. sql, _, _ := users.ToSql()
  6. fmt.Printf("sql: %s\n", sql)

常用方法


  1. type QueryUser struct {
  2. ID uint
  3. Name string
  4. }
  5. func (q QueryUser) Query()
  1. type User struct {
  2. ID uint
  3. }
  4. func (u User) TableName() string {
  5. return "user"
  6. }
  7. func (u User) sqlSB(s string) sq.SelectBuilder {
  8. return sq.Select(s).From(fmt.Sprintf("%s u", User{}.TableName()))
  9. }
  10. func (u User) QueryById(id int) (sql string) {
  11. sql, _, _ = u.sqlSB("*").Where("id = ?", id).ToSql()
  12. return
  13. }
  14. func main() {
  15. fmt.Printf("sql: %s\n", User{}.QueryById(1000))
  16. }