-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
188 lines (152 loc) · 4.13 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package gormx
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"gorm.io/gorm"
)
var (
ErrNoRowsAffected = errors.New("no rows affected")
)
type Config struct {
Dialector gorm.Dialector
MaxIdleConn int
MaxOpenConn int
MaxLifetime int64
Debug bool
}
type Gormx struct {
cfg *Config
db *gorm.DB
}
func New(cfg *Config, opts ...gorm.Option) (*Gormx, error) {
db, err := gorm.Open(cfg.Dialector, opts...)
if err != nil {
return nil, fmt.Errorf("open database connection failed, %w", err)
}
sqlDb, err := db.DB()
if err != nil {
return nil, fmt.Errorf("get origin db instance failed, %w", err)
}
if cfg.MaxIdleConn > 0 {
sqlDb.SetMaxIdleConns(cfg.MaxIdleConn)
}
if cfg.MaxOpenConn > 0 {
sqlDb.SetMaxOpenConns(cfg.MaxOpenConn)
}
if cfg.MaxLifetime > 0 {
sqlDb.SetConnMaxLifetime(time.Duration(cfg.MaxLifetime) * time.Second)
}
return &Gormx{
cfg: cfg,
db: db,
}, nil
}
func NewWithDB(db *gorm.DB) *Gormx {
return &Gormx{
db: db,
}
}
func (s *Gormx) DB() *gorm.DB {
return s.db
}
func (s *Gormx) BuildOptions(opts ...Option) *gorm.DB {
return s.buildWithOptions(opts...)
}
func (s *Gormx) Debug() *Gormx {
return s.clone(s.db.Debug())
}
// WithContext 添加上下文,会新建 Gormx 对象
func (s *Gormx) WithContext(ctx context.Context) *Gormx {
return s.clone(s.db.WithContext(ctx))
}
func (s *Gormx) Model(value interface{}) *Gormx {
return s.clone(s.db.Model(value))
}
func (s *Gormx) WithConn(conn *gorm.DB) *Gormx {
return s.clone(conn)
}
// Tx 开启事务
func (s *Gormx) Tx(fn func(tx *Gormx) error, opts ...*sql.TxOptions) error {
return s.db.Transaction(func(tx *gorm.DB) error {
return fn(s.WithConn(tx))
}, opts...)
}
func (s *Gormx) Insert(doc interface{}, opts ...Option) error {
return s.buildWithOptions(opts...).Create(doc).Error
}
func (s *Gormx) Save(doc interface{}, opts ...Option) error {
return s.buildWithOptions(opts...).Save(doc).Error
}
func (s *Gormx) FindOne(dest interface{}, opts ...Option) error {
return s.buildWithOptions(opts...).First(dest).Error
}
func (s *Gormx) FindMany(dest interface{}, opts ...Option) error {
return s.buildWithOptions(opts...).Find(dest).Error
}
func (s *Gormx) Pluck(column string, dest interface{}, opts ...Option) error {
return s.buildWithOptions(opts...).Pluck(column, dest).Error
}
func (s *Gormx) Count(opts ...Option) (int64, error) {
var total int64
if err := s.buildWithOptions(opts...).Count(&total).Error; err != nil {
return 0, err
}
return total, nil
}
func (s *Gormx) Exists(dest interface{}, opts ...Option) (bool, error) {
var exists bool
opts = append(opts, Wildcard())
stmt := s.dryRun(opts...).Take(dest).Statement
query := s.db.Raw(fmt.Sprintf("SELECT EXISTS(%s)", stmt.SQL.String()), stmt.Vars...)
if err := query.Scan(&exists).Error; err != nil {
return false, err
}
return exists, nil
}
func (s *Gormx) Updates(dest interface{}, opts ...Option) error {
db := s.buildWithOptions(opts...).Updates(dest)
if err := db.Error; err != nil {
return err
}
if db.RowsAffected == 0 {
return ErrNoRowsAffected
}
return nil
}
func (s *Gormx) Update(column string, value interface{}, opts ...Option) error {
db := s.buildWithOptions(opts...).Update(column, value)
if err := db.Error; err != nil {
return err
}
if db.RowsAffected == 0 {
return ErrNoRowsAffected
}
return nil
}
func (s *Gormx) Delete(dest interface{}, opts ...Option) error {
return s.buildWithOptions(opts...).Delete(dest).Error
}
func (s *Gormx) Raw(sql string, values ...interface{}) *Gormx {
return s.clone(s.db.Raw(sql, values...))
}
func (s *Gormx) Exec(sql string, values ...interface{}) error {
return s.db.Exec(sql, values...).Error
}
func (s *Gormx) Scan(dest interface{}) error {
return s.db.Scan(dest).Error
}
// ----------------------------------------------------------------------------------------------------------------------------
func (s *Gormx) dryRun(opts ...Option) *gorm.DB {
return applyOptions(s.db.Session(&gorm.Session{DryRun: true}), opts...)
}
func (s *Gormx) buildWithOptions(opts ...Option) *gorm.DB {
return applyOptions(s.db, opts...)
}
func (s *Gormx) clone(db *gorm.DB) *Gormx {
return &Gormx{
db: db,
}
}