This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
/
updateable.go
207 lines (166 loc) · 6.53 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
package marlow
import "io"
import "fmt"
import "net/url"
import "go/types"
import "github.com/dadleyy/marlow/marlow/writing"
import "github.com/dadleyy/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
}
func updater(record marlowRecord, fieldConfig url.Values, methodName, op string) io.Reader {
pr, pw := io.Pipe()
column := fieldConfig.Get(constants.ColumnConfigOption)
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"))
}
returns := []string{
"int64",
"error",
}
go func() {
gosrc := writing.NewGoWriter(pw)
gosrc.Comment("[marlow] updater method for %s", column)
e := gosrc.WithMethod(methodName, 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)
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 op != "" {
command = fmt.Sprintf("UPDATE %s SET %s = %s", record.table(), column, op)
}
// 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)
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" {
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" {
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: methodName,
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))
prefix := record.config.Get(constants.UpdateFieldMethodPrefixConfigOption)
for name, config := range record.fields {
column := config.Get(constants.ColumnConfigOption)
method := fmt.Sprintf("%s%s%s", prefix, record.name(), name)
up := updater(record, config, method, "")
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, fmt.Sprintf("Add%s%s", record.name(), name), fmt.Sprintf("%s | %%s", column)),
updater(record, config, fmt.Sprintf("Drop%s%s", record.name(), name), fmt.Sprintf("%s & ~%%s", column)),
}
readers = append(readers, bitwise...)
}
readers = append(readers, up)
}
return io.MultiReader(readers...)
}