-
Notifications
You must be signed in to change notification settings - Fork 27
/
querybuilder.go
211 lines (187 loc) · 6.7 KB
/
querybuilder.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package postgrest
import (
"context"
"encoding/json"
"fmt"
"strings"
)
// QueryBuilder describes a builder for a query.
type QueryBuilder struct {
client *Client
method string
body []byte
tableName string
headers map[string]string
params map[string]string
}
// ExecuteString runs the PostgREST query, returning the result as a JSON
// string.
func (q *QueryBuilder) ExecuteString() (string, int64, error) {
return executeString(context.Background(), q.client, q.method, q.body, []string{q.tableName}, q.headers, q.params)
}
// ExecuteStringWithContext runs the PostgREST query, returning the result as
// a JSON string.
func (q *QueryBuilder) ExecuteStringWithContext(ctx context.Context) (string, int64, error) {
return executeString(ctx, q.client, q.method, q.body, []string{q.tableName}, q.headers, q.params)
}
// Execute runs the Postgrest query, returning the result as a byte slice.
func (q *QueryBuilder) Execute() ([]byte, int64, error) {
return execute(context.Background(), q.client, q.method, q.body, []string{q.tableName}, q.headers, q.params)
}
// ExecuteWithContext runs the PostgREST query with the given context,
// returning the result as a byte slice.
func (q *QueryBuilder) ExecuteWithContext(ctx context.Context) ([]byte, int64, error) {
return execute(ctx, q.client, q.method, q.body, []string{q.tableName}, q.headers, q.params)
}
// ExecuteTo runs the PostgREST query, encoding the result to the supplied
// interface. Note that the argument for the to parameter should always be a
// reference to a slice.
func (q *QueryBuilder) ExecuteTo(to interface{}) (int64, error) {
return executeTo(context.Background(), q.client, q.method, q.body, to, []string{q.tableName}, q.headers, q.params)
}
// ExecuteToWithContext runs the PostgREST query with the given context,
// encoding the result to the supplied interface. Note that the argument for
// the to parameter should always be a reference to a slice.
func (q *QueryBuilder) ExecuteToWithContext(ctx context.Context, to interface{}) (int64, error) {
return executeTo(ctx, q.client, q.method, q.body, to, []string{q.tableName}, q.headers, q.params)
}
// Select performs vertical filtering.
func (q *QueryBuilder) Select(columns, count string, head bool) *FilterBuilder {
if head {
q.method = "HEAD"
} else {
q.method = "GET"
}
if columns == "" {
q.params["select"] = "*"
} else {
quoted := false
var resultArr []string
for _, char := range strings.Split(columns, "") {
if char == `"` {
quoted = !quoted
}
if char == " " {
char = ""
}
resultArr = append(resultArr, char)
}
result := strings.Join(resultArr, "")
q.params["select"] = result
}
if count != "" && (count == `exact` || count == `planned` || count == `estimated`) {
currentValue, ok := q.headers["Prefer"]
if ok && currentValue != "" {
q.headers["Prefer"] = fmt.Sprintf("%s,count=%s", currentValue, count)
} else {
q.headers["Prefer"] = fmt.Sprintf("count=%s", count)
}
}
return &FilterBuilder{client: q.client, method: q.method, body: q.body, tableName: q.tableName, headers: q.headers, params: q.params}
}
// Insert performs an insertion into the table.
func (q *QueryBuilder) Insert(value interface{}, upsert bool, onConflict, returning, count string) *FilterBuilder {
q.method = "POST"
if onConflict != "" && upsert {
q.params["on_conflict"] = onConflict
}
var headerList []string
if upsert {
headerList = append(headerList, "resolution=merge-duplicates")
}
if returning == "" {
returning = "representation"
}
if returning == "minimal" || returning == "representation" {
headerList = append(headerList, "return="+returning)
}
if count != "" && (count == `exact` || count == `planned` || count == `estimated`) {
headerList = append(headerList, "count="+count)
}
q.headers["Prefer"] = strings.Join(headerList, ",")
// Get body if exist
var byteBody []byte = nil
if value != nil {
jsonBody, err := json.Marshal(value)
if err != nil {
q.client.ClientError = err
return &FilterBuilder{}
}
byteBody = jsonBody
}
q.body = byteBody
return &FilterBuilder{client: q.client, method: q.method, body: q.body, tableName: q.tableName, headers: q.headers, params: q.params}
}
// Upsert performs an upsert into the table.
func (q *QueryBuilder) Upsert(value interface{}, onConflict, returning, count string) *FilterBuilder {
q.method = "POST"
if onConflict != "" {
q.params["on_conflict"] = onConflict
}
headerList := []string{"resolution=merge-duplicates"}
if returning == "" {
returning = "representation"
}
if returning == "minimal" || returning == "representation" {
headerList = append(headerList, "return="+returning)
}
if count != "" && (count == `exact` || count == `planned` || count == `estimated`) {
headerList = append(headerList, "count="+count)
}
q.headers["Prefer"] = strings.Join(headerList, ",")
// Get body if exist
var byteBody []byte = nil
if value != nil {
jsonBody, err := json.Marshal(value)
if err != nil {
q.client.ClientError = err
return &FilterBuilder{}
}
byteBody = jsonBody
}
q.body = byteBody
return &FilterBuilder{client: q.client, method: q.method, body: q.body, tableName: q.tableName, headers: q.headers, params: q.params}
}
// Delete performs a deletion from the table.
func (q *QueryBuilder) Delete(returning, count string) *FilterBuilder {
q.method = "DELETE"
var headerList []string
if returning == "" {
returning = "representation"
}
if returning == "minimal" || returning == "representation" {
headerList = append(headerList, "return="+returning)
}
if count != "" && (count == `exact` || count == `planned` || count == `estimated`) {
headerList = append(headerList, "count="+count)
}
q.headers["Prefer"] = strings.Join(headerList, ",")
return &FilterBuilder{client: q.client, method: q.method, body: q.body, tableName: q.tableName, headers: q.headers, params: q.params}
}
// Update performs an update on the table.
func (q *QueryBuilder) Update(value interface{}, returning, count string) *FilterBuilder {
q.method = "PATCH"
var headerList []string
if returning == "" {
returning = "representation"
}
if returning == "minimal" || returning == "representation" {
headerList = append(headerList, "return="+returning)
}
if count != "" && (count == `exact` || count == `planned` || count == `estimated`) {
headerList = append(headerList, "count="+count)
}
q.headers["Prefer"] = strings.Join(headerList, ",")
// Get body if it exists
var byteBody []byte = nil
if value != nil {
jsonBody, err := json.Marshal(value)
if err != nil {
q.client.ClientError = err
return &FilterBuilder{}
}
byteBody = jsonBody
}
q.body = byteBody
return &FilterBuilder{client: q.client, method: q.method, body: q.body, tableName: q.tableName, headers: q.headers, params: q.params}
}