2017-01-03 13:50:28 +05:30
|
|
|
// Copyright 2016 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-11-04 03:46:01 +05:30
|
|
|
package builder
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.
|
2016-11-04 03:46:01 +05:30
|
|
|
func WriteMap(w Writer, data map[string]interface{}, op string) error {
|
|
|
|
var args = make([]interface{}, 0, len(data))
|
|
|
|
var i = 0
|
|
|
|
for k, v := range data {
|
|
|
|
switch v.(type) {
|
|
|
|
case expr:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.(expr).WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *Builder:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.(*Builder).WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s%s?", k, op); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args = append(args, v)
|
|
|
|
}
|
|
|
|
if i != len(data)-1 {
|
|
|
|
if _, err := fmt.Fprint(w, " AND "); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = i + 1
|
|
|
|
}
|
|
|
|
w.Append(args...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Lt defines < condition
|
2016-11-04 03:46:01 +05:30
|
|
|
type Lt map[string]interface{}
|
|
|
|
|
|
|
|
var _ Cond = Lt{}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// WriteTo write SQL to Writer
|
2016-11-04 03:46:01 +05:30
|
|
|
func (lt Lt) WriteTo(w Writer) error {
|
|
|
|
return WriteMap(w, lt, "<")
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// And implements And with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (lt Lt) And(conds ...Cond) Cond {
|
|
|
|
return condAnd{lt, And(conds...)}
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Or implements Or with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (lt Lt) Or(conds ...Cond) Cond {
|
|
|
|
return condOr{lt, Or(conds...)}
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// IsValid tests if this Eq is valid
|
2016-11-04 03:46:01 +05:30
|
|
|
func (lt Lt) IsValid() bool {
|
|
|
|
return len(lt) > 0
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Lte defines <= condition
|
2016-11-04 03:46:01 +05:30
|
|
|
type Lte map[string]interface{}
|
|
|
|
|
|
|
|
var _ Cond = Lte{}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// WriteTo write SQL to Writer
|
2016-11-04 03:46:01 +05:30
|
|
|
func (lte Lte) WriteTo(w Writer) error {
|
|
|
|
return WriteMap(w, lte, "<=")
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// And implements And with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (lte Lte) And(conds ...Cond) Cond {
|
|
|
|
return And(lte, And(conds...))
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Or implements Or with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (lte Lte) Or(conds ...Cond) Cond {
|
|
|
|
return Or(lte, Or(conds...))
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// IsValid tests if this Eq is valid
|
2016-11-04 03:46:01 +05:30
|
|
|
func (lte Lte) IsValid() bool {
|
|
|
|
return len(lte) > 0
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Gt defines > condition
|
2016-11-04 03:46:01 +05:30
|
|
|
type Gt map[string]interface{}
|
|
|
|
|
|
|
|
var _ Cond = Gt{}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// WriteTo write SQL to Writer
|
2016-11-04 03:46:01 +05:30
|
|
|
func (gt Gt) WriteTo(w Writer) error {
|
|
|
|
return WriteMap(w, gt, ">")
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// And implements And with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (gt Gt) And(conds ...Cond) Cond {
|
|
|
|
return And(gt, And(conds...))
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Or implements Or with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (gt Gt) Or(conds ...Cond) Cond {
|
|
|
|
return Or(gt, Or(conds...))
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// IsValid tests if this Eq is valid
|
2016-11-04 03:46:01 +05:30
|
|
|
func (gt Gt) IsValid() bool {
|
|
|
|
return len(gt) > 0
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Gte defines >= condition
|
2016-11-04 03:46:01 +05:30
|
|
|
type Gte map[string]interface{}
|
|
|
|
|
|
|
|
var _ Cond = Gte{}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// WriteTo write SQL to Writer
|
2016-11-04 03:46:01 +05:30
|
|
|
func (gte Gte) WriteTo(w Writer) error {
|
|
|
|
return WriteMap(w, gte, ">=")
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// And implements And with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (gte Gte) And(conds ...Cond) Cond {
|
|
|
|
return And(gte, And(conds...))
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Or implements Or with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (gte Gte) Or(conds ...Cond) Cond {
|
|
|
|
return Or(gte, Or(conds...))
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// IsValid tests if this Eq is valid
|
2016-11-04 03:46:01 +05:30
|
|
|
func (gte Gte) IsValid() bool {
|
|
|
|
return len(gte) > 0
|
|
|
|
}
|