postgresql_storage

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2023 License: MIT Imports: 11 Imported by: 2

README

Postgresql Storage

一、这是什么

以Postgresql为存储引擎的Storage实现,当前仓库为比较底层的存储层实现,你可以与storage-lock结合使用,或者这个项目PostgreSQL-locks里专门封装提供了一些PostgreSQL锁相关的更易用友好的API。

二、安装依赖

go get -u github.com/storage-lock/go-postgresql-storage

三、API Examples

3.1 从DSN创建PostgresqlStorage

在Golang的世界中连接数据库最常见的就是DSN,下面的例子演示了如何从一个DSN创建PostgresqlStorage:

package main

import (
	"context"
	"fmt"

	postgresql_storage "github.com/storage-lock/go-postgresql-storage"
)

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
	options := postgresql_storage.NewPostgresqlStorageOptions().SetConnectionManager(connectionManager)
	storage, err := postgresql_storage.NewPostgresqlStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

3.2 从连接属性(ip、端口、用户名、密码等等)中创建PostgresqlStorage

或者你的配置文件中存放的并不是DSN,而是零散的几个连接属性,下面是一个创建PostgresqlStorage的例子:

package main

import (
	"context"
	"fmt"

	postgresql_storage "github.com/storage-lock/go-postgresql-storage"
)

func main() {

	// 使用一个DSN形式的数据库连接字符串创建ConnectionManager
	host := "127.0.0.1"
	port := uint(5432)
	username := "postgres"
	passwd := "UeGqAm8CxYGldMDLoNNt"
	database := "postgres"
	connectionManager := postgresql_storage.NewPostgresqlConnectionManager(host, port, username, passwd, database)

	// 然后从这个ConnectionManager创建PostgreSQL Storage
	options := postgresql_storage.NewPostgresqlStorageOptions().SetConnectionManager(connectionManager)
	storage, err := postgresql_storage.NewPostgresqlStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

3.3 从sql.DB创建PostgresqlStorage

或者现在你已经有从其它渠道创建的能够连接到Postgresql的sql.DB,则也可以从这个*sql.DB创建PostgresqlStorage

package main

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/storage-lock/go-storage"

	postgresql_storage "github.com/storage-lock/go-postgresql-storage"
)

func main() {

	// 使用一个DSN形式的数据库连接字符串创建ConnectionManager
	testDsn := "host=127.0.0.1 user=postgres password=UeGqAm8CxYGldMDLoNNt port=5432 dbname=postgres sslmode=disable"
	db, err := sql.Open("postgres", testDsn)
	if err != nil {
		panic(err)
	}
	connectionManager := storage.NewFixedSqlDBConnectionManager(db)

	// 然后从这个ConnectionManager创建Postgresql Storage
	options := postgresql_storage.NewPostgresqlStorageOptions().SetConnectionManager(connectionManager)
	storage, err := postgresql_storage.NewPostgresqlStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

Documentation

Index

Constants

View Source
const DefaultPostgresqlStorageSchema = "public"

DefaultPostgresqlStorageSchema 默认的schema

View Source
const PostgresqlConnectionManagerName = "postgresql-connection-manager"
View Source
const PostgresqlStorageName = "postgresql-storage"

Variables

View Source
var (
	ErrQueryPostgresqlServerTime = errors.New("query postgresql server time failed")
)

Functions

This section is empty.

Types

type PostgresqlConnectionManager added in v0.0.2

type PostgresqlConnectionManager struct {

	// 主机的名字
	Host string

	// 主机的端口
	Port uint

	// 用户名
	User string

	// 密码
	Passwd string

	DatabaseName string

	// DSN
	// Example: "host=192.168.128.206 user=postgres password=123456 port=5432 dbname=postgres sslmode=disable"
	DSN string
	// contains filtered or unexported fields
}

PostgresqlConnectionManager Postgresql的连接管理器

func NewPostgresqlConnectionGetterFromDsn added in v0.0.2

func NewPostgresqlConnectionGetterFromDsn(dsn string) *PostgresqlConnectionManager

NewPostgresqlConnectionGetterFromDsn 从DSN创建Postgresql连接

func NewPostgresqlConnectionGetterFromSqlDb added in v0.0.2

func NewPostgresqlConnectionGetterFromSqlDb(db *sql.DB) *PostgresqlConnectionManager

NewPostgresqlConnectionGetterFromSqlDb 从一个已经存在的*sql.DB创建连接管理器

func NewPostgresqlConnectionManager added in v0.0.2

func NewPostgresqlConnectionManager(host string, port uint, user, passwd, databaseName string) *PostgresqlConnectionManager

NewPostgresqlConnectionManager 从服务器属性创建数据库连接

func (*PostgresqlConnectionManager) GetDSN added in v0.0.2

func (x *PostgresqlConnectionManager) GetDSN() string

func (*PostgresqlConnectionManager) Name added in v0.0.2

func (*PostgresqlConnectionManager) Return added in v0.0.2

func (x *PostgresqlConnectionManager) Return(ctx context.Context, connection *sql.DB) error

func (*PostgresqlConnectionManager) Shutdown added in v0.0.2

func (*PostgresqlConnectionManager) Take added in v0.0.2

Take 获取到数据库的连接

type PostgresqlSqlProvider added in v0.0.2

type PostgresqlSqlProvider struct {
	*sql_based_storage.Sql92Provider
}

PostgresqlSqlProvider storage sql的postgresql方言,因为使用的驱动的占位符不同,所以基本上每个语句都重写了,但处理占位符其它大差不差

func NewPostgresqlSqlProvider added in v0.0.2

func NewPostgresqlSqlProvider() *PostgresqlSqlProvider

func (*PostgresqlSqlProvider) CreateTableSql added in v0.0.2

func (x *PostgresqlSqlProvider) CreateTableSql(ctx context.Context, tableFullName string) (string, []any)

func (*PostgresqlSqlProvider) CreateWithVersionSql added in v0.0.2

func (x *PostgresqlSqlProvider) CreateWithVersionSql(ctx context.Context, tableFullName string, lockId string, version storage.Version, lockInformation *storage.LockInformation) (string, []any)

func (*PostgresqlSqlProvider) DeleteWithVersionSql added in v0.0.2

func (x *PostgresqlSqlProvider) DeleteWithVersionSql(ctx context.Context, tableFullName string, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) (string, []any)

func (*PostgresqlSqlProvider) FindLockInformationJsonStringByIdSql added in v0.0.2

func (x *PostgresqlSqlProvider) FindLockInformationJsonStringByIdSql(ctx context.Context, tableFullName string, lockId string) (string, []any)

func (*PostgresqlSqlProvider) ListLockInformationJsonStringSql added in v0.0.2

func (x *PostgresqlSqlProvider) ListLockInformationJsonStringSql(ctx context.Context, tableFullName string) (string, []any)

func (*PostgresqlSqlProvider) NowTimestampSql added in v0.0.2

func (x *PostgresqlSqlProvider) NowTimestampSql(ctx context.Context, tableFullName string) (string, []any)

func (*PostgresqlSqlProvider) SelectLockInformationJsonStringSql added in v0.0.2

func (x *PostgresqlSqlProvider) SelectLockInformationJsonStringSql(ctx context.Context, tableFullName string, lockId string) (string, []any)

func (*PostgresqlSqlProvider) UpdateWithVersionSql added in v0.0.2

func (x *PostgresqlSqlProvider) UpdateWithVersionSql(ctx context.Context, tableFullName string, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) (string, []any)

type PostgresqlStorage added in v0.0.2

type PostgresqlStorage struct {

	// postgresql是sql storage的一种具体实现
	*sql_based_storage.SqlBasedStorage
	// contains filtered or unexported fields
}

PostgresqlStorage 基于Postgresql作为存储引擎

func NewPostgresqlStorage added in v0.0.2

func NewPostgresqlStorage(ctx context.Context, options *PostgresqlStorageOptions) (*PostgresqlStorage, error)

func (*PostgresqlStorage) CreateWithVersion added in v0.0.2

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

func (*PostgresqlStorage) GetName added in v0.0.2

func (x *PostgresqlStorage) GetName() string

func (*PostgresqlStorage) GetTime added in v0.0.2

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

func (*PostgresqlStorage) Init added in v0.0.2

func (x *PostgresqlStorage) Init(ctx context.Context) (returnError error)

type PostgresqlStorageOptions added in v0.0.2

type PostgresqlStorageOptions struct {

	// 存在哪个schema下,默认是public
	Schema string

	// 存放锁的表的名字,默认是storage_lock
	TableName string

	// 用于获取数据库连接
	ConnectionManager storage.ConnectionManager[*sql.DB]
}

PostgresqlStorageOptions 创建postgresql时的各种参数选项

func NewPostgresqlStorageOptions added in v0.0.2

func NewPostgresqlStorageOptions() *PostgresqlStorageOptions

NewPostgresqlStorageOptions 创建一个Storage选项

func (*PostgresqlStorageOptions) Check added in v0.0.2

func (x *PostgresqlStorageOptions) Check() error

func (*PostgresqlStorageOptions) GetSchema added in v0.0.2

func (x *PostgresqlStorageOptions) GetSchema() string

func (*PostgresqlStorageOptions) GetTableFullName added in v0.0.2

func (x *PostgresqlStorageOptions) GetTableFullName() string

func (*PostgresqlStorageOptions) GetTableName added in v0.0.2

func (x *PostgresqlStorageOptions) GetTableName() string

func (*PostgresqlStorageOptions) SetConnectionManager added in v0.0.2

func (x *PostgresqlStorageOptions) SetConnectionManager(connectionManager storage.ConnectionManager[*sql.DB]) *PostgresqlStorageOptions

func (*PostgresqlStorageOptions) SetSchema added in v0.0.2

func (*PostgresqlStorageOptions) SetTableName added in v0.0.2

func (x *PostgresqlStorageOptions) SetTableName(tableName string) *PostgresqlStorageOptions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL