forked from chuckpreslar/codex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_manager.go
More file actions
115 lines (96 loc) · 3.31 KB
/
update_manager.go
File metadata and controls
115 lines (96 loc) · 3.31 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
package codex
// UpdateManager manages a tree that compiles to a SQL update statement.
type UpdateManager struct {
Tree *UpdateStatementNode // The AST for the SQL UPDATE statement.
Adapter adapter // The SQL Engine.
}
var _ Scoper = (*UpdateManager)(nil)
func (self *UpdateManager) Scopes(scopes ...ScopeFunc) *UpdateManager {
for _, scope := range scopes {
scope(self)
}
return self
}
func (self *UpdateManager) Scope(expr interface{}, args ...interface{}) {
self.Where(expr, args...)
}
// Set appends to the trees Values slice a list of ColumnNodes
// which are to be modified in the query.
func (self *UpdateManager) Set(columns ...interface{}) *UpdateManager {
for _, column := range columns {
self.Tree.Values = append(self.Tree.Values, Column(column))
}
return self
}
// To alters the trees Values slice to be an AssignmentNode, containing the
// column from Set at the same index of the value.
func (self *UpdateManager) To(values ...interface{}) *UpdateManager {
for index, value := range values {
if index < len(self.Tree.Values) {
column := self.Tree.Values[index]
self.Tree.Values[index] = Assignment(column, value)
}
}
return self
}
// Where appends an sql WHERE condition to the current tree's Wheres slice,
//
// Where("a") // no args -> Group(Literal("a"))
// Where("a = ?", 123) // with args -> Group(Literal("a = ?", 123))
// Where("a = ? AND b = ?", 123, true) // with args -> Group(Literal("a = ? AND b = ?", 123, true))
// Where(Equal(Column("a"), Column("b"))) // no args -> Group(Equal(Column("a"), Column("b")))
func (self *UpdateManager) Where(expr interface{}, args ...interface{}) *UpdateManager {
if str, ok := expr.(string); ok {
expr = Literal(str, args...)
}
// enclose expr in Grouping - except if expr is already a Grouping
if _, ok := expr.(*GroupingNode); !ok {
expr = Grouping(expr)
}
self.Tree.Wheres = append(self.Tree.Wheres, expr)
return self
}
// Sets the Tree's Limit to the given integer.
func (self *UpdateManager) Limit(expr interface{}) *UpdateManager {
self.Tree.Limit = Limit(expr)
return self
}
// Selection returns a *SelectManager while keeping
// wheres, limit and adapter
func (self *UpdateManager) Selection() *SelectManager {
m := Selection(self.Tree.Table)
m.Tree.Wheres = self.Tree.Wheres
m.Tree.Limit = self.Tree.Limit
m.Adapter = self.Adapter
return m
}
// Insertion returns a *InsertManager while keeping
// relation and adapter
func (self *UpdateManager) Insertion() *InsertManager {
m := Insertion(self.Tree.Table)
m.Adapter = self.Adapter
return m
}
// Deletion returns a *DeleteManager while keeping
// wheres, limit and adapter
func (self *UpdateManager) Deletion() *DeleteManager {
m := Deletion(self.Tree.Table)
m.Tree.Wheres = self.Tree.Wheres
m.Tree.Limit = self.Tree.Limit
m.Adapter = self.Adapter
return m
}
// ToSql calls a visitor's Accept method based on the manager's SQL adapter.
func (self *UpdateManager) ToSql() (string, []interface{}, error) {
return VisitorFor(self.Adapter).Accept(self.Tree)
}
func (self *UpdateManager) Table() *TableNode {
return self.Tree.Table
}
// UpdateManager factory method.
func Modification(relation *TableNode) (m *UpdateManager) {
m = new(UpdateManager)
m.Tree = UpdateStatement(relation)
m.Adapter = relation.Adapter
return
}