query

package
v0.21.6 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2020 License: Apache-2.0 Imports: 10 Imported by: 12

Documentation

Overview

Package query is the package used that defines the neuron query it's structure, processor, transactions. The package is used to create new queries, allow to set proper query parameters like: filters, pagination, sort order and includes. It also defines the query processes with their config names.

Index

Constants

View Source
const (
	// ParamInclude is the url.query parameter name for the included fields.
	ParamInclude string = "include"
	// ParamFields is the url.query parameter name for the fieldset.
	ParamFields string = "fields"
)
View Source
const (
	// ParamPageOffset is a query parameter used in an offset based
	// pagination strategy in conjunction with ParamPageLimit.
	ParamPageOffset = "page[offset]"
	// ParamPageLimit is a query parameter used in an offset based
	// pagination strategy in conjunction with ParamPageOffset.
	ParamPageLimit = "page[limit]"
)

Pagination defined constants used for formatting the query.

View Source
const ParamSort = "sort"

ParamSort is the url query parameter name for the sorting fields.

Variables

View Source
var (
	// ErrQuery is the major error classification for the query package.
	ErrQuery = errors.New("query")

	// ErrInternal is the internal error classification.
	ErrInternal = errors.Wrap(errors.ErrInternal, "query")
	// ErrNoResult is the error classification when for the query that returns no result.
	ErrNoResult = errors.Wrap(ErrQuery, "no results")

	// ErrInput is the minor error classification related to the query input.
	ErrInput = errors.Wrap(ErrQuery, "input")
	// ErrInvalidInput is the error classification for invalid query input.
	ErrInvalidInput = errors.Wrap(ErrInput, "invalid")
	// ErrInvalidParameter is the error classification for invalid query parameter.
	ErrInvalidParameter = errors.Wrap(ErrInput, "invalid parameter")
	// ErrInvalidSort is the error classification for invalid sort input.
	ErrInvalidSort = errors.Wrap(ErrInput, "invalid sort")
	// ErrInvalidModels is the error classification for the invalid models input.
	ErrInvalidModels = errors.Wrap(ErrInput, "invalid models")
	// ErrInvalidField is the error classification for the invalid field input
	ErrInvalidField = errors.Wrap(ErrInput, "invalid field")
	// ErrInvalidFieldSet is the error classification for the invalid fieldset input
	ErrInvalidFieldSet = errors.Wrap(ErrInput, "invalid field set")
	// ErrFieldValue is the error classification for the invalid field input
	ErrFieldValue = errors.Wrap(ErrInput, "field value")
	// ErrNoModels is the error classification when there is models provided in the input.
	ErrNoModels = errors.Wrap(ErrInput, "no models")
	// ErrNoFieldsInFieldSet is the error classification when no fields are present in the fieldset.
	ErrNoFieldsInFieldSet = errors.Wrap(ErrInput, "no fields in field set")

	// ErrTransaction is minor error classification for the query transactions.
	ErrTransaction = errors.Wrap(ErrQuery, "transaction")

	// ErrTxDone is the classification for finished transactions.
	ErrTxDone = errors.Wrap(ErrTransaction, "done")
	// ErrTxState is the classification for the transaction state.
	ErrTxState = errors.Wrap(ErrTransaction, "state")
	// ErrTxInvalid is the classification for the invalid transaction.
	ErrTxInvalid = errors.Wrap(ErrTransaction, "invalid")

	// ErrViolation is the minor error classification when query violates some restrictions.
	ErrViolation = errors.Wrap(ErrQuery, "violation")
	// ErrViolationIntegrityConstraint is the violation error classification.
	ErrViolationIntegrityConstraint = errors.Wrap(ErrViolation, "integrity constraint")
	// ErrViolationRestrict is the violation error classification.
	ErrViolationRestrict = errors.Wrap(ErrViolation, "restrict")
	// ErrViolationNotNull is the violation error classification.
	ErrViolationNotNull = errors.Wrap(ErrViolation, "not null")
	// ErrViolationForeignKey is the violation error classification.
	ErrViolationForeignKey = errors.Wrap(ErrViolation, "foreign key")
	// ErrViolationUnique is the violation error classification.
	ErrViolationUnique = errors.Wrap(ErrViolation, "unique")
	// ErrViolationCheck is the violation error classification.
	ErrViolationCheck = errors.Wrap(ErrViolation, "check")
	// ErrViolationDataType is the violation error classification.
	ErrViolationDataType = errors.Wrap(ErrViolation, "data type")
)

Functions

func SplitBracketParameter added in v0.15.0

func SplitBracketParameter(bracketed string) (values []string, err error)

SplitBracketParameter splits the parameters within the '[' and ']' brackets.

Types

type IncludedRelation added in v0.15.0

type IncludedRelation struct {
	StructField       *mapping.StructField
	Fieldset          mapping.FieldSet
	IncludedRelations []*IncludedRelation
}

IncludedRelation is the includes information scope it contains the field to include from the root scope related subScope, and subfields to include.

func (*IncludedRelation) SetFieldset added in v0.16.0

func (i *IncludedRelation) SetFieldset(fields ...*mapping.StructField) error

SetFieldset sets the fieldset for given included.

type IsolationLevel

type IsolationLevel int

IsolationLevel is the

const (
	LevelDefault IsolationLevel = iota
	LevelReadUncommitted
	LevelReadCommitted
	LevelWriteCommitted
	LevelRepeatableRead
	LevelSnapshot
	LevelSerializable
	LevelLinearizable
)

Isolation level enums

func (*IsolationLevel) MarshalJSON

func (i *IsolationLevel) MarshalJSON() ([]byte, error)

MarshalJSON marshals the isolation level into json encoding Implements the json.Marshaller interface

func (IsolationLevel) String

func (i IsolationLevel) String() string

func (*IsolationLevel) UnmarshalJSON

func (i *IsolationLevel) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshal isolation level from the provided data Implements json.Unmarshaler

type Method added in v0.16.0

type Method int

Method is the enum used for the query methods.

const (
	InvalidMethod Method = iota
	Insert
	InsertMany
	InsertRelationship
	Get
	GetRelationship
	GetRelated
	List
	Update
	UpdateMany
	UpdateRelationship
	Delete
	DeleteMany
	DeleteRelationship
)

Enum values for the query methods.

type Pagination added in v0.1.5

type Pagination struct {
	// Limit is a pagination value that defines 'limit' or 'page size'
	Limit int64
	// Offset is a pagination value that defines 'offset' or 'page number'
	Offset int64
}

Pagination defines the query limits and offsets. It defines the maximum size (Limit) as well as an offset at which the query should start. If the pagination type is 'LimitOffsetPagination' the value of 'Limit' defines the 'limit' where the value of 'Offset' defines it's 'offset'. If the pagination type is 'PageNumberPagination' the value of 'Limit' defines 'pageSize' and the value of 'Offset' defines 'pageNumber'. The page number value starts from '1'.

func (*Pagination) First added in v0.15.0

func (p *Pagination) First() (*Pagination, error)

First gets the first pagination for provided 'p' pagination values. If the 'p' pagination is already the 'first' pagination the function returns it as the result directly.

func (*Pagination) FormatQuery added in v0.1.5

func (p *Pagination) FormatQuery(q ...url.Values) url.Values

FormatQuery formats the pagination for the url query.

func (*Pagination) IsZero added in v0.15.0

func (p *Pagination) IsZero() bool

IsZero checks if the pagination is zero valued.

func (*Pagination) Last added in v0.15.0

func (p *Pagination) Last(total int64) (*Pagination, error)

Last gets the last pagination for the provided 'total' count. Returns error if current pagination is not valid, or 'total'. If current pagination 'p' is the last pagination it would be return directly as the result. In order to check if the 'p' is last pagination compare it's pointer with the 'p'.

func (*Pagination) Next added in v0.15.0

func (p *Pagination) Next(total int64) (*Pagination, error)

Next gets the next pagination for the provided 'total' count. If current pagination 'p' is the last one, then the function returns 'p' pagination directly. In order to check if there is a next pagination compare the result pointer with the 'p' pagination.

func (*Pagination) Previous added in v0.15.0

func (p *Pagination) Previous() (*Pagination, error)

Previous gets the pagination for the previous possible size and offset. If current pagination 'p' is the first page then the function returns 'p' pagination. If the previous size would overflow the 0th offset then the previous starts from 0th offset.

func (*Pagination) String added in v0.1.5

func (p *Pagination) String() string

String implements fmt.Stringer interface.

func (*Pagination) Validate added in v0.15.0

func (p *Pagination) Validate() error

Validate checks if the pagination is well formed.

type Parameter added in v0.16.0

type Parameter struct {
	Key, Value string
}

Parameter defines generic query key-value parameter.

func (Parameter) Boolean added in v0.16.0

func (p Parameter) Boolean() (bool, error)

Boolean return boolean parameter value.

func (Parameter) Float64 added in v0.16.0

func (p Parameter) Float64() (float64, error)

Float64 returns 64-bit float parameter.

func (Parameter) Int added in v0.16.0

func (p Parameter) Int() (int, error)

Int returns integer parameter.

func (Parameter) Int64 added in v0.16.0

func (p Parameter) Int64() (int64, error)

Int64 returns 64 bit integer parameter.

func (Parameter) StringSlice added in v0.16.0

func (p Parameter) StringSlice() []string

StringSlice returns string slice parameter values.

func (Parameter) UUID added in v0.16.0

func (p Parameter) UUID() (uuid.UUID, error)

UUID returns uuid integer parameter.

func (Parameter) Uint added in v0.16.0

func (p Parameter) Uint() (uint, error)

Uint returns unsigned integer parameter.

func (Parameter) Uint64 added in v0.16.0

func (p Parameter) Uint64() (uint64, error)

Uint64 returns unsigned 64-bit integer parameter.

type Parameters added in v0.16.0

type Parameters []Parameter

Parameters is the slice wrapper for the query parameters.

func MakeParameters added in v0.16.0

func MakeParameters(values url.Values) (parameters Parameters)

MakeParameters creates new parameters from provided url values.

func (Parameters) Exists added in v0.16.0

func (p Parameters) Exists(key string) bool

Exists checks if given 'key' exists in the parameters

func (Parameters) Get added in v0.16.0

func (p Parameters) Get(key string) (Parameter, bool)

Get gets the parameter stored as 'key'.

func (*Parameters) Set added in v0.16.0

func (p *Parameters) Set(key, value string)

Set parameter with 'key' and 'value'.

func (*Parameters) SetBoolean added in v0.16.0

func (p *Parameters) SetBoolean(key string, b bool)

SetBoolean sets the boolean value.

func (*Parameters) SetFloat64 added in v0.16.0

func (p *Parameters) SetFloat64(key string, f float64, precision int)

SetFloat64 sets the 64-bit float value and trims to selected precision.

func (*Parameters) SetInt added in v0.16.0

func (p *Parameters) SetInt(key string, i int)

SetInt sets the integer parameter value.

func (*Parameters) SetInt64 added in v0.16.0

func (p *Parameters) SetInt64(key string, i int64)

SetInt64 sets the 64 bit integer value.

func (*Parameters) SetUUID added in v0.16.0

func (p *Parameters) SetUUID(key string, id uuid.UUID)

SetUUID sets uuid value.

func (*Parameters) SetUint added in v0.16.0

func (p *Parameters) SetUint(key string, i uint)

SetUint sets the unsigned integer value.

func (*Parameters) SetUint64 added in v0.16.0

func (p *Parameters) SetUint64(key string, i uint64)

SetUint64 sets the unsigned 64-bit integer value.

type RelationSort added in v0.16.0

type RelationSort struct {
	StructField    *mapping.StructField
	SortOrder      SortOrder
	RelationFields []*mapping.StructField
}

RelationSort is an

func (RelationSort) Copy added in v0.16.0

func (r RelationSort) Copy() Sort

Copy implements Sort interface.

func (RelationSort) Field added in v0.16.0

func (r RelationSort) Field() *mapping.StructField

Field implements Sort interface.

func (RelationSort) Order added in v0.16.0

func (r RelationSort) Order() SortOrder

Order implements Sort interface.

type Scope

type Scope struct {
	// id is the unique identification of the scope.
	ID uuid.UUID
	// mStruct is a modelStruct this scope is based on.
	ModelStruct *mapping.ModelStruct
	// Models are the models values used within the context of this query.
	Models mapping.Models
	// Fieldset represents fieldset defined for the whole scope of this query.
	FieldSets []mapping.FieldSet
	// Filters contains all filters for given query.
	Filters filter.Filters
	// SortingOrder are the query sort fields.
	SortingOrder []Sort
	// IncludedRelations contain fields to include. If the included field is a relationship type, then
	// specific included field contains information about it
	IncludedRelations []*IncludedRelation
	// Pagination is the query pagination.
	Pagination *Pagination
	// Transaction is current scope's transaction.
	Transaction *Transaction
	// contains filtered or unexported fields
}

Scope is the query's structure that contains information required for the processor to operate. The scope has its unique 'ID', contains predefined model, operational value, fieldset, filters, sorts and pagination. It also contains the mapping of the included scopes.

func NewScope added in v0.15.0

func NewScope(model *mapping.ModelStruct, models ...mapping.Model) *Scope

NewScope creates the scope for the provided model with respect to the provided internalController 'c'.

func (*Scope) ClearFilters added in v0.15.0

func (s *Scope) ClearFilters()

ClearFilters clears all scope filters.

func (*Scope) CommonFieldSet added in v0.16.0

func (s *Scope) CommonFieldSet() (mapping.FieldSet, bool)

CommonFieldSet gets the common fieldset for all models. CommonField

func (*Scope) Copy added in v0.15.0

func (s *Scope) Copy() *Scope

Copy creates a copy of the given scope.

func (*Scope) Filter added in v0.15.0

func (s *Scope) Filter(f filter.Filter)

Filter adds the filter field to the given query.

func (*Scope) FormatQuery

func (s *Scope) FormatQuery() url.Values

FormatQuery formats the scope's query into the url.Models.

func (*Scope) GetOrCreateRelationFilter added in v0.16.0

func (s *Scope) GetOrCreateRelationFilter(structField *mapping.StructField) filter.Relation

GetOrCreateRelationFilter gets or creates new filter field for given structField within the scope filters.

func (*Scope) Include added in v0.15.0

func (s *Scope) Include(relation *mapping.StructField, relationFieldset ...*mapping.StructField) error

Include includes 'relation' field in the scope's query results.

func (*Scope) Limit added in v0.1.5

func (s *Scope) Limit(limit int64)

Limit sets the maximum number of objects returned by the Find process, Returns error if the given scope has already different type of pagination.

func (*Scope) Offset added in v0.15.0

func (s *Scope) Offset(offset int64)

Offset sets the query result's offset. It says to skip as many object's from the repository before beginning to return the result. 'Offset' 0 is the same as omitting the 'Offset' clause. Returns error if the given scope has already different type of pagination.

func (*Scope) OrderBy added in v0.15.0

func (s *Scope) OrderBy(fields ...string) error

OrderBy adds the sort fields into given scope. If the scope already have sorted fields the function appends newly created sort fields. If the fields are duplicated returns error.

func (*Scope) Select added in v0.15.0

func (s *Scope) Select(fields ...*mapping.StructField) error

Select adds the fields to the scope's fieldset. The fields may be a mapping.StructField as well as field's NeuronName (string) or the StructField Name (string).

func (*Scope) StoreGet added in v0.15.0

func (s *Scope) StoreGet(key interface{}) (value interface{}, ok bool)

StoreGet gets the value from the scope's Store for given 'key'.

func (*Scope) StoreSet added in v0.15.0

func (s *Scope) StoreSet(key, value interface{})

StoreSet sets the 'key' and 'value' in the given scope's store.

func (*Scope) String added in v0.15.0

func (s *Scope) String() string

String implements fmt.Stringer interface.

func (*Scope) Where added in v0.15.0

func (s *Scope) Where(where string, values ...interface{}) error

Where parses the filter into the and adds it to the given scope. The 'filter' should be of form:

  • Field Operator 'ID IN', 'Name CONTAINS', 'id in', 'name contains'
  • Relationship.Field Operator 'Car.UserID IN', 'Car.Doors ==', 'car.user_id >=",

The field might be a Golang model field name or the neuron name.

func (*Scope) WhereOr added in v0.16.0

func (s *Scope) WhereOr(filters ...filter.Simple) error

WhereOr creates multiple or filters.

type Sort added in v0.16.0

type Sort interface {
	Copy() Sort
	Order() SortOrder
	Field() *mapping.StructField
}

Sort is an interface used by the queries.

func NewSort added in v0.15.0

func NewSort(m *mapping.ModelStruct, sort string, order ...SortOrder) (Sort, error)

NewSort creates new 'sort' field for given model 'm'. If the 'disallowFK' is set to true the function would not allow to create OrderBy field of foreign key field.

func NewSortFields added in v0.15.0

func NewSortFields(m *mapping.ModelStruct, sortFields ...string) ([]Sort, error)

NewSortFields creates new 'sortFields' for given model 'm'. If the 'disallowFK' is set to true the function would not allow to create foreign key sort field. The function throws errors on duplicated field values.

type SortField added in v0.1.5

type SortField struct {
	StructField *mapping.StructField
	// Order defines if the sorting order (ascending or descending)
	SortOrder SortOrder
}

SortField is a field that contains sorting information.

func (SortField) Copy added in v0.15.0

func (s SortField) Copy() Sort

Copy creates a copy of the sort field.

func (SortField) Field added in v0.16.0

func (s SortField) Field() *mapping.StructField

Field implements Sort interface.

func (*SortField) FormatQuery added in v0.1.5

func (s *SortField) FormatQuery(q ...url.Values) url.Values

FormatQuery returns the sort field formatted for url query. If the optional argument 'q' is provided the format would be set into the provided url.Models. Otherwise it creates new url.Models instance. Returns modified url.Models

func (SortField) Order added in v0.1.5

func (s SortField) Order() SortOrder

Order implements Sort interface.

func (SortField) String added in v0.15.0

func (s SortField) String() string

type SortOrder added in v0.1.5

type SortOrder int

SortOrder is the enum used as the sorting values order.

const (
	// AscendingOrder defines the sorting ascending order.
	AscendingOrder SortOrder = iota
	// DescendingOrder defines the sorting descending order.
	DescendingOrder
)

func (SortOrder) String added in v0.1.5

func (o SortOrder) String() string

String implements fmt.Stringer interface.

type Transaction added in v0.15.0

type Transaction struct {
	ID      uuid.UUID       `json:"id"`
	Ctx     context.Context `json:"context"`
	State   TxState         `json:"state"`
	Options *TxOptions      `json:"options"`
}

Transaction is the structure that defines the query transaction.

type TxOptions

type TxOptions struct {
	Isolation IsolationLevel `json:"isolation"`
	ReadOnly  bool           `json:"read_only"`
}

TxOptions are the TransactionOptions for the Transaction

type TxState

type TxState int

TxState defines the current transaction Transaction.State

const (
	TxBegin TxState = iota
	TxCommit
	TxRollback
	TxFailed
)

Transaction Transaction.State enums

func (TxState) Done added in v0.15.0

func (t TxState) Done() bool

Done checks if current transaction is already finished.

func (*TxState) MarshalJSON

func (t *TxState) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Transaction.State into string value Implements the json.Marshaler interface

func (TxState) String

func (t TxState) String() string

func (*TxState) UnmarshalJSON

func (t *TxState) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshal the Transaction.State from the json string value Implements json.Unmarshaler interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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