mysql_storage

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2023 License: MIT Imports: 10 Imported by: 5

README

MySQL Storage

一、这是什么

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

二、安装依赖

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

三、API Examples

3.1 从DSN创建MySQLStorage

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

package main

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

func main() {

	// 使用一个DSN形式的数据库连接字符串创建ConnectionManager
	testDsn := "root:UeGqAm8CxYGldMDLoNNt@tcp(127.0.0.1:3306)/storage_lock_test"
	connectionManager := mysql_storage.NewMySQLConnectionManagerFromDSN(testDsn)

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

}

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

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

package main

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

func main() {

	// 数据库连接不是DSN的形式,就是一堆零散的属性,则依次设置,可以得到一个连接管理器
	host := "127.0.0.1"
	port := uint(3306)
	username := "root"
	passwd := "UeGqAm8CxYGldMDLoNNt"
	database := "storage_lock_test"
	connectionManager := mysql_storage.NewMySQLConnectionManager(host, port, username, passwd, database)

	// 然后从这个连接管理器创建MySQL Storage
	options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mysql_storage.NewMySQLStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

3.3 从sql.DB创建MySQLStorage

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

package main

import (
	"context"
	"database/sql"
	"fmt"
	mysql_storage "github.com/storage-lock/go-mysql-storage"
	storage "github.com/storage-lock/go-storage"
)

func main() {

	// 假设已经在其它地方初始化数据库连接得到了一个*sql.DB
	testDsn := "root:UeGqAm8CxYGldMDLoNNt@tcp(127.0.0.1:3306)/storage_lock_test"
	db, err := sql.Open("mysql", testDsn)
	if err != nil {
		panic(err)
	}

	// 则可以从这个*sql.DB中创建一个MySQL Storage
	connectionManager := storage.NewFixedSqlDBConnectionManager(db)
	options := mysql_storage.NewMySQLStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mysql_storage.NewMySQLStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

Documentation

Index

Constants

View Source
const MySQLConnectionManagerName = "mysql-connection-manager"
View Source
const StorageName = "mysql-storage"

Variables

This section is empty.

Functions

This section is empty.

Types

type MySQLConnectionManager

type MySQLConnectionManager struct {

	// 主机的名字
	Host string

	// 主机的端口
	Port uint

	// 用户名
	User string

	// 密码
	Passwd string

	DatabaseName string

	DSN string
	// contains filtered or unexported fields
}

MySQLConnectionManager 创建一个MySQL的连接管理器

func NewMySQLConnectionManager added in v0.0.2

func NewMySQLConnectionManager(host string, port uint, user, passwd, database string) *MySQLConnectionManager

NewMySQLConnectionManager 从连接属性创建数据库连接

func NewMySQLConnectionManagerFromDSN

func NewMySQLConnectionManagerFromDSN(dsn string) *MySQLConnectionManager

NewMySQLConnectionManagerFromDSN 从DSN创建MySQL连接管理器

func (*MySQLConnectionManager) GetDSN

func (x *MySQLConnectionManager) GetDSN() string

func (*MySQLConnectionManager) Name

func (x *MySQLConnectionManager) Name() string

func (*MySQLConnectionManager) Return

func (x *MySQLConnectionManager) Return(ctx context.Context, db *sql.DB) error

func (*MySQLConnectionManager) SetDatabaseName

func (x *MySQLConnectionManager) SetDatabaseName(databaseName string) *MySQLConnectionManager

func (*MySQLConnectionManager) SetHost

func (*MySQLConnectionManager) SetPasswd

func (*MySQLConnectionManager) SetPort

func (*MySQLConnectionManager) SetUser

func (*MySQLConnectionManager) Shutdown

func (x *MySQLConnectionManager) Shutdown(ctx context.Context) error

func (*MySQLConnectionManager) Take

func (x *MySQLConnectionManager) Take(ctx context.Context) (*sql.DB, error)

Take 获取到数据库的连接

type MySQLStorage

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

func NewMySQLStorage

func NewMySQLStorage(ctx context.Context, options *MySQLStorageOptions) (*MySQLStorage, error)

func (*MySQLStorage) Close

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

func (*MySQLStorage) CreateWithVersion added in v0.0.2

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

func (*MySQLStorage) DeleteWithVersion

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

func (*MySQLStorage) Get

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

func (*MySQLStorage) GetName

func (x *MySQLStorage) GetName() string

func (*MySQLStorage) GetTime

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

func (*MySQLStorage) Init

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

func (*MySQLStorage) List

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

func (*MySQLStorage) UpdateWithVersion

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

type MySQLStorageOptions

type MySQLStorageOptions struct {

	// 存放锁的表的名字,如果未指定的话则使用默认的表
	TableName string

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

MySQLStorageOptions 基于MySQL为存储引擎时的选项

func NewMySQLStorageOptions

func NewMySQLStorageOptions() *MySQLStorageOptions

func (*MySQLStorageOptions) SetConnectionManager added in v0.0.2

func (x *MySQLStorageOptions) SetConnectionManager(connManager storage.ConnectionManager[*sql.DB]) *MySQLStorageOptions

func (*MySQLStorageOptions) SetTableName

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

Jump to

Keyboard shortcuts

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