db

package
v0.21.5 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const DriverMysql = "mysql"
View Source
const DriverNameCrateDb = "cratedb"
View Source
const DriverNameRedshift = "redshift"
View Source
const (
	FormatDateTime = "2006-01-02 15:04:05"
)

Variables

View Source
var (
	ErrJSONInvalidType   = errors.New("incoming data type is not []byte for json")
	NullableBehaviour    = Nullable{}
	NonNullableBehaviour = NonNullable{}
)

Functions

func AddMigrationProvider added in v0.18.0

func AddMigrationProvider(name string, provider MigrationProvider)

func CheckBadConnection added in v0.20.0

func CheckBadConnection(result interface{}, err error) exec.ErrorType

func CheckInvalidConnection added in v0.20.0

func CheckInvalidConnection(result interface{}, err error) exec.ErrorType

func CheckIoTimeout added in v0.20.0

func CheckIoTimeout(result interface{}, err error) exec.ErrorType

func IsDuplicateEntryError

func IsDuplicateEntryError(err error) bool

func NewConnection added in v0.20.0

func NewConnection(config cfg.Config, logger log.Logger, name string) (*sqlx.DB, error)

func NewConnectionFromSettings

func NewConnectionFromSettings(logger log.Logger, settings *Settings) (*sqlx.DB, error)

func NewConnectionWithInterfaces

func NewConnectionWithInterfaces(logger log.Logger, settings *Settings) (*sqlx.DB, error)

func NewExecutor added in v0.20.0

func NewExecutor(config cfg.Config, logger log.Logger, name string, backoffType string, notifier ...exec.Notify) exec.Executor

func ProvideConnection

func ProvideConnection(ctx context.Context, config cfg.Config, logger log.Logger, name string) (*sqlx.DB, error)

Types

type Client

type Client interface {
	GetSingleScalarValue(ctx context.Context, query string, args ...interface{}) (int, error)
	GetResult(ctx context.Context, query string, args ...interface{}) (*Result, error)
	Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	NamedExec(ctx context.Context, query string, arg interface{}) (sql.Result, error)
	ExecMultiInTx(ctx context.Context, sqlers ...Sqler) (results []sql.Result, err error)
	BindNamed(query string, arg interface{}) (string, []interface{}, error)
	Prepare(ctx context.Context, query string) (*sql.Stmt, error)
	Preparex(ctx context.Context, query string) (*sqlx.Stmt, error)
	PrepareNamed(ctx context.Context, query string) (*sqlx.NamedStmt, error)
	Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	Queryx(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
	QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
	NamedQuery(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
	Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	NamedSelect(ctx context.Context, dest interface{}, query string, arg interface{}) error
	Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	WithTx(ctx context.Context, ops *sql.TxOptions, do func(ctx context.Context, tx *sql.Tx) error) error
}

func NewClient

func NewClient(config cfg.Config, logger log.Logger, name string) (Client, error)

func NewClientWithInterfaces

func NewClientWithInterfaces(logger log.Logger, connection *sqlx.DB, executor exec.Executor) Client

func NewClientWithSettings

func NewClientWithSettings(config cfg.Config, logger log.Logger, name string, settings *Settings) (Client, error)

func ProvideClient added in v0.20.0

func ProvideClient(ctx context.Context, config cfg.Config, logger log.Logger, name string) (Client, error)

type ClientSqlx

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

func (*ClientSqlx) BeginTx

func (c *ClientSqlx) BeginTx(ctx context.Context, ops *sql.TxOptions) (*sql.Tx, error)

func (*ClientSqlx) BindNamed added in v0.20.1

func (c *ClientSqlx) BindNamed(query string, arg interface{}) (string, []interface{}, error)

func (*ClientSqlx) Exec

func (c *ClientSqlx) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*ClientSqlx) ExecMultiInTx added in v0.19.4

func (c *ClientSqlx) ExecMultiInTx(ctx context.Context, sqlers ...Sqler) (results []sql.Result, err error)

func (*ClientSqlx) Get

func (c *ClientSqlx) Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*ClientSqlx) GetResult

func (c *ClientSqlx) GetResult(ctx context.Context, query string, args ...interface{}) (*Result, error)

func (*ClientSqlx) GetSingleScalarValue

func (c *ClientSqlx) GetSingleScalarValue(ctx context.Context, query string, args ...interface{}) (int, error)

func (*ClientSqlx) NamedExec added in v0.20.1

func (c *ClientSqlx) NamedExec(ctx context.Context, query string, arg interface{}) (sql.Result, error)

func (*ClientSqlx) NamedQuery added in v0.20.1

func (c *ClientSqlx) NamedQuery(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)

func (*ClientSqlx) NamedSelect added in v0.20.1

func (c *ClientSqlx) NamedSelect(ctx context.Context, dest interface{}, query string, arg interface{}) error

func (*ClientSqlx) Prepare

func (c *ClientSqlx) Prepare(ctx context.Context, query string) (*sql.Stmt, error)

func (*ClientSqlx) PrepareNamed added in v0.20.1

func (c *ClientSqlx) PrepareNamed(ctx context.Context, query string) (*sqlx.NamedStmt, error)

func (*ClientSqlx) Preparex added in v0.20.0

func (c *ClientSqlx) Preparex(ctx context.Context, query string) (*sqlx.Stmt, error)

func (*ClientSqlx) Query

func (c *ClientSqlx) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*ClientSqlx) QueryRow

func (c *ClientSqlx) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

func (*ClientSqlx) Queryx

func (c *ClientSqlx) Queryx(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

func (*ClientSqlx) Select

func (c *ClientSqlx) Select(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*ClientSqlx) WithTx

func (c *ClientSqlx) WithTx(ctx context.Context, ops *sql.TxOptions, do func(ctx context.Context, tx *sql.Tx) error) (err error)

type Driver added in v0.19.4

type Driver interface {
	GetDSN(settings *Settings) string
	GetMigrationDriver(db *sql.DB, database string, migrationsTable string) (database.Driver, error)
}

func GetDriver added in v0.19.4

func GetDriver(logger log.Logger, driverName string) (Driver, error)

func NewCrateDbDriver added in v0.19.4

func NewCrateDbDriver(logger log.Logger) (Driver, error)

func NewMysqlDriver added in v0.19.4

func NewMysqlDriver(logger log.Logger) (Driver, error)

func NewRedshiftDriver added in v0.19.4

func NewRedshiftDriver(logger log.Logger) (Driver, error)

type DriverFactory

type DriverFactory func(logger log.Logger) (Driver, error)

type DuplicateEntryError

type DuplicateEntryError struct {
	Err error
}

func (*DuplicateEntryError) As

func (e *DuplicateEntryError) As(target interface{}) bool

func (*DuplicateEntryError) Error

func (e *DuplicateEntryError) Error() string

func (*DuplicateEntryError) Is

func (e *DuplicateEntryError) Is(err error) bool

func (*DuplicateEntryError) Unwrap

func (e *DuplicateEntryError) Unwrap() error

type IsNullable added in v0.21.1

type IsNullable interface {
	IsNullable() bool
}

type JSON added in v0.21.1

type JSON[T any, NullBehaviour Nullable | NonNullable] struct {
	// contains filtered or unexported fields
}

JSON is a type wrapping other types for easy use of database json columns. It is intended to be used without a pointer but having pointers to T.

func NewJSON added in v0.21.1

func NewJSON[T any, NullBehaviour Nullable | NonNullable](val T, _ NullBehaviour) JSON[T, NullBehaviour]

func (JSON[T, NullBehaviour]) Get added in v0.21.1

func (t JSON[T, NullBehaviour]) Get() T

func (*JSON[T, NullBehaviour]) Scan added in v0.21.1

func (t *JSON[T, NullBehaviour]) Scan(src any) error

func (JSON[T, NullBehaviour]) Value added in v0.21.1

func (t JSON[T, NullBehaviour]) Value() (driver.Value, error)

type MigrationProvider added in v0.18.0

type MigrationProvider func(logger log.Logger, settings *Settings, db *sql.DB) error

type MigrationSettings

type MigrationSettings struct {
	Application    string `cfg:"application" default:"{app_name}"`
	Enabled        bool   `cfg:"enabled" default:"false"`
	Path           string `cfg:"path"`
	PrefixedTables bool   `cfg:"prefixed_tables" default:"false"`
	Provider       string `cfg:"provider" default:"goose"`
}

type NonNullable added in v0.21.1

type NonNullable struct{}

func (NonNullable) IsNullable added in v0.21.1

func (n NonNullable) IsNullable() bool

type Nullable added in v0.21.1

type Nullable struct{}

func (Nullable) IsNullable added in v0.21.1

func (n Nullable) IsNullable() bool

type QueryBuilder

type QueryBuilder interface {
	Table(table string) QueryBuilder
	Joins(joins []string) QueryBuilder
	Where(query interface{}, args ...interface{}) QueryBuilder
	GroupBy(field ...string) QueryBuilder
	OrderBy(field string, direction string) QueryBuilder
	Page(offset int, size int) QueryBuilder
}

type RawQueryBuilder

type RawQueryBuilder struct {
	Builder squirrel.SelectBuilder
}

func NewQueryBuilder

func NewQueryBuilder() *RawQueryBuilder

func (*RawQueryBuilder) GroupBy

func (b *RawQueryBuilder) GroupBy(field ...string) QueryBuilder

func (*RawQueryBuilder) Joins

func (b *RawQueryBuilder) Joins(joins []string) QueryBuilder

func (*RawQueryBuilder) OrderBy

func (b *RawQueryBuilder) OrderBy(field string, direction string) QueryBuilder

func (*RawQueryBuilder) Page

func (b *RawQueryBuilder) Page(offset int, size int) QueryBuilder

func (*RawQueryBuilder) Table

func (b *RawQueryBuilder) Table(table string) QueryBuilder

func (*RawQueryBuilder) Where

func (b *RawQueryBuilder) Where(query interface{}, args ...interface{}) QueryBuilder

type Result

type Result []ResultRow

type ResultRow

type ResultRow map[string]string

type Settings

type Settings struct {
	Charset               string            `cfg:"charset" default:"utf8mb4"`
	Collation             string            `cfg:"collation" default:"utf8mb4_general_ci"`
	ConnectionMaxIdleTime time.Duration     `cfg:"connection_max_idletime" default:"120s"`
	ConnectionMaxLifetime time.Duration     `cfg:"connection_max_lifetime" default:"120s"`
	Driver                string            `cfg:"driver"`
	MaxIdleConnections    int               `cfg:"max_idle_connections" default:"2"` // 0 or negative number=no idle connections, sql driver default=2
	MaxOpenConnections    int               `cfg:"max_open_connections" default:"0"` // 0 or negative number=unlimited, sql driver default=0
	Migrations            MigrationSettings `cfg:"migrations"`
	MultiStatements       bool              `cfg:"multi_statements" default:"true"`
	Parameters            map[string]string `cfg:"parameters"`
	ParseTime             bool              `cfg:"parse_time" default:"true"`
	Retry                 SettingsRetry     `cfg:"retry"`
	Timeouts              SettingsTimeout   `cfg:"timeouts"`
	Uri                   Uri               `cfg:"uri"`
}

type SettingsRetry added in v0.20.0

type SettingsRetry struct {
	Enabled bool `cfg:"enabled" default:"false"`
}

type SettingsTimeout added in v0.19.4

type SettingsTimeout struct {
	ReadTimeout  time.Duration `cfg:"readTimeout" default:"0"`  // I/O read timeout. The value must be a decimal number with a unit suffix ("ms", "s", "m", "h"), such as "30s", "0.5m" or "1m30s".
	WriteTimeout time.Duration `cfg:"writeTimeout" default:"0"` // I/O write timeout. The value must be a decimal number with a unit suffix ("ms", "s", "m", "h"), such as "30s", "0.5m" or "1m30s".
	Timeout      time.Duration `cfg:"timeout" default:"0"`      // Timeout for establishing connections, aka dial timeout. The value must be a decimal number with a unit suffix ("ms", "s", "m", "h"), such as "30s", "0.5m" or "1m30s".
}

type SqlResult

type SqlResult interface {
	LastInsertId() (int64, error)
	RowsAffected() (int64, error)
}

type Sqler added in v0.19.4

type Sqler interface {
	ToSql() (string, []interface{}, error)
}

func SqlFmt added in v0.19.4

func SqlFmt(format string, a ...any) Sqler

type Uri

type Uri struct {
	Host     string `cfg:"host" default:"localhost" validation:"required"`
	Port     int    `cfg:"port" default:"3306" validation:"required"`
	User     string `cfg:"user" validation:"required"`
	Password string `cfg:"password" validation:"required"`
	Database string `cfg:"database" validation:"required"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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