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

« back to all changes in this revision

Viewing changes to cond_notin.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 (
 
8
        "fmt"
 
9
        "reflect"
 
10
        "strings"
 
11
)
 
12
 
 
13
type condNotIn condIn
 
14
 
 
15
var _ Cond = condNotIn{}
 
16
 
 
17
// NotIn generate NOT IN condition
 
18
func NotIn(col string, values ...interface{}) Cond {
 
19
        return condNotIn{col, values}
 
20
}
 
21
 
 
22
func (condNotIn condNotIn) handleBlank(w Writer) error {
 
23
        if _, err := fmt.Fprintf(w, "%s NOT IN ()", condNotIn.col); err != nil {
 
24
                return err
 
25
        }
 
26
        return nil
 
27
}
 
28
 
 
29
func (condNotIn condNotIn) WriteTo(w Writer) error {
 
30
        if len(condNotIn.vals) <= 0 {
 
31
                return condNotIn.handleBlank(w)
 
32
        }
 
33
 
 
34
        switch condNotIn.vals[0].(type) {
 
35
        case []int8:
 
36
                vals := condNotIn.vals[0].([]int8)
 
37
                if len(vals) <= 0 {
 
38
                        return condNotIn.handleBlank(w)
 
39
                }
 
40
                questionMark := strings.Repeat("?,", len(vals))
 
41
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
42
                        return err
 
43
                }
 
44
                for _, val := range vals {
 
45
                        w.Append(val)
 
46
                }
 
47
        case []int16:
 
48
                vals := condNotIn.vals[0].([]int16)
 
49
                if len(vals) <= 0 {
 
50
                        return condNotIn.handleBlank(w)
 
51
                }
 
52
                questionMark := strings.Repeat("?,", len(vals))
 
53
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
54
                        return err
 
55
                }
 
56
                for _, val := range vals {
 
57
                        w.Append(val)
 
58
                }
 
59
        case []int:
 
60
                vals := condNotIn.vals[0].([]int)
 
61
                if len(vals) <= 0 {
 
62
                        return condNotIn.handleBlank(w)
 
63
                }
 
64
                questionMark := strings.Repeat("?,", len(vals))
 
65
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
66
                        return err
 
67
                }
 
68
                for _, val := range vals {
 
69
                        w.Append(val)
 
70
                }
 
71
        case []int32:
 
72
                vals := condNotIn.vals[0].([]int32)
 
73
                if len(vals) <= 0 {
 
74
                        return condNotIn.handleBlank(w)
 
75
                }
 
76
                questionMark := strings.Repeat("?,", len(vals))
 
77
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
78
                        return err
 
79
                }
 
80
                for _, val := range vals {
 
81
                        w.Append(val)
 
82
                }
 
83
        case []int64:
 
84
                vals := condNotIn.vals[0].([]int64)
 
85
                if len(vals) <= 0 {
 
86
                        return condNotIn.handleBlank(w)
 
87
                }
 
88
                questionMark := strings.Repeat("?,", len(vals))
 
89
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
90
                        return err
 
91
                }
 
92
                for _, val := range vals {
 
93
                        w.Append(val)
 
94
                }
 
95
        case []uint8:
 
96
                vals := condNotIn.vals[0].([]uint8)
 
97
                if len(vals) <= 0 {
 
98
                        return condNotIn.handleBlank(w)
 
99
                }
 
100
                questionMark := strings.Repeat("?,", len(vals))
 
101
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
102
                        return err
 
103
                }
 
104
                for _, val := range vals {
 
105
                        w.Append(val)
 
106
                }
 
107
        case []uint16:
 
108
                vals := condNotIn.vals[0].([]uint16)
 
109
                if len(vals) <= 0 {
 
110
                        return condNotIn.handleBlank(w)
 
111
                }
 
112
                questionMark := strings.Repeat("?,", len(vals))
 
113
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
114
                        return err
 
115
                }
 
116
                for _, val := range vals {
 
117
                        w.Append(val)
 
118
                }
 
119
        case []uint:
 
120
                vals := condNotIn.vals[0].([]uint)
 
121
                if len(vals) <= 0 {
 
122
                        return condNotIn.handleBlank(w)
 
123
                }
 
124
                questionMark := strings.Repeat("?,", len(vals))
 
125
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
126
                        return err
 
127
                }
 
128
                for _, val := range vals {
 
129
                        w.Append(val)
 
130
                }
 
131
        case []uint32:
 
132
                vals := condNotIn.vals[0].([]uint32)
 
133
                if len(vals) <= 0 {
 
134
                        return condNotIn.handleBlank(w)
 
135
                }
 
136
                questionMark := strings.Repeat("?,", len(vals))
 
137
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
138
                        return err
 
139
                }
 
140
                for _, val := range vals {
 
141
                        w.Append(val)
 
142
                }
 
143
        case []uint64:
 
144
                vals := condNotIn.vals[0].([]uint64)
 
145
                if len(vals) <= 0 {
 
146
                        return condNotIn.handleBlank(w)
 
147
                }
 
148
                questionMark := strings.Repeat("?,", len(vals))
 
149
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
150
                        return err
 
151
                }
 
152
                for _, val := range vals {
 
153
                        w.Append(val)
 
154
                }
 
155
        case []string:
 
156
                vals := condNotIn.vals[0].([]string)
 
157
                if len(vals) <= 0 {
 
158
                        return condNotIn.handleBlank(w)
 
159
                }
 
160
                questionMark := strings.Repeat("?,", len(vals))
 
161
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
162
                        return err
 
163
                }
 
164
                for _, val := range vals {
 
165
                        w.Append(val)
 
166
                }
 
167
        case []interface{}:
 
168
                vals := condNotIn.vals[0].([]interface{})
 
169
                if len(vals) <= 0 {
 
170
                        return condNotIn.handleBlank(w)
 
171
                }
 
172
                questionMark := strings.Repeat("?,", len(vals))
 
173
                if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
174
                        return err
 
175
                }
 
176
                w.Append(vals...)
 
177
        case expr:
 
178
                val := condNotIn.vals[0].(expr)
 
179
                if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
 
180
                        return err
 
181
                }
 
182
                if err := val.WriteTo(w); err != nil {
 
183
                        return err
 
184
                }
 
185
                if _, err := fmt.Fprintf(w, ")"); err != nil {
 
186
                        return err
 
187
                }
 
188
        case *Builder:
 
189
                val := condNotIn.vals[0].(*Builder)
 
190
                if _, err := fmt.Fprintf(w, "%s NOT IN (", condNotIn.col); err != nil {
 
191
                        return err
 
192
                }
 
193
                if err := val.WriteTo(w); err != nil {
 
194
                        return err
 
195
                }
 
196
                if _, err := fmt.Fprintf(w, ")"); err != nil {
 
197
                        return err
 
198
                }
 
199
        default:
 
200
                v := reflect.ValueOf(condNotIn.vals[0])
 
201
                if v.Kind() == reflect.Slice {
 
202
                        l := v.Len()
 
203
                        if l == 0 {
 
204
                                return condNotIn.handleBlank(w)
 
205
                        }
 
206
 
 
207
                        questionMark := strings.Repeat("?,", l)
 
208
                        if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
209
                                return err
 
210
                        }
 
211
 
 
212
                        for i := 0; i < l; i++ {
 
213
                                w.Append(v.Index(i).Interface())
 
214
                        }
 
215
                } else {
 
216
                        questionMark := strings.Repeat("?,", len(condNotIn.vals))
 
217
                        if _, err := fmt.Fprintf(w, "%s NOT IN (%s)", condNotIn.col, questionMark[:len(questionMark)-1]); err != nil {
 
218
                                return err
 
219
                        }
 
220
                        w.Append(condNotIn.vals...)
 
221
                }
 
222
        }
 
223
        return nil
 
224
}
 
225
 
 
226
func (condNotIn condNotIn) And(conds ...Cond) Cond {
 
227
        return And(condNotIn, And(conds...))
 
228
}
 
229
 
 
230
func (condNotIn condNotIn) Or(conds ...Cond) Cond {
 
231
        return Or(condNotIn, Or(conds...))
 
232
}
 
233
 
 
234
func (condNotIn condNotIn) IsValid() bool {
 
235
        return len(condNotIn.col) > 0 && len(condNotIn.vals) > 0
 
236
}