-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.go
More file actions
155 lines (124 loc) · 2.86 KB
/
select.go
File metadata and controls
155 lines (124 loc) · 2.86 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 tsbuilder
import (
"bytes"
"fmt"
"strings"
)
var _ TdEngineSQLBuilder = (*SelectBuilder)(nil)
type SelectBuilder struct {
columns []string
from string
whereConditions []string
groupBy string
partitionBy string
orderBy string
limit *uint32
slimit *uint32
offset *uint32
soffset *uint32
}
func NewSelectBuilder() *SelectBuilder {
return &SelectBuilder{
columns: make([]string, 0),
whereConditions: make([]string, 0),
}
}
func (s *SelectBuilder) Columns(columns ...string) *SelectBuilder {
s.columns = columns
return s
}
func (s *SelectBuilder) From(from string) *SelectBuilder {
s.from = from
return s
}
func (s *SelectBuilder) Where(conditions ...string) *SelectBuilder {
s.whereConditions = append(s.whereConditions, conditions...)
return s
}
func (s *SelectBuilder) GroupBy(value string) *SelectBuilder {
s.groupBy = value
return s
}
func (s *SelectBuilder) PartitionBy(value string) *SelectBuilder {
s.partitionBy = value
return s
}
func (s *SelectBuilder) OrderBy(value string) *SelectBuilder {
s.orderBy = value
return s
}
func (s *SelectBuilder) Limit(value *uint32) *SelectBuilder {
s.limit = value
return s
}
func (s *SelectBuilder) SLimit(value *uint32) *SelectBuilder {
s.slimit = value
return s
}
func (s *SelectBuilder) Offset(value *uint32) *SelectBuilder {
s.offset = value
return s
}
func (s *SelectBuilder) SOffset(value *uint32) *SelectBuilder {
s.soffset = value
return s
}
func (s *SelectBuilder) Build() (string, error) {
if err := s.validate(); err != nil {
return "", fmt.Errorf("validate error: %w", err)
}
b := bytes.NewBuffer([]byte{})
b.WriteString("SELECT ")
// add columns
b.WriteString(strings.Join(s.columns, ", "))
b.WriteString(" ")
// add from
b.WriteString("FROM " + s.from + " ")
// add where conditions
if len(s.whereConditions) > 0 {
b.WriteString("WHERE ")
b.WriteString(strings.Join(s.whereConditions, " AND "))
}
// add group by
if s.groupBy != "" {
b.WriteString(" GROUP BY " + s.groupBy + " ")
}
// add partition by
if s.partitionBy != "" {
b.WriteString(" PARTITION BY " + s.partitionBy + " ")
}
// add order by
if s.orderBy != "" {
b.WriteString(" ORDER BY " + s.orderBy + " ")
}
// add limit
if s.limit != nil {
b.WriteString(fmt.Sprintf("LIMIT %d", *s.limit) + " ")
}
// add slimit
if s.slimit != nil {
b.WriteString(fmt.Sprintf("SLIMIT %d", *s.slimit) + " ")
}
// add offset
if s.offset != nil {
fmt.Fprintf(b, "OFFSET %d", *s.offset)
}
// add soffset
if s.soffset != nil {
if s.offset != nil {
b.WriteString(" ")
}
fmt.Fprintf(b, "SOFFSET %d", *s.soffset)
}
b.WriteString(";")
return b.String(), nil
}
func (s *SelectBuilder) validate() error {
if s.from == "" {
return ErrEmptyFrom
}
if len(s.columns) == 0 {
return ErrEmptyColumns
}
return nil
}