pg

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package pg contains implementations of hohin interfaces for PostgreSQL. It uses the pgx driver.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSQL

func NewSQL(strs ...string) *sqldb.SQL

NewSQL creates a new SQL builder for PostgreSQL.

Types

type Conf

type Conf[T any] struct {
	Table   string            // name of a database table
	Mapping map[string]string // mapping of entity fields to table columns
	Query   string            // SQL query to select records from the database
	// function that transforms an entity to a map where keys are
	// column names of a database table and values are data for a row in that table
	Dump func(T) (map[string]any, error)
	// function that loads a result of an SQL query to an entity
	Load func(Scanner) (T, error)
	// function that builds and returns a sequence of SQL queries to execute after a call of [Repo.Add]
	AfterAdd func(T) []*sqldb.SQL
	// function that builds and returns a sequence of SQL queries to execute after a call of [Repo.Update]
	AfterUpdate func(T) []*sqldb.SQL
}

Conf contains configuration of a Repo.

type DB

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

DB implements hohin.DB for PostgreSQL.

func NewDB

func NewDB(pool *pgxpool.Pool) *DB

NewDB creates a DB.

func (*DB) Simple

func (db *DB) Simple() hohin.SimpleDB

func (*DB) Transaction

func (db *DB) Transaction(ctx context.Context, f func(context.Context, hohin.DB) error) error

func (*DB) Tx

func (db *DB) Tx(ctx context.Context, level hohin.IsolationLevel, f func(context.Context, hohin.DB) error) error

type Repo

type Repo[T any] struct {
	// contains filtered or unexported fields
}

Implementation of hohin.Repo for PostgreSQL.

func NewRepo

func NewRepo[T any](conf Conf[T]) *Repo[T]

NewRepo creates a Repo.

func (*Repo[T]) Add

func (r *Repo[T]) Add(ctx context.Context, d hohin.DB, entity T) error

func (*Repo[T]) AddMany

func (r *Repo[T]) AddMany(ctx context.Context, d hohin.DB, entities []T) error

func (*Repo[T]) Clear

func (r *Repo[T]) Clear(ctx context.Context, d hohin.DB) error

func (Repo[T]) Count

func (r Repo[T]) Count(ctx context.Context, d hohin.DB, f hohin.Filter) (uint64, error)

func (*Repo[T]) CountAll

func (r *Repo[T]) CountAll(ctx context.Context, d hohin.DB) (uint64, error)

func (*Repo[T]) Delete

func (r *Repo[T]) Delete(ctx context.Context, d hohin.DB, f hohin.Filter) error

func (*Repo[T]) Exists

func (r *Repo[T]) Exists(ctx context.Context, d hohin.DB, f hohin.Filter) (bool, error)

func (*Repo[T]) Get

func (r *Repo[T]) Get(ctx context.Context, d hohin.DB, f hohin.Filter) (T, error)

func (*Repo[T]) GetFirst

func (r *Repo[T]) GetFirst(ctx context.Context, d hohin.DB, q hohin.Query) (T, error)

func (*Repo[T]) GetForUpdate

func (r *Repo[T]) GetForUpdate(ctx context.Context, d hohin.DB, f hohin.Filter) (T, error)

func (*Repo[T]) GetMany

func (r *Repo[T]) GetMany(ctx context.Context, d hohin.DB, q hohin.Query) ([]T, error)

func (*Repo[T]) Simple

func (r *Repo[T]) Simple() hohin.SimpleRepo[T]

func (*Repo[T]) Update

func (r *Repo[T]) Update(ctx context.Context, d hohin.DB, f hohin.Filter, entity T) error

type Scanner

type Scanner interface {
	Scan(dest ...any) error
}

Scanner allows to fetch data from a result of an SQL query.

Jump to

Keyboard shortcuts

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