sqlcon

package
v0.0.168 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2020 License: Apache-2.0 Imports: 25 Imported by: 88

Documentation

Overview

Package sqlcon provides helpers for dealing with SQL connectivity.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUniqueViolation is returned when^a SQL INSERT / UPDATE command returns a conflict.
	ErrUniqueViolation = &herodot.DefaultError{
		CodeField:   http.StatusConflict,
		StatusField: http.StatusText(http.StatusConflict),
		ErrorField:  "Unable to insert or update resource because a resource with that value exists already",
	}
	// ErrNoRows is returned when a SQL SELECT statement returns no rows.
	ErrNoRows = &herodot.DefaultError{
		CodeField:   http.StatusNotFound,
		StatusField: http.StatusText(http.StatusNotFound),
		ErrorField:  "Unable to locate the resource",
	}
	// ErrConcurrentUpdate is returned when the database is unable to serialize access due to a concurrent update.
	ErrConcurrentUpdate = &herodot.DefaultError{
		CodeField:   http.StatusBadRequest,
		StatusField: http.StatusText(http.StatusBadRequest),
		ErrorField:  "Unable to serialize access due to a concurrent update in another session",
	}
)

Functions

func FinalizeDSN added in v0.0.116

func FinalizeDSN(l *logrusx.Logger, dsn string) string

FinalizeDSN will return a finalized DSN URI.

func GetDriverName added in v0.0.73

func GetDriverName(dsn string) string

GetDriverName returns the driver name of a given DSN.

func HandleError

func HandleError(err error) error

HandleError returns the right sqlcon.Err* depending on the input error.

func HelpMessage

func HelpMessage() string

HelpMessage returns a string explaining how to set up SQL using environment variables.

func MigratorSQLCmd

func MigratorSQLCmd(path, name string, logger *logrusx.Logger, runners map[string]SchemaCreator) *cobra.Command

MigratorSQLCmd returns a *cobra.Command executing SQL schema migrations.

func ParseConnectionOptions added in v0.0.84

func ParseConnectionOptions(l *logrusx.Logger, dsn string) (maxConns int, maxIdleConns int, maxConnLifetime time.Duration, cleanedDSN string)

ParseConnectionOptions parses values for max_conns, max_idle_conns, max_conn_lifetime from DSNs. It also returns the URI without those query parameters.

Types

type OptionModifier added in v0.0.39

type OptionModifier func(*options)

OptionModifier is a wrapper for options.

func WithAllowRoot added in v0.0.14

func WithAllowRoot() OptionModifier

WithAllowRoot will make it so that root spans will be created if a trace could not be found using opentracing's SpanFromContext method.

func WithDistributedTracing added in v0.0.14

func WithDistributedTracing() OptionModifier

WithDistributedTracing will make it so that a wrapped driver is used that supports the opentracing API.

func WithOmitArgsFromTraceSpans added in v0.0.14

func WithOmitArgsFromTraceSpans() OptionModifier

WithOmitArgsFromTraceSpans will make it so that query arguments are omitted from tracing spans.

func WithRandomDriverName added in v0.0.20

func WithRandomDriverName() OptionModifier

WithRandomDriverName is specifically for writing tests as you can't register a driver with the same name more than once.

type SQLConnection

type SQLConnection struct {
	DSN string
	L   *logrusx.Logger
	// contains filtered or unexported fields
}

SQLConnection represents a connection to a SQL database.

func NewSQLConnection

func NewSQLConnection(dsn string, l *logrusx.Logger, opts ...OptionModifier) (*SQLConnection, error)

NewSQLConnection returns a new SQLConnection.

func (*SQLConnection) GetDatabase

func (c *SQLConnection) GetDatabase() (*sqlx.DB, error)

GetDatabase returns a database instance.

func (*SQLConnection) GetDatabaseRetry

func (c *SQLConnection) GetDatabaseRetry(maxWait time.Duration, failAfter time.Duration) (*sqlx.DB, error)

GetDatabaseRetry tries to connect to a database and fails after failAfter.

type SchemaCreator

type SchemaCreator interface {
	// CreateSchemas migrates or creates one or more SQL schemas.
	CreateSchemas(db *sqlx.DB) (int, error)
}

SchemaCreator is an interface that allows SQL schemas to be created and migrated.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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