~ubuntu-branches/debian/sid/golang-github-go-xorm-builder/sid

« back to all changes in this revision

Viewing changes to cond_neq.go

  • Committer: Package Import Robot
  • Author(s): Michael Lustfield
  • Date: 2017-03-12 03:54:03 UTC
  • Revision ID: package-import@ubuntu.com-20170312035403-csl9031z2j2aswfd
Tags: upstream-0.0~git20170224.0.c6e604e
ImportĀ upstreamĀ versionĀ 0.0~git20170224.0.c6e604e

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 The Xorm Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package builder
 
6
 
 
7
import "fmt"
 
8
 
 
9
// Neq defines not equal conditions
 
10
type Neq map[string]interface{}
 
11
 
 
12
var _ Cond = Neq{}
 
13
 
 
14
// WriteTo writes SQL to Writer
 
15
func (neq Neq) WriteTo(w Writer) error {
 
16
        var args = make([]interface{}, 0, len(neq))
 
17
        var i = 0
 
18
        for k, v := range neq {
 
19
                switch v.(type) {
 
20
                case []int, []int64, []string, []int32, []int16, []int8:
 
21
                        if err := NotIn(k, v).WriteTo(w); err != nil {
 
22
                                return err
 
23
                        }
 
24
                case expr:
 
25
                        if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
 
26
                                return err
 
27
                        }
 
28
 
 
29
                        if err := v.(expr).WriteTo(w); err != nil {
 
30
                                return err
 
31
                        }
 
32
 
 
33
                        if _, err := fmt.Fprintf(w, ")"); err != nil {
 
34
                                return err
 
35
                        }
 
36
                case *Builder:
 
37
                        if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
 
38
                                return err
 
39
                        }
 
40
 
 
41
                        if err := v.(*Builder).WriteTo(w); err != nil {
 
42
                                return err
 
43
                        }
 
44
 
 
45
                        if _, err := fmt.Fprintf(w, ")"); err != nil {
 
46
                                return err
 
47
                        }
 
48
                default:
 
49
                        if _, err := fmt.Fprintf(w, "%s<>?", k); err != nil {
 
50
                                return err
 
51
                        }
 
52
                        args = append(args, v)
 
53
                }
 
54
                if i != len(neq)-1 {
 
55
                        if _, err := fmt.Fprint(w, " AND "); err != nil {
 
56
                                return err
 
57
                        }
 
58
                }
 
59
                i = i + 1
 
60
        }
 
61
        w.Append(args...)
 
62
        return nil
 
63
}
 
64
 
 
65
// And implements And with other conditions
 
66
func (neq Neq) And(conds ...Cond) Cond {
 
67
        return And(neq, And(conds...))
 
68
}
 
69
 
 
70
// Or implements Or with other conditions
 
71
func (neq Neq) Or(conds ...Cond) Cond {
 
72
        return Or(neq, Or(conds...))
 
73
}
 
74
 
 
75
// IsValid tests if this condition is valid
 
76
func (neq Neq) IsValid() bool {
 
77
        return len(neq) > 0
 
78
}