-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_sqldb.go
42 lines (37 loc) · 1.63 KB
/
global_sqldb.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
package mysql_locks
import (
"context"
"database/sql"
sqldb_storage "github.com/storage-lock/go-sqldb-storage"
"github.com/storage-lock/go-storage"
storage_lock "github.com/storage-lock/go-storage-lock"
storage_lock_factory "github.com/storage-lock/go-storage-lock-factory"
)
var sqlDbStorageLockFactoryBeanFactory *storage_lock_factory.StorageLockFactoryBeanFactory[*sql.DB, *sql.DB] = storage_lock_factory.NewStorageLockFactoryBeanFactory[*sql.DB, *sql.DB]()
// NewLockBySqlDb 从sql.DB创建锁
func NewLockBySqlDb(ctx context.Context, db *sql.DB, lockId string) (*storage_lock.StorageLock, error) {
factory, err := GetLockFactoryBySqlDb(ctx, db)
if err != nil {
return nil, err
}
return factory.CreateLock(lockId)
}
// NewLockBySqlDbWithOptions 从sql.DB创建锁,创建锁的时候可以指定锁的选项
func NewLockBySqlDbWithOptions(ctx context.Context, db *sql.DB, options *storage_lock.StorageLockOptions) (*storage_lock.StorageLock, error) {
factory, err := GetLockFactoryBySqlDb(ctx, db)
if err != nil {
return nil, err
}
return factory.CreateLockWithOptions(options)
}
func GetLockFactoryBySqlDb(ctx context.Context, db *sql.DB) (*storage_lock_factory.StorageLockFactory[*sql.DB], error) {
return sqlDbStorageLockFactoryBeanFactory.GetOrInit(ctx, db, func(ctx context.Context) (*storage_lock_factory.StorageLockFactory[*sql.DB], error) {
sqlDbStorage, err := sqldb_storage.NewStorageBySqlDb(db)
if err != nil {
return nil, err
}
connectionManager := storage.NewFixedSqlDBConnectionManager(db)
factory := storage_lock_factory.NewStorageLockFactory[*sql.DB](sqlDbStorage, connectionManager)
return factory, nil
})
}