Skip to content

Commit

Permalink
Merge pull request #7 from storage-lock/dev
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
CC11001100 authored Sep 16, 2023
2 parents 8a3a0d5 + fc6dc8a commit 09f61e0
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion example/create_postgresql_storage_from_dsn/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {

// 使用一个DSN形式的数据库连接字符串创建ConnectionManager
testDsn := "host=127.0.0.1 user=postgres password=UeGqAm8CxYGldMDLoNNt port=5432 dbname=postgres sslmode=disable"
connectionManager := postgresql_storage.NewPostgresqlConnectionGetterFromDSN(testDsn)
connectionManager := postgresql_storage.NewPostgresqlConnectionGetterFromDsn(testDsn)

// 然后从这个ConnectionManager创建PostgreSQL Storage
options := postgresql_storage.NewPostgresqlStorageOptions().SetConnectionManager(connectionManager)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.19

require (
github.com/lib/pq v1.10.9
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903155403-1c8c724d3383
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903181730-3e91a672a521
github.com/storage-lock/go-storage v0.0.2
github.com/storage-lock/go-storage-lock v0.0.4
github.com/storage-lock/go-storage-test-helper v0.0.3
github.com/stretchr/testify v1.8.4
)
Expand All @@ -21,7 +22,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/storage-lock/go-events v0.0.3 // indirect
github.com/storage-lock/go-storage-events v0.0.5 // indirect
github.com/storage-lock/go-storage-lock v0.0.3 // indirect
github.com/storage-lock/go-utils v0.0.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ github.com/storage-lock/go-sql-based-storage v0.0.0-20230815152553-e6057f9c32e7
github.com/storage-lock/go-sql-based-storage v0.0.0-20230815152553-e6057f9c32e7/go.mod h1:kKR7bTZ59cP7jK0tFhQ4yz2hD2XAL8A97Haql9NAygI=
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903155403-1c8c724d3383 h1:fw1cz2FM9h6Y7XmfEP8xgA8AmxpGFZVXcBi0iKa+O8Y=
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903155403-1c8c724d3383/go.mod h1:4ixNpjEQ9Aj4E9GExfopf3vw14oqXRSuFSduq7QRgF4=
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903181730-3e91a672a521 h1:xZQTwQh9F7zKMLPIsHn7mZFSPqpj1OkrGrBVTLKV2c0=
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903181730-3e91a672a521/go.mod h1:4ixNpjEQ9Aj4E9GExfopf3vw14oqXRSuFSduq7QRgF4=
github.com/storage-lock/go-storage v0.0.2 h1:N5glUlrr1bbJ8ixYsPmOv+TuhIOObX/65+hrNMPWFDc=
github.com/storage-lock/go-storage v0.0.2/go.mod h1:4vuU2nLdbmzj2UIBKpwB5lj8RI+GnPDZ4YHZ7QAcFHE=
github.com/storage-lock/go-storage-events v0.0.5 h1:qUgB/VZIQNM0PIos67T9D1JIqu3Yiu3+YDpuHEal3FI=
github.com/storage-lock/go-storage-events v0.0.5/go.mod h1:aBMVsOSr61+BLxO3QnU5FwKd5bdMGE4IpV0oDrAg8uQ=
github.com/storage-lock/go-storage-lock v0.0.3 h1:eobL73fhfAjLKNk4msQNTYFcilrPKwySXY/5ympfeRU=
github.com/storage-lock/go-storage-lock v0.0.3/go.mod h1:kzyjFhgPmiYMwLuuVoQfYxXPUOIGNETA+Z/wDnRc/iw=
github.com/storage-lock/go-storage-lock v0.0.4 h1:Cr75wUDd1wxGyjgavr+jBPQ1zZr6r7TAB6q7NSf1fv0=
github.com/storage-lock/go-storage-lock v0.0.4/go.mod h1:kzyjFhgPmiYMwLuuVoQfYxXPUOIGNETA+Z/wDnRc/iw=
github.com/storage-lock/go-storage-test-helper v0.0.3 h1:y8B24zJkQaI7uYOW2MTBIHTZXbRpkEG6rS1XMWAttjs=
github.com/storage-lock/go-storage-test-helper v0.0.3/go.mod h1:Zn6YIq0r0pmK+naGUhRhb7TqTEFDjoU+25yIugt9iAw=
github.com/storage-lock/go-utils v0.0.2 h1:pdacTOlD+AHqwWVmDqZLcjKF+7p8TjsmlvZ9bmmkIfE=
Expand Down
25 changes: 14 additions & 11 deletions postgresql_connection_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ type PostgresqlConnectionManager struct {

var _ storage.ConnectionManager[*sql.DB] = &PostgresqlConnectionManager{}

// NewPostgresqlConnectionGetterFromDSN 从DSN创建PostgreSQL连接
func NewPostgresqlConnectionGetterFromDSN(dsn string) *PostgresqlConnectionManager {
// NewPostgresqlConnectionGetterFromDsn 从DSN创建Postgresql连接
func NewPostgresqlConnectionGetterFromDsn(dsn string) *PostgresqlConnectionManager {
return &PostgresqlConnectionManager{
DSN: dsn,
}
}

// NewPostgresqlConnectionGetterFromSqlDb 从一个已经存在的*sql.DB创建连接管理器
func NewPostgresqlConnectionGetterFromSqlDb(db *sql.DB) *PostgresqlConnectionManager {
return &PostgresqlConnectionManager{
db: db,
}
}

// NewPostgresqlConnectionManager 从服务器属性创建数据库连接
func NewPostgresqlConnectionManager(host string, port uint, user, passwd, databaseName string) *PostgresqlConnectionManager {
return &PostgresqlConnectionManager{
Expand All @@ -59,17 +66,10 @@ func NewPostgresqlConnectionManager(host string, port uint, user, passwd, databa
}
}

// NewPostgresqlConnectionGetterFromSqlDb 从一个已经存在的*sql.DB创建连接管理器
func NewPostgresqlConnectionGetterFromSqlDb(db *sql.DB) *PostgresqlConnectionManager {
return &PostgresqlConnectionManager{
db: db,
}
}

const PostgreSQLConnectionManagerName = "postgresql-connection-manager"
const PostgresqlConnectionManagerName = "postgresql-connection-manager"

func (x *PostgresqlConnectionManager) Name() string {
return PostgreSQLConnectionManagerName
return PostgresqlConnectionManagerName
}

func (x *PostgresqlConnectionManager) GetDSN() string {
Expand All @@ -82,6 +82,9 @@ func (x *PostgresqlConnectionManager) GetDSN() string {
// Take 获取到数据库的连接
func (x *PostgresqlConnectionManager) Take(ctx context.Context) (*sql.DB, error) {
x.once.Do(func() {
if x.db != nil {
return
}
if x.err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion postgresql_sql_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/storage-lock/go-storage"
)

// PostgresqlSqlProvider storage sql的postgresql方言
// PostgresqlSqlProvider storage sql的postgresql方言,因为使用的驱动的占位符不同,所以基本上每个语句都重写了,但处理占位符其它大差不差
type PostgresqlSqlProvider struct {
*sql_based_storage.Sql92Provider
}
Expand Down
16 changes: 16 additions & 0 deletions postgresql_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
_ "github.com/lib/pq"
sql_based_storage "github.com/storage-lock/go-sql-based-storage"
"github.com/storage-lock/go-storage"
storage_lock "github.com/storage-lock/go-storage-lock"
"strings"
"time"
)

Expand Down Expand Up @@ -96,6 +98,20 @@ func (x *PostgresqlStorage) Init(ctx context.Context) (returnError error) {
return nil
}

// duplicate key value violates unique constraint

func (x *PostgresqlStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) (returnError error) {

returnError = x.SqlBasedStorage.CreateWithVersion(ctx, lockId, version, lockInformation)
if returnError != nil {
// 把重复转为版本miss
if strings.Contains(returnError.Error(), "duplicate key value violates unique constraint") {
return storage_lock.ErrVersionMiss
}
}
return returnError
}

func (x *PostgresqlStorage) GetTime(ctx context.Context) (time.Time, error) {

db, err := x.options.ConnectionManager.Take(ctx)
Expand Down
2 changes: 1 addition & 1 deletion postgresql_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestNewPostgresqlStorage(t *testing.T) {
envName := "STORAGE_LOCK_POSTGRESQL_DSN"
dsn := os.Getenv(envName)
assert.NotEmpty(t, dsn)
connectionGetter := NewPostgresqlConnectionGetterFromDSN(dsn)
connectionGetter := NewPostgresqlConnectionGetterFromDsn(dsn)
s, err := NewPostgresqlStorage(context.Background(), NewPostgresqlStorageOptions().SetConnectionManager(connectionGetter))
assert.Nil(t, err)
storage_test_helper.TestStorage(t, s)
Expand Down

0 comments on commit 09f61e0

Please sign in to comment.