-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.go
189 lines (150 loc) · 4.74 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package azamat
import (
"database/sql"
"fmt"
sq "github.com/Masterminds/squirrel"
)
type SelectBuilder[T any] struct {
sq.SelectBuilder
}
func Select[T any](columns ...string) SelectBuilder[T] {
return SelectBuilder[T]{
SelectBuilder: sq.Select(columns...),
}
}
func (b SelectBuilder[T]) All(runner Runner) ([]T, error) {
sql, args, err := b.ToSql()
if err != nil {
return nil, err
}
var rows []T
err = runner.Select(&rows, sql, args...)
return rows, err
}
func (b SelectBuilder[T]) Only(runner Runner) (T, error) {
var row T
sql, args, err := b.ToSql()
if err != nil {
return row, err
}
var rows []T
if err := runner.Select(&rows, sql, args...); err != nil {
return row, err
}
if len(rows) == 0 {
return row, fmt.Errorf("none found")
}
if len(rows) != 1 {
return row, fmt.Errorf("expected to only get row")
}
return rows[0], nil
}
func (b SelectBuilder[T]) PlaceholderFormat(f sq.PlaceholderFormat) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.PlaceholderFormat(f)
return b
}
func (b SelectBuilder[T]) RunWith(runner sq.BaseRunner) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.RunWith(runner)
return b
}
func (b SelectBuilder[T]) Exec() (sql.Result, error) {
return b.SelectBuilder.Exec()
}
func (b SelectBuilder[T]) Prefix(sql string, args ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Prefix(sql, args...)
return b
}
func (b SelectBuilder[T]) PrefixExpr(expr sq.Sqlizer) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.PrefixExpr(expr)
return b
}
func (b SelectBuilder[T]) Distinct() SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Distinct()
return b
}
func (b SelectBuilder[T]) Options(options ...string) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Options(options...)
return b
}
// Downgrades the builder to a regular squirrel builder
func (b SelectBuilder[T]) Columns(columns ...string) sq.SelectBuilder {
return b.SelectBuilder.Columns(columns...)
}
// Downgrades the builder to a regular squirrel builder
func (b SelectBuilder[T]) Column(
column interface{}, args ...interface{},
) sq.SelectBuilder {
return b.SelectBuilder.Column(column, args...)
}
func (b SelectBuilder[T]) From(from string) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.From(from)
return b
}
func (b SelectBuilder[T]) JoinClause(pred interface{}, args ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.JoinClause(pred, args...)
return b
}
func (b SelectBuilder[T]) Join(join string, rest ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Join(join, rest...)
return b
}
func (b SelectBuilder[T]) LeftJoin(join string, rest ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.LeftJoin(join, rest...)
return b
}
func (b SelectBuilder[T]) RightJoin(join string, rest ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.RightJoin(join, rest...)
return b
}
func (b SelectBuilder[T]) InnerJoin(join string, rest ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.InnerJoin(join, rest...)
return b
}
func (b SelectBuilder[T]) CrossJoin(join string, rest ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.CrossJoin(join, rest...)
return b
}
func (b SelectBuilder[T]) Where(pred interface{}, args ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Where(pred, args...)
return b
}
func (b SelectBuilder[T]) GroupBy(groupBys ...string) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.GroupBy(groupBys...)
return b
}
func (b SelectBuilder[T]) Having(pred interface{}, rest ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Having(pred, rest...)
return b
}
func (b SelectBuilder[T]) OrderByClause(pred interface{}, args ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.OrderByClause(pred, args...)
return b
}
func (b SelectBuilder[T]) OrderBy(orderBys ...string) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.OrderBy(orderBys...)
return b
}
func (b SelectBuilder[T]) Limit(limit uint64) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Limit(limit)
return b
}
func (b SelectBuilder[T]) RemoveLimit() SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.RemoveLimit()
return b
}
func (b SelectBuilder[T]) Offset(offset uint64) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Offset(offset)
return b
}
func (b SelectBuilder[T]) RemoveOffset() SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.RemoveOffset()
return b
}
func (b SelectBuilder[T]) Suffix(sql string, args ...interface{}) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.Suffix(sql, args...)
return b
}
func (b SelectBuilder[T]) SuffixExpr(expr sq.Sqlizer) SelectBuilder[T] {
b.SelectBuilder = b.SelectBuilder.SuffixExpr(expr)
return b
}