Skip to content

brucehsu/sqlego

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sqlego

Build Status License

SQL query building interface for Go.

Usage

Basic CRUD operations

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

Examples

  • Select id, name, email from a table Users
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")

Conditions

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

Note

In functions that accept variadic arguments, given predicates would be concatenated with AND by default except for Or().

Examples

  • Select the user record of Bruce with following columns id, name, email from a table Users
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 to 1 or range from 5566 to 7788 from a table Users
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 ) ;

About

SQL query building interface for Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages