Skip to content

Commit b151a3c

Browse files
committed
refactor
1 parent 745261a commit b151a3c

File tree

11 files changed

+64
-51
lines changed

11 files changed

+64
-51
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func main() {
2727

2828
// 使用一个DSN形式的数据库连接字符串创建ConnectionManager
2929
testDsn := "root:UeGqAm8CxYGldMDLoNNt@tcp(127.0.0.1:3306)/storage_lock_test"
30-
connectionManager := mysql_storage.NewMySQLConnectionManagerFromDSN(testDsn)
30+
connectionManager := mysql_storage.NewMysqlConnectionManagerFromDSN(testDsn)
3131

3232
// 然后从这个ConnectionManager创建MySQL Storage
3333
options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
34-
storage, err := mysql_storage.NewMySQLStorage(context.Background(), options)
34+
storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
3535
if err != nil {
3636
panic(err)
3737
}
@@ -61,11 +61,11 @@ func main() {
6161
username := "root"
6262
passwd := "UeGqAm8CxYGldMDLoNNt"
6363
database := "storage_lock_test"
64-
connectionManager := mysql_storage.NewMySQLConnectionManager(host, port, username, passwd, database)
64+
connectionManager := mysql_storage.NewMysqlConnectionManager(host, port, username, passwd, database)
6565

6666
// 然后从这个连接管理器创建MySQL Storage
6767
options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
68-
storage, err := mysql_storage.NewMySQLStorage(context.Background(), options)
68+
storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
6969
if err != nil {
7070
panic(err)
7171
}
@@ -101,7 +101,7 @@ func main() {
101101
// 则可以从这个*sql.DB中创建一个MySQL Storage
102102
connectionManager := storage.NewFixedSqlDBConnectionManager(db)
103103
options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
104-
storage, err := mysql_storage.NewMySQLStorage(context.Background(), options)
104+
storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
105105
if err != nil {
106106
panic(err)
107107
}

errors.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package mysql_storage
2+
3+
import "fmt"
4+
5+
var (
6+
ErrMysqlStorageOptionsConnectionManagerNil = fmt.Errorf("MysqlStorageOptions.ConnectionManager can not nil")
7+
)

examples/create_mysql_storage_from_dsn/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ func main() {
1010

1111
// 使用一个DSN形式的数据库连接字符串创建ConnectionManager
1212
testDsn := "root:UeGqAm8CxYGldMDLoNNt@tcp(127.0.0.1:3306)/storage_lock_test"
13-
connectionManager := mysql_storage.NewMySQLConnectionManagerFromDSN(testDsn)
13+
connectionManager := mysql_storage.NewMysqlConnectionManagerFromDSN(testDsn)
1414

1515
// 然后从这个ConnectionManager创建MySQL Storage
1616
options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
17-
storage, err := mysql_storage.NewMySQLStorage(context.Background(), options)
17+
storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
1818
if err != nil {
1919
panic(err)
2020
}

examples/create_mysql_storage_from_service_properties/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ func main() {
1414
username := "root"
1515
passwd := "UeGqAm8CxYGldMDLoNNt"
1616
database := "storage_lock_test"
17-
connectionManager := mysql_storage.NewMySQLConnectionManager(host, port, username, passwd, database)
17+
connectionManager := mysql_storage.NewMysqlConnectionManager(host, port, username, passwd, database)
1818

1919
// 然后从这个连接管理器创建MySQL Storage
2020
options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
21-
storage, err := mysql_storage.NewMySQLStorage(context.Background(), options)
21+
storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
2222
if err != nil {
2323
panic(err)
2424
}

examples/create_mysql_storage_from_sqldb/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func main() {
2020
// 则可以从这个*sql.DB中创建一个MySQL Storage
2121
connectionManager := storage.NewFixedSqlDBConnectionManager(db)
2222
options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
23-
storage, err := mysql_storage.NewMySQLStorage(context.Background(), options)
23+
storage, err := mysql_storage.NewMysqlStorage(context.Background(), options)
2424
if err != nil {
2525
panic(err)
2626
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.19
44

55
require (
66
github.com/go-sql-driver/mysql v1.7.1
7-
github.com/storage-lock/go-sql-based-storage v0.0.0-20230815152553-e6057f9c32e7
7+
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903181730-3e91a672a521
88
github.com/storage-lock/go-storage v0.0.2
99
github.com/storage-lock/go-storage-test-helper v0.0.2
1010
github.com/stretchr/testify v1.8.4
@@ -16,7 +16,7 @@ require (
1616
github.com/golang-infrastructure/go-iterator v0.0.0-20230524171120-56988a9b127c // indirect
1717
github.com/golang-infrastructure/go-pointer v0.0.5 // indirect
1818
github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 // indirect
19-
github.com/google/uuid v1.3.0 // indirect
19+
github.com/google/uuid v1.3.1 // indirect
2020
github.com/pmezard/go-difflib v1.0.0 // indirect
2121
github.com/storage-lock/go-events v0.0.3 // indirect
2222
github.com/storage-lock/go-storage-events v0.0.5 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb
1212
github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3/go.mod h1:zqXYxqOBa1mL2ilBK6PuH/Wb/Iego7en6XhiKWdZQHI=
1313
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
1414
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
15+
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
16+
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1517
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
1618
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1719
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -21,6 +23,8 @@ github.com/storage-lock/go-events v0.0.3 h1:QuNbaREe5m1WYC0meKs7SRJ5lY11a18jCJJS
2123
github.com/storage-lock/go-events v0.0.3/go.mod h1:DKwPgfuq8T8CyPLipH+0XHsNt8hEN4g9krUGfosU2oY=
2224
github.com/storage-lock/go-sql-based-storage v0.0.0-20230815152553-e6057f9c32e7 h1:WbtCSU+1NC4eO0QwehH4aQNLGBm9SUyvo39aMEYLFjE=
2325
github.com/storage-lock/go-sql-based-storage v0.0.0-20230815152553-e6057f9c32e7/go.mod h1:kKR7bTZ59cP7jK0tFhQ4yz2hD2XAL8A97Haql9NAygI=
26+
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903181730-3e91a672a521 h1:xZQTwQh9F7zKMLPIsHn7mZFSPqpj1OkrGrBVTLKV2c0=
27+
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903181730-3e91a672a521/go.mod h1:4ixNpjEQ9Aj4E9GExfopf3vw14oqXRSuFSduq7QRgF4=
2428
github.com/storage-lock/go-storage v0.0.2 h1:N5glUlrr1bbJ8ixYsPmOv+TuhIOObX/65+hrNMPWFDc=
2529
github.com/storage-lock/go-storage v0.0.2/go.mod h1:4vuU2nLdbmzj2UIBKpwB5lj8RI+GnPDZ4YHZ7QAcFHE=
2630
github.com/storage-lock/go-storage-events v0.0.5 h1:qUgB/VZIQNM0PIos67T9D1JIqu3Yiu3+YDpuHEal3FI=

mysql_connection_manager.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"sync"
99
)
1010

11-
// MySQLConnectionManager 创建一个MySQL的连接管理器
12-
type MySQLConnectionManager struct {
11+
// MysqlConnectionManager 创建一个MySQL的连接管理器
12+
type MysqlConnectionManager struct {
1313

1414
// 主机的名字
1515
Host string
@@ -33,18 +33,18 @@ type MySQLConnectionManager struct {
3333
once sync.Once
3434
}
3535

36-
var _ storage.ConnectionManager[*sql.DB] = &MySQLConnectionManager{}
36+
var _ storage.ConnectionManager[*sql.DB] = &MysqlConnectionManager{}
3737

38-
// NewMySQLConnectionManagerFromDSN 从DSN创建MySQL连接管理器
39-
func NewMySQLConnectionManagerFromDSN(dsn string) *MySQLConnectionManager {
40-
return &MySQLConnectionManager{
38+
// NewMysqlConnectionManagerFromDSN 从DSN创建MySQL连接管理器
39+
func NewMysqlConnectionManagerFromDSN(dsn string) *MysqlConnectionManager {
40+
return &MysqlConnectionManager{
4141
DSN: dsn,
4242
}
4343
}
4444

45-
// NewMySQLConnectionManager 从连接属性创建数据库连接
46-
func NewMySQLConnectionManager(host string, port uint, user, passwd, database string) *MySQLConnectionManager {
47-
return &MySQLConnectionManager{
45+
// NewMysqlConnectionManager 从连接属性创建数据库连接
46+
func NewMysqlConnectionManager(host string, port uint, user, passwd, database string) *MysqlConnectionManager {
47+
return &MysqlConnectionManager{
4848
Host: host,
4949
Port: port,
5050
User: user,
@@ -53,39 +53,39 @@ func NewMySQLConnectionManager(host string, port uint, user, passwd, database st
5353
}
5454
}
5555

56-
func (x *MySQLConnectionManager) SetHost(host string) *MySQLConnectionManager {
56+
func (x *MysqlConnectionManager) SetHost(host string) *MysqlConnectionManager {
5757
x.Host = host
5858
return x
5959
}
6060

61-
func (x *MySQLConnectionManager) SetPort(port uint) *MySQLConnectionManager {
61+
func (x *MysqlConnectionManager) SetPort(port uint) *MysqlConnectionManager {
6262
x.Port = port
6363
return x
6464
}
6565

66-
func (x *MySQLConnectionManager) SetUser(user string) *MySQLConnectionManager {
66+
func (x *MysqlConnectionManager) SetUser(user string) *MysqlConnectionManager {
6767
x.User = user
6868
return x
6969
}
7070

71-
func (x *MySQLConnectionManager) SetPasswd(passwd string) *MySQLConnectionManager {
71+
func (x *MysqlConnectionManager) SetPasswd(passwd string) *MysqlConnectionManager {
7272
x.Passwd = passwd
7373
return x
7474
}
7575

76-
func (x *MySQLConnectionManager) SetDatabaseName(databaseName string) *MySQLConnectionManager {
76+
func (x *MysqlConnectionManager) SetDatabaseName(databaseName string) *MysqlConnectionManager {
7777
x.DatabaseName = databaseName
7878
return x
7979
}
8080

81-
const MySQLConnectionManagerName = "mysql-connection-manager"
81+
const MysqlConnectionManagerName = "mysql-connection-manager"
8282

83-
func (x *MySQLConnectionManager) Name() string {
84-
return MySQLConnectionManagerName
83+
func (x *MysqlConnectionManager) Name() string {
84+
return MysqlConnectionManagerName
8585
}
8686

8787
// Take 获取到数据库的连接
88-
func (x *MySQLConnectionManager) Take(ctx context.Context) (*sql.DB, error) {
88+
func (x *MysqlConnectionManager) Take(ctx context.Context) (*sql.DB, error) {
8989
x.once.Do(func() {
9090
db, err := sql.Open("mysql", x.GetDSN())
9191
if err != nil {
@@ -97,18 +97,18 @@ func (x *MySQLConnectionManager) Take(ctx context.Context) (*sql.DB, error) {
9797
return x.db, x.err
9898
}
9999

100-
func (x *MySQLConnectionManager) GetDSN() string {
100+
func (x *MysqlConnectionManager) GetDSN() string {
101101
if x.DSN != "" {
102102
return x.DSN
103103
}
104104
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", x.User, x.Passwd, x.Host, x.Port, x.DatabaseName)
105105
}
106106

107-
func (x *MySQLConnectionManager) Return(ctx context.Context, db *sql.DB) error {
107+
func (x *MysqlConnectionManager) Return(ctx context.Context, db *sql.DB) error {
108108
return nil
109109
}
110110

111-
func (x *MySQLConnectionManager) Shutdown(ctx context.Context) error {
111+
func (x *MysqlConnectionManager) Shutdown(ctx context.Context) error {
112112
if x.db != nil {
113113
return x.db.Close()
114114
}

mysql_storage.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import (
77
"github.com/storage-lock/go-storage"
88
)
99

10-
type MySQLStorage struct {
10+
// MysqlStorage 基于Mysql的存储
11+
type MysqlStorage struct {
12+
// mysql的操作基本都是支持sql92语法的
1113
*sql_based_storage.SqlBasedStorage
12-
options *MySQLStorageOptions
14+
15+
options *MysqlStorageOptions
1316
}
1417

15-
var _ storage.Storage = &MySQLStorage{}
18+
var _ storage.Storage = &MysqlStorage{}
1619

17-
func NewMySQLStorage(ctx context.Context, options *MySQLStorageOptions) (*MySQLStorage, error) {
20+
func NewMysqlStorage(ctx context.Context, options *MysqlStorageOptions) (*MysqlStorage, error) {
1821

1922
// 参数检查
2023
if err := options.Check(); err != nil {
@@ -24,14 +27,14 @@ func NewMySQLStorage(ctx context.Context, options *MySQLStorageOptions) (*MySQLS
2427
// sql storage的基础Storage
2528
baseStorageOption := sql_based_storage.NewSqlBasedStorageOptions().
2629
SetConnectionManager(options.ConnectionManager).
27-
SetSqlProvider(sql_based_storage.NewSql97Provider()).
30+
SetSqlProvider(sql_based_storage.NewSql92Provider()).
2831
SetTableFullName(options.TableName)
2932
baseStorage, err := sql_based_storage.NewSqlBasedStorage(baseStorageOption)
3033
if err != nil {
3134
return nil, err
3235
}
3336

34-
s := &MySQLStorage{
37+
s := &MysqlStorage{
3538
SqlBasedStorage: baseStorage,
3639
options: options,
3740
}
@@ -46,6 +49,6 @@ func NewMySQLStorage(ctx context.Context, options *MySQLStorageOptions) (*MySQLS
4649

4750
const StorageName = "mysql-storage"
4851

49-
func (x *MySQLStorage) GetName() string {
52+
func (x *MysqlStorage) GetName() string {
5053
return StorageName
5154
}

mysql_storage_options.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package mysql_storage
22

33
import (
44
"database/sql"
5-
"fmt"
65
"github.com/storage-lock/go-storage"
76
)
87

9-
// MySQLStorageOptions 基于MySQL为存储引擎时的选项
10-
type MySQLStorageOptions struct {
8+
// MysqlStorageOptions 基于MySQL为存储引擎时的选项
9+
type MysqlStorageOptions struct {
1110

1211
// 存放锁的表的名字,如果未指定的话则使用默认的表
1312
TableName string
@@ -16,30 +15,30 @@ type MySQLStorageOptions struct {
1615
ConnectionManager storage.ConnectionManager[*sql.DB]
1716
}
1817

19-
func NewMySQLStorageOptions() *MySQLStorageOptions {
20-
return &MySQLStorageOptions{
18+
func NewMySQLStorageOptions() *MysqlStorageOptions {
19+
return &MysqlStorageOptions{
2120
TableName: storage.DefaultStorageTableName,
2221
}
2322
}
2423

25-
func (x *MySQLStorageOptions) SetConnectionManager(connManager storage.ConnectionManager[*sql.DB]) *MySQLStorageOptions {
24+
func (x *MysqlStorageOptions) SetConnectionManager(connManager storage.ConnectionManager[*sql.DB]) *MysqlStorageOptions {
2625
x.ConnectionManager = connManager
2726
return x
2827
}
2928

30-
func (x *MySQLStorageOptions) SetTableName(tableName string) *MySQLStorageOptions {
29+
func (x *MysqlStorageOptions) SetTableName(tableName string) *MysqlStorageOptions {
3130
x.TableName = tableName
3231
return x
3332
}
3433

35-
func (x *MySQLStorageOptions) Check() error {
34+
func (x *MysqlStorageOptions) Check() error {
3635

3736
if x.TableName == "" {
3837
x.TableName = storage.DefaultStorageDatabaseName
3938
}
4039

4140
if x.ConnectionManager == nil {
42-
return fmt.Errorf("MySQLStorageOptions.ConnectionManager can not nil")
41+
return ErrMysqlStorageOptionsConnectionManagerNil
4342
}
4443

4544
return nil

mysql_storage_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ func TestNewMySQLStorage(t *testing.T) {
1313
envName := "STORAGE_LOCK_MYSQL_DSN"
1414
dsn := os.Getenv(envName)
1515
assert.NotEmpty(t, dsn)
16-
connectionGetter := NewMySQLConnectionManagerFromDSN(dsn)
17-
s, err := NewMySQLStorage(context.Background(), &MySQLStorageOptions{
16+
connectionGetter := NewMysqlConnectionManagerFromDSN(dsn)
17+
s, err := NewMysqlStorage(context.Background(), &MysqlStorageOptions{
1818
ConnectionManager: connectionGetter,
1919
TableName: storage.DefaultStorageTableName,
2020
})

0 commit comments

Comments
 (0)