-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
85 lines (80 loc) · 2.28 KB
/
sql.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
package go_trellis_db
import (
"fmt"
"io"
"strings"
)
func writeEdgeInsert(out io.Writer, chunk []Edge) (err error) {
cols := []string{"id", "source_respondent_id", "target_respondent_id", "created_at", "updated_at", "note"}
if len(chunk) == 0 {
return
}
if _, err = fmt.Fprintf(out, "INSERT INTO edge (%s) VALUES \n", strings.Join(cols, ", ")); err != nil {
return
}
for i, r := range chunk {
if r.Note.Valid {
if _, err = fmt.Fprintf(out, "(\"%s\", \"%s\", \"%s\", now(), now(), \"%s\")", r.Id, r.SourceRespondentId, r.TargetRespondentId, r.Note.String); err != nil {
return
}
} else {
if _, err = fmt.Fprintf(out, "(\"%s\", \"%s\", \"%s\", now(), now(), null)", r.Id, r.SourceRespondentId, r.TargetRespondentId); err != nil {
return
}
}
if i != len(chunk)-1 {
if _, err = fmt.Fprintln(out, ","); err != nil {
return
}
}
}
_, err = fmt.Fprintf(out, ";\n\n")
return
}
func writePreloadInsert(out io.Writer, chunk []PreloadAction, questionId string) (err error) {
cols := []string{"id", "respondent_id", "question_id", "payload", "action_type", "created_at"}
if len(chunk) == 0 {
return
}
if _, err = fmt.Fprintf(out, "INSERT INTO preload_action (%s) VALUES \n", strings.Join(cols, ", ")); err != nil {
return
}
for i, r := range chunk {
if _, err = fmt.Fprintf(out, "(\"%s\", \"%s\", \"%s\", \"%s\", \"add-edge\", now())", r.Id, r.RespondentId, questionId, strings.ReplaceAll(r.Payload, `"`, `\"`)); err != nil {
return
}
if i != len(chunk)-1 {
if _, err = fmt.Fprintln(out, ","); err != nil {
return
}
}
}
_, err = fmt.Fprintf(out, ";\n\n")
return
}
func WriteEdges(out io.Writer, edgeMap map[string]Edge) (err error) {
chunk := []Edge{}
for _, edge := range edgeMap {
chunk = append(chunk, edge)
if len(chunk) >= 2000 {
if err = writeEdgeInsert(out, chunk); err != nil {
return
}
chunk = chunk[:0]
}
}
return writeEdgeInsert(out, chunk)
}
func WritePreloads(out io.Writer, preloads []PreloadAction, questionId string) (err error) {
chunk := []PreloadAction{}
for _, edge := range preloads {
chunk = append(chunk, edge)
if len(chunk) >= 2000 {
if err = writePreloadInsert(out, chunk, questionId); err != nil {
return
}
chunk = chunk[:0]
}
}
return writePreloadInsert(out, chunk, questionId)
}