-
Notifications
You must be signed in to change notification settings - Fork 1
/
backends_pgsql.go
448 lines (404 loc) · 12.3 KB
/
backends_pgsql.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package forms
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
jtd "github.com/jsontypedef/json-typedef-go"
)
// PgsqlBackend is a backend using PostgreSQL
type PgsqlBackend struct {
Conn *pgxpool.Pool
}
func (b PgsqlBackend) Type() string {
return "Pgsql"
}
func (b PgsqlBackend) Configuration() map[string]interface{} {
return map[string]interface{}{
"type": b.Type(),
}
}
// NewPgsqlBackend initializes a new empty PgsqlBackend
func NewPgsqlBackend(databaseurl string) PgsqlBackend {
conn, err := pgxpool.Connect(context.Background(), databaseurl)
if err != nil {
panic(err)
}
if _, err := conn.Exec(context.Background(), "create table if not exists __config(id varchar(50) primary key, type varchar(20), schema json, additional_backends json)"); err != nil {
panic(err)
}
return PgsqlBackend{
Conn: conn,
}
}
// AddForm creates a new form in the backend
func (b PgsqlBackend) AddForm(f Form) error {
tx, err := b.Conn.Begin(context.Background())
if err != nil {
return err
}
defer tx.Rollback(context.Background())
if _, err := tx.Exec(
context.Background(),
"insert into __config (id, type, schema) values($1, $2, $3)", f.ID(), f.Type(), f.GetSchema(),
); err != nil {
return errors.New("a form already exists with this id")
}
sql := fmt.Sprintf("create table form_%s (", pgx.Identifier{f.ID()}.Sanitize()) +
schemaToSQL(f.GetSchema()) +
")"
if _, err := tx.Exec(
context.Background(),
sql,
); err != nil {
return err
}
err = tx.Commit(context.Background())
if err != nil {
return err
}
return nil
}
// DeleteForm removes the form from the backend using formId
func (b PgsqlBackend) DeleteForm(formId string) error {
tx, err := b.Conn.Begin(context.Background())
if err != nil {
return err
}
defer tx.Rollback(context.Background())
if _, err := tx.Exec(context.Background(), "delete from __config where id=$1", formId); err != nil {
return err
}
if _, err := tx.Exec(context.Background(), fmt.Sprintf("drop table %s", pgx.Identifier{"form_" + formId}.Sanitize())); err != nil {
return err
}
if err := tx.Commit(context.Background()); err != nil {
return err
}
return nil
}
// GetForms retrieves all forms from this backend
func (b PgsqlBackend) GetForms() ([]Form, error) {
forms := []Form{}
rows, err := b.Conn.Query(context.Background(), "select id, type, schema from __config")
if err != nil {
return nil, err
}
for rows.Next() {
var formid string
var formtype string
var formschema []byte
err := rows.Scan(&formid, &formtype, &formschema)
if err != nil {
return nil, err
}
if formtype == "Structured" {
var s *jtd.Schema
json.Unmarshal(formschema, s)
forms = append(forms, NewStructuredForm(formid, s))
} else {
forms = append(forms, NewUnstructuredForm(formid))
}
}
return forms, nil
}
// GetForm retrieves the form from the backend given a formId
func (b PgsqlBackend) GetForm(id string) (Form, error) {
var formid string
var formtype string
var formschema []byte
if err := b.Conn.QueryRow(context.Background(), "select id, type, schema from __config where id=$1", id).Scan(&formid, &formtype, &formschema); err != nil {
return nil, err
}
if formtype == "Structured" {
var s jtd.Schema
if err := json.Unmarshal(formschema, &s); err != nil {
return nil, err
}
form := NewStructuredForm(formid, &s)
return form, nil
} else {
form := NewUnstructuredForm(formid)
return form, nil
}
}
// Submitresponse registers a response to the form given it's formid
func (b PgsqlBackend) SubmitResponse(form Form, response Response) error {
keys := []string{}
values := []interface{}{}
responsemap := response.(map[string]interface{})
for k, v := range responsemap {
keys = append(keys, k)
values = append(values, v)
}
if form.Type() == "Structuted" {
if _, err := b.Conn.CopyFrom(
context.Background(),
pgx.Identifier{"form_" + form.ID()},
keys,
pgx.CopyFromRows([][]interface{}{values}),
); err != nil {
return err
}
} else {
tx, err := b.Conn.Begin(context.Background())
defer tx.Rollback(context.Background())
if err != nil {
return err
}
for _, k := range keys {
kind := reflect.TypeOf(responsemap[k]).Kind()
switch {
case kind == reflect.String:
if _, err := tx.Exec(
context.Background(),
fmt.Sprintf("alter table %s add column if not exists %s varchar(250)",
pgx.Identifier{"form_" + form.ID()}.Sanitize(),
pgx.Identifier{k}.Sanitize(),
),
); err != nil {
return err
}
break
case kind == reflect.Bool:
if _, err := tx.Exec(
context.Background(),
fmt.Sprintf(
"alter table %s add column if not exists %s boolean",
pgx.Identifier{"form_" + form.ID()}.Sanitize(),
pgx.Identifier{k}.Sanitize(),
),
); err != nil {
return err
}
break
case kind == reflect.Map:
if _, err := tx.Exec(
context.Background(),
fmt.Sprintf(
"alter table %s add column if not exists %s json",
pgx.Identifier{"form_" + form.ID()}.Sanitize(),
pgx.Identifier{k}.Sanitize(),
),
); err != nil {
return err
}
break
case kind == reflect.Int || kind == reflect.Uint || kind == reflect.Uint32 || kind == reflect.Uint64 || kind == reflect.Int32 || kind == reflect.Int64:
if _, err := tx.Exec(
context.Background(),
fmt.Sprintf(
"alter table %s add column if not exists %s integer",
pgx.Identifier{"form_" + form.ID()}.Sanitize(),
pgx.Identifier{k}.Sanitize(),
),
); err != nil {
return err
}
break
case kind == reflect.Float32 || kind == reflect.Float64:
if _, err := tx.Exec(
context.Background(),
fmt.Sprintf(
"alter table %s add column if not exists %s numeric",
pgx.Identifier{"form_" + form.ID()}.Sanitize(),
pgx.Identifier{k}.Sanitize(),
),
); err != nil {
return err
}
break
}
}
if _, err := tx.CopyFrom(
context.Background(),
pgx.Identifier{"form_" + form.ID()},
keys,
pgx.CopyFromRows([][]interface{}{values}),
); err != nil {
return err
}
err = tx.Commit(context.Background())
if err != nil {
return err
}
}
return nil
}
// GetFormResponses retrieves the responses for a given form (with formId)
func (b PgsqlBackend) GetFormResponses(formId string) ([]Response, error) {
responses := []Response{}
rows, err := b.Conn.Query(context.Background(), fmt.Sprintf("select * from %s", pgx.Identifier{"form_" + formId}.Sanitize()))
if err != nil {
fmt.Println("err 1")
return nil, err
}
fieldDescriptions := rows.FieldDescriptions()
var columns []string
for _, col := range fieldDescriptions {
columns = append(columns, string(col.Name))
}
for rows.Next() {
values, err := rows.Values()
if err != nil {
return nil, err
}
r := map[string]interface{}{}
for i := 0; i < len(columns); i++ {
r[columns[i]] = values[i]
}
responses = append(responses, reflect.ValueOf(r).Interface())
}
return responses, nil
}
// AddFormBackend adds an additional backend to a given form
func (b PgsqlBackend) AddFormBackend(id string, backend Backend) error {
var formbackends []byte
if err := b.Conn.QueryRow(context.Background(), "select additional_backends from __config where id=$1", id).Scan(&formbackends); err != nil {
fmt.Println("1 : ", err)
return err
}
backends := []map[string]interface{}{}
if formbackends != nil {
if err := json.Unmarshal(formbackends, &backends); err != nil {
fmt.Println("2 : ", err)
return err
}
}
backends = append(backends, backend.Configuration())
if _, err := b.Conn.Exec(context.Background(), "update __config set additional_backends = $1 where id=$2", backends, id); err != nil {
fmt.Println("3 : ", err)
return err
}
return nil
}
func (b PgsqlBackend) GetFormBackends(id string) ([]Backend, error) {
var formbackends []byte
if err := b.Conn.QueryRow(context.Background(), "select additional_backends from __config where id=$1", id).Scan(&formbackends); err != nil {
return nil, err
}
backends := []Backend{}
var backendconfigs []map[string]interface{}
if err := json.Unmarshal(formbackends, &backendconfigs); err != nil {
return nil, err
}
for _, bc := range backendconfigs {
if reflect.ValueOf(bc["type"]).Kind() == reflect.String && bc["type"].(string) == "Kantree" {
backends = append(backends, NewKantreeBackend(bc["configuration"].(map[string]interface{})))
}
}
return backends, nil
}
// schemaToSQL is a small function to generate table columns name and types from a JTD schema (used during table creation)
// Returns "" (empty string) in case of a nil schema
//
// TODO Refactor this function to make it recursive and support more complex schemas + don't repeat yourself !
func schemaToSQL(s *jtd.Schema) string {
if s == nil {
return ""
}
create := ""
first := true
for k, v := range s.Properties {
if !first {
create += ", "
}
first = false
switch {
case v.Type == "string":
create += fmt.Sprintf("%s varchar", pgx.Identifier{k}.Sanitize())
break
case v.Type == "boolean":
create += fmt.Sprintf("%s boolean", pgx.Identifier{k}.Sanitize())
break
case v.Type == "float64" || v.Type == "float32":
create += fmt.Sprintf("%s numeric", pgx.Identifier{k}.Sanitize())
break
case v.Type == "int8" || v.Type == "uint8" || v.Type == "int16" || v.Type == "uint16" || v.Type == "int32" || v.Type == "uint32":
create += fmt.Sprintf("%s integer", pgx.Identifier{k}.Sanitize())
break
case v.Type == "timestamp":
create += fmt.Sprintf("%s timestamp", pgx.Identifier{k}.Sanitize())
break
case v.Enum != nil && len(v.Enum) > 0:
create += fmt.Sprintf("%s varchar", pgx.Identifier{k}.Sanitize())
break
case v.Elements != nil:
//It is an array !
switch {
case v.Elements.Type == "string":
create += fmt.Sprintf("%s varchar[] ", pgx.Identifier{k}.Sanitize())
break
case v.Elements.Type == "boolean":
create += fmt.Sprintf("%s boolean ", pgx.Identifier{k}.Sanitize())
break
case v.Elements.Type == "float64" || v.Type == "float32":
create += fmt.Sprintf("%s numeric[] ", pgx.Identifier{k}.Sanitize())
break
case v.Elements.Type == "int8" || v.Type == "uint8" || v.Type == "int16" || v.Type == "uint16" || v.Type == "int32" || v.Type == "uint32":
create += fmt.Sprintf("%s integer[] ", pgx.Identifier{k}.Sanitize())
break
case v.Elements.Type == "timestamp":
create += fmt.Sprintf("%s timestamp[]", pgx.Identifier{k}.Sanitize())
break
case v.Elements.Enum != nil && len(v.Enum) > 0:
create += fmt.Sprintf("%s varchar[]", pgx.Identifier{k}.Sanitize())
break
}
break
}
}
for k, v := range s.OptionalProperties {
if !first {
create += ", "
}
first = false
switch {
case v.Type == "string":
create += fmt.Sprintf("%s varchar(250) ", pgx.Identifier{k}.Sanitize())
break
case v.Type == "boolean":
create += fmt.Sprintf("%s boolean ", pgx.Identifier{k}.Sanitize())
break
case v.Type == "float64" || v.Type == "float32":
create += fmt.Sprintf("%s numeric ", pgx.Identifier{k}.Sanitize())
break
case v.Type == "int8" || v.Type == "uint8" || v.Type == "int16" || v.Type == "uint16" || v.Type == "int32" || v.Type == "uint32":
create += fmt.Sprintf("%s integer ", pgx.Identifier{k}.Sanitize())
break
case v.Type == "timestamp":
create += fmt.Sprintf("%s timestamp", pgx.Identifier{k}.Sanitize())
break
case v.Enum != nil && len(v.Enum) > 0:
create += fmt.Sprintf("%s string", pgx.Identifier{k}.Sanitize())
break
case v.Elements != nil:
//It is an array !
switch {
case v.Type == "string":
create += fmt.Sprintf("%s varchar(250)[] ", pgx.Identifier{k}.Sanitize())
break
case v.Type == "boolean":
create += fmt.Sprintf("%s boolean ", pgx.Identifier{k}.Sanitize())
break
case v.Type == "float64" || v.Type == "float32":
create += fmt.Sprintf("%s numeric[] ", pgx.Identifier{k}.Sanitize())
break
case v.Type == "int8" || v.Type == "uint8" || v.Type == "int16" || v.Type == "uint16" || v.Type == "int32" || v.Type == "uint32":
create += fmt.Sprintf("%s integer[] ", pgx.Identifier{k}.Sanitize())
break
case v.Type == "timestamp":
create += fmt.Sprintf("%s timestamp[]", pgx.Identifier{k}.Sanitize())
break
case v.Enum != nil && len(v.Enum) > 0:
create += fmt.Sprintf("%s string[]", pgx.Identifier{k}.Sanitize())
break
}
break
}
}
return create
}