-
Notifications
You must be signed in to change notification settings - Fork 1
/
select.go
149 lines (129 loc) · 2.62 KB
/
select.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package templates
import (
"bytes"
"fmt"
"github.com/madcatz0r/tmpl/conditions"
"strings"
)
const (
inner string = "INNER JOIN"
left = "LEFT OUTER JOIN"
right = "RIGHT OUTER JOIN"
full = "FULL OUTER JOIN"
)
type sel struct {
table string
fields []string
join []*join
where string
groupBy string
orderBy string
limit int
offset int
err error
}
type selString struct {
Table string
Fields []string
Join []*join
Where string
GroupBy string
OrderBy string
Limit int
Offset int
err error
}
func Select(fields ...string) *sel {
return &sel{fields: fields}
}
func (s *sel) From(model interface{}) *sel {
t, err := GetTmpl(model)
if err != nil {
s.err = err
return s
}
s.table = t.TableName
return s
}
func (s *sel) Where(builder *conditions.ConditionBuilder) *sel {
s.where = builder.String()
return s
}
func (s *sel) Limit(limit int) *sel {
s.limit = limit
return s
}
func (s *sel) Offset(offset int) *sel {
s.offset = offset
return s
}
func (s *sel) GroupBy(fields ...string) *sel {
s.groupBy = strings.Join(fields, ",")
return s
}
func (s *sel) OrderBy(fields ...string) *sel {
s.orderBy = strings.Join(fields, ",")
return s
}
func (s *sel) InnerJoin(model interface{}) *join {
j := &join{JoinType: inner, sel: s}
return j.join(model)
}
func (s *sel) LeftJoin(model interface{}) *join {
j := &join{JoinType: left, sel: s}
return j.join(model)
}
func (s *sel) RightJoin(model interface{}) *join {
j := &join{JoinType: right, sel: s}
return j.join(model)
}
func (s *sel) FullJoin(model interface{}) *join {
j := &join{JoinType: full, sel: s}
return j.join(model)
}
type join struct {
Table string
JoinType string
Condition string
sel *sel
}
func (j *join) join(model interface{}) *join {
t, err := GetTmpl(model)
if err != nil {
j.sel.err = err
return j
}
j.Table = t.TableName
j.sel.join = append(j.sel.join, j)
return j
}
func (j *join) On(builder *conditions.ConditionBuilder) *sel {
j.Condition = builder.String()
return j.sel
}
func Syn(syn string, fields ...string) string {
if len(fields) == 0 {
return ""
}
synAdd := fmt.Sprintf(",%s.", syn)
return synAdd[1:] + strings.Join(stripFieldNames(fields), synAdd)
}
func (s *sel) String() (string, error) {
if s.err != nil {
return "", s.err
}
selStr := &selString{
Table: s.table,
Fields: s.fields,
Join: s.join,
Where: s.where,
GroupBy: s.groupBy,
OrderBy: s.orderBy,
Limit: s.limit,
Offset: s.offset,
err: s.err,
}
var selectSql = new(bytes.Buffer)
err := selectTmpl.Execute(selectSql, selStr)
return selectSql.String(), err
}