rel

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: MIT Imports: 15 Imported by: 3

README

REL

GoDoc Build Status Go Report Card Maintainability Test Coverage

Golang SQL Database Layer for Layered Architecture.

REL is golang orm-ish database layer for layered architecture. It's testable and comes with its own test library. REL also features extendable query builder that allows you to write query using builder or plain sql.

Features

  • Testable repository with builtin reltest package.
  • Elegant, yet extendable query builder.
  • Supports Eager loading.
  • Multi adapter.
  • Soft Deletion.

Install

go get github.com/Fs02/rel

Getting Started

License

Released under the MIT License

Documentation

Overview

Package rel is a Database Layer for SQL Database.

Index

Constants

View Source
const (
	// BelongsTo association.
	BelongsTo = iota
	// HasOne association.
	HasOne
	// HasMany association.
	HasMany
)

Variables

Setf is an alias for SetFragment

Functions

func DefaultLogger

func DefaultLogger(query string, duration time.Duration, err error)

DefaultLogger log query suing standard log library.

func Log

func Log(logger []Logger, statement string, duration time.Duration, err error)

Log using multiple logger. This function intended to be used within adapter.

func Nullable

func Nullable(dest interface{}) interface{}

Nullable wrap value as a nullable sql.Scanner. If value returned from database is nil, nullable scanner will set dest to zero value.

Types

type Adapter

type Adapter interface {
	Ping(ctx context.Context) error
	Aggregate(ctx context.Context, query Query, mode string, field string, loggers ...Logger) (int, error)
	Query(ctx context.Context, query Query, loggers ...Logger) (Cursor, error)
	Insert(ctx context.Context, query Query, modifies map[string]Modify, loggers ...Logger) (interface{}, error)
	InsertAll(ctx context.Context, query Query, fields []string, bulkModifies []map[string]Modify, loggers ...Logger) ([]interface{}, error)
	Update(ctx context.Context, query Query, modifies map[string]Modify, loggers ...Logger) (int, error)
	Delete(ctx context.Context, query Query, loggers ...Logger) (int, error)

	Begin(ctx context.Context) (Adapter, error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

Adapter interface

type AssocModification added in v0.2.0

type AssocModification struct {
	Modifications []Modification
	DeletedIDs    []interface{}
}

AssocModification represents modification for association.

type Association

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

Association provides abstraction to work with association of document or collection.

func (Association) Collection

func (a Association) Collection() (*Collection, bool)

Collection returns association target as collection. If association is zero, second return value will be false.

func (Association) Document

func (a Association) Document() (*Document, bool)

Document returns association target as document. If association is zero, second return value will be false.

func (Association) ForeignField

func (a Association) ForeignField() string

ForeignField of the association.

func (Association) ForeignValue

func (a Association) ForeignValue() interface{}

ForeignValue of the association. It'll panic if association type is has many.

func (Association) IsZero

func (a Association) IsZero() bool

IsZero returns true if association is not loaded.

func (Association) ReferenceField

func (a Association) ReferenceField() string

ReferenceField of the association.

func (Association) ReferenceValue

func (a Association) ReferenceValue() interface{}

ReferenceValue of the association.

func (Association) Type

func (a Association) Type() AssociationType

Type of association.

type AssociationType

type AssociationType uint8

AssociationType defines the type of association in database.

type ChangeOp

type ChangeOp int

ChangeOp represents type of modify operation.

const (
	// ChangeInvalidOp operation.
	ChangeInvalidOp ChangeOp = iota
	// ChangeSetOp operation.
	ChangeSetOp
	// ChangeIncOp operation.
	ChangeIncOp
	// ChangeFragmentOp operation.
	ChangeFragmentOp
)

type Collection

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

Collection provides an abstraction over reflect to easily works with slice for database purpose.

func NewCollection

func NewCollection(records interface{}, readonly ...bool) *Collection

NewCollection used to create abstraction to work with slice. COllection can be created using interface or reflect.Value.

func (Collection) Add

func (c Collection) Add() *Document

Add new document into collection.

func (Collection) Get

func (c Collection) Get(index int) *Document

Get an element from the underlying slice as a document.

func (Collection) Len

func (c Collection) Len() int

Len of the underlying slice.

func (Collection) PrimaryField

func (c Collection) PrimaryField() string

PrimaryField column name of this collection.

func (Collection) PrimaryValue

func (c Collection) PrimaryValue() interface{}

PrimaryValue of collection. Returned value will be interface of slice interface.

func (Collection) ReflectValue

func (c Collection) ReflectValue() reflect.Value

ReflectValue of referenced document.

func (Collection) Reset

func (c Collection) Reset()

Reset underlying slice to be zero length.

func (Collection) Slice added in v0.2.0

func (c Collection) Slice(i, j int) *Collection

Slice returns a new collection that is a slice of the original collection.s

func (Collection) Swap

func (c Collection) Swap(i, j int)

Swap element in the collection.

func (*Collection) Table

func (c *Collection) Table() string

Table returns name of the table.

func (Collection) Truncate

func (c Collection) Truncate(i, j int)

Truncate collection.

type ConstraintError

type ConstraintError struct {
	Key  string
	Type ConstraintType
	Err  error
}

ConstraintError returned whenever constraint error encountered.

func (ConstraintError) Error

func (ce ConstraintError) Error() string

Error message.

func (ConstraintError) Unwrap

func (ce ConstraintError) Unwrap() error

Unwrap internal error returned by database driver.

type ConstraintType

type ConstraintType int8

ConstraintType defines the type of constraint error.

const (
	// CheckConstraint error type.
	CheckConstraint ConstraintType = iota
	// NotNullConstraint error type.1
	NotNullConstraint
	// UniqueConstraint error type.1
	UniqueConstraint
	// PrimaryKeyConstraint error type.1
	PrimaryKeyConstraint
	// ForeignKeyConstraint error type.1
	ForeignKeyConstraint
)

func (ConstraintType) String

func (ct ConstraintType) String() string

String representation of the constraint type.

type Cursor

type Cursor interface {
	Close() error
	Fields() ([]string, error)
	Next() bool
	Scan(...interface{}) error
	NopScanner() interface{} // TODO: conflict with manual scanners interface
}

Cursor is interface to work with database result (used by adapter).

type Document

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

Document provides an abstraction over reflect to easily works with struct for database purpose.

func NewDocument

func NewDocument(record interface{}, readonly ...bool) *Document

NewDocument used to create abstraction to work with struct. Document can be created using interface or reflect.Value.

func (*Document) Add

func (d *Document) Add() *Document

Add returns this document, this is a noop for compatibility with collection.

func (Document) Association

func (d Document) Association(name string) Association

Association of this document with given name.

func (Document) BelongsTo

func (d Document) BelongsTo() []string

BelongsTo fields of this document.

func (Document) Fields

func (d Document) Fields() []string

Fields returns list of fields available on this document.

func (Document) Flag added in v0.2.0

func (d Document) Flag(flag DocumentFlag) bool

Flag returns true if struct contains specified flag.

func (*Document) Get

func (d *Document) Get(index int) *Document

Get always returns this document, this is a noop for compatibility with collection.

func (Document) HasMany

func (d Document) HasMany() []string

HasMany fields of this document.

func (Document) HasOne

func (d Document) HasOne() []string

HasOne fields of this document.

func (Document) Index

func (d Document) Index() map[string]int

Index returns map of column name and it's struct index.

func (*Document) Len

func (d *Document) Len() int

Len always returns 1 for document, this is a noop for compatibility with collection.

func (Document) PrimaryField

func (d Document) PrimaryField() string

PrimaryField column name of this document.

func (Document) PrimaryValue

func (d Document) PrimaryValue() interface{}

PrimaryValue of this document.

func (Document) ReflectValue

func (d Document) ReflectValue() reflect.Value

ReflectValue of referenced document.

func (Document) Reset

func (d Document) Reset()

Reset this document, this is a noop for compatibility with collection.

func (Document) Scanners

func (d Document) Scanners(fields []string) []interface{}

Scanners returns slice of sql.Scanner for given fields.

func (Document) SetValue

func (d Document) SetValue(field string, value interface{}) bool

SetValue of the field, it returns false if field does not exist, or it's not assignable.

func (Document) Table

func (d Document) Table() string

Table returns name of the table.

func (Document) Type

func (d Document) Type(field string) (reflect.Type, bool)

Type returns reflect.Type of given field. if field does not exist, second returns value will be false.

func (Document) Value

func (d Document) Value(field string) (interface{}, bool)

Value returns value of given field. if field does not exist, second returns value will be false.

type DocumentFlag added in v0.2.0

type DocumentFlag int8

DocumentFlag stores information about document as a flag.

const (
	// Invalid flag.
	Invalid DocumentFlag = 1 << iota
	// HasCreatedAt flag.
	HasCreatedAt
	// HasUpdatedAt flag.
	HasUpdatedAt
	// HasDeletedAt flag.
	HasDeletedAt
)

func (DocumentFlag) Is added in v0.2.0

func (df DocumentFlag) Is(flag DocumentFlag) bool

Is returns true if it's defined.

type FilterOp

type FilterOp int

FilterOp defines enumeration of all supported filter types.

const (
	// FilterAndOp is filter type for and operator.
	FilterAndOp FilterOp = iota
	// FilterOrOp is filter type for or operator.
	FilterOrOp
	// FilterNotOp is filter type for not operator.
	FilterNotOp

	// FilterEqOp is filter type for equal comparison.
	FilterEqOp
	// FilterNeOp is filter type for not equal comparison.
	FilterNeOp

	// FilterLtOp is filter type for less than comparison.
	FilterLtOp
	// FilterLteOp is filter type for less than or equal comparison.
	FilterLteOp
	// FilterGtOp is filter type for greater than comparison.
	FilterGtOp
	// FilterGteOp is filter type for greter than or equal comparison.
	FilterGteOp

	// FilterNilOp is filter type for nil check.
	FilterNilOp
	// FilterNotNilOp is filter type for not nil check.
	FilterNotNilOp

	// FilterInOp is filter type for inclusion comparison.
	FilterInOp
	// FilterNinOp is filter type for not inclusion comparison.
	FilterNinOp

	// FilterLikeOp is filter type for like comparison.
	FilterLikeOp
	// FilterNotLikeOp is filter type for not like comparison.
	FilterNotLikeOp

	// FilterFragmentOp is filter type for custom filter.
	FilterFragmentOp
)

type FilterQuery

type FilterQuery struct {
	Type  FilterOp
	Field string
	Value interface{}
	Inner []FilterQuery
}

FilterQuery defines details of a coundition type.

func And

func And(inner ...FilterQuery) FilterQuery

And compares other filters using and.

func Eq

func Eq(field string, value interface{}) FilterQuery

Eq expression field equal to value.

func FilterFragment

func FilterFragment(expr string, values ...interface{}) FilterQuery

FilterFragment add custom filter.

func Gt

func Gt(field string, value interface{}) FilterQuery

Gt compares that left value is greater than to right value.

func Gte

func Gte(field string, value interface{}) FilterQuery

Gte compares that left value is greater than or equal to right value.

func In

func In(field string, values ...interface{}) FilterQuery

In check whethers value of the field is included in values.

func InInt

func InInt(field string, values []int) FilterQuery

InInt check whethers integer values of the field is included.

func InString

func InString(field string, values []string) FilterQuery

InString check whethers string values of the field is included.

func InUint

func InUint(field string, values []uint) FilterQuery

InUint check whethers unsigned integer values of the field is included.

func Like

func Like(field string, pattern string) FilterQuery

Like compares value of field to match string pattern.

func Lt

func Lt(field string, value interface{}) FilterQuery

Lt compares that left value is less than to right value.

func Lte

func Lte(field string, value interface{}) FilterQuery

Lte compares that left value is less than or equal to right value.

func Ne

func Ne(field string, value interface{}) FilterQuery

Ne compares that left value is not equal to right value.

func Nil

func Nil(field string) FilterQuery

Nil check whether field is nil.

func Nin

func Nin(field string, values ...interface{}) FilterQuery

Nin check whethers value of the field is not included in values.

func NinInt

func NinInt(field string, values []int) FilterQuery

NinInt check whethers integer values of the is not included.

func NinString

func NinString(field string, values []string) FilterQuery

NinString check whethers string values of the is not included.

func NinUint

func NinUint(field string, values []uint) FilterQuery

NinUint check whethers unsigned integer values of the is not included.

func Not

func Not(inner ...FilterQuery) FilterQuery

Not wraps filters using not. It'll negate the filter type if possible.

func NotLike

func NotLike(field string, pattern string) FilterQuery

NotLike compares value of field to not match string pattern.

func NotNil

func NotNil(field string) FilterQuery

NotNil check whether field is not nil.

func Or

func Or(inner ...FilterQuery) FilterQuery

Or compares other filters using and.

func (FilterQuery) And

func (fq FilterQuery) And(filters ...FilterQuery) FilterQuery

And wraps filters using and.

func (FilterQuery) AndEq

func (fq FilterQuery) AndEq(field string, value interface{}) FilterQuery

AndEq append equal expression using and.

func (FilterQuery) AndFragment

func (fq FilterQuery) AndFragment(expr string, values ...interface{}) FilterQuery

AndFragment append fragment using and.

func (FilterQuery) AndGt

func (fq FilterQuery) AndGt(field string, value interface{}) FilterQuery

AndGt append greater than expression using and.

func (FilterQuery) AndGte

func (fq FilterQuery) AndGte(field string, value interface{}) FilterQuery

AndGte append greater than or equal expression using and.

func (FilterQuery) AndIn

func (fq FilterQuery) AndIn(field string, values ...interface{}) FilterQuery

AndIn append is in expression using and.

func (FilterQuery) AndLike

func (fq FilterQuery) AndLike(field string, pattern string) FilterQuery

AndLike append like expression using and.

func (FilterQuery) AndLt

func (fq FilterQuery) AndLt(field string, value interface{}) FilterQuery

AndLt append lesser than expression using and.

func (FilterQuery) AndLte

func (fq FilterQuery) AndLte(field string, value interface{}) FilterQuery

AndLte append lesser than or equal expression using and.

func (FilterQuery) AndNe

func (fq FilterQuery) AndNe(field string, value interface{}) FilterQuery

AndNe append not equal expression using and.

func (FilterQuery) AndNil

func (fq FilterQuery) AndNil(field string) FilterQuery

AndNil append is nil expression using and.

func (FilterQuery) AndNin

func (fq FilterQuery) AndNin(field string, values ...interface{}) FilterQuery

AndNin append is not in expression using and.

func (FilterQuery) AndNotLike

func (fq FilterQuery) AndNotLike(field string, pattern string) FilterQuery

AndNotLike append not like expression using and.

func (FilterQuery) AndNotNil

func (fq FilterQuery) AndNotNil(field string) FilterQuery

AndNotNil append is not nil expression using and.

func (FilterQuery) Build

func (fq FilterQuery) Build(query *Query)

Build Filter query.

func (FilterQuery) None

func (fq FilterQuery) None() bool

None returns true if no filter is specified.

func (FilterQuery) Or

func (fq FilterQuery) Or(filter ...FilterQuery) FilterQuery

Or wraps filters using or.

func (FilterQuery) OrEq

func (fq FilterQuery) OrEq(field string, value interface{}) FilterQuery

OrEq append equal expression using or.

func (FilterQuery) OrFragment

func (fq FilterQuery) OrFragment(expr string, values ...interface{}) FilterQuery

OrFragment append fragment using or.

func (FilterQuery) OrGt

func (fq FilterQuery) OrGt(field string, value interface{}) FilterQuery

OrGt append greater than expression using or.

func (FilterQuery) OrGte

func (fq FilterQuery) OrGte(field string, value interface{}) FilterQuery

OrGte append greater than or equal expression using or.

func (FilterQuery) OrIn

func (fq FilterQuery) OrIn(field string, values ...interface{}) FilterQuery

OrIn append is in expression using or.

func (FilterQuery) OrLike

func (fq FilterQuery) OrLike(field string, pattern string) FilterQuery

OrLike append like expression using or.

func (FilterQuery) OrLt

func (fq FilterQuery) OrLt(field string, value interface{}) FilterQuery

OrLt append lesser than expression using or.

func (FilterQuery) OrLte

func (fq FilterQuery) OrLte(field string, value interface{}) FilterQuery

OrLte append lesser than or equal expression using or.

func (FilterQuery) OrNe

func (fq FilterQuery) OrNe(field string, value interface{}) FilterQuery

OrNe append not equal expression using or.

func (FilterQuery) OrNil

func (fq FilterQuery) OrNil(field string) FilterQuery

OrNil append is nil expression using or.

func (FilterQuery) OrNin

func (fq FilterQuery) OrNin(field string, values ...interface{}) FilterQuery

OrNin append is not in expression using or.

func (FilterQuery) OrNotLike

func (fq FilterQuery) OrNotLike(field string, pattern string) FilterQuery

OrNotLike append not like expression using or.

func (FilterQuery) OrNotNil

func (fq FilterQuery) OrNotNil(field string) FilterQuery

OrNotNil append is not nil expression using or.

type GroupQuery

type GroupQuery struct {
	Fields []string
	Filter FilterQuery
}

GroupQuery defines group clause of the query.

func NewGroup

func NewGroup(fields ...string) GroupQuery

NewGroup query.

func (GroupQuery) Build

func (gq GroupQuery) Build(query *Query)

Build query.

func (GroupQuery) Having

func (gq GroupQuery) Having(filters ...FilterQuery) GroupQuery

Having appends filter for group query with and operand.

func (GroupQuery) OrHaving

func (gq GroupQuery) OrHaving(filters ...FilterQuery) GroupQuery

OrHaving appends filter for group query with or operand.

func (GroupQuery) OrWhere

func (gq GroupQuery) OrWhere(filters ...FilterQuery) GroupQuery

OrWhere is alias for OrHaving.

func (GroupQuery) Where

func (gq GroupQuery) Where(filters ...FilterQuery) GroupQuery

Where is alias for having.

type JoinQuery

type JoinQuery struct {
	Mode      string
	Table     string
	From      string
	To        string
	Arguments []interface{}
}

JoinQuery defines join clause in query.

func NewFullJoin

func NewFullJoin(table string) JoinQuery

NewFullJoin with given table.

func NewFullJoinOn

func NewFullJoinOn(table string, from string, to string) JoinQuery

NewFullJoinOn table with given field.

func NewInnerJoin

func NewInnerJoin(table string) JoinQuery

NewInnerJoin with given table.

func NewInnerJoinOn

func NewInnerJoinOn(table string, from string, to string) JoinQuery

NewInnerJoinOn table with given field.

func NewJoin

func NewJoin(table string) JoinQuery

NewJoin with given table.

func NewJoinFragment

func NewJoinFragment(expr string, args ...interface{}) JoinQuery

NewJoinFragment defines a join clause using raw query.

func NewJoinOn

func NewJoinOn(table string, from string, to string) JoinQuery

NewJoinOn table with given field.

func NewJoinWith

func NewJoinWith(mode string, table string, from string, to string) JoinQuery

NewJoinWith query with custom join mode, table and field.

func NewLeftJoin

func NewLeftJoin(table string) JoinQuery

NewLeftJoin with given table.

func NewLeftJoinOn

func NewLeftJoinOn(table string, from string, to string) JoinQuery

NewLeftJoinOn table with given field.

func NewRightJoin

func NewRightJoin(table string) JoinQuery

NewRightJoin with given table.

func NewRightJoinOn

func NewRightJoinOn(table string, from string, to string) JoinQuery

NewRightJoinOn table with given field.

func (JoinQuery) Build

func (jq JoinQuery) Build(query *Query)

Build query.

type Limit

type Limit int

Limit query.

func (Limit) Build

func (l Limit) Build(query *Query)

Build query.

type Lock

type Lock string

Lock query. This query will be ignored if used outside of transaction.

func ForUpdate

func ForUpdate() Lock

ForUpdate lock query.

func (Lock) Build

func (l Lock) Build(query *Query)

Build query.

type Logger

type Logger func(string, time.Duration, error)

Logger defines function signature for custom logger.

type Map

type Map map[string]interface{}

Map can be used as modification for repository insert or update operation. This allows inserting or updating only on specified field. Insert/Update of has one or belongs to can be done using other Map as a value. Insert/Update of has many can be done using slice of Map as a value. Map is intended to be used internally within application, and not to be exposed directly as an APIs.

func (Map) Apply added in v0.2.0

func (m Map) Apply(doc *Document, modification *Modification)

Apply modification.

type Modification added in v0.2.0

type Modification struct {
	Modifies map[string]Modify
	Assoc    map[string]AssocModification
	Unscoped Unscoped
	Reload   bool
}

Modification represents value to be inserted or updated to database. It's not safe to be used multiple time. some operation my alter modification data.

func Apply added in v0.2.0

func Apply(doc *Document, modifiers ...Modifier) Modification

Apply using given modifiers.

func (*Modification) Add added in v0.2.0

func (m *Modification) Add(mod Modify)

Add a modify.

func (*Modification) SetAssoc added in v0.2.0

func (m *Modification) SetAssoc(field string, mods ...Modification)

SetAssoc modification.

func (*Modification) SetDeletedIDs added in v0.2.0

func (m *Modification) SetDeletedIDs(field string, ids []interface{})

SetDeletedIDs modification. nil slice will clear association.

type Modifier added in v0.2.0

type Modifier interface {
	Apply(doc *Document, modification *Modification)
}

Modifier is interface for a record modifier.

type Modify added in v0.2.0

type Modify struct {
	Type  ChangeOp
	Field string
	Value interface{}
}

Modify stores modification instruction.

func Dec

func Dec(field string) Modify

Dec create a modify using deccrement operation.

func DecBy

func DecBy(field string, n int) Modify

DecBy create a modify using decrement operation with custom decrement value.

func Inc

func Inc(field string) Modify

Inc create a modify using increment operation.

func IncBy

func IncBy(field string, n int) Modify

IncBy create a modify using increment operation with custom increment value.

func Set

func Set(field string, value interface{}) Modify

Set create a modify using set operation.

func SetFragment added in v0.2.0

func SetFragment(raw string, args ...interface{}) Modify

SetFragment create a modify operation using randoc fragment operation. Only available for Update.

func (Modify) Apply added in v0.2.0

func (m Modify) Apply(doc *Document, modification *Modification)

Apply modification.

type NotFoundError added in v0.2.0

type NotFoundError struct{}

NotFoundError returned whenever Find returns no result.

func (NotFoundError) Error added in v0.2.0

func (nfe NotFoundError) Error() string

Error message.

type Offset

type Offset int

Offset Query.

func (Offset) Build

func (o Offset) Build(query *Query)

Build query.

type Querier

type Querier interface {
	Build(*Query)
}

Querier interface defines contract to be used for query builder.

type Query

type Query struct {
	Table         string
	SelectQuery   SelectQuery
	JoinQuery     []JoinQuery
	WhereQuery    FilterQuery
	GroupQuery    GroupQuery
	SortQuery     []SortQuery
	OffsetQuery   Offset
	LimitQuery    Limit
	LockQuery     Lock
	UnscopedQuery Unscoped
	// contains filtered or unexported fields
}

Query defines information about query generated by query builder.

func Build added in v0.2.0

func Build(table string, queriers ...Querier) Query

Build for given table using given queriers.

func From

func From(table string) Query

From create a query with chainable syntax, using from as the starting point.

func Join

func Join(table string) Query

Join create a query with chainable syntax, using join as the starting point.

func JoinOn

func JoinOn(table string, from string, to string) Query

JoinOn create a query with chainable syntax, using join as the starting point.

func JoinWith

func JoinWith(mode string, table string, from string, to string) Query

JoinWith create a query with chainable syntax, using join as the starting point.

func Joinf

func Joinf(expr string, args ...interface{}) Query

Joinf create a query with chainable syntax, using join as the starting point.

func Select

func Select(fields ...string) Query

Select query create a query with chainable syntax, using select as the starting point.

func Where

func Where(filters ...FilterQuery) Query

Where create a query with chainable syntax, using where as the starting point.

func (Query) Build

func (q Query) Build(query *Query)

Build query.

func (Query) Distinct

func (q Query) Distinct() Query

Distinct sets select query to be distinct.

func (Query) From

func (q Query) From(table string) Query

From set the table to be used for query.

func (Query) Group

func (q Query) Group(fields ...string) Query

Group query.

func (Query) Having

func (q Query) Having(filters ...FilterQuery) Query

Having query.

func (Query) Havingf

func (q Query) Havingf(expr string, args ...interface{}) Query

Havingf create having query using a raw query.

func (Query) Join

func (q Query) Join(table string) Query

Join current table with other table.

func (Query) JoinOn

func (q Query) JoinOn(table string, from string, to string) Query

JoinOn current table with other table.

func (Query) JoinWith

func (q Query) JoinWith(mode string, table string, from string, to string) Query

JoinWith current table with other table with custom join mode.

func (Query) Joinf

func (q Query) Joinf(expr string, args ...interface{}) Query

Joinf create join query using a raw query.

func (Query) Limit

func (q Query) Limit(limit Limit) Query

Limit result returned by database.

func (Query) Lock

func (q Query) Lock(lock Lock) Query

Lock query expression.

func (Query) Offset

func (q Query) Offset(offset Offset) Query

Offset the result returned by database.

func (Query) OrHaving

func (q Query) OrHaving(filters ...FilterQuery) Query

OrHaving query.

func (Query) OrHavingf

func (q Query) OrHavingf(expr string, args ...interface{}) Query

OrHavingf create having query using a raw query.

func (Query) OrWhere

func (q Query) OrWhere(filters ...FilterQuery) Query

OrWhere query.

func (Query) OrWheref

func (q Query) OrWheref(expr string, args ...interface{}) Query

OrWheref create where query using a raw query.

func (Query) Select

func (q Query) Select(fields ...string) Query

Select filter fields to be selected from database.

func (Query) Sort

func (q Query) Sort(fields ...string) Query

Sort query.

func (Query) SortAsc

func (q Query) SortAsc(fields ...string) Query

SortAsc query.

func (Query) SortDesc

func (q Query) SortDesc(fields ...string) Query

SortDesc query.

func (Query) Unscoped added in v0.2.0

func (q Query) Unscoped() Query

Unscoped allows soft-delete to be ignored.

func (Query) Where

func (q Query) Where(filters ...FilterQuery) Query

Where query.

func (Query) Wheref

func (q Query) Wheref(expr string, args ...interface{}) Query

Wheref create where query using a raw query.

type Reload added in v0.2.0

type Reload bool

Reload force reload after insert/update.

func (Reload) Apply added in v0.2.0

func (r Reload) Apply(doc *Document, modification *Modification)

Apply modification.

type Repository

type Repository interface {
	Adapter() Adapter
	SetLogger(logger ...Logger)
	Ping(ctx context.Context) error
	Aggregate(ctx context.Context, query Query, aggregate string, field string) (int, error)
	MustAggregate(ctx context.Context, query Query, aggregate string, field string) int
	Count(ctx context.Context, collection string, queriers ...Querier) (int, error)
	MustCount(ctx context.Context, collection string, queriers ...Querier) int
	Find(ctx context.Context, record interface{}, queriers ...Querier) error
	MustFind(ctx context.Context, record interface{}, queriers ...Querier)
	FindAll(ctx context.Context, records interface{}, queriers ...Querier) error
	MustFindAll(ctx context.Context, records interface{}, queriers ...Querier)
	Insert(ctx context.Context, record interface{}, modifiers ...Modifier) error
	MustInsert(ctx context.Context, record interface{}, modifiers ...Modifier)
	InsertAll(ctx context.Context, records interface{}) error
	MustInsertAll(ctx context.Context, records interface{})
	Update(ctx context.Context, record interface{}, modifiers ...Modifier) error
	MustUpdate(ctx context.Context, record interface{}, modifiers ...Modifier)
	Delete(ctx context.Context, record interface{}) error
	MustDelete(ctx context.Context, record interface{})
	DeleteAll(ctx context.Context, queriers ...Querier) error
	MustDeleteAll(ctx context.Context, queriers ...Querier)
	Preload(ctx context.Context, records interface{}, field string, queriers ...Querier) error
	MustPreload(ctx context.Context, records interface{}, field string, queriers ...Querier)
	Transaction(ctx context.Context, fn func(Repository) error) error
}

Repository defines sets of available database operations. TODO: support update all.

func New

func New(adapter Adapter) Repository

New create new repo using adapter.

type SelectQuery

type SelectQuery struct {
	OnlyDistinct bool
	Fields       []string
}

SelectQuery defines select clause of the query.

func NewSelect

func NewSelect(fields ...string) SelectQuery

NewSelect query.

func (SelectQuery) Distinct

func (sq SelectQuery) Distinct() SelectQuery

Distinct select query.

type SortQuery

type SortQuery struct {
	Field string
	Sort  int
}

SortQuery defines sort information of query.

func NewSortAsc

func NewSortAsc(field string) SortQuery

NewSortAsc sorts field with ascending sort.

func NewSortDesc

func NewSortDesc(field string) SortQuery

NewSortDesc sorts field with descending sort.

func (SortQuery) Asc

func (sq SortQuery) Asc() bool

Asc returns true if sort is ascending.

func (SortQuery) Build

func (sq SortQuery) Build(query *Query)

Build sort query.

func (SortQuery) Desc

func (sq SortQuery) Desc() bool

Desc returns true if s is descending.

type Structset

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

Structset can be used as modification for repository insert or update operation. This will save every field in struct and it's association as long as it's loaded. This is the default modifier used by repository.

func NewStructset

func NewStructset(record interface{}, skipZero bool) Structset

NewStructset from a struct.

func (Structset) Apply added in v0.2.0

func (s Structset) Apply(doc *Document, mod *Modification)

Apply modification.

type Unscoped added in v0.2.0

type Unscoped bool

Unscoped query.

func (Unscoped) Apply added in v0.2.0

func (u Unscoped) Apply(doc *Document, modification *Modification)

Apply modification.

func (Unscoped) Build added in v0.2.0

func (u Unscoped) Build(query *Query)

Build query.

Directories

Path Synopsis
adapter
mysql
Package mysql wraps mysql driver as an adapter for REL.
Package mysql wraps mysql driver as an adapter for REL.
postgres
Package postgres wraps postgres (pq) driver as an adapter for REL.
Package postgres wraps postgres (pq) driver as an adapter for REL.
specs
Package specs defines test specifications for rel's adapter.
Package specs defines test specifications for rel's adapter.
sql
Package sql is general sql adapter that wraps database/sql.
Package sql is general sql adapter that wraps database/sql.
sqlite3
Package sqlite3 wraps go-sqlite3 driver as an adapter for rel.
Package sqlite3 wraps go-sqlite3 driver as an adapter for rel.
Package group is syntatic sugar for building group query.
Package group is syntatic sugar for building group query.
Package join is syntatic sugar for building join query.
Package join is syntatic sugar for building join query.
Package sort is syntatic sugar for building sort query.
Package sort is syntatic sugar for building sort query.
Package where is syntatic sugar for building where query.
Package where is syntatic sugar for building where query.

Jump to

Keyboard shortcuts

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