sql

package
v0.2.12-beta-0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2022 License: MIT Imports: 13 Imported by: 4

Documentation

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.
	// TODO(dshulyak) is an alias to datatabase.ErrNotFound until full transition is implemented.
	ErrNotFound = database.ErrNotFound
	// ErrObjectExists is returned if database contraints didn't allow to insert an object.
	ErrObjectExists = errors.New("database: object exists")
)

Functions

This section is empty.

Types

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) 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 datatabase is closed or statement has finished. If application needs to control statement execution lifetime use one of the transaction.

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

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 Migrations

type Migrations func(Executor) error

Migrations is interface for migrations provider.

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 WithMigrations

func WithMigrations(migrations Migrations) Opt

WithMigrations overwrites embedded migrations.

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) 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) Release

func (tx *Tx) Release() error

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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