SQL query building interface for Go.
func Select(table string, columns []string) *Statement
func Insert(table string, values map[string]string) *Statement
func Update(table string, values map[string]string) *Statement
func Delete(table string) *Statement
- Select
id, name, email
from a tableUsers
sqlego.Select("Users", []string{"id", "name", "email"})
- Insert/Update a new record to table
Users
sqlego.Insert("Users", map[string]string{"id": "2", "name": "brucehsu"}
sqlego.Update("Users", map[string]string{"id": "2", "name": "brucehsu"}
- Delete all records in table
Users
sqlego.Delete("Users")
WHERE clause
func (node *Statement) Where(preds ...*Predicate) *Statement
Adding condition predicates
func Eq(operand string, operand_second string) *Predicate // =
func Neq(operand string, operand_second string) *Predicate // <>
func Gt(operand string, operand_second string) *Predicate // >
func Gte(operand string, operand_second string) *Predicate // >=
func Lt(operand string, operand_second string) *Predicate // <
func Lte(operand string, operand_second string) *Predicate // <=
func Between(operand string, range_start string, range_end string) *Predicate // BETWEEN range_start AND range_end
Concatenating conditions
func (pred *Predicate) And(preds ...*Predicate) *Predicate
func (pred *Predicate) Or(preds ...*Predicate) *Predicate
Grouping predicates explicitly
func ExplicitPredicates(preds ...*Predicate) *Predicate
In functions that accept variadic arguments, given predicates would be concatenated with AND
by default except for Or()
.
- Select the user record of
Bruce
with following columnsid, name, email
from a tableUsers
node := sqlego.Select("Users", []string{"id", "name", "email"})
node.Where(sqlego.Eq("name", "Bruce"))
node.Compile() // => SELECT id,name,email FROM Users WHERE name=Bruce;
- Select the user records whose
id
equals to1
or range from5566
to7788
from a tableUsers
node := sqlego.Select("Users", []string{"id", "name", "email"})
node.Where(sqlego.Eq("id", "1").Or(sqlego.ExplicitPredicates(sqlego.Gte("id", "5566"), sqlego.Lte("id", "7788"))))
node.Compile() // => SELECT id,name,email FROM Users WHERE id=1 OR ( id>=5566 AND id<=7788 ) ;