-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
107 lines (95 loc) · 2.39 KB
/
func.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
package main
import (
"context"
"database/sql"
"time"
"github.com/pkg/errors"
)
func CurrentTime(ctx context.Context, db *sql.DB) (*time.Time, error) {
var t time.Time
if err := db.QueryRowContext(ctx, `select now()`).Scan(&t); err != nil {
return nil, err
}
return &t, nil
}
type T1 struct {
ID int64
Val string
CreatedAt time.Time
}
func (t *T1) Create(ctx context.Context, tx *sql.Tx) error {
if err := tx.QueryRowContext(
ctx, `insert into t1 (val ,created_at) values ($1, $2) returning id`,
&t.Val, &t.CreatedAt).Scan(&t.ID); err != nil {
return err
}
return nil
}
type ErrorT1 struct {
ID int64
Val string
CreatedAt time.Time
}
func (t *ErrorT1) Create(ctx context.Context, tx *sql.Tx) error {
if err := tx.QueryRowContext(
ctx, `insert into error_t1 (val ,created_at) values ($1, $2) returning id`,
&t.Val, &t.CreatedAt).Scan(&t.ID); err != nil {
return err
}
return nil
}
func CreateT1AndCommit(ctx context.Context, db *sql.DB, t1 *T1) error {
tx, err := db.Begin()
if err != nil {
return errors.Wrap(err, "failed to begin tx")
}
defer tx.Rollback()
if err := t1.Create(ctx, tx); err != nil {
return errors.Wrap(err, "failed to create the first t1")
}
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "failed to commit the first t1")
}
return nil
}
func CreateTwoT1s(ctx context.Context, db *sql.DB, t1, t2 *T1) error {
tx, err := db.Begin()
if err != nil {
return errors.Wrap(err, "failed to begin tx")
}
defer tx.Rollback()
if err := t1.Create(ctx, tx); err != nil {
return errors.Wrap(err, "failed to create the first t1")
}
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "failed to commit the first t1")
}
tx2, err := db.Begin()
if err != nil {
return errors.Wrap(err, "failed to begin tx2")
}
defer tx2.Rollback()
if err := t2.Create(ctx, tx2); err != nil {
tx2.Rollback()
tx3, err := db.Begin()
if err != nil {
return errors.Wrap(err, "failed to begin tx3")
}
defer tx3.Rollback()
et2 := ErrorT1{
Val: t2.Val,
CreatedAt: t2.CreatedAt,
}
if err := et2.Create(ctx, tx3); err != nil {
return errors.Wrap(err, "failed to create ErrorT1")
}
if err := tx3.Commit(); err != nil {
return errors.Wrap(err, "failed to commit ErrorT1")
}
return nil
}
if err := tx2.Commit(); err != nil {
return errors.Wrap(err, "failed to commit the first t1")
}
return nil
}