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

« back to all changes in this revision

Viewing changes to builder.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
type optype byte
 
8
 
 
9
const (
 
10
        condType   optype = iota // only conditions
 
11
        selectType               // select
 
12
        insertType               // insert
 
13
        updateType               // update
 
14
        deleteType               // delete
 
15
)
 
16
 
 
17
type join struct {
 
18
        joinType  string
 
19
        joinTable string
 
20
        joinCond  Cond
 
21
}
 
22
 
 
23
// Builder describes a SQL statement
 
24
type Builder struct {
 
25
        optype
 
26
        tableName string
 
27
        cond      Cond
 
28
        selects   []string
 
29
        joins     []join
 
30
        inserts   Eq
 
31
        updates   []Eq
 
32
}
 
33
 
 
34
// Select creates a select Builder
 
35
func Select(cols ...string) *Builder {
 
36
        builder := &Builder{cond: NewCond()}
 
37
        return builder.Select(cols...)
 
38
}
 
39
 
 
40
// Insert creates an insert Builder
 
41
func Insert(eq Eq) *Builder {
 
42
        builder := &Builder{cond: NewCond()}
 
43
        return builder.Insert(eq)
 
44
}
 
45
 
 
46
// Update creates an update Builder
 
47
func Update(updates ...Eq) *Builder {
 
48
        builder := &Builder{cond: NewCond()}
 
49
        return builder.Update(updates...)
 
50
}
 
51
 
 
52
// Delete creates a delete Builder
 
53
func Delete(conds ...Cond) *Builder {
 
54
        builder := &Builder{cond: NewCond()}
 
55
        return builder.Delete(conds...)
 
56
}
 
57
 
 
58
// Where sets where SQL
 
59
func (b *Builder) Where(cond Cond) *Builder {
 
60
        b.cond = b.cond.And(cond)
 
61
        return b
 
62
}
 
63
 
 
64
// From sets the table name
 
65
func (b *Builder) From(tableName string) *Builder {
 
66
        b.tableName = tableName
 
67
        return b
 
68
}
 
69
 
 
70
// Into sets insert table name
 
71
func (b *Builder) Into(tableName string) *Builder {
 
72
        b.tableName = tableName
 
73
        return b
 
74
}
 
75
 
 
76
// Join sets join table and contions
 
77
func (b *Builder) Join(joinType, joinTable string, joinCond interface{}) *Builder {
 
78
        switch joinCond.(type) {
 
79
        case Cond:
 
80
                b.joins = append(b.joins, join{joinType, joinTable, joinCond.(Cond)})
 
81
        case string:
 
82
                b.joins = append(b.joins, join{joinType, joinTable, Expr(joinCond.(string))})
 
83
        }
 
84
 
 
85
        return b
 
86
}
 
87
 
 
88
// InnerJoin sets inner join
 
89
func (b *Builder) InnerJoin(joinTable string, joinCond interface{}) *Builder {
 
90
        return b.Join("INNER", joinTable, joinCond)
 
91
}
 
92
 
 
93
// LeftJoin sets left join SQL
 
94
func (b *Builder) LeftJoin(joinTable string, joinCond interface{}) *Builder {
 
95
        return b.Join("LEFT", joinTable, joinCond)
 
96
}
 
97
 
 
98
// RightJoin sets right join SQL
 
99
func (b *Builder) RightJoin(joinTable string, joinCond interface{}) *Builder {
 
100
        return b.Join("RIGHT", joinTable, joinCond)
 
101
}
 
102
 
 
103
// CrossJoin sets cross join SQL
 
104
func (b *Builder) CrossJoin(joinTable string, joinCond interface{}) *Builder {
 
105
        return b.Join("CROSS", joinTable, joinCond)
 
106
}
 
107
 
 
108
// FullJoin sets full join SQL
 
109
func (b *Builder) FullJoin(joinTable string, joinCond interface{}) *Builder {
 
110
        return b.Join("FULL", joinTable, joinCond)
 
111
}
 
112
 
 
113
// Select sets select SQL
 
114
func (b *Builder) Select(cols ...string) *Builder {
 
115
        b.selects = cols
 
116
        b.optype = selectType
 
117
        return b
 
118
}
 
119
 
 
120
// And sets AND condition
 
121
func (b *Builder) And(cond Cond) *Builder {
 
122
        b.cond = And(b.cond, cond)
 
123
        return b
 
124
}
 
125
 
 
126
// Or sets OR condition
 
127
func (b *Builder) Or(cond Cond) *Builder {
 
128
        b.cond = Or(b.cond, cond)
 
129
        return b
 
130
}
 
131
 
 
132
// Insert sets insert SQL
 
133
func (b *Builder) Insert(eq Eq) *Builder {
 
134
        b.inserts = eq
 
135
        b.optype = insertType
 
136
        return b
 
137
}
 
138
 
 
139
// Update sets update SQL
 
140
func (b *Builder) Update(updates ...Eq) *Builder {
 
141
        b.updates = updates
 
142
        b.optype = updateType
 
143
        return b
 
144
}
 
145
 
 
146
// Delete sets delete SQL
 
147
func (b *Builder) Delete(conds ...Cond) *Builder {
 
148
        b.cond = b.cond.And(conds...)
 
149
        b.optype = deleteType
 
150
        return b
 
151
}
 
152
 
 
153
// WriteTo implements Writer interface
 
154
func (b *Builder) WriteTo(w Writer) error {
 
155
        switch b.optype {
 
156
        case condType:
 
157
                return b.cond.WriteTo(w)
 
158
        case selectType:
 
159
                return b.selectWriteTo(w)
 
160
        case insertType:
 
161
                return b.insertWriteTo(w)
 
162
        case updateType:
 
163
                return b.updateWriteTo(w)
 
164
        case deleteType:
 
165
                return b.deleteWriteTo(w)
 
166
        }
 
167
 
 
168
        return ErrNotSupportType
 
169
}
 
170
 
 
171
// ToSQL convert a builder to SQL and args
 
172
func (b *Builder) ToSQL() (string, []interface{}, error) {
 
173
        w := NewWriter()
 
174
        if err := b.WriteTo(w); err != nil {
 
175
                return "", nil, err
 
176
        }
 
177
 
 
178
        return w.writer.String(), w.args, nil
 
179
}
 
180
 
 
181
// ToSQL convert a builder or condtions to SQL and args
 
182
func ToSQL(cond interface{}) (string, []interface{}, error) {
 
183
        switch cond.(type) {
 
184
        case Cond:
 
185
                return condToSQL(cond.(Cond))
 
186
        case *Builder:
 
187
                return cond.(*Builder).ToSQL()
 
188
        }
 
189
        return "", nil, ErrNotSupportType
 
190
}