Skip to content

Repository files navigation

sqlbuilder

sqlbuilder is a simple sql query string builder

sqlbuilder its recursive struct call, that you can easy to build sql string

GoDoc

simple example :

b:=sb.NewSQLBuilder("SQLite")
b.Select("a", "b").
From("tblA").
Join("tblB").
Where("a", "=", 1).
Where("b", "=", "str").
Limit(100).
BuildSelectSQL()
fmt.Println(b.BuildedSQL())
b.Release()

more case can see test case

go-model

how to use sqlbuilder with go-model :

package db
import (
"database/sql""strconv"
model "github.com/eehsiao/go-models"
sb "github.com/eehsiao/sqlbuilder"
)
func (dao*Dao) GetExgs(params...string) (e*[]*Exg, errerror) {
var (
b=sb.NewSQLBuilder("SQLite")
rows*sql.Rows
)
e=&[]*Exg{}
deferfunc() {
ifrows!=nil {
rows.Close()
}
b.Release()
rows=nil
}()
b.Select(model.Inst2Fields(Exg{})...).
From(TbExgs).
Where("is_active", "=", 1)
iflen(params) >0&&params[0] !="" {
b.Where("exg_code", "=", params[0])
}
iflen(params) >1&&params[1] !="" {
ifi, err:=strconv.Atoi(params[1]); err==nil&&i>0 {
b.Limit(i)
}
}
ifrows, err=dao.Get(b); err==nil {
forrows.Next() {
exg:=Exg{}
iferr=rows.Scan(model.Struct4Scan(&exg)...); err==nil {
*e=append(*e, &exg)
} else {
return
}
}
}
return
}
func (dao*Dao) UpdateOrInsertExg(t*Exg) (r sql.Result, errerror) {
var (
b=sb.NewSQLBuilder("SQLite")
cntint64withoutKey= []string{"id", "exg_code"}
)
deferfunc() {
b.Release()
}()
b.Set(model.Inst2Set(*t, withoutKey...)).
From(TbExgs).
Where("exg_code", "=", t.ExgCode).
BuildUpdateSQL()
ifr, err=dao.ExecBuilder(b); err==nil {
ifcnt, err=r.RowsAffected(); err==nil&&cnt==0 {
b.Fields(model.Inst2FieldWithoutID(*t)...).
Values(model.Inst2Values(*t, "id")...).
Into(TbExgs).
BuildInsertSQL()
r, err=dao.ExecBuilder(b)
}
}
return
}

About

easy to build sql string

Topics

Resources

Stars

1 star

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages