/
template.go
102 lines (89 loc) · 2.91 KB
/
template.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
package main
var tpl = `package {{.Package}}
// Code generated by shiftgen at {{.GenSource}}. DO NOT EDIT.
import (
"context"
"database/sql"
"strings"
"time"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/j"
"github.com/luno/shift"
)
{{ range .Inserters }}
// Insert inserts a new {{.Table}} table entity. All the fields of the
// {{.Type}} receiver are set, as well as status, created_at and updated_at.
// The newly created entity id is returned on success or an error.
func (一 {{.Type}}) Insert(
ctx context.Context, tx *sql.Tx, st shift.Status,
) ({{.IDType}}, error) {
var (
q strings.Builder
args []interface{}
)
{{if .CustomCreatedAt -}}
if 一.CreatedAt.IsZero() {
return {{.IDZeroValue}}, errors.New("created_at is required")
}
{{end -}}
{{if .CustomUpdatedAt}}
if 一.UpdatedAt.IsZero() {
return {{.IDZeroValue}}, errors.New("updated_at is required")
}
{{end -}}
q.WriteString("insert into {{.Table}} set {{if .HasID}}` + "`id`=?" + `, {{end}}{{col .StatusField}}=?{{if not .CustomCreatedAt}}, {{col "created_at"}}=?{{end}}{{if not .CustomCreatedAt}}, {{col "updated_at"}}=?{{end}} ")
args = append(args, {{if .HasID}}一.ID, {{end}}st.ShiftStatus(){{if not .CustomCreatedAt}}, time.Now(){{end}}{{if not .CustomCreatedAt}}, time.Now(){{end}})
{{range .Fields}}
q.WriteString(", {{col .Col}}=?")
args = append(args, 一.{{.Name}})
{{end}}
{{if .HasID}}_{{else}}res{{end}}, err := tx.ExecContext(ctx, q.String(), args...)
if err != nil {
return {{.IDZeroValue}}, err
}
{{if not .HasID}}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
{{end}}
return {{if .HasID}}一.ID{{else}}id{{end}}, nil
}
{{end}}{{ range .Updaters }}
// Update updates the status of a {{.Table}} table entity. All the fields of the
// {{.Type}} receiver are updated, as well as status and updated_at.
// The entity id is returned on success or an error.
func (一 {{.Type}}) Update(
ctx context.Context, tx *sql.Tx, from shift.Status, to shift.Status,
) ({{.IDType}}, error) {
var (
q strings.Builder
args []interface{}
)
{{if .CustomUpdatedAt -}}
if 一.UpdatedAt.IsZero() {
return {{.IDZeroValue}}, errors.New("updated_at is required")
}
{{end -}}
q.WriteString("update {{.Table}} set {{col .StatusField}}=?{{if not .CustomUpdatedAt}}, {{col "updated_at"}}=?{{end}} ")
args = append(args, to.ShiftStatus(){{if not .CustomUpdatedAt}}, time.Now(){{end}})
{{range .Fields}}
q.WriteString(", {{col .Col}}=?")
args = append(args, 一.{{.Name}})
{{end}}
q.WriteString(" where {{col "id"}}=? and {{col .StatusField}}=?")
args = append(args, 一.ID, from.ShiftStatus())
res, err := tx.ExecContext(ctx, q.String(), args...)
if err != nil {
return {{.IDZeroValue}}, err
}
n, err := res.RowsAffected()
if err != nil {
return {{.IDZeroValue}}, err
}
if n != 1 {
return {{.IDZeroValue}}, errors.Wrap(shift.ErrRowCount, "{{.Type}}", j.KV("count", n))
}
return 一.ID, nil
}{{ end }}
`