SQL构造器的使用场景与原生SQL基本一致,但是使用SQL构造器能够以一种更为简单的方式来编写SQL语句,并能够减少SQL编码过程中的错误。
func demoBuilderExec() {
//插入记录
id, err := worm.Table("users").Value("name", "name1").Value("age", 21).Insert()
log.Println(id)
//insert into users set name=?,age=?
//更新记录
affected, err := worm.Table("users").Value("name", "name2").Value("age", 22).Where("id=?", id).Update()
log.Println(affected)
//update users set age=22,name=? where id=?
//删除记录
affected, err = worm.Table("users").Where("id=?", id).Delete()
log.Println(affected)
//delete from users where id=?
}
说明:
func demoBuilderGet() {
//查询单条记录到model对象
var user User
has, err := worm.Table("users").Select("*").Where("id=?", 1).GetModel(&ent)
log.Println(user)
//select * from users where id=? limit 1
//查询单条记录
var name string; var age int64
has, err = worm.Table("users").Select("name", "age").Where("id=?", 1).Get(&name, &age)
log.Println(name, age)
//select name,age from users where id=? limit 1
//单条记录的单个字段的查询:
strval, err := worm.Table("users").Select("name").Where("id=?", 1).GetString()
log.Println(strval)
//select name from users where id=? limit 1
//单条记录的单个字段的查询:
intval, err := worm.Table("users").Select("age").Where("id=?", 1).GetInt()
log.Println(intval)
//select age from users where id=? limit 1
}
GetRow查询一条记录,并返回map[string]string类型的结果。
func demoBuilderGetRow() {
val, err := worm.Table("users").Select("*").Where("id=?", 1).GetRow()
if err != nil{
log.Println(err)
return
}
log.Println(val)
}
func demoBuilderFind() {
//查询多条记录到model数组
var users []User
err := worm.Table("users").Select("*").Where("id>?", 0).FindModel(&users)
log.Println(users)
//select * from users where id>?
//查询多条记录的单个字段到[]string:
arrstr, err := worm.Table("users").Select("name").Where("id>?", 0).FindString()
log.Println(arrstr)
//select name from users where id>?
//查询多条记录的单个字段到[]int64:
arrint, err := worm.Table("users").Select("age").Where("id>?", 0).FindInt()
log.Println(arrint)
//select age from users where id>?
}
worm支持链式API,可使用Where, And, Or, ID, In, Limit, GroupBy, OrderBy, Having等函数构造查询条件。
func demoBuilderWhere() {
var users []User
err := worm.Table("users").Select("*").Where("id>?", 0).FindModel(&users)
//select * from users where id>?
//limit
users = []User{}
err = worm.Table("users").Select("*").Where("age>?", 0).Offset(2).Limit(3).FindModel(&users)
//select * from users where age>? limit 2, 3
//OrderBy
users = []User{}
err = worm.Table("users").Select("*").Where("age>?", 0).OrderBy("name asc").FindModel(&users)
//select * from users where age>? order by name asc
//and
users = []User{}
err = worm.Table("users").Select("*").Where("age>?", 0).And("id<?", 10).FindModel(&users)
//select * from users where age>? and id<?
//like
users = []User{}
err = worm.Table("users").Select("*").Where("name like ?", "%name%").FindModel(&users)
//select * from users where name like ?
//in
users = []User{}
err = worm.Table("users").Select("*").Where("age>?", 0).AndIn("id", 5,6).FindModel(&users)
//select * from users where age>0 and id in (?,?)
}
func demoBuilderRows() {
sql := worm.Table("users").Select("*").Where("id>?", 0)
sql.OrderBy("name desc")
rows, err := sql.ModelRows()
if err != nil{
log.Println(err)
return
}
defer rows.Close()
for rows.Next(){
var user User
err = rows.Scan(&user)
if err != nil{
log.Println(err)
}
log.Println(user)
}
}
您也可以使用StringRows来获取map[string]string类型的结果:
func demoBuilderRows2() {
sql := worm.Table("users").Select("*").Where("id>?", 0)
sql.OrderBy("name desc")
rows, err := sql.StringRows()
if err != nil{
log.Println(err)
return
}
defer rows.Close()
for rows.Next(){
data := make(worm.StringRow)
err := rows.Scan(&data)
if err != nil{
log.Println(err)
}
log.Println(data)
}
}
worm允许使用SQL表达式更新列,例如:
func demoBuilderExpr() {
tb := worm.Table("user")
tb.Value("age", worm.Expr("age+?", 1))
tb.Value("created", time.Now())
ret, err := tb.Where("id=?", 1).Update()
if err != nil{
log.Println(err)
return
}
log.Println(ret)
}
//update user set age=age+?,created=? where id=?