-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_builder.go
102 lines (89 loc) · 2.83 KB
/
update_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package query
//UpdateBuilder is a builder for UPDATE statements
type UpdateBuilder struct {
query string
}
//NewUpdateBuilder returns a new *UpdateBuilder
func NewUpdateBuilder() *UpdateBuilder {
return new(UpdateBuilder)
}
//Update adds an UPDATE statemens to the builder's query
//table represents the database table to update.
func (u *UpdateBuilder) Update(table string) *UpdateBuilder {
u.query = "UPDATE " + table
return u
}
//Set adds a field and its new value to the builder's query
//Update must be called prior to Set.
func (u *UpdateBuilder) Set(field string) *UpdateBuilder {
u.query += " SET " + field
return u
}
//SetFromMap adds a WHERE clause to the builder's query with fields and new values
//derived from ixToField which should map integers(allows for proper ordering)
//to fields equating their new values.
//
//You should use consecutive integers starting from zero.
//
//Usage example:
// SetFromMap(map[int]string{
// 0: "FirstName='Daniel'",
// 1: "LastName='Jamie'",
// })
//
// Note that string values in ixToValues beginning with '(' won't be quoted
// by this method, as they will be assumed to be subqueries.
func (u *UpdateBuilder) SetFromMap(ixToField map[int]interface{}) *UpdateBuilder {
u.query += withSetMap("SET", ixToField)
return u
}
//Where adds a WHERE clause to the builder's query.
//condition is the desired condition
func (u *UpdateBuilder) Where(condition string) *UpdateBuilder {
u.query += where(condition)
return u
}
//WhereWithMap adds a WHERE clause to the builder's query with fields and conditions
//derived from ixToCond which should map integers(allows for proper ordering)
//to conditions desired to be met.
//You should use consecutive integers starting from zero.
//Usage example:
// WhereWithMap(map[int]string{
// 0: "CategoryID=3 OR",
// 1: "BarcodeID=22",
// })
func (u *UpdateBuilder) WhereWithMap(ixToCond map[int]interface{}) *UpdateBuilder {
u.query += withMap("WHERE", ixToCond, false)
return u
}
//Returning selects fields from the temporary inserted table
func (u *UpdateBuilder) Returning(fields ...string) *UpdateBuilder {
u.query += " RETURNING" + addFields("", false, fields...)
return u
}
//ReturningAll selects all fields from the temporary inserted table
func (u *UpdateBuilder) ReturningAll() *UpdateBuilder {
u.query += " RETURNING *"
return u
}
//And is an alternative to WhereWithMap
//
//it adds an AND along with the condition specified
func (u *UpdateBuilder) And(condition string) *UpdateBuilder {
u.query += and(condition)
return u
}
//Or is an alternative to WhereWithMap
//
//it adds an OR along with the condition specified
func (u *UpdateBuilder) Or(condition string) *UpdateBuilder {
u.query += or(condition)
return u
}
//Clear erases the builder's query
func (u *UpdateBuilder) Clear() {
u.query = ""
}
func (u *UpdateBuilder) String() string {
return u.query + ";"
}