This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tables.go
386 lines (330 loc) · 10.9 KB
/
tables.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package controllers
import (
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/prest/adapters"
"github.com/prest/config"
)
// GetTables list all (or filter) tables
func GetTables(w http.ResponseWriter, r *http.Request) {
requestWhere, values, err := config.PrestConf.Adapter.WhereByRequest(r, 1)
if err != nil {
err = fmt.Errorf("could not perform WhereByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
requestWhere = config.PrestConf.Adapter.TableWhere(requestWhere)
order, err := config.PrestConf.Adapter.OrderByRequest(r)
if err != nil {
err = fmt.Errorf("could not perform OrderByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
order = config.PrestConf.Adapter.TableOrderBy(order)
sqlTables := config.PrestConf.Adapter.TableClause()
distinct, err := config.PrestConf.Adapter.DistinctClause(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if distinct != "" {
sqlTables = strings.Replace(sqlTables, "SELECT", distinct, 1)
}
sqlTables = fmt.Sprint(sqlTables, requestWhere, order)
sc := config.PrestConf.Adapter.Query(sqlTables, values...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
}
// GetTablesByDatabaseAndSchema list all (or filter) tables based on database and schema
func GetTablesByDatabaseAndSchema(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
database := vars["database"]
schema := vars["schema"]
config.PrestConf.Adapter.SetDatabase(database)
requestWhere, values, err := config.PrestConf.Adapter.WhereByRequest(r, 3)
if err != nil {
err = fmt.Errorf("could not perform WhereByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
requestWhere = config.PrestConf.Adapter.SchemaTablesWhere(requestWhere)
sqlSchemaTables := config.PrestConf.Adapter.SchemaTablesClause()
order, err := config.PrestConf.Adapter.OrderByRequest(r)
if err != nil {
err = fmt.Errorf("could not perform OrderByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
order = config.PrestConf.Adapter.SchemaTablesOrderBy(order)
page, err := config.PrestConf.Adapter.PaginateIfPossible(r)
if err != nil {
err = fmt.Errorf("could not perform PaginateIfPossible: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sqlSchemaTables = fmt.Sprint(sqlSchemaTables, requestWhere, order, " ", page)
valuesAux := make([]interface{}, 0)
valuesAux = append(valuesAux, database)
valuesAux = append(valuesAux, schema)
valuesAux = append(valuesAux, values...)
sc := config.PrestConf.Adapter.Query(sqlSchemaTables, valuesAux...)
if sc.Err() != nil {
http.Error(w, sc.Err().Error(), http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
}
// SelectFromTables perform select in database
func SelectFromTables(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
database := vars["database"]
schema := vars["schema"]
table := vars["table"]
config.PrestConf.Adapter.SetDatabase(database)
// get selected columns, "*" if empty "_columns"
cols, err := config.PrestConf.Adapter.FieldsPermissions(r, table, "read")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(cols) == 0 {
err := fmt.Errorf("you don't have permission for this action, please check the permitted fields for this table")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
selectStr, err := config.PrestConf.Adapter.SelectFields(cols)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
query := config.PrestConf.Adapter.SelectSQL(selectStr, database, schema, table)
countQuery, err := config.PrestConf.Adapter.CountByRequest(r)
if err != nil {
err = fmt.Errorf("could not perform CountByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if countQuery != "" {
query = config.PrestConf.Adapter.SelectSQL(countQuery, database, schema, table)
}
joinValues, err := config.PrestConf.Adapter.JoinByRequest(r)
if err != nil {
err = fmt.Errorf("could not perform JoinByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, j := range joinValues {
query = fmt.Sprint(query, j)
}
requestWhere, values, err := config.PrestConf.Adapter.WhereByRequest(r, 1)
if err != nil {
err = fmt.Errorf("could not perform WhereByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sqlSelect := query
if requestWhere != "" {
sqlSelect = fmt.Sprint(
query,
" WHERE ",
requestWhere)
}
groupBySQL := config.PrestConf.Adapter.GroupByClause(r)
if groupBySQL != "" {
sqlSelect = fmt.Sprintf("%s %s", sqlSelect, groupBySQL)
}
order, err := config.PrestConf.Adapter.OrderByRequest(r)
if err != nil {
err = fmt.Errorf("could not perform OrderByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if order != "" {
sqlSelect = fmt.Sprintf("%s %s", sqlSelect, order)
}
page, err := config.PrestConf.Adapter.PaginateIfPossible(r)
if err != nil {
err = fmt.Errorf("could not perform PaginateIfPossible: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sqlSelect = fmt.Sprint(sqlSelect, " ", page)
runQuery := config.PrestConf.Adapter.Query
if countQuery != "" {
runQuery = config.PrestConf.Adapter.QueryCount
}
sc := runQuery(sqlSelect, values...)
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
}
// InsertInTables perform insert in specific table
func InsertInTables(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
database := vars["database"]
schema := vars["schema"]
table := vars["table"]
config.PrestConf.Adapter.SetDatabase(database)
names, placeholders, values, err := config.PrestConf.Adapter.ParseInsertRequest(r)
if err != nil {
err = fmt.Errorf("could not perform InsertInTables: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sql := config.PrestConf.Adapter.InsertSQL(database, schema, table, names, placeholders)
sc := config.PrestConf.Adapter.Insert(sql, values...)
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
w.Write(sc.Bytes())
}
// BatchInsertInTables perform insert in specific table from a batch request
func BatchInsertInTables(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
database := vars["database"]
schema := vars["schema"]
table := vars["table"]
config.PrestConf.Adapter.SetDatabase(database)
names, placeholders, values, err := config.PrestConf.Adapter.ParseBatchInsertRequest(r)
if err != nil {
err = fmt.Errorf("could not perform BatchInsertInTables: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var sc adapters.Scanner
method := r.Header.Get("Prest-Batch-Method")
if strings.ToLower(method) != "copy" {
sql := config.PrestConf.Adapter.InsertSQL(database, schema, table, names, placeholders)
sc = config.PrestConf.Adapter.BatchInsertValues(sql, values...)
} else {
sc = config.PrestConf.Adapter.BatchInsertCopy(database, schema, table, strings.Split(names, ","), values...)
}
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusCreated)
w.Write(sc.Bytes())
}
// DeleteFromTable perform delete sql
func DeleteFromTable(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
database := vars["database"]
schema := vars["schema"]
table := vars["table"]
config.PrestConf.Adapter.SetDatabase(database)
where, values, err := config.PrestConf.Adapter.WhereByRequest(r, 1)
if err != nil {
err = fmt.Errorf("could not perform WhereByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sql := config.PrestConf.Adapter.DeleteSQL(database, schema, table)
if where != "" {
sql = fmt.Sprint(sql, " WHERE ", where)
}
returningSyntax, err := config.PrestConf.Adapter.ReturningByRequest(r)
if err != nil {
err = fmt.Errorf("could not perform ReturningByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if returningSyntax != "" {
sql = fmt.Sprint(
sql,
" RETURNING ",
returningSyntax)
}
sc := config.PrestConf.Adapter.Delete(sql, values...)
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
}
// UpdateTable perform update table
func UpdateTable(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
database := vars["database"]
schema := vars["schema"]
table := vars["table"]
config.PrestConf.Adapter.SetDatabase(database)
setSyntax, values, err := config.PrestConf.Adapter.SetByRequest(r, 1)
if err != nil {
err = fmt.Errorf("could not perform UPDATE: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sql := config.PrestConf.Adapter.UpdateSQL(database, schema, table, setSyntax)
pid := len(values) + 1 // placeholder id
where, whereValues, err := config.PrestConf.Adapter.WhereByRequest(r, pid)
if err != nil {
err = fmt.Errorf("could not perform WhereByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if where != "" {
sql = fmt.Sprint(
sql,
" WHERE ",
where)
values = append(values, whereValues...)
}
returningSyntax, err := config.PrestConf.Adapter.ReturningByRequest(r)
if err != nil {
err = fmt.Errorf("could not perform ReturningByRequest: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if returningSyntax != "" {
sql = fmt.Sprint(
sql,
" RETURNING ",
returningSyntax)
}
sc := config.PrestConf.Adapter.Update(sql, values...)
if err = sc.Err(); err != nil {
errorMessage := sc.Err().Error()
if errorMessage == fmt.Sprintf(`pq: relation "%s.%s" does not exist`, schema, table) {
fmt.Println(errorMessage)
http.Error(w, errorMessage, http.StatusNotFound)
return
}
http.Error(w, errorMessage, http.StatusBadRequest)
return
}
w.Write(sc.Bytes())
}