sql_based_storage

package module
v0.0.0-...-3e91a67 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2023 License: MIT Imports: 8 Imported by: 1

README

SQL Based Storage

一、这是什么?

基于SQL的关系型数据库的Storage的通用实现。

二、安装依赖

go get -u github.com/storage-lock/go-sql-based-storage

三、组件介绍

SqlBasedStorage

把基于SQL构建Storage的流程的公共部分抽象出来,这样对于所有支持SQL的存储设备来说就只需要实现不同的部分就可以了,不同的部分使用SqlProvider来封装不同的SQL方言。

SqlBasedStorageOptions

创建SqlBasedStorage的时候需要提供一些上下文参数,是通过这个Options来传递的:

type SqlBasedStorageOptions struct {
	SqlProvider       SqlProvider
	ConnectionManager storage.ConnectionManager[*sql.DB]
	TableFullName     string
}

SqlProvider

用于封装不同的SQL方言:

package sql_based_storage

import (
	"context"
	"github.com/storage-lock/go-storage"
)

// SqlProvider 用于提供各种SQl方言
type SqlProvider interface {

	// CreateTableSql 构造创建存储锁的表的sql语句以及参数
	CreateTableSql(ctx context.Context, tableFullName string) (string, []any)

	// UpdateWithVersionSql 构造根据版本更新锁信息的sql
	UpdateWithVersionSql(ctx context.Context, tableFullName string, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) (string, []any)

	// CreateWithVersionSql 构造根据版本创建锁信息的sql
	CreateWithVersionSql(ctx context.Context, tableFullName string, lockId string, version storage.Version, lockInformation *storage.LockInformation) (string, []any)

	// DeleteWithVersionSql 构造根据版本删除锁信息的sql
	DeleteWithVersionSql(ctx context.Context, tableFullName string, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) (string, []any)

	// NowTimestampSql 构造获取锁的unix mil时间戳的sql
	NowTimestampSql(ctx context.Context, tableFullName string) (string, []any)

	// SelectLockInformationJsonStringSql 构造根据 lock_id 查询锁信息的sql
	SelectLockInformationJsonStringSql(ctx context.Context, tableFullName string, lockId string) (string, []any)

	// ListLockInformationJsonStringSql 提供列出所有的锁的json string的sql
	ListLockInformationJsonStringSql(ctx context.Context, tableFullName string) (string, []any)
}

Sql97Provider

SqlProvidersql97实现。

四、使用示例

创建你自己的Storage,内嵌*sql_based_storage.SqlBasedStorage,只覆写必要的方法 :

type FooStorage struct {
	*sql_based_storage.SqlBasedStorage
}

var _ storage.Storage = &FooStorage{}

func NewFooStorage(manager storage.ConnectionManager[*sql.DB]) (*FooStorage, error) {
	options := sql_based_storage.NewSqlBasedStorageOptions().SetConnectionManager(manager)
	storage, err := sql_based_storage.NewSqlBasedStorage(options)
	if err != nil {
		return nil, err
	}
	return &FooStorage{
		SqlBasedStorage: storage,
	}, nil
}

func (x *FooStorage) GetName() string {
	return "foo-storage"
}

然后实现自己的sql方言,可以从SqlProvider从头开始写,也可以内嵌Sql97Provider只覆写必要的方法:

type FooSqlProvider struct {
	sql_based_storage.Sql97Provider
}

var _ sql_based_storage.SqlProvider = &FooSqlProvider{}

func (x *FooSqlProvider) NowTimestampSql(ctx context.Context, tableFullName string) (string, []any) {
	return "SELECT NOW()", nil
}

最后使用的话:

func main() {
	db, err := sql.Open("mysql", "xxx")
	if err != nil {
		panic(err)
	}
	connectionManager := storage.NewFixedSqlDBConnectionManager(db)
	fooStorage, err := NewFooStorage(connectionManager)
	if err != nil {
		panic(err)
	}
	fmt.Println(fooStorage.GetName())
}

Documentation

Index

Constants

View Source
const StorageName = "sql-based-storage"

Variables

View Source
var (
	ErrSqlProviderCanNotNil       = fmt.Errorf("SqlProvider can not nil")
	ErrConnectionManagerCanNotNil = fmt.Errorf("ConnectionManager can not nil")
	ErrTableFullNameCanNotEmpty   = fmt.Errorf("TableFullName can not empty")
)

Functions

This section is empty.

Types

type Sql92Provider

type Sql92Provider struct {
}

Sql92Provider SQL Provider的一个实现,SQL92标准

func NewSql92Provider

func NewSql92Provider() *Sql92Provider

func (*Sql92Provider) CreateTableSql

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

func (*Sql92Provider) CreateWithVersionSql

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

func (*Sql92Provider) DeleteWithVersionSql

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

func (*Sql92Provider) FindLockInformationJsonStringByIdSql

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

func (*Sql92Provider) ListLockInformationJsonStringSql

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

func (*Sql92Provider) NowTimestampSql

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

func (*Sql92Provider) UpdateWithVersionSql

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

type SqlBasedStorage

type SqlBasedStorage struct {
	// contains filtered or unexported fields
}

SqlBasedStorage 基于SQL实现的Storage的抽象实现

func NewSqlBasedStorage

func NewSqlBasedStorage(options *SqlBasedStorageOptions) (*SqlBasedStorage, error)

func (*SqlBasedStorage) Close

func (x *SqlBasedStorage) Close(ctx context.Context) error

Close 释放Storage占用的资源

func (*SqlBasedStorage) CreateWithVersion

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

func (*SqlBasedStorage) DeleteWithVersion

func (x *SqlBasedStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) (returnError error)

func (*SqlBasedStorage) Get

func (x *SqlBasedStorage) Get(ctx context.Context, lockId string) (lockInformationJsonString string, returnError error)

func (*SqlBasedStorage) GetName

func (x *SqlBasedStorage) GetName() string

func (*SqlBasedStorage) GetTime

func (x *SqlBasedStorage) GetTime(ctx context.Context) (now time.Time, returnError error)

GetTime 获取Storage实例的时间

func (*SqlBasedStorage) Init

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

func (*SqlBasedStorage) List

func (x *SqlBasedStorage) List(ctx context.Context) (iterator iterator.Iterator[*storage.LockInformation], returnError error)

List 列出当前存储的所有的锁

func (*SqlBasedStorage) UpdateWithVersion

func (x *SqlBasedStorage) UpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) (returnError error)

type SqlBasedStorageOptions

type SqlBasedStorageOptions struct {

	// 用于提供SQL
	SqlProvider SqlProvider

	// 用于管理连接
	ConnectionManager storage.ConnectionManager[*sql.DB]

	// 存储锁的表的名称
	TableFullName string
}

func NewSqlBasedStorageOptions

func NewSqlBasedStorageOptions() *SqlBasedStorageOptions

func (*SqlBasedStorageOptions) Check

func (x *SqlBasedStorageOptions) Check() error

func (*SqlBasedStorageOptions) SetConnectionManager

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

func (*SqlBasedStorageOptions) SetSqlProvider

func (x *SqlBasedStorageOptions) SetSqlProvider(sqlProvider SqlProvider) *SqlBasedStorageOptions

func (*SqlBasedStorageOptions) SetTableFullName

func (x *SqlBasedStorageOptions) SetTableFullName(tableFullName string) *SqlBasedStorageOptions

type SqlProvider

type SqlProvider interface {

	// CreateTableSql 返回创建存储锁的表的SQL,创建时请根据需要自行设置主键约束
	CreateTableSql(ctx context.Context, tableFullName string) (string, []any)

	// UpdateWithVersionSql 根据LockId、Version更新LockInformation的SQL
	UpdateWithVersionSql(ctx context.Context, tableFullName string, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) (string, []any)

	// CreateWithVersionSql 根据LockId、Version创建LockInformation的SQL
	CreateWithVersionSql(ctx context.Context, tableFullName string, lockId string, version storage.Version, lockInformation *storage.LockInformation) (string, []any)

	// DeleteWithVersionSql 根据LockId和Version删除锁信息的SQL
	DeleteWithVersionSql(ctx context.Context, tableFullName string, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) (string, []any)

	// NowTimestampSql 获取Storage当前时间的Unix时间戳的SQL
	NowTimestampSql(ctx context.Context, tableFullName string) (string, []any)

	// FindLockInformationJsonStringByIdSql 根据LockId查询锁信息的JsonString的SQL
	FindLockInformationJsonStringByIdSql(ctx context.Context, tableFullName string, lockId string) (string, []any)

	// ListLockInformationJsonStringSql 返回能够列出所有的锁的JsonString的SQL
	ListLockInformationJsonStringSql(ctx context.Context, tableFullName string) (string, []any)
}

SqlProvider 用于提供各种用途的SQl方言

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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