query

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: Apache-2.0 Imports: 17 Imported by: 14

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 (
	// ParamLanguage is the language query parameter used in the url values.
	ParamLanguage = "lang"
	// ParamFilter is the filter query parameter used as the key in the url values.
	ParamFilter = "filter"
)
View Source
const (
	// ParamPage is a JSON API query parameter used as for pagination.
	ParamPage = "page"
	// ParamPageOffset is a JSON API query parameter used in an offset based
	// pagination strategy in conjunction with ParamPageLimit.
	ParamPageOffset = "page[offset]"
	// ParamPageLimit is a JSON API 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 (
	// MjrQuery is the major error classification for the query package.
	MjrQuery errors.Major

	// ClassInternal is the internal error classification.
	ClassInternal errors.Class
	// ClassNoResult is the error classification when for the query that returns no result.
	ClassNoResult errors.Class

	// MnrInput is the minor error classification related to the query input.
	MnrInput errors.Minor
	// ClassInvalidInput is the error classification for invalid query input.
	ClassInvalidInput errors.Class
	// ClassInvalidSort is the error classification for invalid sort input.
	ClassInvalidSort errors.Class
	// ClassInvalidModels is the error classification for the invalid models input.
	ClassInvalidModels errors.Class
	// ClassInvalidField is the error classification for the invalid field input
	ClassInvalidField errors.Class
	// ClassNoModels is the error classification when there is models provided in the input.
	ClassNoModels errors.Class

	// MnrFilter is the minor error classification related to the filters.
	MnrFilter errors.Minor
	// ClassFilterField is the error classification for the filter fields.
	ClassFilterField errors.Class
	// ClassFilterFormat is the error classification for the filter format.
	ClassFilterFormat errors.Class
	// ClassFilterCollection is the error classification for the filter collection.
	ClassFilterCollection errors.Class

	// MnrTransaction is minor error classification for the query transactions.
	MnrTransaction errors.Minor

	// ClassTxDone is the classification for finished transactions.
	ClassTxDone errors.Class
	// ClassTxState is the classification for the transaction state.
	ClassTxState errors.Class
)
View Source
var (
	OpEqual        = &Operator{Value: "=", URLAlias: "$eq", Name: "Equal", Aliases: []string{"=="}}
	OpIn           = &Operator{Value: "in", URLAlias: "$in", Name: "In"}
	OpNotEqual     = &Operator{Value: "!=", URLAlias: "$ne", Name: "NotEqual", Aliases: []string{"<>"}}
	OpNotIn        = &Operator{Value: "not in", URLAlias: "$not_in", Name: "NotIn"}
	OpGreaterThan  = &Operator{Value: ">", URLAlias: "$gt", Name: "GreaterThan"}
	OpGreaterEqual = &Operator{Value: ">=", URLAlias: "$ge", Name: "GreaterThanOrEqualTo"}
	OpLessThan     = &Operator{Value: "<", URLAlias: "$lt", Name: "LessThan"}
	OpLessEqual    = &Operator{Value: "<=", URLAlias: "$le", Name: "LessThanOrEqualTo"}
)

Logical Operators

View Source
var (
	OpContains   = &Operator{Value: "contains", URLAlias: "$contains", Name: "Contains"}
	OpStartsWith = &Operator{Value: "starts with", URLAlias: "$starts_with", Name: "StartsWith"}
	OpEndsWith   = &Operator{Value: "ends with", URLAlias: "$ends_with", Name: "EndsWith"}
)

Strings Only operators.

View Source
var (
	OpIsNull  = &Operator{Value: "is null", URLAlias: "$is_null", Name: "IsNull"}
	OpNotNull = &Operator{Value: "not null", URLAlias: "$not_null", Name: "NotNull"}
)

Null and Existence operators.

View Source
var FilterOperators = newOpContainer()

FilterOperators is the container that stores all query filter operators.

Functions

func RegisterMultipleOperators added in v0.15.0

func RegisterMultipleOperators(operators ...*Operator) error

RegisterMultipleOperators registers multiple operators at once

func RegisterOperator added in v0.15.0

func RegisterOperator(o *Operator) error

RegisterOperator registers the operator in the provided container

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 AfterDeleter

type AfterDeleter interface {
	AfterDelete(context.Context, DB) error
}

AfterDeleter is the interface used as an after delete hook.

type AfterFinder added in v0.15.0

type AfterFinder interface {
	AfterFind(context.Context, DB) error
}

AfterFinder is the interface used as a after find hook.

type AfterInserter added in v0.15.0

type AfterInserter interface {
	AfterInsert(context.Context, DB) error
}

AfterInserter is the interface that has a method used as a hook after the creation process.

type AfterUpdater added in v0.15.0

type AfterUpdater interface {
	AfterUpdate(context.Context, DB) error
}

AfterUpdater is the interface used as a after patch hook.

type BeforeDeleter

type BeforeDeleter interface {
	BeforeDelete(context.Context, DB) error
}

BeforeDeleter is the interface used as a before delete hook.

type BeforeInserter added in v0.15.0

type BeforeInserter interface {
	BeforeInsert(context.Context, DB) error
}

BeforeInserter is the interface used for hooks before the creation process.

type BeforeUpdater added in v0.15.0

type BeforeUpdater interface {
	BeforeUpdate(context.Context, DB) error
}

BeforeUpdater is the interface used as a before patch hook.

type Builder added in v0.15.0

type Builder interface {
	Scope() *Scope
	Err() error
	Ctx() context.Context

	Count() (int64, error)
	Insert() error
	Update() (int64, error)
	Exists() (bool, error)
	Get() (mapping.Model, error)
	Find() ([]mapping.Model, error)
	Delete() (int64, error)

	Select(fields ...*mapping.StructField) Builder
	Where(filter string, values ...interface{}) Builder
	Include(relation *mapping.StructField, relationFieldset ...*mapping.StructField) Builder
	OrderBy(fields ...*SortField) Builder
	Limit(limit int64) Builder
	Offset(offset int64) Builder
	Filter(filter *FilterField) Builder

	AddRelations(relationField *mapping.StructField, relations ...mapping.Model) error
	SetRelations(relationField *mapping.StructField, relations ...mapping.Model) error
	RemoveRelations(relationField *mapping.StructField) (int64, error)
}

Builder is the interface used to build queries.

type CRUDRepository added in v0.15.0

type CRUDRepository interface {
	Counter
	Exister
	Inserter
	Finder
	Updater
	Deleter
}

CRUDRepository is an interface that implements all possible repository methods interfaces.

type Counter added in v0.15.0

type Counter interface {
	Count(ctx context.Context, s *Scope) (int64, error)
}

Counter is the interface used for query repositories that allows to count the result number for the provided query.

type Creator added in v0.15.0

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

Creator is the default query composer that implements DB interface.

func NewComposer added in v0.15.0

func NewComposer(c *controller.Controller) *Creator

NewComposer creates new query composer.

func (*Creator) Controller added in v0.15.0

func (c *Creator) Controller() *controller.Controller

Controller implements DB interface.

func (*Creator) Query added in v0.15.0

func (c *Creator) Query(model *mapping.ModelStruct, models ...mapping.Model) Builder

Query creates new query builder for given 'model' and it's optional instances 'models'.

func (*Creator) QueryCtx added in v0.15.0

func (c *Creator) QueryCtx(ctx context.Context, model *mapping.ModelStruct, models ...mapping.Model) Builder

QueryCtx creates new query builder for given 'model' and it's optional instances 'models'.

type DB added in v0.15.0

type DB interface {
	// Controller gets current controller.
	Controller() *controller.Controller
	// Query creates a new query for provided 'model'.
	Query(model *mapping.ModelStruct, models ...mapping.Model) Builder
	// QueryCtx creates a new query for provided 'model'. The query should take a context on it's
	QueryCtx(ctx context.Context, model *mapping.ModelStruct, models ...mapping.Model) Builder
}

DB is the common interface that allows to do the queries.

type Deleter

type Deleter interface {
	Delete(ctx context.Context, s *Scope) (int64, error)
}

Deleter is the interface for the repositories that deletes provided query value.

type Exister added in v0.15.0

type Exister interface {
	Exists(context.Context, *Scope) (bool, error)
}

Exister is the interface used to check if given query object exists.

type Factory added in v0.15.0

type Factory struct {
	mock.Mock
}

Factory is the repository.Factory mock implementation

func (*Factory) DriverName added in v0.15.0

func (f *Factory) DriverName() string

DriverName returns the factory repository name Implements repository.Repository

func (*Factory) New added in v0.15.0

New creates new repository Implements repository.Factory method

type FilterField added in v0.15.0

type FilterField struct {
	StructField *mapping.StructField
	// Models are the filter values for given attribute Filter
	Values []OperatorValues
	// Nested are the relationship fields filters.
	Nested []*FilterField
}

FilterField is a struct that keeps information about given query filters. It is based on the mapping.StructField.

func NewFilter added in v0.15.0

func NewFilter(model interface{}, filter string, values ...interface{}) (*FilterField, error)

NewFilter creates new filterField for the default controller, 'model', 'filter' query and 'values'. 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 NewFilterC added in v0.15.0

func NewFilterC(c *controller.Controller, model interface{}, filter string, values ...interface{}) (*FilterField, error)

NewFilterC creates new filterField for the controller 'c', 'model', 'filter' query and 'values'. 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 NewFilterField added in v0.15.0

func NewFilterField(field *mapping.StructField, op *Operator, values ...interface{}) *FilterField

NewFilterField creates new filterField for given 'field', operator and 'values'.

func NewStringFilterWithForeignKey added in v0.15.0

func NewStringFilterWithForeignKey(c *controller.Controller, filter string, values ...interface{}) (*FilterField, error)

NewStringFilterWithForeignKey creates the filter field based on the provided filter, schemaName and values. Example:

  • 'fitler': "filter[collection][fieldName][operator]"
  • 'schema': "schemaName"
  • 'values': 5, 13

This function allow to filter over the foreign key fields.

func NewURLStringFilter added in v0.15.0

func NewURLStringFilter(c *controller.Controller, filter string, values ...interface{}) (*FilterField, error)

NewURLStringFilter creates the filter field based on the provided 'filter' and 'values' in the 'url' format. Example:

  • 'filter': "[collection][fieldName][operator]"
  • 'values': 5, 13

This function doesn't allow to filter over foreign keys.

func (*FilterField) Copy added in v0.15.0

func (f *FilterField) Copy() *FilterField

Copy returns the copy of the filter field.

func (*FilterField) FormatQuery added in v0.15.0

func (f *FilterField) FormatQuery(q ...url.Values) url.Values

FormatQuery formats the filter field into url.Models. If the 'q' optional parameter is set, then the function would add the values into the provided argument 'q' url.Models. Otherwise it creates new url.Models. Returns updated (new) url.Models.

func (*FilterField) String added in v0.15.0

func (f *FilterField) String() string

String implements fmt.Stringer interface.

type Filters added in v0.15.0

type Filters []*FilterField

Filters is the wrapper over the slice of filter fields.

func (Filters) String added in v0.15.0

func (f Filters) String() string

String implements fmt.Stringer interface.

type Finder added in v0.15.0

type Finder interface {
	Find(ctx context.Context, s *Scope) error
}

Finder is the repository interface that Lists provided query values.

type FullRepository

type FullRepository interface {
	CRUDRepository
	Transactioner
}

FullRepository is the interface that implements both repository CRUDRepository and the transactioner interfaces.

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.

type Inserter added in v0.15.0

type Inserter interface {
	Insert(ctx context.Context, s *Scope) error
}

Inserter is the repository interface that creates the value within the query.Scope.

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 Operator added in v0.15.0

type Operator struct {
	// ID is the filter operator id used for comparing the operator type.
	ID uint16
	// Models is the operator query value
	Value string
	// Name is the human readable filter operator name.
	Name string
	// URLAlias is the alias value for the url parsable value operator.
	URLAlias string
	// Aliases is the alias for the operator raw value.
	Aliases []string
}

Operator is the operator used for filtering the query.

func (*Operator) IsBasic added in v0.15.0

func (f *Operator) IsBasic() bool

IsBasic checks if the operator is 'OpEqual' or OpNotEqual.

func (*Operator) IsRangeable added in v0.15.0

func (f *Operator) IsRangeable() bool

IsRangeable checks if the operator allows to have value ranges.

func (*Operator) IsStandard added in v0.15.0

func (f *Operator) IsStandard() bool

IsStandard checks if the operator is standard.

func (*Operator) IsStringOnly added in v0.15.0

func (f *Operator) IsStringOnly() bool

IsStringOnly checks if the operator is 'string only'.

func (*Operator) String added in v0.15.0

func (f *Operator) String() string

String implements Stringer interface.

type OperatorValues added in v0.15.0

type OperatorValues struct {
	Values   []interface{}
	Operator *Operator
}

OperatorValues is a struct that holds the Operator with the filter values.

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 Repository added in v0.15.0

type Repository struct {
	mock.Mock
}

Repository is an autogenerated mock type for the Repository type

func (*Repository) Begin added in v0.15.0

func (_m *Repository) Begin(ctx context.Context, tx *Tx) error

Begin provides a mock function with given fields: TransactionCtx, s

func (*Repository) Close added in v0.15.0

func (_m *Repository) Close(context.Context) error

Close closes the repository connection

func (*Repository) Commit added in v0.15.0

func (_m *Repository) Commit(ctx context.Context, tx *Tx) error

Commit provides a mock function with given fields: TransactionCtx, s

func (*Repository) Count added in v0.15.0

func (_m *Repository) Count(ctx context.Context, s *Scope) (int64, error)

Count provides a mock function with given fields: TransactionCtx, s

func (*Repository) Delete added in v0.15.0

func (_m *Repository) Delete(ctx context.Context, s *Scope) (int64, error)

Delete provides a mock function with given fields: TransactionCtx, s

func (*Repository) Dial added in v0.15.0

func (_m *Repository) Dial(context.Context) error

Dial implements repository.Repository

func (*Repository) Exists added in v0.15.0

func (_m *Repository) Exists(ctx context.Context, s *Scope) (bool, error)

Exists provides a mock function with given fields: TransactionCtx, s

func (*Repository) FactoryName added in v0.15.0

func (_m *Repository) FactoryName() string

FactoryName provides a mock function that implements FactoryName method.

func (*Repository) Find added in v0.15.0

func (_m *Repository) Find(ctx context.Context, s *Scope) error

Find provides a mock function with given fields: TransactionCtx, s

func (*Repository) HealthCheck added in v0.15.0

HealthCheck implements repository.Repository.

func (*Repository) ID added in v0.15.0

func (_m *Repository) ID() string

ID implements repository.Repository interface.

func (*Repository) Insert added in v0.15.0

func (_m *Repository) Insert(ctx context.Context, s *Scope) error

Insert provides a mock function with given fields: TransactionCtx, s

func (*Repository) RegisterModels added in v0.15.0

func (_m *Repository) RegisterModels(...*mapping.ModelStruct) error

RegisterModels implements repository.Repository interface.

func (*Repository) Rollback added in v0.15.0

func (_m *Repository) Rollback(ctx context.Context, tx *Tx) error

Rollback provides a mock function with given fields: TransactionCtx, s

func (*Repository) Update added in v0.15.0

func (_m *Repository) Update(ctx context.Context, s *Scope) (int64, error)

Update provides a mock function with given fields: TransactionCtx, s

type Scope

type Scope struct {

	// Models are the models values used within the context of this query.
	Models []mapping.Model
	// Fieldset represents fieldset defined for the whole scope of this query.
	Fieldset mapping.FieldSet
	// ModelsFieldsets are the fieldsets stored for the batch processes. This values are set only when the
	// main fieldset is not defined for the query.
	ModelsFieldsets []mapping.FieldSet
	// Filters contains all filters for given query.
	Filters Filters
	// SortingOrder are the query sort fields.
	SortingOrder []*SortField
	// 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(db DB, model *mapping.ModelStruct) *Scope

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

func (*Scope) AutoSelectedFields added in v0.15.0

func (s *Scope) AutoSelectedFields() bool

AutoSelectedFields checks if the scope fieldset was set automatically. This function returns false if a user had defined any field in the Fieldset.

func (*Scope) ClearFilters added in v0.15.0

func (s *Scope) ClearFilters()

ClearFilters clears all scope filters.

func (*Scope) Copy added in v0.15.0

func (s *Scope) Copy() *Scope

Copy creates a copy of the given scope.

func (*Scope) Count added in v0.15.0

func (s *Scope) Count(ctx context.Context) (int64, error)

Count returns the number of all model instances in the repository that matches given query.

func (*Scope) DB added in v0.15.0

func (s *Scope) DB() DB

DB gets the scope's DB creator.

func (*Scope) Delete

func (s *Scope) Delete(ctx context.Context) (int64, error)

Delete deletes the values provided in the query's scope.

func (*Scope) Exists added in v0.15.0

func (s *Scope) Exists(ctx context.Context) (bool, error)

Exists returns true or false depending if there are any rows matching the query.

func (*Scope) Filter added in v0.15.0

func (s *Scope) Filter(filter *FilterField) error

Filter adds the filter field to the given query.

func (*Scope) Find added in v0.15.0

func (s *Scope) Find(ctx context.Context) ([]mapping.Model, error)

Find gets the values from the repository taking with respect to the query filters, sorts, pagination and included values. Provided context.Context 'TransactionCtx' would be used while querying the repositories.

func (*Scope) FormatQuery

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

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

func (*Scope) Get

func (s *Scope) Get(ctx context.Context) (mapping.Model, error)

Get gets single value from the repository taking into account the scope filters and parameters.

func (*Scope) ID

func (s *Scope) ID() uuid.UUID

ID returns the scope's identity number stored as the UUID.

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) Insert added in v0.15.0

func (s *Scope) Insert(ctx context.Context) (err error)

Insert stores the values within the given scope's value repository.

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) OrderedFieldset added in v0.15.0

func (s *Scope) OrderedFieldset() (ordered mapping.OrderedFieldset)

OrderedFieldset gets the fieldset fields sorted by the struct field's index.

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) SortField added in v0.15.0

func (s *Scope) SortField(field interface{}) error

SortField adds the sort 'field' to the scope.

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) Struct

func (s *Scope) Struct() *mapping.ModelStruct

Struct returns scope's model's structure - *mapping.ModelStruct.

func (*Scope) Update added in v0.15.0

func (s *Scope) Update(ctx context.Context) (modelsAffected int64, err error)

Update patches all selected values for given scope value.

func (*Scope) Where added in v0.15.0

func (s *Scope) Where(filter 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.

type SortField added in v0.1.5

type SortField struct {
	StructField *mapping.StructField
	// Order defines if the sorting order (ascending or descending)
	Order SortOrder
	// SubFields are the relationship sub field sorts
	SubFields []*SortField
}

SortField is a field that contains sorting information.

func NewSort added in v0.15.0

func NewSort(m *mapping.ModelStruct, sort string, disallowFK bool, order ...SortOrder) (*SortField, 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, disallowFK bool, sortFields ...string) ([]*SortField, 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.

func (*SortField) Copy added in v0.15.0

func (s *SortField) Copy() *SortField

Copy creates a copy of the sort field.

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) 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 Transactioner

type Transactioner interface {
	ID() string
	// Begin the scope's transaction.
	Begin(ctx context.Context, tx *Tx) error
	// Commit the scope's transaction.
	Commit(ctx context.Context, tx *Tx) error
	// Rollback the scope's transaction.
	Rollback(ctx context.Context, tx *Tx) error
}

Transactioner is the interface used for the transactions.

type Tx

type Tx struct {
	Transaction *Transaction
	// contains filtered or unexported fields
}

Tx is an in-progress transaction orm. A transaction must end with a call to Commit or Rollback. After a call to Commit or Rollback all operations on the transaction fail with an error of class

func Begin added in v0.15.0

func Begin(ctx context.Context, c *controller.Controller, options *TxOptions) *Tx

Begin startsC new transaction with respect to the 'Transaction.Ctx' context and transaction TransactionOptions 'TransactionOptions' and controller 'c'.

func (*Tx) Commit added in v0.15.0

func (t *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Controller added in v0.15.0

func (t *Tx) Controller() *controller.Controller

Controller returns transaction controller.

func (*Tx) Err added in v0.15.0

func (t *Tx) Err() error

Err returns current transaction runtime error.

func (*Tx) ID

func (t *Tx) ID() uuid.UUID

ID gets unique transaction uuid.

func (*Tx) Options

func (t *Tx) Options() TxOptions

Options gets transaction TransactionOptions.

func (*Tx) Query added in v0.15.0

func (t *Tx) Query(model *mapping.ModelStruct, models ...mapping.Model) Builder

Query builds up a new query for given 'model'. The query is executed using transaction context.

func (*Tx) QueryCtx added in v0.15.0

func (t *Tx) QueryCtx(ctx context.Context, model *mapping.ModelStruct, models ...mapping.Model) Builder

QueryCtx builds up a new query for given 'model'. The query is executed using transaction context - provided context is used only for Builder purpose.

func (*Tx) Rollback added in v0.15.0

func (t *Tx) Rollback() error

Rollback aborts the transaction.

func (*Tx) State

func (t *Tx) State() TxState

State gets current transaction Transaction.State.

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
)

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

type Updater added in v0.15.0

type Updater interface {
	Update(ctx context.Context, s *Scope) (int64, error)
}

Updater is the repository interface that Update given query values.

type Upserter added in v0.15.0

type Upserter interface {
	Upsert(ctx context.Context, s *Scope) error
}

Upserter is the repository interface that upserts given query values.

Jump to

Keyboard shortcuts

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