You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
3.3 KiB
161 lines
3.3 KiB
8 years ago
|
// 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.
|
||
|
|
||
8 years ago
|
package builder
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
8 years ago
|
// WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.
|
||
8 years ago
|
func WriteMap(w Writer, data map[string]interface{}, op string) error {
|
||
|
var args = make([]interface{}, 0, len(data))
|
||
|
var i = 0
|
||
6 years ago
|
keys := make([]string, 0, len(data))
|
||
|
for k := range data {
|
||
|
keys = append(keys, k)
|
||
|
}
|
||
|
|
||
|
for _, k := range keys {
|
||
|
v := data[k]
|
||
8 years ago
|
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
|
||
|
}
|
||
|
|
||
8 years ago
|
// Lt defines < condition
|
||
8 years ago
|
type Lt map[string]interface{}
|
||
|
|
||
|
var _ Cond = Lt{}
|
||
|
|
||
8 years ago
|
// WriteTo write SQL to Writer
|
||
8 years ago
|
func (lt Lt) WriteTo(w Writer) error {
|
||
|
return WriteMap(w, lt, "<")
|
||
|
}
|
||
|
|
||
8 years ago
|
// And implements And with other conditions
|
||
8 years ago
|
func (lt Lt) And(conds ...Cond) Cond {
|
||
|
return condAnd{lt, And(conds...)}
|
||
|
}
|
||
|
|
||
8 years ago
|
// Or implements Or with other conditions
|
||
8 years ago
|
func (lt Lt) Or(conds ...Cond) Cond {
|
||
|
return condOr{lt, Or(conds...)}
|
||
|
}
|
||
|
|
||
8 years ago
|
// IsValid tests if this Eq is valid
|
||
8 years ago
|
func (lt Lt) IsValid() bool {
|
||
|
return len(lt) > 0
|
||
|
}
|
||
|
|
||
8 years ago
|
// Lte defines <= condition
|
||
8 years ago
|
type Lte map[string]interface{}
|
||
|
|
||
|
var _ Cond = Lte{}
|
||
|
|
||
8 years ago
|
// WriteTo write SQL to Writer
|
||
8 years ago
|
func (lte Lte) WriteTo(w Writer) error {
|
||
|
return WriteMap(w, lte, "<=")
|
||
|
}
|
||
|
|
||
8 years ago
|
// And implements And with other conditions
|
||
8 years ago
|
func (lte Lte) And(conds ...Cond) Cond {
|
||
|
return And(lte, And(conds...))
|
||
|
}
|
||
|
|
||
8 years ago
|
// Or implements Or with other conditions
|
||
8 years ago
|
func (lte Lte) Or(conds ...Cond) Cond {
|
||
|
return Or(lte, Or(conds...))
|
||
|
}
|
||
|
|
||
8 years ago
|
// IsValid tests if this Eq is valid
|
||
8 years ago
|
func (lte Lte) IsValid() bool {
|
||
|
return len(lte) > 0
|
||
|
}
|
||
|
|
||
8 years ago
|
// Gt defines > condition
|
||
8 years ago
|
type Gt map[string]interface{}
|
||
|
|
||
|
var _ Cond = Gt{}
|
||
|
|
||
8 years ago
|
// WriteTo write SQL to Writer
|
||
8 years ago
|
func (gt Gt) WriteTo(w Writer) error {
|
||
|
return WriteMap(w, gt, ">")
|
||
|
}
|
||
|
|
||
8 years ago
|
// And implements And with other conditions
|
||
8 years ago
|
func (gt Gt) And(conds ...Cond) Cond {
|
||
|
return And(gt, And(conds...))
|
||
|
}
|
||
|
|
||
8 years ago
|
// Or implements Or with other conditions
|
||
8 years ago
|
func (gt Gt) Or(conds ...Cond) Cond {
|
||
|
return Or(gt, Or(conds...))
|
||
|
}
|
||
|
|
||
8 years ago
|
// IsValid tests if this Eq is valid
|
||
8 years ago
|
func (gt Gt) IsValid() bool {
|
||
|
return len(gt) > 0
|
||
|
}
|
||
|
|
||
8 years ago
|
// Gte defines >= condition
|
||
8 years ago
|
type Gte map[string]interface{}
|
||
|
|
||
|
var _ Cond = Gte{}
|
||
|
|
||
8 years ago
|
// WriteTo write SQL to Writer
|
||
8 years ago
|
func (gte Gte) WriteTo(w Writer) error {
|
||
|
return WriteMap(w, gte, ">=")
|
||
|
}
|
||
|
|
||
8 years ago
|
// And implements And with other conditions
|
||
8 years ago
|
func (gte Gte) And(conds ...Cond) Cond {
|
||
|
return And(gte, And(conds...))
|
||
|
}
|
||
|
|
||
8 years ago
|
// Or implements Or with other conditions
|
||
8 years ago
|
func (gte Gte) Or(conds ...Cond) Cond {
|
||
|
return Or(gte, Or(conds...))
|
||
|
}
|
||
|
|
||
8 years ago
|
// IsValid tests if this Eq is valid
|
||
8 years ago
|
func (gte Gte) IsValid() bool {
|
||
|
return len(gte) > 0
|
||
|
}
|