-
Notifications
You must be signed in to change notification settings - Fork 14
/
example_test.go
148 lines (115 loc) · 2.89 KB
/
example_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
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
package sqlx_test
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
trmsqlx "github.com/avito-tech/go-transaction-manager/drivers/sqlx/v2"
"github.com/avito-tech/go-transaction-manager/trm/v2/manager"
)
// Example demonstrates the implementation of the Repository pattern by trm.Manager.
func Example() {
db := newDB()
defer db.Close() //nolint:errcheck
sqlStmt := `CREATE TABLE IF NOT EXISTS user (user_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, username TEXT);`
_, err := db.Exec(sqlStmt)
checkErr(err, sqlStmt)
r := newRepo(db, trmsqlx.DefaultCtxGetter)
u := &user{
Username: "username",
}
ctx := context.Background()
trManager := manager.Must(trmsqlx.NewDefaultFactory(db))
err = trManager.Do(ctx, func(ctx context.Context) error {
if err := r.Save(ctx, u); err != nil {
return err
}
return trManager.Do(ctx, func(ctx context.Context) error {
u.Username = "new_username"
return r.Save(ctx, u)
})
})
checkErr(err)
userFromDB, err := r.GetByID(ctx, u.ID)
checkErr(err)
fmt.Println(userFromDB)
// Output: &{1 new_username}
}
func newDB() *sqlx.DB {
db, err := sqlx.Open("sqlite3", "file:test?mode=memory")
checkErr(err)
return db
}
type repo struct {
db *sqlx.DB
getter *trmsqlx.CtxGetter
}
func newRepo(db *sqlx.DB, c *trmsqlx.CtxGetter) *repo {
return &repo{
db: db,
getter: c,
}
}
type user struct {
ID int64
Username string
}
type userRow struct {
ID int64 `db:"user_id"`
Username string `db:"username"`
}
func (r *repo) GetByID(ctx context.Context, id int64) (*user, error) {
query := "SELECT * FROM user WHERE user_id = ?;"
row := userRow{}
err := r.getter.DefaultTrOrDB(ctx, r.db).GetContext(ctx, &row, r.db.Rebind(query), id)
if err != nil {
return nil, err
}
return r.toModel(row), nil
}
func (r *repo) Save(ctx context.Context, u *user) error {
isNew := u.ID == 0
query := `UPDATE user SET username = :username WHERE user_id = :user_id;`
if isNew {
query = `INSERT INTO user (username) VALUES (:username);`
}
res, err := sqlx.NamedExecContext(
ctx,
r.getter.DefaultTrOrDB(ctx, r.db),
r.db.Rebind(query),
r.toRow(u),
)
if err != nil {
return err
} else if !isNew {
return nil
} else if u.ID, err = res.LastInsertId(); err != nil {
return err
}
// For PostgreSql need to use NamedQueryContext with RETURNING
// DO UPDATE SET username = EXCLUDED.username RETURNING id;
// defer res.Next()
// if u.ID == 0 && res.Next() {
// if err = res.Scan(&u.ID); err != nil {
// return err
// }
// }
return nil
}
func (r *repo) toRow(model *user) userRow {
return userRow{
ID: model.ID,
Username: model.Username,
}
}
func (r *repo) toModel(row userRow) *user {
return &user{
ID: row.ID,
Username: row.Username,
}
}
func checkErr(err error, args ...interface{}) {
if err != nil {
panic(fmt.Sprint(append([]interface{}{err}, args...)...))
}
}