postgres

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const Value = "$#"

Variables

View Source
var DefaultQueryOptions = QueryOptions{
	IgnoreReturn: false,
}

Functions

func New

func New(c *Config) (*sqlx.DB, error)

New returns a new database client

func WrapError

func WrapError(err error) error

Types

type Config

type Config struct {
	Username            string        `conf:"username" default:"postgres"`
	Password            string        `conf:"password" default:"password"`
	Host                string        `conf:"host" default:"postgres"`
	Port                string        `conf:"port" default:"5432"`
	Database            string        `conf:"database" default:"postgres"`
	Schema              string        `conf:"schema" default:"public"`
	AutoCreate          bool          `conf:"auto_create" default:"false"`
	SearchPath          string        `conf:"search_path" default:""`
	SSLMode             string        `conf:"sslmode" default:"disable"`
	SSLCert             string        `conf:"sslcert" default:""`
	SSLKey              string        `conf:"sslkey" default:""`
	SSLRootCert         string        `conf:"sslrootcert" default:""`
	Retries             int           `conf:"retries" default:"5"`
	SleepBetweenRetries time.Duration `conf:"sleep_between_retries" default:"7s"`
	MaxConnections      int           `conf:"max_connections" default:"40"`
	WipeConfirm         bool          `conf:"wipe_confirm" default:"false"`

	Logger          Logger
	QueryLogger     Logger
	MigrationSource source.Driver
}

type DB

type DB interface {
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

type Field

type Field[T any] struct {
	// The field name
	Name string
	// Is this field part of the record ID (a primary key). You can have
	// multiple ID fields on a record.
	ID bool
	// Should this field be used on a select statement. (used for auto
	// generating select statements.)
	Select bool
	// The value to use when inserting this field into the database. If you want
	// to use a positional argument, use the `Value` constant.
	Insert string
	// The value to use when updating this field in the database. If you want
	// to use a positional argument, use the `Value` constant.
	Update string
	// This function is used to fetch the value for insert or update from a record.
	Value func(*T) (driver.Value, error)
	// This is used to determine the value that should be returned if the
	// value is being returned in a COALESCED way. For example, if you left join this
	// table and there is no value, this would be the value returned if you use the
	// GenerateAdditionalFields(coalesce=true) to generate the AdditionalFields
	// string
	NullVal any
}

Field is the field representation for each field in the table.

type Logger

type Logger interface {
	Printf(template string, args ...interface{})
}

The Logger Inferface for the database driver.

type QueryOption

type QueryOption func(opt *QueryOptions) error

func QueryOptionIgnoreReturn

func QueryOptionIgnoreReturn(v bool) QueryOption

type QueryOptions

type QueryOptions struct {
	// Ignore return will cause the query to ignore the return value from a
	// Insert, Upsert or Update query. Normally these queries will return/update
	// in place the new value it is returning.
	IgnoreReturn bool
}

type Scanner

type Scanner[T any] interface {
	Scan() *T
}

type Selector

type Selector[T any] struct {
	// The query to use
	Query string
	// OmitCount will not run the count query
	OmitCount bool
	// CountQuery allows you to override the query used to obtain counts with the same filter parameters.
	// If not specified it will wrap the Query provided in select count(*)
	CountQuery string

	// The fields you want to allow filtering on and the types
	FilterFieldTypes queryp.FilterFieldTypes
	// The fields you want to allow sorting on
	SortFields queryp.SortFields
	// If no sort is provided in the QueryParameters, the default sort to use.
	DefaultSort queryp.Sort

	// A callback to be used on every record to provide any transformations.
	PostProcessRecord func(*T) error
	// A callback to be used on the slice of records being returned before they are returned.
	PostProcessRecords func([]*T) error
}

Selector is a tool for fetching slices of records based on any query.

func (*Selector[T]) Select

func (s *Selector[T]) Select(ctx context.Context, db DB, qp *queryp.QueryParameters) ([]*T, *int64, error)

func (*Selector[T]) SelectFirst

func (s *Selector[T]) SelectFirst(ctx context.Context, db DB, qp *queryp.QueryParameters) (*T, error)

type Table

type Table[T any] struct {
	// Schema to use if you want to hard code it
	Schema string
	// Table name
	Table string
	// The fields you wish to reference for select, insert and update operations
	Fields []*Field[T]
	// Additional joins when fetching data from the table
	Joins string

	// Selector is a tool for fetching multiple rows from a table, using
	// queryp to filter results.
	Selector[T]

	// Scanner is used
	Scanner Scanner[T]

	// This is a callback that is used after fetching a row of data before
	// returning it.
	PostProcessRecord func(*T) error

	// The select portion of the query for just the fields in this table.
	// It should not include the SELECT keyword, just comma separated fields.
	// If this is not specified it will be built automatically based on the fields
	// provided above.
	SelectFields string
	// Additional fields you wish to select from the main query. Generally
	// associated with the Joins but could be anything. Just provide comma
	// separated field statements.
	SelectAdditionalFields string
	// The query used to get a record by ID. If not specified will be auto
	// generated.
	GetByIDQuery string
	// The query used to delete a record by ID. If not specified will be
	// auto generated.
	DeleteByIDQuery string
	// The query used to insert a record. If not specified will be auto generated.
	InsertQuery string
	// The query used to update a record. If not specified will be auto generated by ID.
	UpdateQuery string
	// The query used to upsert a record. If not specified will be auto generated by ID.
	UpsertQuery string
}

Table is the query builder table representation.

func Generate

func Generate[T any](t Table[T]) *Table[T]

func (*Table[T]) DeleteByID

func (t *Table[T]) DeleteByID(ctx context.Context, db DB, ids ...interface{}) error

DeleteByID deletes a single record by ID(s)

func (*Table[T]) GenerateAdditionalFields

func (t *Table[T]) GenerateAdditionalFields(coalesce bool) string

func (*Table[T]) GenerateDeleteByIDQuery

func (t *Table[T]) GenerateDeleteByIDQuery() string

func (*Table[T]) GenerateGetByFieldsQuery

func (t *Table[T]) GenerateGetByFieldsQuery(fields ...string) string

func (*Table[T]) GenerateGetByIDQuery

func (t *Table[T]) GenerateGetByIDQuery() string

func (*Table[T]) GenerateInsertQuery

func (t *Table[T]) GenerateInsertQuery() string

func (*Table[T]) GenerateSelectFields

func (t *Table[T]) GenerateSelectFields() string

func (*Table[T]) GenerateSelectorQuery

func (t *Table[T]) GenerateSelectorQuery() string

func (*Table[T]) GenerateUpdateQuery

func (t *Table[T]) GenerateUpdateQuery() string

func (*Table[T]) GenerateUpsertQuery

func (t *Table[T]) GenerateUpsertQuery() string

func (*Table[T]) GetByID

func (t *Table[T]) GetByID(ctx context.Context, db DB, ids ...interface{}) (*T, error)

GetByID fetches a single record by ID(s)

func (*Table[T]) GetByQuery

func (t *Table[T]) GetByQuery(ctx context.Context, db DB, query string, values ...interface{}) (*T, error)

GetByQuery fetches a single record by the given query and values

func (*Table[T]) Insert

func (t *Table[T]) Insert(ctx context.Context, db DB, record *T, opts ...QueryOption) error

Insert inserts a record

func (*Table[T]) Update

func (t *Table[T]) Update(ctx context.Context, db DB, record *T, opts ...QueryOption) error

Updates a record using the Update query

func (*Table[T]) Upsert

func (t *Table[T]) Upsert(ctx context.Context, db DB, record *T, opts ...QueryOption) error

Upsert a record using the Upsert query.

Jump to

Keyboard shortcuts

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