sqlite3_storage

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 9 Imported by: 3 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 DriverName = "sqlite3"

DriverName 是本包默认使用的 SQLite 驱动名。

选 "sqlite3" 而不是更具体名字是为了兼容既有调用方:mattn/go-sqlite3 注册的就是这个名字,而现代替代品(modernc.org/sqlite)用同一名字注册, 两者可以互换,调用方的 DSN 不用改。

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) Capabilities added in v0.0.2

func (x *Sqlite3Storage) Capabilities() []storage.StorageCapability

Capabilities 声明 SQLite3 存储支持的能力 SQLite3 通过主键唯一约束和 WHERE 条件更新支持 CAS, 通过 STRFTIME('%s','now') 提供数据库服务器时间 注意:SQLite3 是单文件数据库,主要用于单进程场景,多进程并发需要启用 WAL 模式

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)

func (*Sqlite3Storage) Watch added in v0.0.2

func (x *Sqlite3Storage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, 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
⌂ 换网址