This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
/
deleteable.go
104 lines (81 loc) · 2.96 KB
/
deleteable.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
package marlow
import "io"
import "fmt"
import "net/url"
import "github.com/gedex/inflector"
import "github.com/dadleyy/marlow/marlow/writing"
import "github.com/dadleyy/marlow/marlow/constants"
type deleteableSymbols struct {
e string
count string
blueprint string
result string
statement string
prepared string
statementError string
}
// newDeleteableGenerator is responsible for creating a generator that will write out the Delete api methods.
func newDeleteableGenerator(record marlowRecord) io.Reader {
pr, pw := io.Pipe()
methodName := fmt.Sprintf("Delete%s", inflector.Pluralize(record.name()))
symbols := deleteableSymbols{
e: "_e",
count: "_count",
blueprint: "_blueprint",
statement: "_query",
prepared: "_statement",
statementError: "_se",
result: "_execResult",
}
params := []writing.FuncParam{
{Type: fmt.Sprintf("*%s", record.blueprint()), Symbol: symbols.blueprint},
}
returns := []string{
"int64",
"error",
}
go func() {
gosrc := writing.NewGoWriter(pw)
gosrc.Comment("[marlow] deleteable")
e := gosrc.WithMethod(methodName, record.store(), params, returns, func(scope url.Values) error {
receiver := scope.Get("receiver")
logwriter := logWriter{receiver: receiver, output: gosrc}
gosrc.WithIf("%s == nil || %s.String() == \"\"", func(url.Values) error {
return gosrc.Returns("-1", fmt.Sprintf("fmt.Errorf(\"%s\")", constants.InvalidDeletionBlueprint))
}, symbols.blueprint, symbols.blueprint)
deleteString := fmt.Sprintf("DELETE FROM %s", record.table())
gosrc.Println("%s := fmt.Sprintf(\"%s %%s\", %s)", symbols.statement, deleteString, symbols.blueprint)
gosrc.Println("%s, %s := %s.Prepare(%s + \";\")", symbols.prepared, symbols.e, receiver, symbols.statement)
// Check for preparation error.
gosrc.WithIf("%s != nil", func(url.Values) error { return gosrc.Returns("-1", symbols.e) }, symbols.e)
// Always close the prepared statement.
gosrc.Println("defer %s.Close()", symbols.prepared)
logwriter.AddLog(symbols.statement, fmt.Sprintf("%s.Values()", symbols.blueprint))
// Executre the prepared statement with the values from the blueprint.
gosrc.Println(
"%s, %s := %s.Exec(%s.Values()...)",
symbols.result,
symbols.e,
symbols.prepared,
symbols.blueprint,
)
// Check for statement.Exec error
gosrc.WithIf("%s != nil", func(url.Values) error { return gosrc.Returns("-1", symbols.e) }, symbols.e)
gosrc.Println("%s, %s := %s.RowsAffected()", symbols.count, symbols.e, symbols.result)
gosrc.WithIf("%s != nil", func(url.Values) error {
return gosrc.Returns("-1", symbols.e)
}, symbols.e)
return gosrc.Returns(symbols.count, writing.Nil)
})
if e == nil {
record.registerImports("fmt")
record.registerStoreMethod(writing.FuncDecl{
Name: methodName,
Returns: returns,
Params: params,
})
}
pw.CloseWithError(e)
}()
return pr
}