postgres

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: MIT Imports: 16 Imported by: 2

README

go-common > postgres

Based on the pgx library, this package contains utilities for working with Postgres. For example, CRUD operations, utilities for mapping time and dates from Go to Postgres, and utilities for mapping database types to Go and common types.

Documentation

Index

Constants

View Source
const TimePrecision = time.Microsecond

Variables

View Source
var ErrJoinNotEnded = fmt.Errorf("%w: join started but not ended", ErrTemplate)
View Source
var ErrJoinNotStarted = fmt.Errorf("%w: join not started", ErrTemplate)
View Source
var ErrTemplate = errors.New("postgres template")
View Source
var ErrTemplateFuncNotAvail = fmt.Errorf("%w: function not available in this execution context", ErrTemplate)

Functions

func Count

func Count(
	ctx context.Context,
	querier Querier,
	sql string,
	args ...any,
) (int64, error)

func CountT

func CountT(
	ctx context.Context,
	querier Querier,
	templ *Template,
	data map[string]any,
) (int64, error)

func Exec

func Exec(
	ctx context.Context,
	querier Querier,
	sql string,
	args ...any,
) (int64, error)

func ExecT

func ExecT(
	ctx context.Context,
	querier Querier,
	templ *Template,
	data map[string]any,
) (int64, error)

func ListT

func ListT[T any](
	ctx context.Context,
	querier Querier,
	templ *Template,
	data map[string]any,
	pageIndex int64,
	pageSize int64,
	minimumPageSize int64,
	defaultPageSize int64,
	maximumPageSize int64,
) (*pagination.Result[*T], error)

func NilableArray added in v1.3.0

func NilableArray[T any](a pgtype.Array[T]) nilable.Slice[T]

func NilableBool added in v1.3.0

func NilableBool(b pgtype.Bool) nilable.Value[bool]

func NilableFlatArray added in v1.4.0

func NilableFlatArray[T any](a pgtype.FlatArray[T]) nilable.Slice[T]

func NilableFloat4 added in v1.3.0

func NilableFloat4[T ~float32](n pgtype.Float4) nilable.Value[T]

func NilableFloat8 added in v1.3.0

func NilableFloat8[T ~float64](n pgtype.Float8) nilable.Value[T]

func NilableIPAddr added in v1.3.0

func NilableIPAddr(ip netip.Addr) nilable.Value[netip.Addr]

func NilableInt2 added in v1.6.0

func NilableInt2[T ~int16](n pgtype.Int2) nilable.Value[T]

func NilableInt4 added in v1.3.0

func NilableInt4[T ~int | ~int32](n pgtype.Int4) nilable.Value[T]

func NilableInt8 added in v1.3.0

func NilableInt8[T ~int64](n pgtype.Int8) nilable.Value[T]

func NilableJSON added in v1.3.0

func NilableJSON(value map[string]any) nilable.Map[string, any]

func NilablePoint added in v1.3.0

func NilablePoint[T any](p pgtype.Point, fn func(pgtype.Point) T) nilable.Value[T]

func NilableText added in v1.3.0

func NilableText[T ~string](t pgtype.Text) nilable.Value[T]

func NilableTimestamp added in v1.3.0

func NilableTimestamp(t pgtype.Timestamp) nilable.Value[time.Time]

func NilableUUID added in v1.3.0

func NilableUUID[T ~string](u pgtype.UUID) nilable.Value[T]

func NormalizeError

func NormalizeError(err error) error

func ReadMany

func ReadMany[T any](
	ctx context.Context,
	querier Querier,
	sql string,
	args ...any,
) ([]*T, error)

func ReadManyT

func ReadManyT[T any](
	ctx context.Context,
	querier Querier,
	templ *Template,
	data map[string]any,
) ([]*T, error)

func ReadOne

func ReadOne[T any](
	ctx context.Context,
	querier Querier,
	sql string,
	args ...any,
) (*T, error)

func ReadOneT

func ReadOneT[T any](
	ctx context.Context,
	querier Querier,
	templ *Template,
	data map[string]any,
) (*T, error)

func RequiredArray

func RequiredArray[T any](a pgtype.Array[T]) []T

func RequiredBool

func RequiredBool(b pgtype.Bool) bool

func RequiredFlatArray added in v1.4.0

func RequiredFlatArray[T any](a pgtype.FlatArray[T]) []T

func RequiredFloat4 added in v1.3.0

func RequiredFloat4[T ~float32](n pgtype.Float4) T

func RequiredFloat8 added in v1.3.0

func RequiredFloat8[T ~float64](n pgtype.Float8) T

func RequiredIPAddr

func RequiredIPAddr(ip netip.Addr) netip.Addr

func RequiredInt2 added in v1.6.0

func RequiredInt2[T ~int16](n pgtype.Int2) T

func RequiredInt4 added in v1.3.0

func RequiredInt4[T ~int | ~int32](n pgtype.Int4) T

func RequiredInt8 added in v1.3.0

func RequiredInt8[T ~int64](n pgtype.Int8) T

func RequiredJSON

func RequiredJSON(value map[string]any) map[string]any

func RequiredPoint added in v1.3.0

func RequiredPoint[T any](p pgtype.Point, fn func(pgtype.Point) T) T

func RequiredText

func RequiredText[T ~string](t pgtype.Text) T

func RequiredTimestamp

func RequiredTimestamp(t pgtype.Timestamp) time.Time

func RequiredUUID

func RequiredUUID[T ~string](u pgtype.UUID) T

func Time

func Time(t time.Time) time.Time

Time sets the location to UTC and truncates to the highest precision that Postgres supports.

func ToVec2 added in v1.3.0

func ToVec2(p pgtype.Point) pgtype.Vec2

Types

type Querier

type Querier interface {
	Begin(ctx context.Context) (pgx.Tx, error)
	Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

type Template

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

func MustParse

func MustParse(text string) *Template

func Parse

func Parse(text string) (*Template, error)

func (*Template) Execute

func (t *Template) Execute(data interface{}) (string, []any, error)

func (*Template) ExecuteCount

func (t *Template) ExecuteCount(data interface{}) (string, []any, error)

func (*Template) ExecuteList

func (t *Template) ExecuteList(data interface{}, firstItemIndex int64, pageSize int64) (string, []any, error)

Jump to

Keyboard shortcuts

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