sqlite3_storage

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: MIT Imports: 9 Imported by: 1

README

SQLite3 Storage

一、这是什么

以sqlite3为存储引擎的Storage实现,当前仓库为比较底层的存储层实现,你可以与storage-lock结合使用。

sqlite3的数据库文件通常以.db或者.sqlite3结尾,可以使用SQLiteSpy查看。

二、安装依赖

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

三、API Examples

3.1 从数据库路径创建Sqlite3Storage

sqlite3是单文件数据库,连接的时候指定数据库文件的路径,下面的例子演示了如何从一个数据库文件路径创建Sqlite3Storage:

package main

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

func main() {

	// 使用一个sqlite3的数据库的路径创建ConnectionManager
	dbPath := "test_data/test.db3"
	connectionManager, err := sqlite3_storage.NewSqlite3ConnectionManager(dbPath)
	if err != nil {
		panic(err)
	}

	// 然后从这个ConnectionManager创建Sqlite3Storage
	options := sqlite3_storage.NewSqlite3StorageOptions().SetConnectionManager(connectionManager)
	storage, err := sqlite3_storage.NewSqlite3Storage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

3.2 从sql.DB创建Sqlite3Storage

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

package main

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

func main() {

	// 假设已经在其它地方初始化数据库连接得到了一个*sql.DB
	dbPath := "test_data/test.db3"
	db, err := sql.Open("sqlite3", dbPath)
	if err != nil {
		panic(err)
	}

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

}

Documentation

Index

Constants

View Source
const SqliteConnectionManagerName = "sqlite-connection-manager"
View Source
const SqliteStorageName = "sqlite3-storage"

Variables

This section is empty.

Functions

This section is empty.

Types

type Sqlite3ConnectionManager

type Sqlite3ConnectionManager struct {
	*storage.FixedSqlDBConnectionManager
}

Sqlite3ConnectionManager 用于连接到sqlite3的数据库

func NewSqlite3ConnectionManager

func NewSqlite3ConnectionManager(dbPath string) (*Sqlite3ConnectionManager, error)

func (*Sqlite3ConnectionManager) Name

func (x *Sqlite3ConnectionManager) Name() string

type Sqlite3Storage

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

func NewSqlite3Storage

func NewSqlite3Storage(ctx context.Context, options *Sqlite3StorageOptions) (*Sqlite3Storage, error)

func (*Sqlite3Storage) Close

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

func (*Sqlite3Storage) CreateWithVersion

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

func (*Sqlite3Storage) DeleteWithVersion

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

func (*Sqlite3Storage) Get

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

func (*Sqlite3Storage) GetName

func (x *Sqlite3Storage) GetName() string

func (*Sqlite3Storage) GetTime

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

func (*Sqlite3Storage) Init

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

func (*Sqlite3Storage) List

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

func (*Sqlite3Storage) UpdateWithVersion

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

type Sqlite3StorageOptions

type Sqlite3StorageOptions struct {
	ConnectionManager storage.ConnectionManager[*sql.DB]
	TableName         string
}

func NewSqlite3StorageOptions

func NewSqlite3StorageOptions() *Sqlite3StorageOptions

func (*Sqlite3StorageOptions) SetConnectionManager

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

func (*Sqlite3StorageOptions) SetTableName

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

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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