2017-01-03 16:20:28 +08:00
|
|
|
// 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 06:16:01 +08:00
|
|
|
package builder
|
|
|
|
|
2018-07-20 10:10:17 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
)
|
2016-11-04 06:16:01 +08:00
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
// Neq defines not equal conditions
|
2016-11-04 06:16:01 +08:00
|
|
|
type Neq map[string]interface{}
|
|
|
|
|
|
|
|
var _ Cond = Neq{}
|
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
// WriteTo writes SQL to Writer
|
2016-11-04 06:16:01 +08:00
|
|
|
func (neq Neq) WriteTo(w Writer) error {
|
|
|
|
var args = make([]interface{}, 0, len(neq))
|
|
|
|
var i = 0
|
2018-07-20 10:10:17 +08:00
|
|
|
for _, k := range neq.sortedKeys() {
|
|
|
|
v := neq[k]
|
2016-11-04 06:16:01 +08:00
|
|
|
switch v.(type) {
|
|
|
|
case []int, []int64, []string, []int32, []int16, []int8:
|
|
|
|
if err := NotIn(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
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if _, err := fmt.Fprintf(w, "%s<>?", k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args = append(args, v)
|
|
|
|
}
|
|
|
|
if i != len(neq)-1 {
|
|
|
|
if _, err := fmt.Fprint(w, " AND "); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = i + 1
|
|
|
|
}
|
|
|
|
w.Append(args...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
// And implements And with other conditions
|
2016-11-04 06:16:01 +08:00
|
|
|
func (neq Neq) And(conds ...Cond) Cond {
|
|
|
|
return And(neq, And(conds...))
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
// Or implements Or with other conditions
|
2016-11-04 06:16:01 +08:00
|
|
|
func (neq Neq) Or(conds ...Cond) Cond {
|
|
|
|
return Or(neq, Or(conds...))
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:20:28 +08:00
|
|
|
// IsValid tests if this condition is valid
|
2016-11-04 06:16:01 +08:00
|
|
|
func (neq Neq) IsValid() bool {
|
|
|
|
return len(neq) > 0
|
|
|
|
}
|
2018-07-20 10:10:17 +08:00
|
|
|
|
|
|
|
// sortedKeys returns all keys of this Neq sorted with sort.Strings.
|
|
|
|
// It is used internally for consistent ordering when generating
|
2019-09-24 21:22:39 +08:00
|
|
|
// SQL, see https://gitea.com/xorm/builder/issues/10
|
2018-07-20 10:10:17 +08:00
|
|
|
func (neq Neq) sortedKeys() []string {
|
|
|
|
keys := make([]string, 0, len(neq))
|
|
|
|
for key := range neq {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
return keys
|
|
|
|
}
|