sql

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 26 Imported by: 4

Documentation

Overview

Package sql is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoConnection is returned if pooled connection is not available.
	ErrNoConnection = errors.New("database: no free connection")
	// ErrNotFound is returned if requested record is not found.
	ErrNotFound = errors.New("database: not found")
	// ErrObjectExists is returned if database constraints didn't allow to insert an object.
	ErrObjectExists = errors.New("database: object exists")
	// ErrTooNew is returned if database version is newer than expected.
	ErrTooNew = errors.New("database version is too new")
)

Functions

func AppendToCachedSlice added in v1.4.0

func AppendToCachedSlice[T any](db any, key queryCacheKey, v T)

AppendToCachedSlice adds a value to the slice stored in the cache by invoking the specified SliceAppender. If the entry is not cached, the function does nothing.

func GetBlobSizes added in v1.4.1

func GetBlobSizes(db Executor, cmd string, ids [][]byte) (sizes []int, err error)

GetBlobSizes returns a slice containing the sizes of blobs corresponding to the specified ids. For non-existent ids the corresponding value is -1.

func IsCached added in v1.4.1

func IsCached(db any) bool

IsCached returns true if the database is cached.

func IsNull added in v1.5.0

func IsNull(stmt *Statement, col int) bool

IsNull returns true if the specified result column is null.

func LoadBlob added in v1.4.1

func LoadBlob(db Executor, cmd string, id []byte, blob *Blob) error

LoadBlob loads an encoded blob.

func QueryCacheKey added in v1.4.0

func QueryCacheKey(kind QueryCacheKind, key string) queryCacheKey

QueryCacheKey creates a key for QueryCache.

func Vacuum added in v1.1.10

func Vacuum(db Executor) error

func Version added in v1.5.0

func Version(uri string) (int, error)

func WithCachedSubKey added in v1.4.0

func WithCachedSubKey[T any](
	ctx context.Context,
	db any,
	key queryCacheKey,
	subKey QueryCacheSubKey,
	retrieve func(ctx context.Context) (T, error),
) (T, error)

WithCachedValue retrieves the specified value identified by the key and subKey from the cache. If the entry is absent from the cache, it's populated by calling retrieve func. Note that the retrieve func should never cause UpdateSlice to be called.

func WithCachedValue added in v1.4.0

func WithCachedValue[T any](
	ctx context.Context,
	db any,
	key queryCacheKey,
	retrieve func(ctx context.Context) (T, error),
) (T, error)

WithCachedValue retrieves the specified value from the cache. If the entry is absent from the cache, it's populated by calling retrieve func. Note that the retrieve func should never cause UpdateSlice to be called.

Types

type Blob added in v1.4.1

type Blob struct {
	Bytes []byte
}

Blob represents a binary blob data. It can be reused efficiently across multiple data retrieval operations, minimizing reallocations of the underlying byte slice.

type Database

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

Database is an instance of sqlite database.

func InMemory

func InMemory(opts ...Opt) *Database

InMemory database for testing.

func Open

func Open(uri string, opts ...Opt) (*Database, error)

Open database with options.

Database is opened in WAL mode and pragma synchronous=normal. https://sqlite.org/wal.html https://www.sqlite.org/pragma.html#pragma_synchronous

func (Database) ClearCache added in v1.4.0

func (c Database) ClearCache()

func (*Database) Close

func (db *Database) Close() error

Close closes all pooled connections.

func (*Database) Exec

func (db *Database) Exec(query string, encoder Encoder, decoder Decoder) (int, error)

Exec statement using one of the connection from the pool.

If you care about atomicity of the operation (for example writing rewards to multiple accounts) Tx should be used. Otherwise sqlite will not guarantee that all side-effects of operations are applied to the database if machine crashes.

Note that Exec will block until database is closed or statement has finished. If application needs to control statement execution lifetime use one of the transaction.

func (Database) GetValue added in v1.4.0

func (c Database) GetValue(
	ctx context.Context,
	key queryCacheKey,
	subKey QueryCacheSubKey,
	retrieve UntypedRetrieveFunc,
) (any, error)

func (Database) IsCached added in v1.4.1

func (c Database) IsCached() bool

func (*Database) QueryCache added in v1.4.0

func (db *Database) QueryCache() QueryCache

Return database's QueryCache.

func (*Database) QueryCount added in v1.4.0

func (db *Database) QueryCount() int

QueryCount returns the number of queries executed, including failed queries, but not counting transaction start / commit / rollback.

func (*Database) Tx

func (db *Database) Tx(ctx context.Context) (*Tx, error)

Tx creates deferred sqlite transaction.

Deferred transactions are not started until the first statement. Transaction may be started in read mode and automatically upgraded to write mode after one of the write statements.

https://www.sqlite.org/lang_transaction.html

func (*Database) TxImmediate

func (db *Database) TxImmediate(ctx context.Context) (*Tx, error)

TxImmediate creates immediate transaction.

IMMEDIATE cause the database connection to start a new write immediately, without waiting for a write statement. The BEGIN IMMEDIATE might fail with SQLITE_BUSY if another write transaction is already active on another database connection.

func (Database) UpdateSlice added in v1.4.0

func (c Database) UpdateSlice(key queryCacheKey, update SliceAppender)

func (*Database) WithTx

func (db *Database) WithTx(ctx context.Context, exec func(*Tx) error) error

WithTx will pass initialized deferred transaction to exec callback. Will commit only if error is nil.

func (*Database) WithTxImmediate

func (db *Database) WithTxImmediate(ctx context.Context, exec func(*Tx) error) error

WithTxImmediate will pass initialized immediate transaction to exec callback. Will commit only if error is nil.

type Decoder

type Decoder func(*Statement) bool

Decoder for sqlite rows.

type Encoder

type Encoder func(*Statement)

Encoder for parameters. Both positional parameters: select block from blocks where id = ?1;

and named parameters are supported: select blocks from blocks where id = @id;

For complete information see https://www.sqlite.org/c3ref/bind_blob.html.

type Executor

type Executor interface {
	Exec(string, Encoder, Decoder) (int, error)
}

Executor is an interface for executing raw statement.

type Migration added in v1.3.0

type Migration interface {
	Apply(db Executor) error
	Rollback() error
	Name() string
	Order() int
}

Migration is interface for migrations provider.

func LocalMigrations added in v1.3.0

func LocalMigrations() ([]Migration, error)

func StateMigrations added in v1.3.0

func StateMigrations() ([]Migration, error)

type MockMigration added in v1.3.0

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

MockMigration is a mock of Migration interface.

func NewMockMigration added in v1.3.0

func NewMockMigration(ctrl *gomock.Controller) *MockMigration

NewMockMigration creates a new mock instance.

func (*MockMigration) Apply added in v1.3.0

func (m *MockMigration) Apply(db Executor) error

Apply mocks base method.

func (*MockMigration) EXPECT added in v1.3.0

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockMigration) Name added in v1.3.0

func (m *MockMigration) Name() string

Name mocks base method.

func (*MockMigration) Order added in v1.3.0

func (m *MockMigration) Order() int

Order mocks base method.

func (*MockMigration) Rollback added in v1.3.0

func (m *MockMigration) Rollback() error

Rollback mocks base method.

type MockMigrationApplyCall added in v1.4.0

type MockMigrationApplyCall struct {
	*gomock.Call
}

MockMigrationApplyCall wrap *gomock.Call

func (*MockMigrationApplyCall) Do added in v1.4.0

Do rewrite *gomock.Call.Do

func (*MockMigrationApplyCall) DoAndReturn added in v1.4.0

DoAndReturn rewrite *gomock.Call.DoAndReturn

func (*MockMigrationApplyCall) Return added in v1.4.0

Return rewrite *gomock.Call.Return

type MockMigrationMockRecorder added in v1.3.0

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

MockMigrationMockRecorder is the mock recorder for MockMigration.

func (*MockMigrationMockRecorder) Apply added in v1.3.0

Apply indicates an expected call of Apply.

func (*MockMigrationMockRecorder) Name added in v1.3.0

Name indicates an expected call of Name.

func (*MockMigrationMockRecorder) Order added in v1.3.0

Order indicates an expected call of Order.

func (*MockMigrationMockRecorder) Rollback added in v1.3.0

Rollback indicates an expected call of Rollback.

type MockMigrationNameCall added in v1.4.0

type MockMigrationNameCall struct {
	*gomock.Call
}

MockMigrationNameCall wrap *gomock.Call

func (*MockMigrationNameCall) Do added in v1.4.0

Do rewrite *gomock.Call.Do

func (*MockMigrationNameCall) DoAndReturn added in v1.4.0

func (c *MockMigrationNameCall) DoAndReturn(f func() string) *MockMigrationNameCall

DoAndReturn rewrite *gomock.Call.DoAndReturn

func (*MockMigrationNameCall) Return added in v1.4.0

Return rewrite *gomock.Call.Return

type MockMigrationOrderCall added in v1.4.0

type MockMigrationOrderCall struct {
	*gomock.Call
}

MockMigrationOrderCall wrap *gomock.Call

func (*MockMigrationOrderCall) Do added in v1.4.0

Do rewrite *gomock.Call.Do

func (*MockMigrationOrderCall) DoAndReturn added in v1.4.0

func (c *MockMigrationOrderCall) DoAndReturn(f func() int) *MockMigrationOrderCall

DoAndReturn rewrite *gomock.Call.DoAndReturn

func (*MockMigrationOrderCall) Return added in v1.4.0

Return rewrite *gomock.Call.Return

type MockMigrationRollbackCall added in v1.4.0

type MockMigrationRollbackCall struct {
	*gomock.Call
}

MockMigrationRollbackCall wrap *gomock.Call

func (*MockMigrationRollbackCall) Do added in v1.4.0

Do rewrite *gomock.Call.Do

func (*MockMigrationRollbackCall) DoAndReturn added in v1.4.0

func (c *MockMigrationRollbackCall) DoAndReturn(f func() error) *MockMigrationRollbackCall

DoAndReturn rewrite *gomock.Call.DoAndReturn

func (*MockMigrationRollbackCall) Return added in v1.4.0

Return rewrite *gomock.Call.Return

type Opt

type Opt func(c *conf)

Opt for configuring database.

func WithConnections

func WithConnections(n int) Opt

WithConnections overwrites number of pooled connections.

func WithLatencyMetering

func WithLatencyMetering(enable bool) Opt

WithLatencyMetering enables metric that track latency for every database query. Note that it will be a significant amount of data, and should not be enabled on multiple nodes by default.

func WithLogger added in v1.3.6

func WithLogger(logger *zap.Logger) Opt

func WithMigration added in v1.3.0

func WithMigration(migration Migration) Opt

WithMigration adds migration to the list of migrations. It will overwrite an existing migration with the same order.

func WithMigrations

func WithMigrations(migrations []Migration) Opt

WithMigrations overwrites embedded migrations. Migrations are sorted by order before applying.

func WithQueryCache added in v1.4.0

func WithQueryCache(enable bool) Opt

WithQueryCache enables in-memory caching of results of some queries.

func WithQueryCacheSizes added in v1.4.0

func WithQueryCacheSizes(sizes map[QueryCacheKind]int) Opt

WithQueryCacheSizes sets query cache sizes for the specified cache kinds.

func WithSkipMigrations added in v1.3.0

func WithSkipMigrations(i ...int) Opt

WithSkipMigrations will update database version with executing associated migrations. It should be used at your own risk.

func WithVacuumState added in v1.3.0

func WithVacuumState(i int) Opt

WithVacuumState will execute vacuum if database version before the migration was less or equal to the provided value.

type QueryCache added in v1.4.0

type QueryCache interface {
	// IsCached returns true if the requests are being cached.
	IsCached() bool
	// GetValue retrieves the specified key+subKey value from the cache. If
	// the entry is absent from cache, it's populated by calling retrieve func.
	// Note that the retrieve func should never cause UpdateSlice to be
	// called for this cache.
	GetValue(
		ctx context.Context,
		key queryCacheKey,
		subKey QueryCacheSubKey,
		retrieve UntypedRetrieveFunc,
	) (any, error)
	// UpdateSlice updates the slice stored in the cache by invoking the
	// specified SliceAppender. If the entry is not cached, the method does
	// nothing.
	UpdateSlice(key queryCacheKey, update SliceAppender)
	// ClearCache empties the cache.
	ClearCache()
}

QueryCache stores results of SQL queries and data derived from these results. Presently, the cached entries are never removed, but eventually, it might become an LRU cache.

var NullQueryCache QueryCache = (*queryCache)(nil)

type QueryCacheKind added in v1.4.0

type QueryCacheKind string

type QueryCacheSubKey added in v1.4.0

type QueryCacheSubKey string

QueryCacheSubKey denotes a cache subkey. The empty subkey refers to the main key. All other subkeys are cleared by UpdateSlice for the key. The subkeys are intended to store data derived from the query results, such as serialized responses.

type RetrieveFunc added in v1.4.0

type RetrieveFunc[T any] func() (T, error)

RetrieveFunc retrieves a value to be stored in the cache.

type SliceAppender added in v1.4.0

type SliceAppender func(s any) any

SliceAppender modifies slice value stored in the cache, appending the specified item to it and returns the updated slice.

type Statement

type Statement = sqlite.Stmt

Statement is an sqlite statement.

type Tx

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

Tx is wrapper for database transaction.

func (Tx) ClearCache added in v1.4.0

func (c Tx) ClearCache()

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit transaction.

func (*Tx) Exec

func (tx *Tx) Exec(query string, encoder Encoder, decoder Decoder) (int, error)

Exec query.

func (Tx) GetValue added in v1.4.0

func (c Tx) GetValue(
	ctx context.Context,
	key queryCacheKey,
	subKey QueryCacheSubKey,
	retrieve UntypedRetrieveFunc,
) (any, error)

func (Tx) IsCached added in v1.4.1

func (c Tx) IsCached() bool

func (*Tx) Release

func (tx *Tx) Release() error

Release transaction. Every transaction that was created must be released.

func (Tx) UpdateSlice added in v1.4.0

func (c Tx) UpdateSlice(key queryCacheKey, update SliceAppender)

type UntypedRetrieveFunc added in v1.4.0

type UntypedRetrieveFunc func(ctx context.Context) (any, error)

UntypedRetrieveFunc retrieves a value to be cached.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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