dialect/sql: add predicates for composite where query (#264)

* add predicates for composite where query

Signed-off-by: Christoph Hartmann <chris@lollyrock.com>

* use existing table.Columns method

* simplify the composite operator methods
This commit is contained in:
Christoph Hartmann
2020-01-02 17:42:26 +01:00
committed by Ariel Mashraki
parent 0dfc9954a3
commit 46982d2f05
2 changed files with 76 additions and 0 deletions

View File

@@ -1061,6 +1061,38 @@ func (p *Predicate) ContainsFold(col, sub string) *Predicate {
})
}
func CompositeGT(columns []string, args ...interface{}) *Predicate {
return (&Predicate{}).CompositeGT(columns, args...)
}
func CompositeLT(columns []string, args ...interface{}) *Predicate {
return (&Predicate{}).CompositeLT(columns, args...)
}
func (p *Predicate) compositeP(operator string, columns []string, args ...interface{}) *Predicate {
return p.append(func(b *Builder) {
b.Nested(func(nb *Builder) {
nb.IdentComma(columns...)
})
b.WriteString(operator)
b.WriteString("(")
b.Args(args...)
b.WriteString(")")
})
}
// GT returns a composite ">" predicate.
func (p *Predicate) CompositeGT(columns []string, args ...interface{}) *Predicate {
const operator = " > "
return p.compositeP(operator, columns, args...)
}
// LT appends a composite "<" predicate.
func (p *Predicate) CompositeLT(columns []string, args ...interface{}) *Predicate {
const operator = " < "
return p.compositeP(operator, columns, args...)
}
// Query returns query representation of a predicate.
func (p *Predicate) Query() (string, []interface{}) {
for _, f := range p.fns {