forked from wheelcomplex/kmg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SelectCommand.go
132 lines (126 loc) · 3.32 KB
/
SelectCommand.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package MysqlAst
type SelectCommand struct {
selectExprList Node
tableReferenceList Node
whereCondition WhereCondition
groupByList Node
have WhereCondition
orderByList Node
limit Node
isForUpdate bool
isLockInShareMode bool
}
func NewSelectCommand() *SelectCommand {
return &SelectCommand{}
}
func (c *SelectCommand) GetPrepareParameter() (output string, parameterList []string) {
output = "SELECT "
if c.selectExprList == nil {
output += "*"
} else {
output += c.selectExprList.GetText()
}
if c.tableReferenceList != nil {
output += " FROM " + c.tableReferenceList.GetText()
}
if c.whereCondition != nil && !c.whereCondition.IsEmpty() {
text, parameter := c.whereCondition.GetPrepareParameter()
output += " WHERE " + text
parameterList = append(parameterList, parameter...)
}
if c.groupByList != nil {
output += " GROUP BY " + c.groupByList.GetText()
}
if c.have != nil && c.have.IsEmpty() {
text, parameter := c.have.GetPrepareParameter()
output += " HAVE " + text
parameterList = append(parameterList, parameter...)
}
if c.orderByList != nil {
output += " ORDER BY " + c.orderByList.GetText()
}
if c.limit != nil {
output += " LIMIT " + c.limit.GetText()
}
if c.isForUpdate {
output += " FOR UPDATE "
}
if c.isLockInShareMode {
output += " LOCK IN SHARE MODE "
}
return
}
func (c *SelectCommand) Copy() *SelectCommand {
s := &SelectCommand{}
if c.selectExprList != nil {
s.selectExprList = c.selectExprList.Copy()
}
if c.tableReferenceList != nil {
s.tableReferenceList = c.tableReferenceList.Copy()
}
if c.whereCondition != nil {
s.whereCondition = c.whereCondition.Copy().(WhereCondition)
}
if c.groupByList != nil {
s.groupByList = c.groupByList.Copy()
}
if c.have != nil {
s.have = c.have.Copy().(WhereCondition)
}
if c.orderByList != nil {
s.orderByList = c.orderByList.Copy()
}
if c.limit != nil {
s.limit = c.limit.Copy()
}
s.isForUpdate = c.isForUpdate
s.isLockInShareMode = c.isLockInShareMode
return s
}
func (c *SelectCommand) Select(text string) *SelectCommand {
c.selectExprList = Text(text)
return c
}
func (c *SelectCommand) From(text string) *SelectCommand {
c.tableReferenceList = Text(text)
return c
}
func (c *SelectCommand) Where(text string, parameterList ...string) *SelectCommand {
c.whereCondition = Prepare(text, parameterList...)
return c
}
func (c *SelectCommand) WhereObj(obj WhereCondition) *SelectCommand {
c.whereCondition = obj
return c
}
func (c *SelectCommand) GroupBy(text string) *SelectCommand {
c.groupByList = Text(text)
return c
}
func (c *SelectCommand) Have(text string, parameterList ...string) *SelectCommand {
c.have = Prepare(text, parameterList...)
return c
}
func (c *SelectCommand) OrderBy(text string) *SelectCommand {
c.orderByList = Text(text)
return c
}
func (c *SelectCommand) Limit(text string) *SelectCommand {
c.limit = Text(text)
return c
}
func (c *SelectCommand) ForUpdate() *SelectCommand {
c.isForUpdate = true
return c
}
func (c *SelectCommand) LockInShareMode() *SelectCommand {
c.isLockInShareMode = true
return c
}
func (c *SelectCommand) GetWhereCondition() WhereCondition {
return c.whereCondition
}
func (c *SelectCommand) AddAndWhereCondition(node PreparedNode) *SelectCommand {
c.whereCondition = c.whereCondition.AddAndCondition(node)
return c
}