connection

package
v2.1.8 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginTransaction added in v2.1.7

func BeginTransaction(ctx context.Context, conn DB) (DB, TXFinishFunc, error)

BeginTransaction will wrap the passed DB into a transaction handler that supports it being used with less care and prevents having to check if we are already in a tx and failures due to eager committers.

func EscapeArgs

func EscapeArgs(query string, args []interface{}) (string, []interface{}, error)

EscapeArgs return the query and args with the argument placeholder escaped.

The argument placeholder is `?`. If you need an actual `?` in the output, you can input `\?`.If you need an actual `\` in the output, input `\\`.

Types

type DB

type DB interface {
	// Clone returns a stateful copy of this connection.
	Clone() DB
	// Close does so for all the underlying connections and returns an error if the driver provides one.
	Close() error
	// QueryIter returns closure allowing to load/fetch roads one by one.
	QueryIter(ctx context.Context, statement string, fields []string, args ...interface{}) (ResultFetchIter, error)
	// EQueryIter is QueryIter but will use EscapeArgs.
	EQueryIter(ctx context.Context, statement string, fields []string, args ...interface{}) (ResultFetchIter, error)
	// Query returns a closure that allows fetching of the results of the query.
	Query(ctx context.Context, statement string, fields []string, args ...interface{}) (ResultFetch, error)
	// EQuery is Query but will use EscapeArgs.
	EQuery(ctx context.Context, statement string, fields []string, args ...interface{}) (ResultFetch, error)
	// QueryPrimitive returns a closure that allows fetching of the results of a query to a
	// slice of primitives.
	QueryPrimitive(ctx context.Context, statement string, field string, args ...interface{}) (ResultFetch, error)
	// EQueryPrimitive is QueryPrimitive but will use EscapeArgs
	EQueryPrimitive(ctx context.Context, statement string, field string, args ...interface{}) (ResultFetch, error)
	// Raw ins intended to be an all raw query that runs statement with args and tries
	// to retrieve the results into fields without much magic whatsoever.
	Raw(ctx context.Context, statement string, args []interface{}, fields ...interface{}) error
	// ERaw is Raw but will use EscapeArgs
	ERaw(ctx context.Context, statement string, args []interface{}, fields ...interface{}) error
	// Exec is intended for queries that do not yield results (data modifiers)
	Exec(ctx context.Context, statement string, args ...interface{}) error
	// ExecResult is intended for queries that modify data and respond with how many rows were affected.
	ExecResult(ctx context.Context, statement string, args ...interface{}) (int64, error)
	// EExec is Exec but will use EscapeArgs.
	EExec(ctx context.Context, statement string, args ...interface{}) error
	// BeginTransaction returns a new DB that will use the transaction instead of the basic conn.
	BeginTransaction(ctx context.Context) (DB, error)
	// CommitTransaction commits the transaction
	CommitTransaction(ctx context.Context) error
	// RollbackTransaction rolls back the transaction
	RollbackTransaction(ctx context.Context) error
	// IsTransaction indicates if the DB is in the middle of a transaction.
	IsTransaction() bool
	// Set allows to change settings for the current transaction.
	Set(ctx context.Context, set string) error
	// BulkInsert Inserts in the most efficient way possible a lot of data.
	BulkInsert(ctx context.Context, tableName string, columns []string, values [][]interface{}) (execError error)
}

DB represents an active database connection.

type DatabaseHandler

type DatabaseHandler interface {
	// Open must be able to connect to the handled engine and return a db.
	Open(context.Context, *Information) (DB, error)
}

DatabaseHandler represents the boundary with a db.

type FlexibleTransaction added in v2.1.7

type FlexibleTransaction struct {
	DB
	// contains filtered or unexported fields
}

FlexibleTransaction allows for a DB transaction to be passed through functions and avoid multiple commit/rollbacks it also takes care of some of the most repeated checks at the time of commit/rollback and tx checking.

func (*FlexibleTransaction) BeginTransaction added in v2.1.7

func (f *FlexibleTransaction) BeginTransaction(ctx context.Context) (DB, error)

BeginTransaction implements DB for FlexibleTransaction

func (*FlexibleTransaction) Cleanup added in v2.1.7

func (f *FlexibleTransaction) Cleanup(ctx context.Context) (bool, bool, error)

Cleanup is an implementation of TXFinishFunc for FlexibleTransaction, it handles an attempt to either Commit or rollback a transaction depending on the perceived outcome: If someone invoked rollback on the FlexibleTransaction we assume the process went wrong and will rollback all. This is intended as a way to mitigate the lack of different abstractions for Transaction and Connection in the current version of gaum, retaining the ability to finalize the transaction at the initiator level. This does however allow some bad habits such as functions acting different depending on if they think they receive a transaction or a connection instead of having two functions that force the former or later as arguments.

func (*FlexibleTransaction) CommitTransaction added in v2.1.7

func (f *FlexibleTransaction) CommitTransaction(ctx context.Context) error

CommitTransaction implements DB for FlexibleTransaction

func (*FlexibleTransaction) RollbackTransaction added in v2.1.7

func (f *FlexibleTransaction) RollbackTransaction(ctx context.Context) error

RollbackTransaction implements DB for FlexibleTransaction

type Information

type Information struct {
	Database        string
	User            string
	Password        string
	ConnMaxLifetime *time.Duration

	CustomDial func(ctx context.Context, network, addr string) (net.Conn, error)

	// MaxConnPoolConns where applies will be used to determine the maximum amount of connections
	// a pool can have.
	MaxConnPoolConns int

	Logger   logging.Logger
	LogLevel LogLevel
}

Information contains all required information to create a connection into a db. Copied almost verbatim from https://godoc.org/github.com/jackc/pgx#ConnConfig

type LogLevel

type LogLevel string

LogLevel is the type for the potential log levels a db can have

var (
	// Trace sets log level to trace.
	Trace LogLevel = "trace"
	// Debug sets log level to debug.
	Debug LogLevel = "debug"
	// Info sets log level to info.
	Info LogLevel = "info"
	// Warn sets log level to warn.
	Warn LogLevel = "warn"
	// Error sets log level to error.
	Error LogLevel = "error"
	// None sets log level to none.
	None LogLevel = "none"
)

type ResultFetch

type ResultFetch func(interface{}) error

ResultFetch represents a closure that receives a receiver struct and wil assign all the results it is expected that it receives a slice.

type ResultFetchIter

type ResultFetchIter func(interface{}) (bool, func(), error)

ResultFetchIter represents a closure that receives a receiver struct that will get the results assigned for one row and returns a tuple of `next item present`, `close function`, error

type TXFinishFunc added in v2.1.7

type TXFinishFunc func(ctx context.Context) (committed, rolled bool, err error)

TXFinishFunc represents an all-encompassing function that either rolls or commits a tx based on the outcome.

Jump to

Keyboard shortcuts

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