diff --git a/example/create_postgresql_storage_from_dsn/main.go b/example/create_postgresql_storage_from_dsn/main.go index 4685519..831f32d 100644 --- a/example/create_postgresql_storage_from_dsn/main.go +++ b/example/create_postgresql_storage_from_dsn/main.go @@ -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) diff --git a/go.mod b/go.mod index 3234e99..4276644 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 ) diff --git a/go.sum b/go.sum index 2bd9867..32e11e2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/postgresql_connection_manager.go b/postgresql_connection_manager.go index 1d682eb..a09b1b2 100644 --- a/postgresql_connection_manager.go +++ b/postgresql_connection_manager.go @@ -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{ @@ -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 { @@ -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 } diff --git a/postgresql_sql_provider.go b/postgresql_sql_provider.go index 20ccc0e..150a819 100644 --- a/postgresql_sql_provider.go +++ b/postgresql_sql_provider.go @@ -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 } diff --git a/postgresql_storage.go b/postgresql_storage.go index 6f52205..eb7c83e 100644 --- a/postgresql_storage.go +++ b/postgresql_storage.go @@ -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" ) @@ -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) diff --git a/postgresql_storage_test.go b/postgresql_storage_test.go index 267666d..526e04f 100644 --- a/postgresql_storage_test.go +++ b/postgresql_storage_test.go @@ -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)