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

« back to all changes in this revision

Viewing changes to cond_compare.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
// WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.
 
10
func WriteMap(w Writer, data map[string]interface{}, op string) error {
 
11
        var args = make([]interface{}, 0, len(data))
 
12
        var i = 0
 
13
        for k, v := range data {
 
14
                switch v.(type) {
 
15
                case expr:
 
16
                        if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
 
17
                                return err
 
18
                        }
 
19
 
 
20
                        if err := v.(expr).WriteTo(w); err != nil {
 
21
                                return err
 
22
                        }
 
23
 
 
24
                        if _, err := fmt.Fprintf(w, ")"); err != nil {
 
25
                                return err
 
26
                        }
 
27
                case *Builder:
 
28
                        if _, err := fmt.Fprintf(w, "%s%s(", k, op); err != nil {
 
29
                                return err
 
30
                        }
 
31
 
 
32
                        if err := v.(*Builder).WriteTo(w); err != nil {
 
33
                                return err
 
34
                        }
 
35
 
 
36
                        if _, err := fmt.Fprintf(w, ")"); err != nil {
 
37
                                return err
 
38
                        }
 
39
                default:
 
40
                        if _, err := fmt.Fprintf(w, "%s%s?", k, op); err != nil {
 
41
                                return err
 
42
                        }
 
43
                        args = append(args, v)
 
44
                }
 
45
                if i != len(data)-1 {
 
46
                        if _, err := fmt.Fprint(w, " AND "); err != nil {
 
47
                                return err
 
48
                        }
 
49
                }
 
50
                i = i + 1
 
51
        }
 
52
        w.Append(args...)
 
53
        return nil
 
54
}
 
55
 
 
56
// Lt defines < condition
 
57
type Lt map[string]interface{}
 
58
 
 
59
var _ Cond = Lt{}
 
60
 
 
61
// WriteTo write SQL to Writer
 
62
func (lt Lt) WriteTo(w Writer) error {
 
63
        return WriteMap(w, lt, "<")
 
64
}
 
65
 
 
66
// And implements And with other conditions
 
67
func (lt Lt) And(conds ...Cond) Cond {
 
68
        return condAnd{lt, And(conds...)}
 
69
}
 
70
 
 
71
// Or implements Or with other conditions
 
72
func (lt Lt) Or(conds ...Cond) Cond {
 
73
        return condOr{lt, Or(conds...)}
 
74
}
 
75
 
 
76
// IsValid tests if this Eq is valid
 
77
func (lt Lt) IsValid() bool {
 
78
        return len(lt) > 0
 
79
}
 
80
 
 
81
// Lte defines <= condition
 
82
type Lte map[string]interface{}
 
83
 
 
84
var _ Cond = Lte{}
 
85
 
 
86
// WriteTo write SQL to Writer
 
87
func (lte Lte) WriteTo(w Writer) error {
 
88
        return WriteMap(w, lte, "<=")
 
89
}
 
90
 
 
91
// And implements And with other conditions
 
92
func (lte Lte) And(conds ...Cond) Cond {
 
93
        return And(lte, And(conds...))
 
94
}
 
95
 
 
96
// Or implements Or with other conditions
 
97
func (lte Lte) Or(conds ...Cond) Cond {
 
98
        return Or(lte, Or(conds...))
 
99
}
 
100
 
 
101
// IsValid tests if this Eq is valid
 
102
func (lte Lte) IsValid() bool {
 
103
        return len(lte) > 0
 
104
}
 
105
 
 
106
// Gt defines > condition
 
107
type Gt map[string]interface{}
 
108
 
 
109
var _ Cond = Gt{}
 
110
 
 
111
// WriteTo write SQL to Writer
 
112
func (gt Gt) WriteTo(w Writer) error {
 
113
        return WriteMap(w, gt, ">")
 
114
}
 
115
 
 
116
// And implements And with other conditions
 
117
func (gt Gt) And(conds ...Cond) Cond {
 
118
        return And(gt, And(conds...))
 
119
}
 
120
 
 
121
// Or implements Or with other conditions
 
122
func (gt Gt) Or(conds ...Cond) Cond {
 
123
        return Or(gt, Or(conds...))
 
124
}
 
125
 
 
126
// IsValid tests if this Eq is valid
 
127
func (gt Gt) IsValid() bool {
 
128
        return len(gt) > 0
 
129
}
 
130
 
 
131
// Gte defines >= condition
 
132
type Gte map[string]interface{}
 
133
 
 
134
var _ Cond = Gte{}
 
135
 
 
136
// WriteTo write SQL to Writer
 
137
func (gte Gte) WriteTo(w Writer) error {
 
138
        return WriteMap(w, gte, ">=")
 
139
}
 
140
 
 
141
// And implements And with other conditions
 
142
func (gte Gte) And(conds ...Cond) Cond {
 
143
        return And(gte, And(conds...))
 
144
}
 
145
 
 
146
// Or implements Or with other conditions
 
147
func (gte Gte) Or(conds ...Cond) Cond {
 
148
        return Or(gte, Or(conds...))
 
149
}
 
150
 
 
151
// IsValid tests if this Eq is valid
 
152
func (gte Gte) IsValid() bool {
 
153
        return len(gte) > 0
 
154
}