sql

package
v0.0.0-...-e63d53d Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: Apache-2.0 Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const (
	LevelDefault         = sql.LevelDefault
	LevelReadUncommitted = sql.LevelReadUncommitted
	LevelReadCommitted   = sql.LevelReadCommitted
	LevelWriteCommitted  = sql.LevelWriteCommitted
	LevelRepeatableRead  = sql.LevelRepeatableRead
	LevelSnapshot        = sql.LevelSnapshot
	LevelSerializable    = sql.LevelSerializable
	LevelLinearizable    = sql.LevelLinearizable
)
View Source
const DatabaseComponentName = "database"

Variables

View Source
var (
	ErrDatabaseIDRequired = fmt.Errorf("DatabaseID is required")
	ErrPortIsRequired     = fmt.Errorf("Port is required")
	ErrHostIsRequired     = fmt.Errorf("Host is required")
	ErrUserIsRequired     = fmt.Errorf("User is required")
	ErrDatabaseIsRequired = fmt.Errorf("Database is required")
)
View Source
var (
	ErrNoRows = sql.ErrNoRows
)
View Source
var ErrSkipValue = errors.New("skip value")
View Source
var ErrTxDone = sql.ErrTxDone

Functions

func FetchDictionary

func FetchDictionary[T comparable, V any](
	fetcher Fetcher,
) (res map[T]V, err error)

FetchDictionary - returns a dictionary from fetcher

func FetchMap

func FetchMap[K comparable](fetcher Fetcher) (map[K]any, error)

FetchMap - returns a map from fetcher

func FetchMaps

func FetchMaps[K comparable](fetcher Fetcher) ([]map[K]any, error)

FetchMaps - returns a slice of maps from fetcher

func FetchModel

func FetchModel[T any](
	fetcher Fetcher,
	reader func(scanner Scanner) (T, error),
) (model T, err error)

FetchModel - returns a single model from fetcher

func FetchModelMap

func FetchModelMap[K comparable, T any](
	fetcher Fetcher,
	reader func(scanner Scanner) (K, T, error),
) (models map[K]T, err error)

FetchModelMap - returns multiple models from fetcher as a map

func FetchModels

func FetchModels[T any](
	fetcher Fetcher,
	reader func(scanner Scanner) (T, error),
) (models []T, err error)

FetchModels - returns multiple models from fetcher

func FreeContext

func FreeContext(ctx context.Context, key DbId) context.Context

FreeContext - Free context from scope

func MakeLiteral

func MakeLiteral(v any) string

func NewContextWithoutCancel

func NewContextWithoutCancel(ctx context.Context) context.Context

func NormalizeSQL

func NormalizeSQL(sql string) string

NormalizeSQL строит нормализованный шаблон SQL: литералы → "?", параметры → "?", числа → "?", схлопывание IN/ANY/VALUES, пробелы и регистр.

func Register

func Register(name string, driver driver.Driver)

func SQLFingerprint

func SQLFingerprint(sql string) string

SQLFingerprint возвращает sha256 hex отпечаток нормализованного SQL.

func SafeTemplate

func SafeTemplate(tmpl string, max int) string

func ToContext

func ToContext(
	ctx context.Context,
	scope Scope,
) context.Context

ToContext - Append scope into context

Types

type Act

type Act = func(ctx context.Context) error

type Action

type Action func(ctx context.Context, tx Tx) error

type BeginAction

type BeginAction func(ctx context.Context, opts *TxOptions) (Tx, error)

type Behavior

type Behavior interface {
	Apply(
		ctx context.Context,
		action func(ctx context.Context) error,
		query string,
		args ...interface{},
	) error
}

type Builder

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

func NewBuilder

func NewBuilder() *Builder

func (*Builder) Build

func (that *Builder) Build() (DB, error)

func (*Builder) WithBeforeAcquire

func (that *Builder) WithBeforeAcquire(fn func(ctx context.Context, conn *pgx.Conn) bool) *Builder

func (*Builder) WithConnMaxIdleTime

func (that *Builder) WithConnMaxIdleTime(d time.Duration) *Builder

func (*Builder) WithConnMaxLifetime

func (that *Builder) WithConnMaxLifetime(d time.Duration) *Builder

func (*Builder) WithDSN

func (that *Builder) WithDSN(dsn string) *Builder

func (*Builder) WithDatabase

func (that *Builder) WithDatabase(database string) *Builder

func (*Builder) WithDatabaseID

func (that *Builder) WithDatabaseID(id DbId) *Builder

func (*Builder) WithErrorBuilder

func (that *Builder) WithErrorBuilder(errors ErrorBuilder) *Builder

func (*Builder) WithHost

func (that *Builder) WithHost(host string) *Builder

func (*Builder) WithMaxIdleConns

func (that *Builder) WithMaxIdleConns(max int) *Builder

func (*Builder) WithMaxOpenConns

func (that *Builder) WithMaxOpenConns(max int) *Builder

func (*Builder) WithPassword

func (that *Builder) WithPassword(password string) *Builder

func (*Builder) WithPort

func (that *Builder) WithPort(port uint16) *Builder

func (*Builder) WithQueryTracer

func (that *Builder) WithQueryTracer(tracer pgx.QueryTracer) *Builder

func (*Builder) WithUser

func (that *Builder) WithUser(user string) *Builder

type ColumnType

type ColumnType struct {
	Name string
	OID  uint32
}

type CommitAction

type CommitAction func(ctx context.Context) error

type ContextWithoutCancel

type ContextWithoutCancel struct {
	context.Context
}

func (*ContextWithoutCancel) Done

func (that *ContextWithoutCancel) Done() <-chan struct{}

type CustomHandler

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

func NewCustomHandler

func NewCustomHandler(behavior Behavior, next Handler) *CustomHandler

func (*CustomHandler) BeginTx

func (that *CustomHandler) BeginTx(ctx context.Context, opts *TxOptions, action BeginAction) (tx Tx, err error)

func (*CustomHandler) Commit

func (that *CustomHandler) Commit(ctx context.Context, action CommitAction) error

func (*CustomHandler) Exec

func (that *CustomHandler) Exec(ctx context.Context, action ExecAction, query string, args ...interface{}) (res Result, err error)

func (*CustomHandler) Query

func (that *CustomHandler) Query(ctx context.Context, action QueryAction, query string, args ...interface{}) (rows Rows, err error)

func (*CustomHandler) QueryRow

func (that *CustomHandler) QueryRow(ctx context.Context, action QueryRowAction, query string, args ...interface{}) (row Row)

func (*CustomHandler) Rollback

func (that *CustomHandler) Rollback(ctx context.Context, action RollbackAction) error

type DB

type DB interface {
	Scope
	Pool() *pgxpool.Pool
	Scope(ctx context.Context) Scope
	Transact(ctx context.Context, action Act) error
	Transaction(ctx context.Context, action Action) error
	TransactionTx(ctx context.Context, action Action, options *TxOptions) error
	InTransaction(ctx context.Context) bool
	WithCancel(ctx context.Context) Scope
	Close()
}

type DBStats

type DBStats = sql.DBStats

type DSN

type DSN struct {
	Host     string
	Port     uint16
	User     string
	Password string
	Database string
	SSLMode  string
	Charset  string
}

func DefaultDSN

func DefaultDSN() DSN

func (*DSN) String

func (that *DSN) String() string

type DbId

type DbId string

type DummyBehavior

type DummyBehavior struct{}

func (*DummyBehavior) Apply

func (that *DummyBehavior) Apply(
	ctx context.Context,
	action func(ctx context.Context) error,
	query string,
	args ...interface{},
) error

type DummyHandler

type DummyHandler struct{}

func NewDummyHandler

func NewDummyHandler() *DummyHandler

func (*DummyHandler) BeginTx

func (that *DummyHandler) BeginTx(ctx context.Context, opts *TxOptions, action BeginAction) (Tx, error)

func (*DummyHandler) Commit

func (that *DummyHandler) Commit(ctx context.Context, action CommitAction) error

func (*DummyHandler) Exec

func (that *DummyHandler) Exec(ctx context.Context, action ExecAction, query string, args ...interface{}) (Result, error)

func (*DummyHandler) Query

func (that *DummyHandler) Query(ctx context.Context, action QueryAction, query string, args ...interface{}) (Rows, error)

func (*DummyHandler) QueryRow

func (that *DummyHandler) QueryRow(ctx context.Context, action QueryRowAction, query string, args ...interface{}) Row

func (*DummyHandler) Rollback

func (that *DummyHandler) Rollback(ctx context.Context, action RollbackAction) error

type ErrorBuilder

type ErrorBuilder interface {
	Build(ctx context.Context, err error, msg string) error
}

type ExecAction

type ExecAction func(ctx context.Context, query string, args ...interface{}) (Result, error)

type Fetcher

type Fetcher func(func(rows Rows) error) error

type Handler

type Handler interface {
	Query(ctx context.Context, action QueryAction, query string, args ...interface{}) (Rows, error)
	QueryRow(ctx context.Context, action QueryRowAction, query string, args ...interface{}) Row
	Exec(ctx context.Context, action ExecAction, query string, args ...interface{}) (Result, error)
	BeginTx(ctx context.Context, opts *TxOptions, action BeginAction) (Tx, error)
	Commit(ctx context.Context, action CommitAction) error
	Rollback(ctx context.Context, action RollbackAction) error
}

type IsolationLevel

type IsolationLevel = sql.IsolationLevel

type LogOptions

type LogOptions struct {
	MaxTemplateLen     int           // например, 500
	MaxAllowedDuration time.Duration // например, 200ms
}

type Manipulator

type Manipulator interface {
	Exec(ctx context.Context, args ...interface{}) (Result, error)
	Query(ctx context.Context, args ...interface{}) (Rows, error)
	QueryRow(ctx context.Context, args ...interface{}) Row
}

type Middleware

type Middleware func(handler Handler) Handler

type NullBool

type NullBool = sql.NullBool

type NullFloat64

type NullFloat64 = sql.NullFloat64

type NullInt64

type NullInt64 = sql.NullInt64

type NullString

type NullString = sql.NullString

type NullTime

type NullTime = sql.NullTime

type Out

type Out = sql.Out

type QueryAction

type QueryAction func(ctx context.Context, query string, args ...interface{}) (Rows, error)

type QueryRowAction

type QueryRowAction func(ctx context.Context, query string, args ...interface{}) Row

type RawBytes

type RawBytes = sql.RawBytes

type Result

type Result interface {
	RowsAffected() (int64, error)
}

type RollbackAction

type RollbackAction func(ctx context.Context) error

type Row

type Row interface {
	Scanner
	Err() error
}

type Rows

type Rows interface {
	Scanner
	Err() error
	Next() bool
	Columns() ([]string, error)
	ColumnTypes() ([]*ColumnType, error)
	Close() error
}

type Scanner

type Scanner interface {
	Scan(src ...interface{}) error
}

type Scope

type Scope interface {
	DbId(ctx context.Context) DbId
	WithCancel(ctx context.Context) Scope
	Exec(ctx context.Context, query string, args ...interface{}) (Result, error)
	Query(ctx context.Context, query string, args ...interface{}) (Rows, error)
	QueryRow(ctx context.Context, query string, args ...interface{}) Row
	Fetch(ctx context.Context, query string, args ...any) Fetcher
	Begin(ctx context.Context) (Tx, error)
	BeginTx(ctx context.Context, opts *TxOptions) (Tx, error)
}

func FromContext

func FromContext(ctx context.Context, key DbId) Scope

FromContext - Extract scope from context

type Tracer

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

func NewLogger

func NewLogger(logger log.Logger, options LogOptions) *Tracer

func (*Tracer) TraceQueryEnd

func (that *Tracer) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData)

func (*Tracer) TraceQueryStart

func (that *Tracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context

type Tx

type Tx interface {
	Scope
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type TxOptions

type TxOptions = pgx.TxOptions

Jump to

Keyboard shortcuts

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