forked from shenghui0779/yiigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
90 lines (80 loc) · 2.19 KB
/
db.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
package yiigo
import (
"context"
"database/sql"
"fmt"
"runtime/debug"
"time"
_ "github.com/go-sql-driver/mysql"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
// DBConfig 数据库初始化配置
type DBConfig struct {
// Driver 驱动名称
Driver string
// DSN 数据源名称
// - [-- MySQL] username:password@tcp(localhost:3306)/dbname?timeout=10s&charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&loc=Local
// - [Postgres] host=localhost port=5432 user=root password=secret dbname=test search_path=schema connect_timeout=10 sslmode=disable
// - [- SQLite] file::memory:?cache=shared
DSN string
// MaxOpenConns 设置最大可打开的连接数
MaxOpenConns int
// MaxIdleConns 连接池最大闲置连接数
MaxIdleConns int
// ConnMaxLifetime 连接的最大生命时长
ConnMaxLifetime time.Duration
// ConnMaxIdleTime 连接最大闲置时间
ConnMaxIdleTime time.Duration
}
// NewDB sql.DB
func NewDB(cfg *DBConfig) (*sql.DB, error) {
db, err := sql.Open(cfg.Driver, cfg.DSN)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
_ = db.Close()
return nil, err
}
db.SetMaxOpenConns(cfg.MaxOpenConns)
db.SetMaxIdleConns(cfg.MaxIdleConns)
db.SetConnMaxLifetime(cfg.ConnMaxLifetime)
db.SetConnMaxIdleTime(cfg.ConnMaxIdleTime)
return db, nil
}
// NewDBx sqlx.DB
func NewDBx(cfg *DBConfig) (*sqlx.DB, error) {
db, err := NewDB(cfg)
if err != nil {
return nil, err
}
return sqlx.NewDb(db, cfg.Driver), nil
}
// Transaction 执行数据库事物
func Transaction(ctx context.Context, db *sqlx.DB, fn func(ctx context.Context, tx *sqlx.Tx) error) (err error) {
tx, _err := db.BeginTxx(ctx, nil)
if _err != nil {
err = fmt.Errorf("db.BeginTxx: %w", _err)
return
}
defer func() {
if r := recover(); r != nil {
_ = tx.Rollback() // if panic, should rollback
err = fmt.Errorf("transaction panic recovered: %+v\n%s", r, string(debug.Stack()))
}
}()
err = fn(ctx, tx)
if err != nil {
if err_ := tx.Rollback(); err_ != nil {
err = fmt.Errorf("%w: tx.Rollback: %w", err, err_)
}
return
}
if err_ := tx.Commit(); err_ != nil {
err = fmt.Errorf("tx.Commit: %w", err_)
return
}
return
}