database

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2020 License: Apache-2.0 Imports: 11 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Count

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

Count gets given scope models count.

func Exists

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

Exists checks if given query model exists.

func New

func New(c *core.Controller) *db

New creates new DB for given controller.

func RefreshModels

func RefreshModels(ctx context.Context, db DB, mStruct *mapping.ModelStruct, models []mapping.Model, fieldSet ...*mapping.StructField) error

RefreshModels refreshes all 'fields' (attributes, foreign keys) for provided input 'models'.

func RunInTransaction added in v0.17.0

func RunInTransaction(ctx context.Context, db DB, options *query.TxOptions, txFunc TxFunc) error

RunInTransaction runs specific function 'txFunc' within a transaction. If an error would return from that function the transaction would be rolled back. Otherwise it commits the changes. If an input 'db' is already within a transaction it would just execute txFunc for given transaction.

Types

type AfterDeleter

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

AfterDeleter is the interface used as an after delete hook.

type AfterFinder

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

AfterFinder is the interface used as a after find hook.

type AfterInserter

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

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

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

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

type BeforeUpdater

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

BeforeUpdater is the interface used as a before patch hook.

type Builder

type Builder interface {
	// Scope returns current query scope.
	Scope() *query.Scope
	// Err returns error for given query.
	Err() error
	// Ctx returns current query context.
	Ctx() context.Context

	// Count returns the number of model instances in the repository matching given query.
	Count() (int64, error)
	// Insert models into repository. Returns error with class ErrViolationUnique if the model with given primary key already exists.
	Insert() error
	// update updates query models in the repository. Input models with non zero primary key the function would take
	// update these models instances. For a non zero single model with filters it would update all models that match
	// given query. In order to update all models in the repository use UpdateAll method.
	Update() (int64, error)
	// Exists check if input model exists.
	Exists() (bool, error)
	// Get gets single model that matches given query.
	Get() (mapping.Model, error)
	// Find gets all models that matches given query. For models with DeletedAt timestamp field, adds the
	// filter for not nulls DeletedAt models, unless there is other DeletedAt filter.
	Find() ([]mapping.Model, error)
	// deleteQuery deletes models matching given query. If the model has DeletedAt timestamp field it would do the
	// soft delete - update all models matching given query and set their deleted_at field with current timestamp.
	Delete() (int64, error)
	// refreshQuery gets all input models and refreshes all selected fields and included relations.
	Refresh() error

	// Select model fields for the query fieldSet.
	Select(fields ...*mapping.StructField) Builder
	// Where adds the query 'filter' with provided arguments. The query should be composed of the field name and it's
	// operator. In example: 'ID in', 3,4,5 - would result with a filter on primary key field named 'ID' with the
	// IN operator and it's value equal to 3,4 or 5.
	Where(filter string, arguments ...interface{}) Builder
	// Include adds the 'relation' with it's optional fieldset to get for each resulting model to find.
	Include(relation *mapping.StructField, relationFieldset ...*mapping.StructField) Builder
	// OrderBy sorts the results by the fields in the given order.
	OrderBy(fields ...query.Sort) Builder
	// Limit sets the maximum number of the results for given query.
	Limit(limit int64) Builder
	// Offset sets the number of the results to omit in the query.
	Offset(offset int64) Builder
	// Filter adds the filter field to the given query.
	Filter(filter filter.Filter) Builder
	// WhereOr creates a OrGroup filter for provided simple filters.
	WhereOr(filters ...filter.Simple) Builder

	// AddRelations adds the 'relations' models to input models for 'relationField'
	AddRelations(relationField *mapping.StructField, relations ...mapping.Model) error
	// querySetRelations clears all 'relationField' for the input models and set their values to the 'relations'.
	// The relation's foreign key must be allowed to set to null.
	SetRelations(relationField *mapping.StructField, relations ...mapping.Model) error
	// ClearRelations clears all 'relationField' relations for given input models.
	// The relation's foreign key must be allowed to set to null.
	RemoveRelations(relationField *mapping.StructField) (int64, error)
	// IncludeRelation adds the 'relations' models to input models for 'relationField'
	GetRelations(relationField *mapping.StructField, relationFieldSet ...*mapping.StructField) ([]mapping.Model, error)
}

Builder is the interface used to build queries.

type DB

type DB interface {
	// Controller returns orm based controller.
	Controller() *core.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

	// Insert inserts provided models into mapped repositories. The DB would use models non zero fields. Provided models
	// must have non zero primary key values. The function allows to insert multiple models at once.
	Insert(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error
	// update updates provided models in their mapped repositories. The DB would use models non zero fields. Provided models
	// must have non zero primary key values. The function allows to update multiple models at once.
	Update(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)
	// deleteQuery deletes provided models from their mapped repositories. The DB would use models non zero fields. Provided models
	// must have non zero primary key values. The function allows to delete multiple model values at once.
	Delete(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)
	// refreshQuery refreshes fields (attributes, foreign keys) for provided models.
	Refresh(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error

	// AddRelations adds the 'relations' models to input models for 'relationField'
	AddRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, relations ...mapping.Model) error
	// querySetRelations clears all 'relationField' for the input models and set their values to the 'relations'.
	// The relation's foreign key must be allowed to set to null.
	SetRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, relations ...mapping.Model) error
	// ClearRelations clears all 'relationField' relations for given input models.
	// The relation's foreign key must be allowed to set to null.
	ClearRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField) (int64, error)
	// IncludeRelation includes the relations at the 'relationField' for provided models. An optional relationFieldset might be provided.
	IncludeRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, relationField *mapping.StructField, relationFieldset ...*mapping.StructField) error
	// GetRelations gets the 'relatedField' Models for provided the input 'models. An optional relationFieldset might be provided for the relation models.
	GetRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, relationField *mapping.StructField, relationFieldset ...*mapping.StructField) ([]mapping.Model, error)
}

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

type QueryDeleter

type QueryDeleter interface {
	// DeleteQuery deletes the values provided in the query's scope.
	DeleteQuery(ctx context.Context, q *query.Scope) (int64, error)
}

QueryDeleter is an interface that allows to delete the model values in the query scope.

type QueryFinder

type QueryFinder interface {
	// QueryFind gets the values from the repository with respect to the query filters, sorts, pagination and included values.
	// Provided 'ctx' context.Context would be used while querying the repositories.
	QueryFind(ctx context.Context, q *query.Scope) ([]mapping.Model, error)
}

QueryFinder is an interface that allows to list results from the query.

type QueryGetter

type QueryGetter interface {
	// QueryGet gets single value from the repository taking into account the scope filters and parameters.
	QueryGet(ctx context.Context, q *query.Scope) (mapping.Model, error)
}

QueryGetter is an interface that allows to get single result from query.

type QueryInserter

type QueryInserter interface {
	// InsertQuery stores the values within the given scope's value repository.
	InsertQuery(ctx context.Context, q *query.Scope) error
}

QueryInserter is an interface that allows to store the values in the query scope.

type QueryRefresher

type QueryRefresher interface {
	QueryRefresh(ctx context.Context, q *query.Scope) error
}

QueryRefresher is an interface that allows to refresh the models in the query scope.

type QueryRelationAdder

type QueryRelationAdder interface {
	// AddRelations appends relationship 'relField' 'relationModels' to the scope values.
	QueryAddRelations(ctx context.Context, s *query.Scope, relField *mapping.StructField, relationModels ...mapping.Model) error
}

QueryRelationAdder is an interface that allows to get the relations from the provided query results.

type QueryRelationClearer

type QueryRelationClearer interface {
	// ClearRelations clears all 'relationField' relations for given query.
	// The relation's foreign key must be allowed to set to null.
	QueryClearRelations(ctx context.Context, s *query.Scope, relField *mapping.StructField) (int64, error)
}

QueryRelationClearer is an interface that allows to clear the relations for provided query scope.

type QueryRelationSetter

type QueryRelationSetter interface {
	// querySetRelations clears all 'relationField' for the input models and set their values to the 'relations'.
	// The relation's foreign key must be allowed to set to null.
	QuerySetRelations(ctx context.Context, s *query.Scope, relationField *mapping.StructField, relationModels ...mapping.Model) error
}

QueryRelationSetter is an interface that allows to set the query relation for provided query scope.

type QueryUpdater

type QueryUpdater interface {
	// UpdateQuery updates the models with selected fields or a single model with provided filters.
	UpdateQuery(ctx context.Context, q *query.Scope) (modelsAffected int64, err error)
}

QueryUpdater is an interface that allows to update the values in the query scope.

type Tx

type Tx struct {
	Transaction *query.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

func Begin(ctx context.Context, db DB, options *query.TxOptions) (*Tx, error)

Begin starts new transaction with respect to the transaction context and transaction options with controller 'c'. If the 'db' is already a transaction the function

func (*Tx) AddRelations

func (t *Tx) AddRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, relations ...mapping.Model) error

AddRelations implements DB interface.

func (*Tx) ClearRelations

func (t *Tx) ClearRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField) (int64, error)

ClearRelations clears all 'relationField' relations for given input models. The relation's foreign key must be allowed to set to null.

func (*Tx) Commit

func (t *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Controller

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

Controller returns transaction controller.

func (*Tx) Delete

func (t *Tx) Delete(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)

Delete implements DB interface.

func (*Tx) DeleteQuery

func (t *Tx) DeleteQuery(ctx context.Context, s *query.Scope) (int64, error)

DeleteQuery implements QueryDeleter interface.

func (*Tx) Err

func (t *Tx) Err() error

Err returns current transaction runtime error.

func (*Tx) GetRelations

func (t *Tx) GetRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, relationField *mapping.StructField, relationFieldset ...*mapping.StructField) ([]mapping.Model, error)

GetRelations implements DB interface.

func (*Tx) ID

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

ID gets unique transaction uuid.

func (*Tx) IncludeRelations

func (t *Tx) IncludeRelations(ctx context.Context, mStruct *mapping.ModelStruct, models []mapping.Model, relationField *mapping.StructField, relationFieldset ...*mapping.StructField) error

IncludeRelations implements DB interface.

func (*Tx) Insert

func (t *Tx) Insert(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error

Insert implements DB interface.

func (*Tx) InsertQuery

func (t *Tx) InsertQuery(ctx context.Context, q *query.Scope) error

InsertQuery implements QueryInserter interface.

func (*Tx) Options

func (t *Tx) Options() query.TxOptions

Options gets transaction TransactionOptions.

func (*Tx) Query

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

func (t *Tx) QueryAddRelations(ctx context.Context, s *query.Scope, relationField *mapping.StructField, relationModels ...mapping.Model) error

QueryAddRelations implements QueryRelationAdder interface.

func (*Tx) QueryClearRelations

func (t *Tx) QueryClearRelations(ctx context.Context, q *query.Scope, relationField *mapping.StructField) (int64, error)

QueryClearRelations implements QueryRelationClearer interface.

func (*Tx) QueryCtx

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

func (t *Tx) QueryFind(ctx context.Context, q *query.Scope) ([]mapping.Model, error)

QueryFind implements QueryFinder interface.

func (*Tx) QueryGet

func (t *Tx) QueryGet(ctx context.Context, q *query.Scope) (mapping.Model, error)

QueryGet implements QueryGetter interface.

func (*Tx) QueryRefresh

func (t *Tx) QueryRefresh(ctx context.Context, q *query.Scope) error

QueryRefresh implements QueryRefresher interface.

func (*Tx) QuerySetRelations

func (t *Tx) QuerySetRelations(ctx context.Context, q *query.Scope, relationField *mapping.StructField, relations ...mapping.Model) error

QuerySetRelations implements QueryRelationSetter interface.

func (*Tx) Refresh

func (t *Tx) Refresh(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) error

Refresh implements DB interface.

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback aborts the transaction.

func (*Tx) SetRelations

func (t *Tx) SetRelations(ctx context.Context, model mapping.Model, relationField *mapping.StructField, relations ...mapping.Model) error

SetRelations clears all 'relationField' for the input models and set their values to the 'relations'. The relation's foreign key must be allowed to set to null.

func (*Tx) State

func (t *Tx) State() query.TxState

State gets current transaction Transaction.State.

func (*Tx) Update

func (t *Tx) Update(ctx context.Context, mStruct *mapping.ModelStruct, models ...mapping.Model) (int64, error)

Update implements DB interface.

func (*Tx) UpdateQuery

func (t *Tx) UpdateQuery(ctx context.Context, q *query.Scope) (int64, error)

UpdateQuery implements QueryUpdater interface.

type TxFunc added in v0.17.0

type TxFunc func(db DB) error

TxFunc is the execute function for the the RunInTransaction function.

Jump to

Keyboard shortcuts

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