dukedb

package module
v0.0.0-...-fbe7d06 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2015 License: MIT Imports: 14 Imported by: 13

README

DukeDB

Databasse abstraction system/ORM for the GO language. The project aims to provide a unified interface to access different underlying database systems.

Warning

This project is still under heavy development. Use with caution.

License

This project is under the MIT License. For Details, see LICENSE.txt

Documentation

Index

Constants

View Source
const (
	RELATION_TYPE_HAS_ONE    = "has_one"
	RELATION_TYPE_HAS_MANY   = "has_many"
	RELATION_TYPE_BELONGS_TO = "belongs_to"
	RELATION_TYPE_M2M        = "m2m"
)
View Source
const (
	HOOK_BEFORE_CREATE = "before_create"
	HOOK_AFTER_CREATE  = "after_create"
	HOOK_BEFORE_UPDATE = "before_update"
	HOOK_AFTER_UPDATE  = "after_update"
	HOOK_BEFORE_DELETE = "before_delete"
	HOOK_AFTER_DELETE  = "after_delete"
)

Variables

View Source
var RELATION_TYPE_MAP map[string]bool = map[string]bool{
	"has_one":    true,
	"has_many":   true,
	"belongs_to": true,
	"m2m":        true,
}

Functions

func CallModelHook

func CallModelHook(b Backend, m interface{}, hook string) apperror.Error

func GetModelCollection

func GetModelCollection(model interface{}) (string, apperror.Error)

func MustGetModelCollection

func MustGetModelCollection(model interface{}) string

func PickRandom

func PickRandom(slice interface{}) interface{}

func PickRandomSubSlice

func PickRandomSubSlice(slice interface{}, count int) interface{}

func Pluralize

func Pluralize(str string) string

func RandomBool

func RandomBool() bool

func RandomInt

func RandomInt(min, max int) int

func RandomTime

func RandomTime(min, max time.Time) time.Time

func SetPointer

func SetPointer(ptr, val interface{})

Set a pointer to a value with reflect. If the value is a pointer to a type, and the the pointer target is not, the value is automatically dereferenced.

func SetSlicePointer

func SetSlicePointer(ptr interface{}, values []interface{})

func StrAfterFirst

func StrAfterFirst(str, separator string) string

func StrAfterLast

func StrAfterLast(str, separator string) string

func StrBeforeFirst

func StrBeforeFirst(str, separator string) string

func StrBeforeLast

func StrBeforeLast(str, separator string) string

func StubText

func StubText(length int) string

func StubTextRandom

func StubTextRandom(min, max int) string

Types

type Attribute

type Attribute struct {
	// Embed field.
	Field
	// contains filtered or unexported fields
}

func BuildAttribute

func BuildAttribute(field *Field) *Attribute

buildAttribute builds up an attribute based on a field.

func (*Attribute) AutoIncrement

func (a *Attribute) AutoIncrement() bool

func (*Attribute) BackendEmbed

func (a *Attribute) BackendEmbed() bool

func (*Attribute) BackendMarshal

func (a *Attribute) BackendMarshal() bool

func (*Attribute) BackendType

func (a *Attribute) BackendType() string

func (*Attribute) BuildFieldExpression

func (a *Attribute) BuildFieldExpression() *FieldExpr

func (*Attribute) DefaultValue

func (a *Attribute) DefaultValue() interface{}

func (*Attribute) IgnoreIfZero

func (a *Attribute) IgnoreIfZero() bool

func (*Attribute) IndexName

func (a *Attribute) IndexName() string

func (*Attribute) IsIndex

func (a *Attribute) IsIndex() bool

func (*Attribute) IsPrimaryKey

func (a *Attribute) IsPrimaryKey() bool

func (*Attribute) IsUnique

func (a *Attribute) IsUnique() bool

func (*Attribute) IsUniqueWith

func (a *Attribute) IsUniqueWith() []string

func (*Attribute) Max

func (a *Attribute) Max() float64

func (*Attribute) Min

func (a *Attribute) Min() float64

func (*Attribute) SetAutoIncrement

func (a *Attribute) SetAutoIncrement(val bool)

func (*Attribute) SetBackendEmbed

func (a *Attribute) SetBackendEmbed(val bool)

func (*Attribute) SetBackendMarshal

func (a *Attribute) SetBackendMarshal(val bool)

func (*Attribute) SetBackendType

func (a *Attribute) SetBackendType(val string)

func (*Attribute) SetDefaultValue

func (a *Attribute) SetDefaultValue(val interface{})

func (*Attribute) SetIgnoreIfZero

func (a *Attribute) SetIgnoreIfZero(val bool)

func (*Attribute) SetIndexName

func (a *Attribute) SetIndexName(val string)

func (*Attribute) SetIsIndex

func (a *Attribute) SetIsIndex(x bool)

func (*Attribute) SetIsPrimaryKey

func (a *Attribute) SetIsPrimaryKey(val bool)

func (*Attribute) SetIsUnique

func (a *Attribute) SetIsUnique(val bool)

func (*Attribute) SetIsUniqueWith

func (a *Attribute) SetIsUniqueWith(val []string)

func (*Attribute) SetMax

func (a *Attribute) SetMax(val float64)

func (*Attribute) SetMin

func (a *Attribute) SetMin(val float64)

type Backend

type Backend interface {
	// Returns the name of the backend.
	Name() string
	SetName(name string)

	// Returns true if the backend uses string Ids like MongoDB.
	HasStringIds() bool

	HasNativeJoins() bool

	// Debug returns true if debugging is enabled.
	Debug() bool

	// SetDebug enables or disables the debug mode.
	// In debug mode, all queries will be logged, and some methods will panic
	// instead of returning an error.
	SetDebug(bool)

	Logger() *logrus.Logger
	SetLogger(*logrus.Logger)

	ProfilingEnabled() bool
	EnableProfiling()
	DisableProfiling()

	// Duplicate the backend.
	Clone() Backend

	// RegisterHook registers a hook function that will be called for a model.
	// The available hooks are: (before/after)_(create/update/delete).
	RegisterHook(hook string, handler HookHandler)

	// GetHooks returns a slice with all hooks of the hook type.
	GetHooks(hook string) []HookHandler

	// Get model info for all registered collections.
	ModelInfos() ModelInfos

	// Get the model info for a collection.
	// Returns nil if not found.
	ModelInfo(collection string) *ModelInfo

	// Retrieve the ModelInfo for a model instance.
	InfoForModel(model interface{}) (*ModelInfo, apperror.Error)

	// Determine if a collection is registered with the backend.
	HasCollection(collection string) bool

	// RegisterModel registers a model type witht the backend.
	//
	// The first argument must be a pointer to an instance of the model,
	// for example: &MyModel{}
	RegisterModel(model interface{}) *ModelInfo

	// Build analyzes the relationships between models and does all neccessary
	// preparations for using the backend.
	//
	// Build MUST be called AFTER all models have been registered with
	// backend.RegisterModel() and BEFORE the backend is used.
	Build()

	// NewModel creates a new model instance of the specified collection.
	NewModel(collection string) (interface{}, apperror.Error)

	// Build a slice of a model for model Collection.
	NewModelSlice(collection string) (interface{}, apperror.Error)

	// ModelToMap converts a model to a map.
	ModelToMap(model interface{}, marshal bool, includeRelations bool) (map[string]interface{}, apperror.Error)

	// Exec executes an expression.
	// The result will be nil for all statements except a SelectStatement.
	Exec(statement Expression) apperror.Error

	ExecQuery(statement FieldedExpression) (result []interface{}, err apperror.Error)

	// Create the specified collection in the backend.
	// (eg the table or the mongo collection)
	CreateCollection(collection ...string) apperror.Error

	// RenameCollection renames a collection to a new name.
	RenameCollection(collection, newName string) apperror.Error
	DropCollection(collection string, ifExists, cascade bool) apperror.Error
	DropAllCollections() apperror.Error

	// CreateField creates the specified field on a collection.
	// Note that the field must already be on the model struct, or an error
	// will be returned.
	//
	// If you need to create arbitrary fields that are not on your model,
	// use Exec() with a CreateFieldStatement.
	CreateField(collection, field string) apperror.Error

	// RenameField renames a collection field.
	// The model must already have the new name, or an error will be returned.
	RenameField(collection, field, newName string) apperror.Error

	// DropField drops a field from a collection.
	// The field must be the backend name.
	DropField(collection, field string) apperror.Error

	// Create an index for fields on a collection.
	// If you need more complex indexes, use Exec() with a CreateIndexStatement.
	CreateIndex(collection, indexName string, unique bool, fields ...string) apperror.Error

	// Drop an index.
	DropIndex(indexName string) apperror.Error

	NewQuery(collection string) (*Query, apperror.Error)
	NewModelQuery(model interface{}) (*Query, apperror.Error)

	// Create a new query.
	//
	// Can be used with different signatures:
	// backend.Q("collection_name") => Get a query for a collection.
	// backend.Q("collection_name", Id) => Get a query for a model in a collection. Id must be numeric or string.
	// backend.Q(&myModel) => Get a query for a model.
	Q(collectionOrModel interface{}, extraModels ...interface{}) *Query

	// Executes a query, fetches ALL results and returns them.
	// If you expect a large number of results, you should use QueryCursor(), which
	// returns an iterable cursor.
	Query(q *Query, targetSlice ...interface{}) ([]interface{}, apperror.Error)

	// Executes a query, and returns a cursor.
	QueryCursor(q *Query) (Cursor, apperror.Error)

	// Perform a query and get the first result.
	QueryOne(q *Query, targetModel ...interface{}) (interface{}, apperror.Error)

	// Perform a query and get the last result.
	Last(q *Query, targetModel ...interface{}) (interface{}, apperror.Error)

	// Find first model with primary key Id.
	FindBy(collection, field string, value interface{}, targetSlice ...interface{}) ([]interface{}, apperror.Error)

	// Find a model in a collection by Id.
	FindOne(collection string, id interface{}, targetModel ...interface{}) (interface{}, apperror.Error)

	// Find a model  in a collection based on a field value.
	FindOneBy(collection, field string, value interface{}, targetModel ...interface{}) (interface{}, apperror.Error)

	// Retrieve the count for a query.
	Count(*Query) (int, apperror.Error)

	// Pluck retrieves all fields specified on a query, and returns them as a
	// map.
	Pluck(q *Query) ([]map[string]interface{}, apperror.Error)

	// Based on a RelationQuery, return a query for the specified
	// relation.
	BuildRelationQuery(q *RelationQuery) (*Query, apperror.Error)

	// Retrieve a query for a relationship.
	Related(model interface{}, name string) (*RelationQuery, apperror.Error)

	// Return a M2MCollection instance for a model, which allows
	// to add/remove/clear items in the m2m relationship.
	M2M(model interface{}, name string) (M2MCollection, apperror.Error)

	// Create creates the model in the backend.
	Create(model ...interface{}) apperror.Error

	CreateByMap(collection string, data map[string]interface{}) (result interface{}, err apperror.Error)

	// Update a model.
	Update(model interface{}) apperror.Error

	// Save is a convenience method that created the passed model if it
	// is new, or updates it otherwise.
	Save(model interface{}) apperror.Error

	// Updat all models matching a query by values in a map.
	UpdateByMap(query *Query, data map[string]interface{}) apperror.Error

	// Delete deletes the model from the backend.
	Delete(model interface{}) apperror.Error

	// DeleteQ deletes all models that match the passed query.
	DeleteMany(*Query) apperror.Error
}

type BaseBackend

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

func NewBaseBackend

func NewBaseBackend(backend Backend) BaseBackend

func (*BaseBackend) Build

func (b *BaseBackend) Build()

func (*BaseBackend) BuildJoins

func (b *BaseBackend) BuildJoins(info *ModelInfo, q *Query) apperror.Error

func (*BaseBackend) BuildLogger

func (b *BaseBackend) BuildLogger()

func (*BaseBackend) BuildRelationQuery

func (b *BaseBackend) BuildRelationQuery(q *RelationQuery) (*Query, apperror.Error)

func (*BaseBackend) Clone

func (b *BaseBackend) Clone() *BaseBackend

func (*BaseBackend) Count

func (b *BaseBackend) Count(q *Query) (int, apperror.Error)

func (*BaseBackend) Create

func (b *BaseBackend) Create(models ...interface{}) apperror.Error

func (*BaseBackend) CreateByMap

func (b *BaseBackend) CreateByMap(collection string, data map[string]interface{}) (interface{}, apperror.Error)

func (*BaseBackend) CreateCollection

func (b *BaseBackend) CreateCollection(collections ...string) apperror.Error

func (*BaseBackend) CreateField

func (b *BaseBackend) CreateField(collection, fieldName string) apperror.Error

func (*BaseBackend) CreateIndex

func (b *BaseBackend) CreateIndex(collection, indexName string, unique bool, fields ...string) apperror.Error

func (*BaseBackend) Debug

func (b *BaseBackend) Debug() bool

func (*BaseBackend) Delete

func (b *BaseBackend) Delete(model interface{}) apperror.Error

func (*BaseBackend) DeleteMany

func (b *BaseBackend) DeleteMany(query *Query) apperror.Error

func (*BaseBackend) DisableProfiling

func (b *BaseBackend) DisableProfiling()

func (*BaseBackend) DoJoin

func (b *BaseBackend) DoJoin(baseInfo *ModelInfo, objs []interface{}, joinQ *RelationQuery) apperror.Error

func (*BaseBackend) DoJoins

func (b *BaseBackend) DoJoins(baseInfo *ModelInfo, q *Query, models []interface{}) apperror.Error

func (*BaseBackend) DropAllCollections

func (b *BaseBackend) DropAllCollections() apperror.Error

func (*BaseBackend) DropCollection

func (b *BaseBackend) DropCollection(collection string, ifExists, cascade bool) apperror.Error

func (*BaseBackend) DropField

func (b *BaseBackend) DropField(collection, field string) apperror.Error

func (*BaseBackend) DropIndex

func (b *BaseBackend) DropIndex(indexName string) apperror.Error

func (*BaseBackend) EnableProfiling

func (b *BaseBackend) EnableProfiling()

func (*BaseBackend) FindBy

func (b *BaseBackend) FindBy(collection, field string, value interface{}, targetSlice ...interface{}) ([]interface{}, apperror.Error)

func (*BaseBackend) FindOne

func (b *BaseBackend) FindOne(collection string, id interface{}, targetModel ...interface{}) (interface{}, apperror.Error)

func (*BaseBackend) FindOneBy

func (b *BaseBackend) FindOneBy(collection, field string, value interface{}, targetModel ...interface{}) (interface{}, apperror.Error)

func (*BaseBackend) GetHooks

func (b *BaseBackend) GetHooks(hook string) []HookHandler

func (*BaseBackend) HasCollection

func (b *BaseBackend) HasCollection(collection string) bool

func (*BaseBackend) HasNativeJoins

func (b *BaseBackend) HasNativeJoins() bool

func (*BaseBackend) InfoForModel

func (b *BaseBackend) InfoForModel(model interface{}) (*ModelInfo, apperror.Error)

func (*BaseBackend) Last

func (b *BaseBackend) Last(q *Query, targetModels ...interface{}) (interface{}, apperror.Error)

func (*BaseBackend) Logger

func (b *BaseBackend) Logger() *logrus.Logger

func (*BaseBackend) M2M

func (b *BaseBackend) M2M(model interface{}, name string) (M2MCollection, apperror.Error)

func (*BaseBackend) ModelInfo

func (b *BaseBackend) ModelInfo(collection string) *ModelInfo

func (*BaseBackend) ModelInfos

func (b *BaseBackend) ModelInfos() ModelInfos

func (*BaseBackend) ModelToMap

func (b *BaseBackend) ModelToMap(model interface{}, marshal bool, includeRelations bool) (map[string]interface{}, apperror.Error)

func (*BaseBackend) Name

func (b *BaseBackend) Name() string

func (*BaseBackend) NewModel

func (b *BaseBackend) NewModel(collection string) (interface{}, apperror.Error)

func (*BaseBackend) NewModelQuery

func (b *BaseBackend) NewModelQuery(model interface{}) (*Query, apperror.Error)

func (*BaseBackend) NewModelSlice

func (b *BaseBackend) NewModelSlice(collection string) (interface{}, apperror.Error)

func (*BaseBackend) NewQuery

func (b *BaseBackend) NewQuery(collection string) (*Query, apperror.Error)

func (*BaseBackend) PersistRelations

func (b *BaseBackend) PersistRelations(action string, beforePersist bool, info *ModelInfo, model interface{}) apperror.Error

action may be either "create", "update" or "delete"

func (*BaseBackend) Pluck

func (b *BaseBackend) Pluck(q *Query) ([]map[string]interface{}, apperror.Error)

func (*BaseBackend) ProfilingEnabled

func (b *BaseBackend) ProfilingEnabled() bool

func (*BaseBackend) Q

func (b *BaseBackend) Q(arg interface{}, args ...interface{}) *Query

func (*BaseBackend) Query

func (b *BaseBackend) Query(q *Query, targetSlice ...interface{}) ([]interface{}, apperror.Error)

func (*BaseBackend) QueryCursor

func (b *BaseBackend) QueryCursor(q *Query) (Cursor, apperror.Error)

func (*BaseBackend) QueryOne

func (b *BaseBackend) QueryOne(q *Query, targetModels ...interface{}) (interface{}, apperror.Error)

func (*BaseBackend) RegisterHook

func (b *BaseBackend) RegisterHook(hook string, handler HookHandler)

func (*BaseBackend) RegisterModel

func (b *BaseBackend) RegisterModel(model interface{}) *ModelInfo

func (*BaseBackend) Related

func (b *BaseBackend) Related(model interface{}, name string) (*RelationQuery, apperror.Error)

func (*BaseBackend) RenameCollection

func (b *BaseBackend) RenameCollection(collection, newName string) apperror.Error

func (*BaseBackend) RenameField

func (b *BaseBackend) RenameField(collection, field, newName string) apperror.Error

func (*BaseBackend) Save

func (b *BaseBackend) Save(model interface{}) apperror.Error

func (*BaseBackend) SetDebug

func (b *BaseBackend) SetDebug(x bool)

func (*BaseBackend) SetLogger

func (b *BaseBackend) SetLogger(x *logrus.Logger)

func (*BaseBackend) SetName

func (b *BaseBackend) SetName(name string)

func (*BaseBackend) Update

func (b *BaseBackend) Update(model interface{}) apperror.Error

func (*BaseBackend) UpdateByMap

func (b *BaseBackend) UpdateByMap(query *Query, data map[string]interface{}) apperror.Error

type BaseMigrationAttempt

type BaseMigrationAttempt struct {
	Version    int
	StartedAt  time.Time
	FinishedAt time.Time
	Complete   bool
}

func (BaseMigrationAttempt) Collection

func (m BaseMigrationAttempt) Collection() string

func (*BaseMigrationAttempt) GetComplete

func (a *BaseMigrationAttempt) GetComplete() bool

func (*BaseMigrationAttempt) GetFinishedAt

func (a *BaseMigrationAttempt) GetFinishedAt() time.Time

func (*BaseMigrationAttempt) GetStartedAt

func (a *BaseMigrationAttempt) GetStartedAt() time.Time

func (*BaseMigrationAttempt) GetVersion

func (a *BaseMigrationAttempt) GetVersion() int

func (*BaseMigrationAttempt) SetComplete

func (a *BaseMigrationAttempt) SetComplete(x bool)

func (*BaseMigrationAttempt) SetFinishedAt

func (a *BaseMigrationAttempt) SetFinishedAt(x time.Time)

func (*BaseMigrationAttempt) SetStartedAt

func (a *BaseMigrationAttempt) SetStartedAt(x time.Time)

func (*BaseMigrationAttempt) SetVersion

func (a *BaseMigrationAttempt) SetVersion(x int)

type BaseMigrationAttemptIntId

type BaseMigrationAttemptIntId struct {
	BaseMigrationAttempt
	Id uint64
}

func (*BaseMigrationAttemptIntId) GetId

func (a *BaseMigrationAttemptIntId) GetId() string

func (*BaseMigrationAttemptIntId) SetId

type Cursor

type Cursor interface {
	// Count returns the total number of items.
	Count() int

	// HasNext returns true if a next item exists.
	HasNext() bool

	// Retrieve the next result item.
	//
	// As with other query methods, you may pass a pointer to the model
	// that should be filled with the data.
	Next(targetModel ...interface{}) (interface{}, apperror.Error)
}

type DefaultM2MCollection

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

func (*DefaultM2MCollection) Add

func (c *DefaultM2MCollection) Add(models ...interface{}) apperror.Error

func (*DefaultM2MCollection) All

func (c *DefaultM2MCollection) All() ([]interface{}, apperror.Error)

func (*DefaultM2MCollection) Clear

func (c *DefaultM2MCollection) Clear() apperror.Error

func (*DefaultM2MCollection) Contains

func (c *DefaultM2MCollection) Contains(model interface{}) (bool, apperror.Error)

func (*DefaultM2MCollection) ContainsId

func (c *DefaultM2MCollection) ContainsId(id interface{}) (bool, apperror.Error)

func (*DefaultM2MCollection) Count

func (c *DefaultM2MCollection) Count() (int, apperror.Error)

func (*DefaultM2MCollection) Q

func (c *DefaultM2MCollection) Q() *Query

func (*DefaultM2MCollection) Remove

func (c *DefaultM2MCollection) Remove(models ...interface{}) apperror.Error

func (*DefaultM2MCollection) Replace

func (c *DefaultM2MCollection) Replace(models ...interface{}) apperror.Error

type Field

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

func (*Field) BackendName

func (f *Field) BackendName() string

func (*Field) EmbeddingStructName

func (f *Field) EmbeddingStructName() string

func (*Field) IsRequired

func (f *Field) IsRequired() bool

func (*Field) MarshalName

func (f *Field) MarshalName() string

func (*Field) Name

func (f *Field) Name() string

func (*Field) SetBackendName

func (f *Field) SetBackendName(val string)

func (*Field) SetEmbeddingStructName

func (f *Field) SetEmbeddingStructName(val string)

func (*Field) SetIsRequired

func (f *Field) SetIsRequired(val bool)

func (*Field) SetMarshalName

func (f *Field) SetMarshalName(val string)

func (*Field) SetName

func (f *Field) SetName(val string)

func (*Field) SetStructName

func (f *Field) SetStructName(val string)

func (*Field) SetStructType

func (f *Field) SetStructType(x reflect.Type)

func (*Field) SetType

func (f *Field) SetType(val reflect.Type)

func (*Field) StructName

func (f *Field) StructName() string

func (*Field) StructType

func (f *Field) StructType() reflect.Type

func (*Field) Type

func (f *Field) Type() reflect.Type

type HookHandler

type HookHandler func(backend Backend, obj interface{}) apperror.Error

type IntIdModel

type IntIdModel struct {
	Id uint64
}

Base model with a integer Id.

func (*IntIdModel) GetId

func (m *IntIdModel) GetId() interface{}

func (*IntIdModel) GetStrId

func (m *IntIdModel) GetStrId() string

func (*IntIdModel) SetId

func (m *IntIdModel) SetId(id interface{}) error

func (*IntIdModel) SetStrId

func (m *IntIdModel) SetStrId(rawId string) error

type JoinAssigner

type JoinAssigner func(relation *Relation, joinQ *RelationQuery, resultQuery *Query, objs, joinedModels []interface{})

type M2MCollection

type M2MCollection interface {
	Add(models ...interface{}) apperror.Error
	Remove(models ...interface{}) apperror.Error
	Clear() apperror.Error
	Replace(models ...interface{}) apperror.Error

	Count() (int, apperror.Error)
	Contains(model interface{}) (bool, apperror.Error)
	ContainsId(id interface{}) (bool, apperror.Error)
	All() ([]interface{}, apperror.Error)

	Q() *Query
}

func NewM2MCollection

func NewM2MCollection(backend Backend, relation *Relation, model interface{}) (M2MCollection, apperror.Error)

type Migration

type Migration struct {
	Version     int
	Name        string
	Description string
	SkipOnNew   bool // Determines if this migration can be skipped if setting up a new database.

	// WrapTransaction specifies if the whole migration should be wrapped in a transaction.
	WrapTransaction bool
	Up              func(MigrationBackend) error
	Down            func(MigrationBackend) error
}

type MigrationAttempt

type MigrationAttempt interface {
	GetVersion() int
	SetVersion(int)

	GetStartedAt() time.Time
	SetStartedAt(time.Time)

	GetFinishedAt() time.Time
	SetFinishedAt(time.Time)

	GetComplete() bool
	SetComplete(bool)
}

type MigrationBackend

type MigrationBackend interface {
	Backend
	GetMigrationHandler() *MigrationHandler

	MigrationsSetup() apperror.Error

	IsMigrationLocked() (bool, apperror.Error)
	DetermineMigrationVersion() (int, apperror.Error)

	NewMigrationAttempt() MigrationAttempt
}

type MigrationHandler

type MigrationHandler struct {
	Backend MigrationBackend
	// contains filtered or unexported fields
}

func NewMigrationHandler

func NewMigrationHandler(backend Backend) *MigrationHandler

func (*MigrationHandler) Add

func (m *MigrationHandler) Add(migrations ...Migration)

func (*MigrationHandler) Get

func (m *MigrationHandler) Get(version int) *Migration

func (*MigrationHandler) HasMigration

func (m *MigrationHandler) HasMigration(version int) bool

func (*MigrationHandler) Migrate

func (m *MigrationHandler) Migrate(force bool) apperror.Error

func (*MigrationHandler) MigrateTo

func (m *MigrationHandler) MigrateTo(targetVersion int, force bool) apperror.Error

func (*MigrationHandler) RunMigration

func (handler *MigrationHandler) RunMigration(m *Migration) apperror.Error

type ModelAfterCreateHook

type ModelAfterCreateHook interface {
	AfterCreate(Backend)
}

type ModelAfterDeleteHook

type ModelAfterDeleteHook interface {
	AfterDelete(Backend)
}

type ModelAfterQueryHook

type ModelAfterQueryHook interface {
	AfterQuery(Backend)
}

type ModelAfterUpdateHook

type ModelAfterUpdateHook interface {
	AfterUpdate(Backend)
}

type ModelBackendNameHook

type ModelBackendNameHook interface {
	BackendName() string
}

type ModelBeforeCreateHook

type ModelBeforeCreateHook interface {
	BeforeCreate(Backend) error
}

type ModelBeforeDeleteHook

type ModelBeforeDeleteHook interface {
	BeforeDelete(Backend) error
}

type ModelBeforeUpdateHook

type ModelBeforeUpdateHook interface {
	BeforeUpdate(Backend) error
}

type ModelCollectionHook

type ModelCollectionHook interface {
	Collection() string
}

type ModelIdGetterHook

type ModelIdGetterHook interface {
	GetId() interface{}
}

type ModelIdSetterHook

type ModelIdSetterHook interface {
	SetId(id interface{}) error
}

type ModelInfo

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

func BuildModelInfo

func BuildModelInfo(model interface{}) (*ModelInfo, apperror.Error)

Builds the ModelInfo for a model and returns it.

func (*ModelInfo) AddAttribute

func (m *ModelInfo) AddAttribute(attr *Attribute)

func (*ModelInfo) AddRelation

func (m *ModelInfo) AddRelation(rel *Relation)

func (*ModelInfo) Attribute

func (m *ModelInfo) Attribute(name string) *Attribute

func (*ModelInfo) Attributes

func (m *ModelInfo) Attributes() map[string]*Attribute

func (*ModelInfo) BackendName

func (m *ModelInfo) BackendName() string

func (*ModelInfo) BuildCreateStmt

func (info *ModelInfo) BuildCreateStmt(withReferences bool) *CreateCollectionStmt

func (*ModelInfo) Collection

func (m *ModelInfo) Collection() string

func (*ModelInfo) DetermineModelId

func (info *ModelInfo) DetermineModelId(model interface{}) (interface{}, apperror.Error)

func (*ModelInfo) DetermineModelStrId

func (info *ModelInfo) DetermineModelStrId(model interface{}) (string, apperror.Error)

Determine the Id for a model and convert it to string.

func (*ModelInfo) FindAttribute

func (m *ModelInfo) FindAttribute(name string) *Attribute

FindField tries to find a field by checking its Name, BackendName and MarshalName.

func (*ModelInfo) FindRelation

func (m *ModelInfo) FindRelation(name string) *Relation

func (*ModelInfo) FullStructName

func (m *ModelInfo) FullStructName() string

func (*ModelInfo) HasAttribute

func (m *ModelInfo) HasAttribute(name string) bool

func (*ModelInfo) HasRelation

func (m *ModelInfo) HasRelation(name string) bool

func (*ModelInfo) HasStruct

func (m *ModelInfo) HasStruct() bool

func (*ModelInfo) Item

func (m *ModelInfo) Item() interface{}

func (*ModelInfo) MarshalName

func (m *ModelInfo) MarshalName() string

func (*ModelInfo) ModelDeleteStmt

func (info *ModelInfo) ModelDeleteStmt(model interface{}) *DeleteStmt

func (*ModelInfo) ModelFilter

func (info *ModelInfo) ModelFilter(model interface{}) Expression

func (*ModelInfo) ModelFromMap

func (info *ModelInfo) ModelFromMap(data map[string]interface{}) (interface{}, apperror.Error)

ModelFromMap creates a new model, fills it with data from the map, and returns a pointer to the new model struct. Data may contain the struct field names, the backend names or the marshal names as keys.

func (*ModelInfo) ModelHasId

func (info *ModelInfo) ModelHasId(model interface{}) (bool, apperror.Error)

func (*ModelInfo) ModelSelect

func (info *ModelInfo) ModelSelect(model interface{}) *SelectStmt

func (*ModelInfo) ModelToFieldExpressions

func (info *ModelInfo) ModelToFieldExpressions(model interface{}) ([]*FieldValueExpr, apperror.Error)

func (*ModelInfo) ModelToMap

func (info *ModelInfo) ModelToMap(model interface{}, forBackend, marshal bool, includeRelations bool) (map[string]interface{}, apperror.Error)

func (*ModelInfo) MustDetermineModelId

func (info *ModelInfo) MustDetermineModelId(model interface{}) interface{}

func (*ModelInfo) MustDetermineModelStrId

func (info *ModelInfo) MustDetermineModelStrId(model interface{}) string

Determine the Id for a model and convert it to string. Panics on error.

func (*ModelInfo) New

func (m *ModelInfo) New() interface{}

func (*ModelInfo) NewReflector

func (m *ModelInfo) NewReflector() *reflector.StructReflector

func (*ModelInfo) NewSlice

func (m *ModelInfo) NewSlice() *reflector.SliceReflector

func (*ModelInfo) PkAttribute

func (m *ModelInfo) PkAttribute() *Attribute

func (*ModelInfo) Reflector

func (m *ModelInfo) Reflector() *reflector.StructReflector

func (*ModelInfo) Relation

func (m *ModelInfo) Relation(name string) *Relation

func (*ModelInfo) Relations

func (m *ModelInfo) Relations() map[string]*Relation

*

  • Relations.

func (*ModelInfo) SetAttributes

func (m *ModelInfo) SetAttributes(attrs map[string]*Attribute)

func (*ModelInfo) SetBackendName

func (m *ModelInfo) SetBackendName(val string)

func (*ModelInfo) SetCollection

func (m *ModelInfo) SetCollection(val string)

func (*ModelInfo) SetFullStructName

func (m *ModelInfo) SetFullStructName(val string)

func (*ModelInfo) SetItem

func (m *ModelInfo) SetItem(val interface{})

func (*ModelInfo) SetMarshalName

func (m *ModelInfo) SetMarshalName(val string)

func (*ModelInfo) SetModelId

func (info *ModelInfo) SetModelId(model, id interface{}) apperror.Error

func (*ModelInfo) SetRelations

func (m *ModelInfo) SetRelations(rels map[string]*Relation)

func (*ModelInfo) SetStructName

func (m *ModelInfo) SetStructName(val string)

func (*ModelInfo) SetTransientFields

func (m *ModelInfo) SetTransientFields(x map[string]*Field)

func (*ModelInfo) StructName

func (m *ModelInfo) StructName() string

func (*ModelInfo) TransientFields

func (m *ModelInfo) TransientFields() map[string]*Field

func (*ModelInfo) UpdateModelFromData

func (info *ModelInfo) UpdateModelFromData(model interface{}, data map[string]interface{}) apperror.Error

func (*ModelInfo) ValidateModel

func (info *ModelInfo) ValidateModel(m interface{}) apperror.Error

type ModelInfos

type ModelInfos map[string]*ModelInfo

func (ModelInfos) Add

func (i ModelInfos) Add(info *ModelInfo)

func (ModelInfos) AnalyzeRelations

func (m ModelInfos) AnalyzeRelations() apperror.Error

func (ModelInfos) Find

func (i ModelInfos) Find(name string) *ModelInfo

func (ModelInfos) Get

func (i ModelInfos) Get(collection string) *ModelInfo

func (ModelInfos) Has

func (i ModelInfos) Has(collection string) bool

type ModelMarshalNameHook

type ModelMarshalNameHook interface {
	MarshalName() string
}

type ModelStrIdGetterHook

type ModelStrIdGetterHook interface {
	GetStrId() string
}

type ModelStrIdSetterHook

type ModelStrIdSetterHook interface {
	SetStrId(id string) error
}

type ModelValidateHook

type ModelValidateHook interface {
	Validate() error
}

type Point

type Point struct {
	Lat float32
	Lon float32
}

type Query

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

func NewQuery

func NewQuery(collection string, backend Backend) *Query

func ParseJsonQuery

func ParseJsonQuery(backend Backend, js []byte) (*Query, apperror.Error)

func ParseQuery

func ParseQuery(backend Backend, data map[string]interface{}) (*Query, apperror.Error)

Build a database query based a map[string]interface{} data structure resembling a Mongo query.

It returns a Query equal to the Mongo query, with unsupported features omitted. An error is returned if the building of the query fails.

Format: {
  // Order by field:
  order: "field",
 //  Order descending:
 order: "-field",

 // Joins:
 joins: ["myJoin", "my.nestedJoin"],

 // Filters:
 Filters conform to the mongo query syntax.
 See http://docs.mongodb.org/manual/reference/operator/query/.
 filters: {
 	id: "22",
   weight: {$gt: 222},
   type: {$in: ["type1", "type2"]}
 },

 // Limiting:
 limit: 100,

 // Offset:
 offset: 20
}

func (*Query) And

func (q *Query) And(field string, val interface{}) *Query

func (*Query) AndCond

func (q *Query) AndCond(field, condition string, val interface{}) *Query

func (*Query) AndExpr

func (q *Query) AndExpr(filters ...Expression) *Query

func (*Query) Count

func (q *Query) Count() (int, apperror.Error)

func (*Query) Delete

func (q *Query) Delete() apperror.Error

func (*Query) Field

func (q *Query) Field(fields ...string) *Query

func (*Query) FieldExpr

func (q *Query) FieldExpr(exprs ...Expression) *Query

func (*Query) Filter

func (q *Query) Filter(field string, val interface{}) *Query

func (*Query) FilterCond

func (q *Query) FilterCond(field string, condition string, val interface{}) *Query

func (*Query) FilterExpr

func (q *Query) FilterExpr(expressions ...Expression) *Query

func (*Query) Find

func (q *Query) Find(targetSlice ...interface{}) ([]interface{}, apperror.Error)

func (*Query) First

func (q *Query) First(targetModel ...interface{}) (interface{}, apperror.Error)

func (*Query) GetBackend

func (q *Query) GetBackend() Backend

func (*Query) GetCollection

func (q *Query) GetCollection() string

func (*Query) GetJoin

func (q *Query) GetJoin(relationName string) *RelationQuery

Retrieve a join query for the specified field. Returns a *RelationQuery, or nil if not found. Supports nested Joins like 'Parent.Tags'.

func (*Query) GetJoinResultAssigner

func (q *Query) GetJoinResultAssigner() JoinAssigner

func (*Query) GetJoins

func (q *Query) GetJoins() map[string]*RelationQuery

func (*Query) GetLimit

func (q *Query) GetLimit() int

func (*Query) GetModels

func (q *Query) GetModels() []interface{}

func (*Query) GetName

func (q *Query) GetName() string

func (*Query) GetOffset

func (q *Query) GetOffset() int

func (*Query) GetStatement

func (q *Query) GetStatement() *SelectStmt

func (*Query) HasJoin

func (q *Query) HasJoin(relationName string) bool

func (*Query) Join

func (q *Query) Join(relationName string, joinType ...string) *Query

func (*Query) JoinQ

func (q *Query) JoinQ(jqs ...*RelationQuery) *Query

func (*Query) Last

func (q *Query) Last(targetModel ...interface{}) (interface{}, apperror.Error)

func (*Query) Limit

func (q *Query) Limit(limit int) *Query

func (*Query) Name

func (q *Query) Name(x string) *Query

func (*Query) Normalize

func (q *Query) Normalize() apperror.Error

func (*Query) Not

func (q *Query) Not(field string, val interface{}) *Query

func (*Query) NotCond

func (q *Query) NotCond(field string, condition string, val interface{}) *Query

func (*Query) NotExpr

func (q *Query) NotExpr(filters ...Expression) *Query

func (*Query) Offset

func (q *Query) Offset(offset int) *Query

func (*Query) Or

func (q *Query) Or(field string, val interface{}) *Query

func (*Query) OrCond

func (q *Query) OrCond(field string, condition string, val interface{}) *Query

func (*Query) OrExpr

func (q *Query) OrExpr(filters ...Expression) *Query

func (*Query) Pluck

func (q *Query) Pluck() ([]map[string]interface{}, apperror.Error)

func (*Query) Related

func (q *Query) Related(name string) *RelationQuery

func (*Query) SetBackend

func (q *Query) SetBackend(x Backend)

func (*Query) SetCollection

func (q *Query) SetCollection(collection string)

func (*Query) SetFieldExpressions

func (q *Query) SetFieldExpressions(expressions []Expression) *Query

func (*Query) SetFields

func (q *Query) SetFields(fields []string) *Query

func (*Query) SetFilters

func (q *Query) SetFilters(expressions ...Expression) *Query

func (*Query) SetJoinResultAssigner

func (q *Query) SetJoinResultAssigner(x JoinAssigner)

func (*Query) SetModels

func (q *Query) SetModels(x []interface{})

func (*Query) SetName

func (q *Query) SetName(name string)

func (*Query) SetSorts

func (q *Query) SetSorts(exprs []*SortExpr) *Query

func (*Query) SetStatement

func (q *Query) SetStatement(stmt *SelectStmt)

func (*Query) Sort

func (q *Query) Sort(field string, asc bool) *Query

func (*Query) SortExpr

func (q *Query) SortExpr(expr *SortExpr) *Query

type QueryStat

type QueryStat struct {
	Name        string
	Started     time.Time
	Finished    time.Time
	Total       time.Duration
	Normalizing time.Duration
	Joining     time.Duration
	Execution   time.Duration
	ModelBuild  time.Duration
}

type Relation

type Relation struct {
	// Embed field.
	Field
	// contains filtered or unexported fields
}

func BuildRelation

func BuildRelation(field *Field) *Relation

buildRelation builds up a relation based on a field.

func (*Relation) AutoCreate

func (r *Relation) AutoCreate() bool

func (*Relation) AutoDelete

func (r *Relation) AutoDelete() bool

func (*Relation) AutoUpdate

func (r *Relation) AutoUpdate() bool

func (*Relation) ForeignField

func (r *Relation) ForeignField() string

func (*Relation) InversingField

func (r *Relation) InversingField() string

func (*Relation) IsMany

func (f *Relation) IsMany() bool

func (*Relation) LocalField

func (r *Relation) LocalField() string

func (*Relation) Model

func (r *Relation) Model() *ModelInfo

func (*Relation) RelatedModel

func (r *Relation) RelatedModel() *ModelInfo

func (*Relation) RelationType

func (r *Relation) RelationType() string

func (*Relation) SetAutoCreate

func (r *Relation) SetAutoCreate(val bool)

func (*Relation) SetAutoDelete

func (r *Relation) SetAutoDelete(val bool)

func (*Relation) SetAutoUpdate

func (r *Relation) SetAutoUpdate(val bool)

func (*Relation) SetForeignField

func (r *Relation) SetForeignField(val string)

func (*Relation) SetInversingField

func (r *Relation) SetInversingField(val string)

func (*Relation) SetLocalField

func (r *Relation) SetLocalField(val string)

func (*Relation) SetModel

func (r *Relation) SetModel(val *ModelInfo)

func (*Relation) SetRelatedModel

func (r *Relation) SetRelatedModel(val *ModelInfo)

func (*Relation) SetRelationType

func (r *Relation) SetRelationType(val string)

type RelationQuery

type RelationQuery struct {
	Query
	// contains filtered or unexported fields
}

func RelQ

func RelQ(q *Query, relationName string, collection string, joinType string) *RelationQuery

func RelQCustom

func RelQCustom(q *Query, collection, joinKey, foreignKey, typ string) *RelationQuery

Create a new relation query that connects two collections via two of their fields. These will normally be the respective primary keys.

func RelQExpr

func RelQExpr(q *Query, collection, typ string, joinCondition Expression) *RelationQuery

RelQExpr creates a new relation query for a collection with an arbitrary join condition.

func (*RelationQuery) And

func (q *RelationQuery) And(field string, val interface{}) *RelationQuery

func (*RelationQuery) AndCond

func (q *RelationQuery) AndCond(field, condition string, val interface{}) *RelationQuery

func (*RelationQuery) AndExpr

func (q *RelationQuery) AndExpr(filters ...Expression) *RelationQuery

func (*RelationQuery) Build

func (q *RelationQuery) Build() (*Query, apperror.Error)

func (*RelationQuery) Count

func (q *RelationQuery) Count() (int, apperror.Error)

func (*RelationQuery) Delete

func (q *RelationQuery) Delete() apperror.Error

func (*RelationQuery) Field

func (q *RelationQuery) Field(fields ...string) *RelationQuery

func (*RelationQuery) FieldExpr

func (q *RelationQuery) FieldExpr(exprs ...Expression) *RelationQuery

func (*RelationQuery) Filter

func (q *RelationQuery) Filter(field string, val interface{}) *RelationQuery

func (*RelationQuery) FilterCond

func (q *RelationQuery) FilterCond(field string, condition string, val interface{}) *RelationQuery

func (*RelationQuery) FilterExpr

func (q *RelationQuery) FilterExpr(expressions ...Expression) *RelationQuery

func (*RelationQuery) Find

func (q *RelationQuery) Find(targetSlice ...interface{}) ([]interface{}, apperror.Error)

func (*RelationQuery) First

func (q *RelationQuery) First(targetModel ...interface{}) (interface{}, apperror.Error)

func (*RelationQuery) GetBaseQuery

func (q *RelationQuery) GetBaseQuery() *Query

func (*RelationQuery) GetJoinType

func (q *RelationQuery) GetJoinType() string

func (*RelationQuery) GetRelationName

func (q *RelationQuery) GetRelationName() string

func (*RelationQuery) GetStatement

func (q *RelationQuery) GetStatement() *JoinStmt

func (*RelationQuery) Join

func (q *RelationQuery) Join(fieldName string, joinType ...string) *RelationQuery

func (*RelationQuery) JoinQ

func (q *RelationQuery) JoinQ(jqs ...*RelationQuery) *RelationQuery

func (*RelationQuery) Last

func (q *RelationQuery) Last(targetModel ...interface{}) (interface{}, apperror.Error)

func (*RelationQuery) Limit

func (q *RelationQuery) Limit(l int) *RelationQuery

func (*RelationQuery) Not

func (q *RelationQuery) Not(field string, val interface{}) *RelationQuery

func (*RelationQuery) NotCond

func (q *RelationQuery) NotCond(field string, condition string, val interface{}) *RelationQuery

func (*RelationQuery) NotExpr

func (q *RelationQuery) NotExpr(filters ...Expression) *RelationQuery

func (*RelationQuery) Offset

func (q *RelationQuery) Offset(o int) *RelationQuery

func (*RelationQuery) Or

func (q *RelationQuery) Or(field string, val interface{}) *RelationQuery

func (*RelationQuery) OrCond

func (q *RelationQuery) OrCond(field string, condition string, val interface{}) *RelationQuery

func (*RelationQuery) OrExpr

func (q *RelationQuery) OrExpr(filters ...Expression) *RelationQuery

func (*RelationQuery) SetBaseQuery

func (q *RelationQuery) SetBaseQuery(bq *Query)

func (*RelationQuery) SetFieldExpressions

func (q *RelationQuery) SetFieldExpressions(expressions []Expression) *RelationQuery

func (*RelationQuery) SetFields

func (q *RelationQuery) SetFields(fields []string) *RelationQuery

func (*RelationQuery) SetFilters

func (q *RelationQuery) SetFilters(expressions ...Expression) *RelationQuery

func (*RelationQuery) SetJoinType

func (q *RelationQuery) SetJoinType(typ string) *RelationQuery

func (*RelationQuery) SetRelationName

func (q *RelationQuery) SetRelationName(name string)

func (*RelationQuery) SetSorts

func (q *RelationQuery) SetSorts(exprs []*SortExpr) *RelationQuery

func (*RelationQuery) Sort

func (q *RelationQuery) Sort(name string, asc bool) *RelationQuery

func (*RelationQuery) SortExpr

func (q *RelationQuery) SortExpr(expr *SortExpr) *RelationQuery

type StrIdModel

type StrIdModel struct {
	Id string
}

Base model with a string Id.

func (*StrIdModel) GetId

func (m *StrIdModel) GetId() interface{}

func (*StrIdModel) GetStrId

func (m *StrIdModel) GetStrId() string

func (*StrIdModel) SetId

func (m *StrIdModel) SetId(id interface{}) error

func (*StrIdModel) SetStrId

func (m *StrIdModel) SetStrId(rawId string) error

type TimeStampedModel

type TimeStampedModel struct {
	CreatedAt time.Time `db:"required"`
	UpdatedAt time.Time `db:"required"`
}

func (*TimeStampedModel) BeforeCreate

func (m *TimeStampedModel) BeforeCreate(b Backend) error

func (*TimeStampedModel) BeforeUpdate

func (m *TimeStampedModel) BeforeUpdate(b Backend) error

type Transaction

type Transaction interface {
	Backend
	Rollback() apperror.Error
	Commit() apperror.Error
}

type TransactionBackend

type TransactionBackend interface {
	Backend
	Begin() (Transaction, apperror.Error)
	MustBegin() Transaction
}

Directories

Path Synopsis
backends
sql
models

Jump to

Keyboard shortcuts

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