/
insert_builder.go
203 lines (169 loc) · 3.65 KB
/
insert_builder.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
package postgres_backend
import (
"bytes"
"encoding/json"
"fmt"
_ "github.com/lib/pq"
"github.com/straumur/straumur"
"time"
)
func writeArrayParams(buffer *bytes.Buffer, arrays [][]string, startParam int, keys []string) int {
//todo, accept more than just string arrays
//todo, remove panic
paramCount := startParam
arrLength := len(arrays)
idx := 0
writeKey := false
if keys != nil {
writeKey = true
if len(keys) != len(arrays) {
panic("Illegal")
}
}
for keyIdx, arr := range arrays {
if writeKey {
buffer.WriteString(fmt.Sprintf(`"%s" = `, keys[keyIdx]))
}
buffer.WriteString("ARRAY[")
arrLen := len(arr)
for arrIdx := range arr {
buffer.WriteString(fmt.Sprintf("$%d", paramCount))
if arrIdx+1 < arrLen {
buffer.WriteString(", ")
}
paramCount++
}
//switch arr.(type)
buffer.WriteString("]::text[]")
if (idx + 1) < arrLength {
buffer.WriteString(", ")
}
idx++
}
return paramCount
}
func getBaseArgs(e *straumur.Event, isUpdate bool) ([]interface{}, error) {
//todo, check if e.Payload/e.KeyParams is string and valid json
payload, err := json.Marshal(e.Payload)
if err != nil {
return nil, err
}
keyparams, err := json.Marshal(e.KeyParams)
if err != nil {
return nil, err
}
if isUpdate {
return []interface{}{
e.Key,
keyparams,
payload,
e.Description,
e.Importance,
e.Origin,
}, nil
}
t := time.Now()
if e.Created.IsZero() {
e.Created = t
}
e.Updated = t
return []interface{}{
e.Key,
keyparams,
e.Created,
e.Updated,
payload,
e.Description,
e.Importance,
e.Origin,
}, nil
}
// Builds an insert query for the event table
func buildInsertQuery(e *straumur.Event) (string, []interface{}, error) {
var buffer bytes.Buffer
columnList := []string{
"key",
"key_params",
"created",
"updated",
"payload",
"description",
"importance",
"origin",
"entities",
"other_references",
"actors",
"tags",
}
args, err := getBaseArgs(e, false)
if err != nil {
return "", nil, nil
}
buffer.WriteString(`insert into "event" (`)
for arrIdx, s := range columnList {
buffer.WriteString(fmt.Sprintf(`"%s"`, s))
if arrIdx+1 < len(columnList) {
buffer.WriteString(", ")
}
}
buffer.WriteString(") values (")
buffer.WriteString("$1, $2, $3, $4, $5, $6, $7, $8,")
writeArrayParams(
&buffer,
[][]string{
e.Entities,
e.OtherReferences,
e.Actors,
e.Tags,
},
len(args)+1,
nil)
buffer.WriteString(`) returning "id", "created", "updated";`)
for _, arr := range [][]string{e.Entities, e.OtherReferences, e.Actors, e.Tags} {
for _, v := range arr {
args = append(args, v)
}
}
return buffer.String(), args, nil
}
// Builds an update query for the event table
func buildUpdateQuery(e *straumur.Event) (string, []interface{}, error) {
//Partial updates?
var buffer bytes.Buffer
paramCount := 1
columnList := []string{
"key",
"key_params",
"payload",
"description",
"importance",
"origin",
}
args, err := getBaseArgs(e, true)
if err != nil {
return "", nil, nil
}
buffer.WriteString(`update "event" set `)
for _, s := range columnList {
buffer.WriteString(fmt.Sprintf(`"%s" = $%d, `, s, paramCount))
paramCount++
}
nextParam := writeArrayParams(
&buffer,
[][]string{
e.Entities,
e.OtherReferences,
e.Actors,
e.Tags,
},
paramCount,
[]string{"entities", "other_references", "actors", "tags"})
for _, arr := range [][]string{e.Entities, e.OtherReferences, e.Actors, e.Tags} {
for _, v := range arr {
args = append(args, v)
}
}
args = append(args, e.ID)
buffer.WriteString(fmt.Sprintf(` where "id" = $%d returning "updated";`, nextParam))
return buffer.String(), args, nil
}