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
|
|
|
|
|
2018-07-20 07:40:17 +05:30
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
)
|
2016-11-04 03:46:01 +05:30
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Incr implements a type used by Eq
|
2016-11-04 03:46:01 +05:30
|
|
|
type Incr int
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Decr implements a type used by Eq
|
2016-11-04 03:46:01 +05:30
|
|
|
type Decr int
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Eq defines equals conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
type Eq map[string]interface{}
|
|
|
|
|
|
|
|
var _ Cond = Eq{}
|
|
|
|
|
2019-09-24 18:52:39 +05:30
|
|
|
// OpWriteTo writes conditions with special operator
|
|
|
|
func (eq Eq) OpWriteTo(op string, w Writer) error {
|
2016-11-04 03:46:01 +05:30
|
|
|
var i = 0
|
2018-07-20 07:40:17 +05:30
|
|
|
for _, k := range eq.sortedKeys() {
|
|
|
|
v := eq[k]
|
2016-11-04 03:46:01 +05:30
|
|
|
switch v.(type) {
|
|
|
|
case []int, []int64, []string, []int32, []int16, []int8, []uint, []uint64, []uint32, []uint16, []interface{}:
|
|
|
|
if err := In(k, v).WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case expr:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s=(", k); 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=(", k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.(*Builder).WriteTo(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := fmt.Fprintf(w, ")"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case Incr:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s=%s+?", k, k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Append(int(v.(Incr)))
|
|
|
|
case Decr:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s=%s-?", k, k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Append(int(v.(Decr)))
|
2020-03-22 20:42:55 +05:30
|
|
|
case nil:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s=null", k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-04 03:46:01 +05:30
|
|
|
default:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s=?", k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Append(v)
|
|
|
|
}
|
|
|
|
if i != len(eq)-1 {
|
|
|
|
if _, err := fmt.Fprint(w, op); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = i + 1
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// WriteTo writes SQL to Writer
|
2016-11-04 03:46:01 +05:30
|
|
|
func (eq Eq) WriteTo(w Writer) error {
|
2019-09-24 18:52:39 +05:30
|
|
|
return eq.OpWriteTo(" AND ", w)
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// And implements And with other conditions
|
2016-11-04 03:46:01 +05:30
|
|
|
func (eq Eq) And(conds ...Cond) Cond {
|
|
|
|
return And(eq, 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 (eq Eq) Or(conds ...Cond) Cond {
|
|
|
|
return Or(eq, 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 (eq Eq) IsValid() bool {
|
|
|
|
return len(eq) > 0
|
|
|
|
}
|
2018-07-20 07:40:17 +05:30
|
|
|
|
|
|
|
// sortedKeys returns all keys of this Eq sorted with sort.Strings.
|
|
|
|
// It is used internally for consistent ordering when generating
|
2019-09-24 18:52:39 +05:30
|
|
|
// SQL, see https://gitea.com/xorm/builder/issues/10
|
2018-07-20 07:40:17 +05:30
|
|
|
func (eq Eq) sortedKeys() []string {
|
|
|
|
keys := make([]string, 0, len(eq))
|
|
|
|
for key := range eq {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
return keys
|
|
|
|
}
|