forked from dadleyy/marlow
/
updateable.go
242 lines (196 loc) · 7.68 KB
/
updateable.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
package marlow
import "io"
import "fmt"
import "net/url"
import "go/types"
import "github.com/090809/marlow/marlow/writing"
import "github.com/090809/marlow/marlow/constants"
type updaterSymbols struct {
valueParam string
blueprint string
queryString string
queryResult string
queryError string
statementResult string
statementError string
rowCount string
rowError string
valueSlice string
valueCount string
targetValue string
}
type updateOperation struct {
name string
operation string
valueless bool
}
func updater(record marlowRecord, fieldConfig url.Values, op *updateOperation) io.Reader {
pr, pw := io.Pipe()
column := fieldConfig.Get(constants.ColumnConfigOption)
if op == nil {
prefix := record.config.Get(constants.UpdateFieldMethodPrefixConfigOption)
method := fmt.Sprintf("%s%s%s", prefix, record.name(), fieldConfig.Get("FieldName"))
op = &updateOperation{name: method, operation: "", valueless: false}
}
symbols := updaterSymbols{
valueParam: "_updates",
blueprint: "_blueprint",
queryString: "_queryString",
queryResult: "_queryResult",
queryError: "_queryError",
statementResult: "_statement",
statementError: "_se",
rowCount: "_rowCount",
rowError: "_re",
valueSlice: "_values",
valueCount: "_valueCount",
targetValue: "_target",
}
params := []writing.FuncParam{
{Type: fieldConfig.Get("type"), Symbol: symbols.valueParam},
{Type: fmt.Sprintf("*%s", record.config.Get(constants.BlueprintNameConfigOption)), Symbol: symbols.blueprint},
}
if fieldConfig.Get("type") == "sql.NullInt64" {
params[0].Type = fmt.Sprintf("*%s", fieldConfig.Get("type"))
}
// Check to see if we have an operation, and if the operation requires a value (hacky). If no value is required then
// we no longer need the first user-provided argument to the deletion method.
if op.valueless {
params = []writing.FuncParam{
{Type: fmt.Sprintf("*%s", record.config.Get(constants.BlueprintNameConfigOption)), Symbol: symbols.blueprint},
}
}
returns := []string{
"int64",
"error",
}
go func() {
gosrc := writing.NewGoWriter(pw)
gosrc.Comment("[marlow] updater method for %s", column)
e := gosrc.WithMethod(op.name, record.store(), params, returns, func(scope url.Values) error {
logwriter := logWriter{output: gosrc, receiver: scope.Get("receiver")}
// Prepare a value count to keep track of the amount of dynamic components will be sent into the query.
gosrc.Println("%s := 1", symbols.valueCount)
// Add the blueprint value count to the query component count.
gosrc.WithIf("%s != nil && len(%s.Values()) > 0", func(url.Values) error {
return gosrc.Println("%s = len(%s.Values()) + 1", symbols.valueCount, symbols.blueprint)
}, symbols.blueprint, symbols.blueprint)
// If our operation requires a value, we to create a variable in the source that will represent the string
// holder during the sql statement execution. For postgres this value is placement-aware, e.g: $1.
if !op.valueless {
switch record.dialect() {
case "postgres":
gosrc.Println("%s := fmt.Sprintf(\"$%%d\", %s)", symbols.targetValue, symbols.valueCount)
break
default:
gosrc.Println("%s := \"?\"", symbols.targetValue)
}
}
command := fmt.Sprintf("UPDATE %s SET %s = %%s", record.table(), column)
// If we have an operation, use it here instead.
if op.operation != "" {
command = fmt.Sprintf("UPDATE %s SET %s = %s", record.table(), column, op.operation)
}
// Start the update template string with the basic SQL-dialect `UPDATE <table> SET <column> = ?` syntax.
template := fmt.Sprintf("fmt.Sprintf(\"%s\", %s)", command, symbols.targetValue)
// If no value was required in the operation string we can simplify the sql.
if op.valueless {
template = fmt.Sprintf("\"%s\"", command)
}
gosrc.Println("%s := bytes.NewBufferString(%s)", symbols.queryString, template)
// Add our blueprint to the WHERE section of our update statement buffer if it is not nil.
gosrc.WithIf("%s != nil", func(url.Values) error {
return gosrc.Println("fmt.Fprintf(%s, \" %%s\", %s)", symbols.queryString, symbols.blueprint)
}, symbols.blueprint)
// Write the query execution statement.
gosrc.Println(
"%s, %s := %s.Prepare(%s.String() + \";\")",
symbols.statementResult,
symbols.statementError,
scope.Get("receiver"),
symbols.queryString,
)
gosrc.WithIf("%s != nil", func(url.Values) error {
return gosrc.Returns("-1", symbols.statementError)
}, symbols.statementError)
gosrc.Println("defer %s.Close()", symbols.statementResult)
// Create an array of `interface` values that will be used during the `Exec` portion of our transaction.
gosrc.Println("%s := make([]interface{}, 0, %s)", symbols.valueSlice, symbols.valueCount)
// The postgres dialect uses numbered placeholder values. If the record is using anything other than that, the
// placeholder for the target value should appear first in the set of values sent to Exec.
if record.dialect() != "postgres" && !op.valueless {
gosrc.Println("%s = append(%s, %s)", symbols.valueSlice, symbols.valueSlice, symbols.valueParam)
}
gosrc.WithIf("%s != nil", func(url.Values) error {
return gosrc.Println(
"%s = append(%s, %s.Values()...)",
symbols.valueSlice,
symbols.valueSlice,
symbols.blueprint,
)
}, symbols.blueprint)
// If we're postgres, add our value to the very end of our value slice.
if record.dialect() == "postgres" && !op.valueless {
gosrc.Println("%s = append(%s, %s)", symbols.valueSlice, symbols.valueSlice, symbols.valueParam)
}
logwriter.AddLog(symbols.queryString, symbols.valueSlice)
gosrc.Println("%s, %s := %s.Exec(%s...)",
symbols.queryResult,
symbols.queryError,
symbols.statementResult,
symbols.valueSlice,
)
gosrc.WithIf("%s != nil", func(url.Values) error {
return gosrc.Returns("-1", symbols.queryError)
}, symbols.queryError)
gosrc.Println("%s, %s := %s.RowsAffected()", symbols.rowCount, symbols.rowError, symbols.queryResult)
gosrc.WithIf("%s != nil", func(url.Values) error {
return gosrc.Returns("-1", symbols.rowError)
}, symbols.rowError)
return gosrc.Returns(symbols.rowCount, writing.Nil)
})
if e != nil {
pw.CloseWithError(e)
return
}
record.registerImports("fmt", "bytes")
record.registerStoreMethod(writing.FuncDecl{
Name: op.name,
Params: params,
Returns: returns,
})
pw.CloseWithError(nil)
}()
return pr
}
// newUpdateableGenerator is responsible for generating updating store methods.
func newUpdateableGenerator(record marlowRecord) io.Reader {
readers := make([]io.Reader, 0, len(record.fields))
for name, config := range record.fields {
column := config.Get(constants.ColumnConfigOption)
up := updater(record, config, nil)
fieldType := getTypeInfo(config.Get("type"))
if _, bit := config[constants.ColumnBitmaskOption]; bit {
valid := (fieldType & (types.IsUnsigned | types.IsInteger)) == fieldType
if !valid {
e := fmt.Errorf("bitmask columns must be unsigned integers, %s has type \"%s\"", column, config.Get("type"))
pr, pw := io.Pipe()
pw.CloseWithError(e)
return pr
}
bitwise := []io.Reader{
updater(record, config, &updateOperation{
name: fmt.Sprintf("Add%s%s", record.name(), name),
operation: fmt.Sprintf("%s | %%s", column),
}),
updater(record, config, &updateOperation{
name: fmt.Sprintf("Drop%s%s", record.name(), name),
operation: fmt.Sprintf("%s & ~%%s", column),
}),
}
readers = append(readers, bitwise...)
}
readers = append(readers, up)
}
return io.MultiReader(readers...)
}