Documentation
¶
Overview ¶
Package pqcomp provides dead simple query builder that support null types from sql package, but also provide interface Appearer.
Benchmarks ¶
To be comprehensive solution, query builder needs to be optimized. Some of the benchmark results:
BenchmarkComposer_AddExpr-4 5000000 470 ns/op 100 B/op 1 allocs/op BenchmarkComposer_Placeholder-4 10000000 130 ns/op 4 B/op 2 allocs/op BenchmarkComposer_Args-4 300000000 4.89 ns/op 0 B/op 0 allocs/op BenchmarkComposer_New-4 200000 15311 ns/op 5120 B/op 4 allocs/op
Example ¶
package main
import (
"fmt"
"database/sql"
"github.com/piotrkowalczuk/pqcomp"
)
func main() {
var (
uquery, wquery string
)
comp := pqcomp.New(1, 1, 1, 3)
update := comp.Compose()
where := comp.Compose()
comp.AddArg(10)
update.AddExpr("u.username", pqcomp.Equal, "johnsnow")
update.AddExpr("u.first_name", pqcomp.Equal, "John")
update.AddExpr("u.last_name", pqcomp.Equal, &sql.NullString{String: "Snow", Valid: true})
where.AddExpr("u.id", pqcomp.Equal, 1)
where.AddExpr("u.age", pqcomp.GreaterThan, &sql.NullInt64{Int64: 1000, Valid: false})
if update.Len() == 0 || where.Len() == 0 {
return
}
for update.Next() {
if update.First() {
uquery += "SET "
} else {
uquery += ", "
}
uquery += fmt.Sprintf("%s %s %s", update.Key(), update.Oper(), update.PlaceHolder())
}
for where.Next() {
if where.First() {
wquery += "WHERE "
} else {
wquery += ", "
}
wquery += fmt.Sprintf("%s %s %s", where.Key(), where.Oper(), where.PlaceHolder())
}
fmt.Println(where.Args()...)
fmt.Println(update.Args()...)
fmt.Println(comp.Args()...)
fmt.Printf("UPDATE users AS u %s %s LIMIT $1 \n", uquery, wquery)
}
Output: 1 johnsnow John &{Snow true} 10 johnsnow John &{Snow true} 1 UPDATE users AS u SET u.username = $2, u.first_name = $3, u.last_name = $4 WHERE u.id = $5 LIMIT $1
Index ¶
- Constants
- Variables
- type Appearer
- type Composer
- func (c *Composer) AddArg(arg interface{})
- func (c *Composer) AddExpr(key, operator string, value interface{})
- func (c *Composer) Args() []interface{}
- func (c *Composer) Compose(nbOfChildExpressions ...int) (comp *Composer)
- func (b *Composer) First() bool
- func (b *Composer) Key() string
- func (b *Composer) Len() int
- func (b *Composer) Next() bool
- func (b *Composer) Oper() string
- func (b *Composer) PlaceHolder() string
- func (b *Composer) Reset()
Examples ¶
Constants ¶
const ( // Like represents LIKE operator. Like = "LIKE" // In represents IN operator. In = "IN" // IsNull represents IS NULL keywords. IsNull = "IS NULL" // IsNotNull represents IS NOT NULL keywords. IsNotNull = "IS NOT NULL" // Is represents IS operator. Is = "IS" // Equal represents equal operator. Equal = "=" // NotEqual represents not equal operator. NotEqual = "<>" // GreaterThan represents greater than operator. GreaterThan = ">" // LessThan represents lower than operator. LessThan = "<" // GTE represents greater than or equal operator. GreaterThanOrEqual = ">=" // LTE represents lower than or equal operator. LessThanOrEqual = "<=" // DESC represents descendant way of sorting. Descendant = "DESC" // ASC represents ascendant way of sorting. Ascendant = "ASC" // Any ... Any = "ANY" // All ... All = "ALL" // Contains ... Contains = "@>" // IsContainedBy ... IsContainedBy = "<@" // Overlap ... Overlap = "??" // Exists ... Exists = "?" // ExistsAny ...` ExistsAny = "?|" // ExistsAll ... ExistsAll = "?&" )
Variables ¶
var (
// Empty ...
Empty = struct{}{}
)
Functions ¶
This section is empty.
Types ¶
type Appearer ¶
type Appearer interface {
// Appear returns true if object should be used by AddExpr method.
// Otherwise it is ignored.
Appear() bool
}
Appearer wraps Appear function.
type Composer ¶
type Composer struct {
// contains filtered or unexported fields
}
Composer is some sort of stateful iterator that helps to build complex SQL queries.
func New ¶
New allocates new Composer and pre-allocates space for given amount of arguments and expressions. Each child expression passed to the constructor creates new child Composer with pre-allocated space for expressions.
func (*Composer) AddExpr ¶
AddExpr adds expression if value meet certain requirements. To know more please read the source code.
func (*Composer) Args ¶
func (c *Composer) Args() []interface{}
Args returns slice of arguments that was passed to the composer or to any child.
func (*Composer) Compose ¶
Compose returns next available composer or if pool of pre-allocated Composer's is empty allocates new one.
func (*Composer) PlaceHolder ¶
PlaceHolder returns placeholder for current cursor position.