query

package
v1.8.7 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrQueryNotSupported = errors.New("the requested query option is not supported")
)

Functions

func FromCursor

func FromCursor(val []byte) uint64

func FromInterval

func FromInterval(val Interval) (string, error)

func FromIntervalWithFallback

func FromIntervalWithFallback(val Interval, fallback string) string

func FromOrdering

func FromOrdering(val Ordering) (string, error)

func FromOrderingWithFallback

func FromOrderingWithFallback(val Ordering, fallback string) string

func PaginateQuery

func PaginateQuery(query string, opts []interface{},
	cursor Cursor, limit uint64, direction Ordering) (string, []interface{})

PaginateQuery returns a paginated query string for the given input options.

The input query string is expected as follows:

"SELECT ... WHERE (...)" <- these brackets are not optional

The output query string would be as follows:

"SELECT ... WHERE (...) AND id > ? ORDER BY ? LIMIT ?"
-or-
"SELECT ... WHERE (...) AND id < ? ORDER BY ? LIMIT ?"

Example:

	query := "SELECT * FROM table WHERE (state = $1 OR age > $2)"

	opts := []interface{}{ state: 123, age: 45, }
	cursor := 123
	limit := 10
	direction := Ascending

 PaginateQuery(query, opts, cursor, limit, direction)
 > "SELECT * FROM table WHERE (state = $1 OR age > $2) AND cursor > $3 ORDER BY id ASC LIMIT 10"

Types

type Cursor

type Cursor []byte
var (
	EmptyCursor Cursor = Cursor([]byte{})
)

func ToCursor

func ToCursor(val uint64) Cursor

func (Cursor) ToBase58

func (c Cursor) ToBase58() string

func (Cursor) ToUint64

func (c Cursor) ToUint64() uint64

type Filter

type Filter struct {
	Value uint64
	Valid bool
}

func NewFilter

func NewFilter(value uint64) Filter

func (*Filter) IsValid

func (f *Filter) IsValid() bool

func (*Filter) Set

func (f *Filter) Set(value uint64)

type Interval

type Interval uint8
const (
	IntervalRaw Interval = iota
	IntervalHour
	IntervalDay
	IntervalWeek
	IntervalMonth
)

func ToInterval

func ToInterval(val string) (Interval, error)

func ToIntervalWithFallback

func ToIntervalWithFallback(val string, fallback Interval) Interval

type Option

type Option func(*QueryOptions) error

func WithCursor

func WithCursor(val []byte) Option

func WithDirection

func WithDirection(val Ordering) Option

func WithEndTime

func WithEndTime(val time.Time) Option

func WithFilter

func WithFilter(val Filter) Option

func WithInterval

func WithInterval(val Interval) Option

func WithLimit

func WithLimit(val uint64) Option

func WithStartTime

func WithStartTime(val time.Time) Option

type Ordering

type Ordering uint

The ordering of a returned set of records

const (
	Ascending Ordering = iota
	Descending
)

func ToOrdering

func ToOrdering(val string) (Ordering, error)

func ToOrderingWithFallback

func ToOrderingWithFallback(val string, fallback Ordering) Ordering

type QueryOptions

type QueryOptions struct {
	Supported SupportedOptions

	Start time.Time
	End   time.Time

	Interval Interval
	SortBy   Ordering
	Limit    uint64
	Cursor   Cursor
	FilterBy Filter
}

func DefaultPaginationHandler

func DefaultPaginationHandler(opts ...Option) (*QueryOptions, error)

func DefaultPaginationHandlerWithLimit

func DefaultPaginationHandlerWithLimit(limit uint64, opts ...Option) (*QueryOptions, error)

func (*QueryOptions) Apply

func (qo *QueryOptions) Apply(opts ...Option) error

type SupportedOptions

type SupportedOptions byte
const (
	CanLimitResults     SupportedOptions = 0x01
	CanSortBy           SupportedOptions = 0x01 << 1
	CanBucketBy         SupportedOptions = 0x01 << 2
	CanQueryByCursor    SupportedOptions = 0x01 << 3
	CanQueryByStartTime SupportedOptions = 0x01 << 4
	CanQueryByEndTime   SupportedOptions = 0x01 << 5
	CanFilterBy         SupportedOptions = 0x01 << 6
)

Jump to

Keyboard shortcuts

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