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

« back to all changes in this revision

Viewing changes to cond_eq.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
// Incr implements a type used by Eq
 
10
type Incr int
 
11
 
 
12
// Decr implements a type used by Eq
 
13
type Decr int
 
14
 
 
15
// Eq defines equals conditions
 
16
type Eq map[string]interface{}
 
17
 
 
18
var _ Cond = Eq{}
 
19
 
 
20
func (eq Eq) opWriteTo(op string, w Writer) error {
 
21
        var i = 0
 
22
        for k, v := range eq {
 
23
                switch v.(type) {
 
24
                case []int, []int64, []string, []int32, []int16, []int8, []uint, []uint64, []uint32, []uint16, []interface{}:
 
25
                        if err := In(k, v).WriteTo(w); err != nil {
 
26
                                return err
 
27
                        }
 
28
                case expr:
 
29
                        if _, err := fmt.Fprintf(w, "%s=(", k); err != nil {
 
30
                                return err
 
31
                        }
 
32
 
 
33
                        if err := v.(expr).WriteTo(w); err != nil {
 
34
                                return err
 
35
                        }
 
36
 
 
37
                        if _, err := fmt.Fprintf(w, ")"); err != nil {
 
38
                                return err
 
39
                        }
 
40
                case *Builder:
 
41
                        if _, err := fmt.Fprintf(w, "%s=(", k); err != nil {
 
42
                                return err
 
43
                        }
 
44
 
 
45
                        if err := v.(*Builder).WriteTo(w); err != nil {
 
46
                                return err
 
47
                        }
 
48
 
 
49
                        if _, err := fmt.Fprintf(w, ")"); err != nil {
 
50
                                return err
 
51
                        }
 
52
                case Incr:
 
53
                        if _, err := fmt.Fprintf(w, "%s=%s+?", k, k); err != nil {
 
54
                                return err
 
55
                        }
 
56
                        w.Append(int(v.(Incr)))
 
57
                case Decr:
 
58
                        if _, err := fmt.Fprintf(w, "%s=%s-?", k, k); err != nil {
 
59
                                return err
 
60
                        }
 
61
                        w.Append(int(v.(Decr)))
 
62
                default:
 
63
                        if _, err := fmt.Fprintf(w, "%s=?", k); err != nil {
 
64
                                return err
 
65
                        }
 
66
                        w.Append(v)
 
67
                }
 
68
                if i != len(eq)-1 {
 
69
                        if _, err := fmt.Fprint(w, op); err != nil {
 
70
                                return err
 
71
                        }
 
72
                }
 
73
                i = i + 1
 
74
        }
 
75
        return nil
 
76
}
 
77
 
 
78
// WriteTo writes SQL to Writer
 
79
func (eq Eq) WriteTo(w Writer) error {
 
80
        return eq.opWriteTo(" AND ", w)
 
81
}
 
82
 
 
83
// And implements And with other conditions
 
84
func (eq Eq) And(conds ...Cond) Cond {
 
85
        return And(eq, And(conds...))
 
86
}
 
87
 
 
88
// Or implements Or with other conditions
 
89
func (eq Eq) Or(conds ...Cond) Cond {
 
90
        return Or(eq, Or(conds...))
 
91
}
 
92
 
 
93
// IsValid tests if this Eq is valid
 
94
func (eq Eq) IsValid() bool {
 
95
        return len(eq) > 0
 
96
}