Back to godoc.org
github.com/geoffreybauduin/yaorm

Package yaorm

v1.2.1
Latest Go to latest

The latest major version is .

Published: Sep 29, 2020 | License: MIT | Module: github.com/geoffreybauduin/yaorm

Index

Constants

const (
	DatabaseCapacitySchema = iota ^ 42
	DatabaseCapacityUUID
)

Variables

var (
	// ErrTableEmpty is returned when a registered model has no exported fields
	ErrTableEmpty = errors.Errorf("Table is empty")
	// ErrTableNotFound is returned when a table cannot be found
	ErrTableNotFound = errors.NotFoundf("Table")
	// ErrDbNotFound is returned when a database cannot be found
	ErrDbNotFound = errors.NotFoundf("Database")
)
var (
	ErrDatabaseConflict = errors.Errorf("Database name conflicts with existing")
)

func GenericCount

func GenericCount(dbp DBProvider, filter yaormfilter.Filter) (uint64, error)

GenericCount selects counts the row in the database panics if filter or dbp is nil

func GenericDelete

func GenericDelete(m Model) (int64, error)

GenericDelete will delete the provided model from its database Returns the number of rows deleted

func GenericInsert

func GenericInsert(m Model) error

GenericInsert inserts the provided model in the database panics if model is nil or not linked to dbp

func GenericSave

func GenericSave(m Model) error

GenericSave updates or inserts the provided model in the database panics if model is nil or not linked to dbp

func GenericSelectOneFromModel

func GenericSelectOneFromModel(dbp DBProvider, m Model) error

GenericSelectOneFromModel selects one row in the database from a model value panics if filter or dbp is nil

func GenericSelectOneWithModel

func GenericSelectOneWithModel(dbp DBProvider, filter yaormfilter.Filter, m Model) error

GenericSelectOneWithModel selects one row in the database providing the destination model directly panics if filter or dbp is nil

func GenericUpdate

func GenericUpdate(m Model) error

GenericUpdate updates the provided model in the database panics if model is nil or not linked to dbp

func RegisterDB

func RegisterDB(config *DatabaseConfiguration) error

RegisterDB creates a new database with configuration

func SaveWithPrimaryKeys

func SaveWithPrimaryKeys(m Model, keys []string) error

SaveWithPrimaryKeys updates or inserts the provided model in the database, using the provided keys to check if it exists

func UnregisterDB

func UnregisterDB(name string) error

UnregisterDB removes the database from the registry

type DB

type DB interface {
	zesty.DB
	System() DMS
	ExecutorHook() ExecutorHook
	DBSpecific() DBSpecific
}

func GetDB

func GetDB(name string) (DB, error)

GetDB returns a database object from its name

type DBProvider

type DBProvider interface {
	zesty.DBProvider
	EscapeValue(value string) string
	CanSelectForUpdate() bool

	Context() context.Context
	UUID() string

	HasCapacity(capacity DatabaseCapacity) bool
	RunInTransaction(func() error) error
	// contains filtered or unexported methods
}

DBProvider provides an abstracted way of accessing the database

func NewDBProvider

func NewDBProvider(ctx context.Context, name string) (DBProvider, error)

NewDBProvider creates a new db provider

type DBSpecific

type DBSpecific interface {
	// OnSessionCreated is a function executed once per DBProvider instance
	OnSessionCreated(DBProvider) error
}

DBSpecific is an interface describing how specificities of each DMS are handled in yaorm

type DMS

type DMS uint8

DMS represents a database management system

const (
	DatabasePostgreSQL DMS = iota ^ 42
	DatabaseMySQL
	DatabaseSqlite3
)

Database management systems.

func (DMS) DriverName

func (d DMS) DriverName() string

DriverName returns the name of the driver for ds.

func (DMS) RekordoValue

func (d DMS) RekordoValue() rekordo.DBMS

RekordoValue returns the rekordo value

type DatabaseCapacity

type DatabaseCapacity int8

DatabaseCapacity describes multiple capacities provided only by some databases systems

type DatabaseConfiguration

type DatabaseConfiguration struct {
	Name             string
	System           DMS
	DSN              string
	MaxOpenConns     int
	MaxIdleConns     int
	ConnMaxLifetime  time.Duration
	AutoCreateTables bool
	// Dialect database dialect, leave empty for automatic guessing
	Dialect gorp.Dialect
	// ExecutorHook is a configurable hook to add logs, for example, to your sql requests
	ExecutorHook ExecutorHook
	DBSpecific   DBSpecific
}

DatabaseConfiguration configures a database

type DatabaseModel

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

DatabaseModel is the struct every model should compose

func (*DatabaseModel) DBHookBeforeDelete

func (dm *DatabaseModel) DBHookBeforeDelete() error

DBHookBeforeDelete is a hook called before performing a DB delete By default, this function does not do anything. If needed, you will have to implement this function in your structure composing this one in order to have your code hook executed

func (*DatabaseModel) DBHookBeforeInsert

func (dm *DatabaseModel) DBHookBeforeInsert() error

DBHookBeforeInsert is a hook called before performing a DB insertion By default, this function does not do anything. If needed, you will have to implement this function in your structure composing this one in order to have your code hook executed

func (*DatabaseModel) DBHookBeforeUpdate

func (dm *DatabaseModel) DBHookBeforeUpdate() error

DBHookBeforeUpdate is a hook called before performing a DB update By default, this function does not do anything. If needed, you will have to implement this function in your structure composing this one in order to have your code hook executed

func (*DatabaseModel) Delete

func (dm *DatabaseModel) Delete() error

Delete removes the model from database In order to be usable, this function needs to be redfined by each composition of this structure

func (*DatabaseModel) GetDBP

func (dm *DatabaseModel) GetDBP() DBProvider

GetDBP should return the DBProvider tied to this model

func (*DatabaseModel) Load

func (dm *DatabaseModel) Load(dbp DBProvider) error

Load loads the model from database given the current struct In order to be usable, this function needs to be redfined by each composition of this structure

func (*DatabaseModel) Save

func (dm *DatabaseModel) Save() error

Save allows saving the model In order to be usable, this function needs to be redfined by each composition of this structure

func (*DatabaseModel) SetDBP

func (dm *DatabaseModel) SetDBP(dbp DBProvider)

SetDBP allows setting a DBProvider to the model, then returned by GetDBP

type DefaultExecutorHook

type DefaultExecutorHook struct{}

DefaultExecutorHook is the default executor hook, returned if no hook has been defined This struct can be composed by any of your executor hooks to avoid having to define every handler

func (DefaultExecutorHook) AfterDelete

func (h DefaultExecutorHook) AfterDelete(ctx context.Context, query string, args ...interface{})

AfterDelete is a hook executed before performing a Delete action on gorp sql executor

func (DefaultExecutorHook) AfterExec

func (h DefaultExecutorHook) AfterExec(ctx context.Context, query string, args ...interface{})

AfterExec is a hook executed before performing an Exec action on gorp sql executor

func (DefaultExecutorHook) AfterInsert

func (h DefaultExecutorHook) AfterInsert(ctx context.Context, query string, args ...interface{})

AfterInsert is a hook executed before performing an Insert action on gorp sql executor

func (DefaultExecutorHook) AfterSelect

func (h DefaultExecutorHook) AfterSelect(ctx context.Context, query string, args ...interface{})

AfterSelect is a hook executed before performing a Select action on gorp sql executor

func (DefaultExecutorHook) AfterSelectOne

func (h DefaultExecutorHook) AfterSelectOne(ctx context.Context, query string, args ...interface{})

AfterSelectOne is a hook executed before performing a SelectOne action on gorp sql executor

func (DefaultExecutorHook) AfterUpdate

func (h DefaultExecutorHook) AfterUpdate(ctx context.Context, query string, args ...interface{})

AfterUpdate is a hook executed before performing an Update action on gorp sql executor

func (DefaultExecutorHook) BeforeDelete

func (h DefaultExecutorHook) BeforeDelete(ctx context.Context, query string, args ...interface{})

BeforeDelete is a hook executed before performing a Delete action on gorp sql executor

func (DefaultExecutorHook) BeforeExec

func (h DefaultExecutorHook) BeforeExec(ctx context.Context, query string, args ...interface{})

BeforeExec is a hook executed before performing an Exec action on gorp sql executor

func (DefaultExecutorHook) BeforeInsert

func (h DefaultExecutorHook) BeforeInsert(ctx context.Context, query string, args ...interface{})

BeforeInsert is a hook executed before performing an Insert action on gorp sql executor

func (DefaultExecutorHook) BeforeSelect

func (h DefaultExecutorHook) BeforeSelect(ctx context.Context, query string, args ...interface{})

BeforeSelect is a hook executed before performing a Select action on gorp sql executor

func (DefaultExecutorHook) BeforeSelectOne

func (h DefaultExecutorHook) BeforeSelectOne(ctx context.Context, query string, args ...interface{})

BeforeSelectOne is a hook executed before performing a SelectOne action on gorp sql executor

func (DefaultExecutorHook) BeforeUpdate

func (h DefaultExecutorHook) BeforeUpdate(ctx context.Context, query string, args ...interface{})

BeforeUpdate is a hook executed before performing an Update action on gorp sql executor

type ExecutorHook

type ExecutorHook interface {
	BeforeSelectOne(ctx context.Context, query string, args ...interface{})
	AfterSelectOne(ctx context.Context, query string, args ...interface{})
	BeforeSelect(ctx context.Context, query string, args ...interface{})
	AfterSelect(ctx context.Context, query string, args ...interface{})
	BeforeInsert(ctx context.Context, query string, args ...interface{})
	AfterInsert(ctx context.Context, query string, args ...interface{})
	BeforeUpdate(ctx context.Context, query string, args ...interface{})
	AfterUpdate(ctx context.Context, query string, args ...interface{})
	BeforeDelete(ctx context.Context, query string, args ...interface{})
	AfterDelete(ctx context.Context, query string, args ...interface{})
	BeforeExec(ctx context.Context, query string, args ...interface{})
	AfterExec(ctx context.Context, query string, args ...interface{})
}

ExecutorHook can be implemented and set on the database handler Before every query, a new object will be created to provide a way to perform operations, like logging / timing your sql queries

type Model

type Model interface {
	// GetDBP should return the DBProvider tied to this model
	GetDBP() DBProvider
	// SetDBP allows setting a DBProvider to the model, then returned by GetDBP
	SetDBP(dbp DBProvider)
	// Save allows saving the model
	Save() error
	// Load loads the model from database given the current struct
	Load(dbp DBProvider) error
	// Delete removes the model from database
	Delete() error
	// DBHookBeforeInsert is a hook called before performing a DB insertion
	DBHookBeforeInsert() error
	// DBHookBeforeUpdate is a hook called before performing a DB update
	DBHookBeforeUpdate() error
	// DBHookBeforeDelete is a hook called before performing a DB delete
	DBHookBeforeDelete() error
}

Model is the interface every model should implement from

func GenericSelectAll

func GenericSelectAll(dbp DBProvider, filter yaormfilter.Filter) ([]Model, error)

GenericSelectAll selects all rows in the database panics if filter or dbp is nil

func GenericSelectOne

func GenericSelectOne(dbp DBProvider, filter yaormfilter.Filter) (Model, error)

GenericSelectOne selects one row in the database panics if filter or dbp is nil

type PostgresSpecific

type PostgresSpecific struct {
	// IntervaStyle holds the style of output of the interval
	// See Postgres manual 8.5.5
	IntervalStyle string
}

PostgresSpecific holds specific configurations used by Postgres

func (PostgresSpecific) OnSessionCreated

func (p PostgresSpecific) OnSessionCreated(dbp DBProvider) error

OnSessionCreated is executed when a Session is created. This function will use the: - IntervalStyle

type SqlExecutor

type SqlExecutor struct {
	gorp.SqlExecutor
	// contains filtered or unexported fields
}

SqlExecutor is a custom SQL Executor, on top of the one provided by gorp used to provide multiple hooks before executing statements

func (*SqlExecutor) Delete

func (e *SqlExecutor) Delete(list ...interface{}) (int64, error)

Delete is a handler to delete a list of models from the database

func (*SqlExecutor) Exec

func (e *SqlExecutor) Exec(query string, args ...interface{}) (sql.Result, error)

Exec is a handler to execute a SQL query

func (*SqlExecutor) Insert

func (e *SqlExecutor) Insert(list ...interface{}) error

Insert is a handler to insert a list of models inside the database

func (*SqlExecutor) Select

func (e *SqlExecutor) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error)

Select is a handler to select multiple rows from database and return them

func (*SqlExecutor) SelectOne

func (e *SqlExecutor) SelectOne(holder interface{}, query string, args ...interface{}) error

SelectOne is a handler to select only 1 row from database and store it inside the first argument

func (*SqlExecutor) Update

func (e *SqlExecutor) Update(list ...interface{}) (int64, error)

Update is a handler to update a list of models inside the database

type SubqueryloadFunc

type SubqueryloadFunc func(dbp DBProvider, ids []interface{}) (interface{}, error)

type Table

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

Table is the type hosting all the table characteristics

func GetTable

func GetTable(dbName, tableName string) (*Table, error)

GetTable returns the table matching the parameters

func GetTableByFilter

func GetTableByFilter(f yaormfilter.Filter) (*Table, error)

GetTableByFilter returns the table using this filter

func GetTableByModel

func GetTableByModel(m Model) (*Table, error)

GetTableByModel returns the table registered for this model

func NewTable

func NewTable(dbName, tableName string, model Model) *Table

NewTable registers a new table

func (Table) FieldIndex

func (t Table) FieldIndex(field string) int

func (Table) Fields

func (t Table) Fields() []string

func (Table) FieldsWithoutPK

func (t Table) FieldsWithoutPK() []string

func (Table) FilterFieldIndex

func (t Table) FilterFieldIndex(field string) int

func (Table) KeyFields

func (t Table) KeyFields() map[string]int

func (Table) Keys

func (t Table) Keys() []string

func (Table) Name

func (t Table) Name() string

func (Table) NameForQuery

func (t Table) NameForQuery(dbp DBProvider) string

NameForQuery returns the name that should be used for SQL queries

func (Table) NewFilter

func (t Table) NewFilter() (yaormfilter.Filter, error)

func (Table) NewModel

func (t Table) NewModel() (Model, error)

func (Table) NewSlice

func (t Table) NewSlice() (interface{}, error)

func (Table) NewSlicePtr

func (t Table) NewSlicePtr() (interface{}, error)

func (*Table) WithAutoIncrement

func (t *Table) WithAutoIncrement(v bool) *Table

func (*Table) WithFilter

func (t *Table) WithFilter(f yaormfilter.Filter) *Table

func (*Table) WithKeys

func (t *Table) WithKeys(keys []string) *Table

func (*Table) WithSchema

func (t *Table) WithSchema(schema string) *Table

func (*Table) WithSubqueryloading

func (t *Table) WithSubqueryloading(fn SubqueryloadFunc, mapperField string) *Table

type TypeConverter

type TypeConverter struct{}

TypeConverter defines conversion from db to golang

func (TypeConverter) FromDb

func (tc TypeConverter) FromDb(target interface{}) (gorp.CustomScanner, bool)

FromDb converts to golang

func (TypeConverter) ToDb

func (tc TypeConverter) ToDb(val interface{}) (interface{}, error)

ToDb converts to database

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier