-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
72 lines (70 loc) · 1.93 KB
/
main.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
package main
import (
"context"
sq "github.com/goclub/sql"
connectMysql "github.com/goclub/sql/example/internal/db"
m "github.com/goclub/sql/example/internal/model"
"log"
)
func main() {
ctx := context.Background()
err := example(ctx)
if err != nil {
log.Print(err)
}
}
func example(ctx context.Context) (err error) {
db := connectMysql.DB
// 准备数据
var insertUser m.User
// 多表插入一定要用事务,否则无法保证数据一致性
rollbackNoError, err := db.Begin(ctx, sq.LevelReadCommitted, func(tx *sq.T) sq.TxResult {
db := false
_ = db // 一般情况下事务中都是使用tx所以重新声明变量db 防止在 tx 中使用db
insertUser = m.User{
Name: "relation mobile",
Mobile: "13411122222",
ChinaIDCardNo: "310113199912121112",
}
err = tx.InsertModel(ctx, &insertUser, sq.QB{
UseInsertIgnoreInto: true, // 为了便于测试忽略重复插入
})
if err != nil {
return tx.RollbackWithError(err)
}
err = tx.InsertModel(ctx, &m.UserAddress{
UserID: insertUser.ID,
Address: "天堂路",
}, sq.QB{
UseInsertIgnoreInto: true, // 为了便于测试忽略重复插入
})
if err != nil {
return tx.RollbackWithError(err)
}
return tx.Commit()
})
if err != nil {
return
}
if rollbackNoError {
// 运行到 Begin 中的 return tx.Rollback() 时, rollbackNoError 为 true
}
userWithAddress := m.UserWithAddress{}
col := userWithAddress.Column()
hasUserWithAddress, err := db.QueryRelation(ctx, &userWithAddress, sq.QB{
Where: sq.And(col.UserID, sq.Equal(insertUser.ID)),
})
if err != nil {
return
}
log.Print("hasUserWithAddress:", hasUserWithAddress)
log.Print("userWithAddress:", userWithAddress)
// QueryRelationSlice 查询多条关联数据
var userWithAddressList []m.UserWithAddress
err = db.QueryRelationSlice(ctx, &userWithAddressList, sq.QB{})
if err != nil {
return
}
log.Print("userWithAddressList:", userWithAddressList)
return
}