-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselect.go
More file actions
155 lines (130 loc) · 3.12 KB
/
select.go
File metadata and controls
155 lines (130 loc) · 3.12 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
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
150
151
152
153
154
155
package q2sql
import (
"bytes"
"errors"
"strconv"
"strings"
)
// this builder is simplified version of the select
// builder from the github.com/Masterminds/squirrel project
// thanks to the project authors
type SelectBuilder struct {
IsDistinct bool
Columns []Sqlizer
FromPart Sqlizer
Joins []Sqlizer
WhereParts []Sqlizer
GroupBys []string
HavingParts []Sqlizer
OrderByParts []Sqlizer
LimitPart string
OffsetPart string
}
func (s *SelectBuilder) Select(columns []string) *SelectBuilder {
if len(columns) == 0 {
return s
}
s.Columns = append(s.Columns, Columns(columns))
return s
}
func (s *SelectBuilder) From(from string) *SelectBuilder {
s.FromPart = RawSQL(from)
return s
}
func (s *SelectBuilder) Join(clause Sqlizer) *SelectBuilder {
s.Joins = append(s.Joins, clause)
return s
}
func (s *SelectBuilder) Where(conditions ...Sqlizer) *SelectBuilder {
s.WhereParts = append(s.WhereParts, conditions...)
return s
}
func (s *SelectBuilder) OrderBy(clause Sqlizer) *SelectBuilder {
s.OrderByParts = append(s.OrderByParts, clause)
return s
}
func (s *SelectBuilder) GroupBy(groupBys ...string) *SelectBuilder {
s.GroupBys = append(s.GroupBys, groupBys...)
return s
}
func (s *SelectBuilder) Having(clause Sqlizer) *SelectBuilder {
s.HavingParts = append(s.HavingParts, clause)
return s
}
func (s *SelectBuilder) Limit(limit uint64) *SelectBuilder {
s.LimitPart = strconv.FormatUint(limit, 10)
return s
}
func (s *SelectBuilder) Offset(offset uint64) *SelectBuilder {
s.OffsetPart = strconv.FormatUint(offset, 10)
return s
}
func (s *SelectBuilder) Distinct() *SelectBuilder {
s.IsDistinct = true
return s
}
func (s *SelectBuilder) ToSQL() (sqlStr string, args []interface{}, err error) {
sql := new(bytes.Buffer)
args = make([]interface{}, 0)
if len(s.Columns) == 0 {
err = errors.New("select must have at least one column")
return
}
sql.WriteString("SELECT ")
if s.IsDistinct {
sql.WriteString("DISTINCT ")
}
args, err = appendToSQL(s.Columns, sql, ",", args)
if err != nil {
return "", nil, err
}
if s.FromPart != nil {
sql.WriteString(" FROM ")
args, err = appendToSQL([]Sqlizer{s.FromPart}, sql, "", args)
if err != nil {
return
}
}
if len(s.Joins) > 0 {
sql.WriteString(" ")
args, err = appendToSQL(s.Joins, sql, " ", args)
if err != nil {
return
}
}
if len(s.WhereParts) > 0 {
sql.WriteString(" WHERE ")
args, err = appendToSQL(s.WhereParts, sql, " AND ", args)
if err != nil {
return
}
}
if len(s.GroupBys) > 0 {
sql.WriteString(" GROUP BY ")
sql.WriteString(strings.Join(s.GroupBys, ", "))
}
if len(s.HavingParts) > 0 {
sql.WriteString(" HAVING ")
args, err = appendToSQL(s.HavingParts, sql, " AND ", args)
if err != nil {
return
}
}
if len(s.OrderByParts) > 0 {
sql.WriteString(" ORDER BY ")
args, err = appendToSQL(s.OrderByParts, sql, ", ", args)
if err != nil {
return
}
}
if s.LimitPart != "" {
sql.WriteString(" LIMIT ")
sql.WriteString(s.LimitPart)
}
if s.OffsetPart != "" {
sql.WriteString(" OFFSET ")
sql.WriteString(s.OffsetPart)
}
sqlStr = sql.String()
return sqlStr, args, err
}