-
Notifications
You must be signed in to change notification settings - Fork 6
/
select.go
142 lines (115 loc) · 2.93 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
// Code generated by queryx, DO NOT EDIT.
package queryx
import (
"fmt"
"strings"
)
type SelectStatement struct {
selection []string
from string
where *Clause
limit *int
offset *int
order []string
group string
having string
distinct []string
joins []string
}
func NewSelect() *SelectStatement {
return &SelectStatement{}
}
func (s *SelectStatement) Select(selection ...string) *SelectStatement {
s.selection = selection
return s
}
func (s *SelectStatement) From(from string) *SelectStatement {
s.from = from
return s
}
func (s *SelectStatement) Where(clauses ...*Clause) *SelectStatement {
if len(clauses) == 0 {
return s
}
if s.where == nil {
s.where = clauses[0].And(clauses[1:]...)
} else {
s.where = s.where.And(clauses...)
}
return s
}
func (s *SelectStatement) Limit(limit int) *SelectStatement {
s.limit = &limit
return s
}
func (s *SelectStatement) Offset(offset int) *SelectStatement {
s.offset = &offset
return s
}
func (s *SelectStatement) Order(order ...string) *SelectStatement {
s.order = append(s.order, order...)
return s
}
func (s *SelectStatement) Distinct(distinct ...string) *SelectStatement {
s.distinct = distinct
return s
}
func (s *SelectStatement) GroupBy(group string) *SelectStatement {
s.group = group
return s
}
func (s *SelectStatement) Having(having string) *SelectStatement {
s.having = having
return s
}
func (s *SelectStatement) Join(join ...string) *SelectStatement {
s.joins = append(s.joins, join...)
return s
}
// convert select statement to update statement
func (s *SelectStatement) Update() *UpdateStatement {
if s.limit != nil {
return NewUpdate().Table(s.from).Where(s.where) // TODO: convert into subquery
}
return NewUpdate().Table(s.from).Where(s.where)
}
// convert select statement to delete statement
func (s *SelectStatement) Delete() *DeleteStatemnet {
return NewDelete().From(s.from).Where(s.where)
}
func (s *SelectStatement) ToSQL() (string, []interface{}) {
var query string
if len(s.distinct) > 0 {
query = fmt.Sprintf("SELECT DISTINCT %s FROM %s", strings.Join(s.distinct, ", "), s.from)
} else {
query = fmt.Sprintf("SELECT %s FROM %s", strings.Join(s.selection, ", "), s.from)
}
args := []interface{}{}
if len(s.joins) > 0 {
for i := 0; i < len(s.joins); i++ {
query = fmt.Sprintf("%s %s", query, s.joins[i])
}
}
if s.where != nil {
query = fmt.Sprintf("%s WHERE %s", query, s.where.fragment)
args = append(args, s.where.args...)
}
if s.group != "" {
query = fmt.Sprintf("%s GROUP BY %s", query, s.group)
}
if s.having != "" {
query = fmt.Sprintf("%s HAVING %s", query, s.having)
}
if s.order != nil {
query = fmt.Sprintf("%s ORDER BY %s", query, strings.Join(s.order, ", "))
}
if s.limit != nil {
query = fmt.Sprintf("%s LIMIT ?", query)
args = append(args, *s.limit)
}
if s.offset != nil {
query = fmt.Sprintf("%s OFFSET ?", query)
args = append(args, *s.offset)
}
return query, args
}