-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgen_4_test.go
82 lines (66 loc) · 1.9 KB
/
gen_4_test.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
package shift_test
// Code generated by shiftgen at arc_test.go:14. DO NOT EDIT.
import (
"context"
"database/sql"
"strings"
"time"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/j"
"github.com/luno/shift"
)
// Insert inserts a new users table entity. All the fields of the
// insert2 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 (一 insert2) Insert(
ctx context.Context, tx *sql.Tx, st shift.Status,
) (int64, error) {
var (
q strings.Builder
args []interface{}
)
q.WriteString("insert into users set `status`=?, `created_at`=?, `updated_at`=? ")
args = append(args, st.ShiftStatus(), time.Now(), time.Now())
q.WriteString(", `name`=?")
args = append(args, 一.Name)
q.WriteString(", `dob`=?")
args = append(args, 一.DateOfBirth)
q.WriteString(", `amount`=?")
args = append(args, 一.Amount)
res, err := tx.ExecContext(ctx, q.String(), args...)
if err != nil {
return 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
return id, nil
}
// Update updates the status of a users table entity. All the fields of the
// move receiver are updated, as well as status and updated_at.
// The entity id is returned on success or an error.
func (一 move) Update(
ctx context.Context, tx *sql.Tx, from shift.Status, to shift.Status,
) (int64, error) {
var (
q strings.Builder
args []interface{}
)
q.WriteString("update users set `status`=?, `updated_at`=? ")
args = append(args, to.ShiftStatus(), time.Now())
q.WriteString(" where `id`=? and `status`=?")
args = append(args, 一.ID, from.ShiftStatus())
res, err := tx.ExecContext(ctx, q.String(), args...)
if err != nil {
return 0, err
}
n, err := res.RowsAffected()
if err != nil {
return 0, err
}
if n != 1 {
return 0, errors.Wrap(shift.ErrRowCount, "move", j.KV("count", n))
}
return 一.ID, nil
}