crdbx

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package crdbx implements cursor pagination for crdb.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidPaginationCursor = errors.New("invalid pagination cursor")

ErrInvalidPaginationCursor is returned when there's an error handling the cursor.

Functions

func AsOfSystemTime

func AsOfSystemTime(ctx context.Context, value any) context.Context

AsOfSystemTime sets the `AS OF SYSTEM TIME` context value to be used.

func ContextAsOfSystemTime

func ContextAsOfSystemTime(ctx context.Context, defaultTime any) any

ContextAsOfSystemTime retrieves the `AS OF SYSTEM TIME` value from the context. If no value is found the default value is returned.

func Limit

func Limit(limit int) int

Limit accepts a requested limit, returning an acceptable limit.

Types

type Cursor

type Cursor string

Cursor handles encoding and decoding a pagination cursor values.

func MustNewCursor

func MustNewCursor(kvpairs ...string) *Cursor

MustNewCursor calls NewCursor. If an error is returned panic is called.

func NewCursor

func NewCursor(kvpairs ...string) (*Cursor, error)

NewCursor creates a new pagination cursor from key/value pairs.

func NewCursorFromValues

func NewCursorFromValues(values url.Values) (*Cursor, error)

NewCursorFromValues creates a new pagination cursor from url values.

func (*Cursor) MarshalJSON

func (c *Cursor) MarshalJSON() ([]byte, error)

MarshalJSON implements json marshaller.

func (*Cursor) Set

func (c *Cursor) Set(key, value string) error

Set lets you define include a field in the cursor.

func (*Cursor) String

func (c *Cursor) String() string

String returns a string value of the cursor.

func (*Cursor) StringPtr

func (c *Cursor) StringPtr() *string

StringPtr returns a pointer to the string value of the cursor.

func (*Cursor) Values

func (c *Cursor) Values() (url.Values, error)

Values parses the cursor returning the values.

type FormatValues

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

FormatValues contains the necessary values to extend a query for pagination.

func Paginate

func Paginate(p Paginator, asOfSystemTime any) FormatValues

Paginate handles setting the pagination for the request.

func (FormatValues) AndWhere

func (v FormatValues) AndWhere(next int) string

AndWhere returns the conditions prefixed by `AND` if conditions have been defined. See FormatValues.Where for more details.

func (FormatValues) AsOfSystemTime

func (v FormatValues) AsOfSystemTime() string

AsOfSystemTime if set will be formatted as `AS OF SYSTEM TIME` followed by a value. This value should be included directly after a `FROM` in the query.

func (FormatValues) LimitClause

func (v FormatValues) LimitClause() string

LimitClause returns the `LIMIT` clause. If no limit is setup the default limit of `10` is used.

func (FormatValues) Order

func (v FormatValues) Order(fields ...string) string

Order provides the `ORDER BY` clause value. Additional fields may be included to build the final value. Fields are included in addition to configured order field are used as is.

func (FormatValues) OrderClause

func (v FormatValues) OrderClause(fields ...string) string

OrderClause provides the `ORDER BY` clause. Additional fields may be included, see FormatValues.Order for more details.

func (FormatValues) Values

func (v FormatValues) Values(values ...any) []any

Values returns the values to be used in a query. Additional values may be included and will be first in the combined values slice.

func (FormatValues) Where

func (v FormatValues) Where(next int) string

Where returns the conditions to be added to a where clause. Next provides the offset for defining bind values. If your query includes additional values, this value should be set to the next unused number. For example if you had the query `SELECT * FROM users WHERE country=$1`. The value you should use here is `2`.

func (FormatValues) WhereClause

func (v FormatValues) WhereClause(next int) string

WhereClause returns a full WHERE clause including the conditions if defined. See FormatValues.Where for more details.

func (FormatValues) WithQualifier

func (v FormatValues) WithQualifier(q string) FormatValues

WithQualifier sets all field qualifiers for referenced fields.

type Pagination

type Pagination struct {
	Cursor     *Cursor
	Limit      int
	OnlyFields []string
}

Pagination define pagination query parameters.

func (Pagination) GetCursor

func (p Pagination) GetCursor() *Cursor

GetCursor implements Paginator returning the cursor

func (Pagination) GetLimit

func (p Pagination) GetLimit() int

GetLimit implements Paginator returning the effective limit.

func (Pagination) GetOnlyFields

func (p Pagination) GetOnlyFields() []string

GetOnlyFields implements Paginator returning the permitted fields.

type Paginator

type Paginator interface {
	GetCursor() *Cursor
	GetLimit() int
	GetOnlyFields() []string
}

Paginator provides the necessary details to paginate a database request.

Jump to

Keyboard shortcuts

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