sqlutil

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUserExists = errors.New("username already exists")

ErrUserExists is returned if a username already exists in the database.

Functions

func EndTransaction

func EndTransaction(txn Transaction, succeeded *bool) error

EndTransaction ends a transaction. If the transaction succeeded then it is committed, otherwise it is rolledback. You MUST check the error returned from this function to be sure that the transaction was applied correctly. For example, 'database is locked' errors in sqlite will happen here.

func EndTransactionWithCheck

func EndTransactionWithCheck(txn Transaction, succeeded *bool, err *error)

EndTransactionWithCheck ends a transaction and overwrites the error pointer if its value was nil. If the transaction succeeded then it is committed, otherwise it is rolledback. Designed to be used with defer (see EndTransaction otherwise).

func ErrorIsNoRows added in v0.6.21

func ErrorIsNoRows(err error) bool

func Open

func Open(ctx context.Context, dbProperties *config.DatabaseOptions, writer Writer) (*sql.DB, error)

Open opens a database specified by its database driver name and a driver-specific data source name, usually consisting of at least a database name and connection information.

func WithTransaction

func WithTransaction(ctx context.Context, txn Transaction, fn func(ctx context.Context) error) (err error)

WithTransaction runs a block of code passing in an SQL transaction If the code returns an error or panics then the transactions is rolledback Otherwise the transaction is committed.

func WithTransactionStack added in v0.6.21

func WithTransactionStack(ctx context.Context, stack *TransactionStack) context.Context

WithTransactionStack adds a transaction stack to the context

Types

type ConnectionManager added in v0.6.21

type ConnectionManager interface {
	Writer
	DS() *config.DataSource
	Connection(ctx context.Context, readOnly bool) *gorm.DB
	BeginTx(ctx context.Context, opts ...*WriterOption) (context.Context, Transaction, error)
	FromOptions(ctx context.Context, opts *config.DatabaseOptions) (ConnectionManager, error)
	Collect(migrations ...*frame.MigrationPatch) error
	Migrate(ctx context.Context) error
}

func NewConnectionManager

func NewConnectionManager(service *frame.Service) ConnectionManager

func NewConnectionManagerWithOptions added in v0.6.21

func NewConnectionManagerWithOptions(ctx context.Context, service *frame.Service, opts *config.DatabaseOptions) (ConnectionManager, error)

NewConnectionManagerWithOptions ensures a Pool exists for the given options and returns a copy with that as primary

type Connections

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

func (*Connections) BeginTx added in v0.6.21

func (c *Connections) BeginTx(ctx context.Context, opts ...*WriterOption) (context.Context, Transaction, error)

func (*Connections) Collect added in v0.6.21

func (c *Connections) Collect(migrations ...*frame.MigrationPatch) error

func (*Connections) Connection

func (c *Connections) Connection(ctx context.Context, readOnly bool) *gorm.DB

func (*Connections) DS added in v0.6.21

func (c *Connections) DS() *config.DataSource

func (*Connections) Do added in v0.6.21

func (c *Connections) Do(ctx context.Context, f func(ctx context.Context) error, opts ...*WriterOption) error

func (*Connections) FromOptions added in v0.6.21

func (*Connections) Migrate added in v0.6.21

func (c *Connections) Migrate(ctx context.Context) error

type DefaultWriter added in v0.6.21

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

DefaultWriter implements sqlutil.Writer. The DefaultWriter is designed to allow reuse of the sqlutil.Writer interface but, unlike ExclusiveWriter, it will not guarantee writer exclusivity. This is fine in PostgreSQL where overlapping transactions and writes are acceptable.

func (*DefaultWriter) Do added in v0.6.21

func (w *DefaultWriter) Do(ctx context.Context, f func(ctx context.Context) error, opts ...*WriterOption) error

type ExclusiveWriter

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

ExclusiveWriter implements sqlutil.Writer. ExclusiveWriter allows queuing database writes so that you don't contend on database locks in, e.g. SQLite. Only one task will run at a time on a given ExclusiveWriter.

func (*ExclusiveWriter) Do

func (w *ExclusiveWriter) Do(ctx context.Context, f func(ctx context.Context) error, opts ...*WriterOption) error

Do queues a task to be run by a TransactionWriter. The function provided will be ran within a transaction as supplied by the txn parameter if one is supplied, and if not, will take out a new transaction from the database supplied in the database parameter. Either way, this will block until the task is done.

type Transaction

type Transaction interface {
	// Commit the transaction
	Commit() error
	// Rollback the transaction.
	Rollback() error
}

A Transaction is something that can be committed or rolledback.

type TransactionStack added in v0.6.21

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

TransactionStack represents a stack of transactions to enable proper nesting

func GetTransactionStack added in v0.6.21

func GetTransactionStack(ctx context.Context) *TransactionStack

GetTransactionStack retrieves the transaction stack from the context or creates a new one

func (*TransactionStack) IsEmpty added in v0.6.21

func (s *TransactionStack) IsEmpty() bool

IsEmpty returns true if the stack has no transactions

func (*TransactionStack) Peek added in v0.6.21

func (s *TransactionStack) Peek() Transaction

Peek returns the transaction at the top of the stack without removing it

func (*TransactionStack) Pop added in v0.6.21

func (s *TransactionStack) Pop() Transaction

Pop removes and returns the transaction at the top of the stack

func (*TransactionStack) Push added in v0.6.21

func (s *TransactionStack) Push(txn Transaction)

Push adds a new transaction to the top of the stack

type Writer

type Writer interface {
	// Do queues up one or more database write operations within the
	// provided function to be executed when it is safe to do so.
	Do(ctx context.Context, f func(ctx context.Context) error, opts ...*WriterOption) error
}

The Writer interface is designed to solve the problem of how to handle database writes for database engines that don't allow concurrent writes, e.g. SQLite.

The interface has a single Do function which takes an optional database parameter, an optional transaction parameter and a required function parameter. The Writer will call the function provided when it is safe to do so, optionally providing a transaction to use.

The Writer will call f() when it is safe to do so. The supplied "txn" will ALWAYS be passed through to f() via the context. Use the connection manager to obtain the connection. If one exists in the context then it will be re used.

You MUST take particular care not to call Do() from within f() on the same Writer, or it will likely result in a deadlock.

func NewDefaultWriter added in v0.6.21

func NewDefaultWriter(cm ConnectionManager) Writer

NewDefaultWriter returns a new dummy writer.

func NewExclusiveWriter

func NewExclusiveWriter(cm ConnectionManager) Writer

type WriterOption added in v0.6.21

type WriterOption struct {
	SqlOpts []*sql.TxOptions
}

WriterOption is an optional parameter to Writer.Do

Jump to

Keyboard shortcuts

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