orm

package
v0.0.0-...-22ac7ce Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package dao is the data access object swiss army knife / "black box".

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoResult = errors.New("dyndao: operation had no result")
)

Functions

func GetDB

func GetDB(driver, dsn string) (*sql.DB, error)

GetDB accepts a Driver and DSN as strings, returning a sql.DB and an error.

Types

type HookFunction

type HookFunction func(*schema.Schema, *object.Object) error

HookFunction is the function type for declaring a software-based trigger, which we refer to as a 'hook function'.

type ORM

type ORM struct {
	RawConn *sql.DB

	// string is the table name that corresponds to a table in the schema. HookFunction
	BeforeCreateHooks map[string]HookFunction
	AfterCreateHooks  map[string]HookFunction

	BeforeUpdateHooks map[string]HookFunction
	AfterUpdateHooks  map[string]HookFunction

	BeforeDeleteHooks map[string]HookFunction
	AfterDeleteHooks  map[string]HookFunction
	// contains filtered or unexported fields
}

ORM is the primary object we expect the caller to operate on. Construct one with orm.New( ... ) and be on your merry way.

func New

func New(gen *sg.SQLGenerator, s *schema.Schema, db *sql.DB) *ORM

New is the ORM constructor. It expects a SQL generator, JSON/SQL Schema object, and database connection.

func (*ORM) CallAfterCreateHookIfNeeded

func (o *ORM) CallAfterCreateHookIfNeeded(obj *object.Object) error

func (*ORM) CallAfterDeleteHookIfNeeded

func (o *ORM) CallAfterDeleteHookIfNeeded(obj *object.Object) error

func (*ORM) CallAfterUpdateHookIfNeeded

func (o *ORM) CallAfterUpdateHookIfNeeded(obj *object.Object) error

func (*ORM) CallBeforeCreateHookIfNeeded

func (o *ORM) CallBeforeCreateHookIfNeeded(obj *object.Object) error

Software trigger functions

func (*ORM) CallBeforeDeleteHookIfNeeded

func (o *ORM) CallBeforeDeleteHookIfNeeded(obj *object.Object) error

func (*ORM) CallBeforeUpdateHookIfNeeded

func (o *ORM) CallBeforeUpdateHookIfNeeded(obj *object.Object) error

func (*ORM) CreateOrUpdate

func (o *ORM) CreateOrUpdate(ctx context.Context, obj *object.Object) (int64, *object.Object, error)

func (*ORM) CreateOrUpdateKV

func (o *ORM) CreateOrUpdateKV(ctx context.Context, typ string, queryKV map[string]interface{}, createKV map[string]interface{}) (int64, *object.Object, error)

func (*ORM) CreateOrUpdateKVHookUpdate

func (o *ORM) CreateOrUpdateKVHookUpdate(ctx context.Context, tx *sql.Tx, typ string, queryKV, createKV map[string]interface{}, beforeUpdateCopyColumns []string) (int64, *object.Object, error)

func (*ORM) CreateOrUpdateKVTx

func (o *ORM) CreateOrUpdateKVTx(ctx context.Context, tx *sql.Tx, typ string, queryKV map[string]interface{}, createKV map[string]interface{}) (int64, *object.Object, error)

func (*ORM) CreateOrUpdateTx

func (o *ORM) CreateOrUpdateTx(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, *object.Object, error)

CreateOrUpdateTx is a typical "Retrieve and Update or Insert" operation. Feed it a context, the current transaction, and the dyndao object you are trying to save. It will return rows affected, the resulting object, or an error.

func (*ORM) CreateTable

func (o *ORM) CreateTable(ctx context.Context, sch *schema.Schema, tableName string) error

CreateTable will execute a CreateTable operation for the specified table in a given schema.

func (*ORM) CreateTables

func (o *ORM) CreateTables(ctx context.Context) error

CreateTables executes a CreateTable operation for every table specified in the schema.

func (*ORM) Delete

func (o *ORM) Delete(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)

Delete function will DELETE a record ...

func (*ORM) DropTable

func (o *ORM) DropTable(ctx context.Context, tableName string) error

DropTable will execute a DropTable operation for the specified table in a given schema.

func (*ORM) DropTables

func (o *ORM) DropTables(ctx context.Context) error

DropTables executes a DropTable operation for every table specified in the schema.

func (*ORM) FindOrCreate

func (o *ORM) FindOrCreate(ctx context.Context, obj *object.Object) (int64, *object.Object, error)

FindOrCreate is a transactionless FindOrCreate operation. This may not be recommended unless you know what you are doing and are comfortable potentially violating transactional integrity.

func (*ORM) FindOrCreateKV

func (o *ORM) FindOrCreateKV(ctx context.Context, typ string, queryKV map[string]interface{}, createKV map[string]interface{}) (int64, *object.Object, error)

FindOrCreateKV is a transactionless FindOrCreate. This may not be recommended unless you know what you are doing and are comfortable potentially violating transactional integrity.

func (*ORM) FindOrCreateKVTx

func (o *ORM) FindOrCreateKVTx(ctx context.Context, tx *sql.Tx, typ string, queryKV map[string]interface{}, createKV map[string]interface{}) (int64, *object.Object, error)

FindOrCreateKVTx is useful when you need to execute the Find with specific query parameters, but if the record does not exist, then a separate set of parameters should be inserted into the database. This matches a typical use-case scenario for a FindOrCreate: primary keys are used to locate the row and if the row doesn't exist, then the other values are used for the INSERT. Using a transaction is recommended.

func (*ORM) FindOrCreateTx

func (o *ORM) FindOrCreateTx(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, *object.Object, error)

FindOrCreateTx accepts a context, transaction, and dyndao object. It returns the number of rows affected, the resulting dyndao object (either freshly Retrieved or freshly Created) and any error that may have occurred. This is one of the recommended methods to use for a FindOrCreate. FindOrCreateKVTx is likely better to use in certain situations.

func (*ORM) FleshenChildren

func (o *ORM) FleshenChildren(ctx context.Context, obj *object.Object) (*object.Object, error)

FleshenChildren function accepts an object and resets it's children.

func (*ORM) GetLock

func (o *ORM) GetLock(ctx context.Context, tx *sql.Tx, lockStr string) (bool, error)

func (*ORM) GetParentsViaChild

func (o *ORM) GetParentsViaChild(ctx context.Context, childObj *object.Object) (object.Array, error)

GetParentsViaChild retrieves all direct (one-level 'up') parents for a given child object. If a child contains multiple parent tables (possibility?) then this would return an Array of objects with multiple potential values for their obj.Type fields.

func (*ORM) GetSQLGenerator

func (o *ORM) GetSQLGenerator() *sg.SQLGenerator

GetSQLGenerator returns the active SQLGenerator adapter

func (*ORM) GetSchema

func (o *ORM) GetSchema() *schema.Schema

GetSchema returns the ORM's active schema

func (*ORM) Insert

func (o *ORM) Insert(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)

Insert function will INSERT a record, given an optional transaction and an object. It returns the number of rows affected (int64) and any error that may have occurred.

func (*ORM) ReleaseLock

func (o *ORM) ReleaseLock(ctx context.Context, tx *sql.Tx, lockStr string) (bool, error)

func (*ORM) Retrieve

func (o *ORM) Retrieve(ctx context.Context, table string, queryVals map[string]interface{}) (*object.Object, error)

Retrieve function will fleshen an object structure, given some primary keys. Technically, we call RetrieveMany internally. Since we do not have LIMIT implemented yet, it's just a cheap implementation that returns the zeroeth value. Nil will be returned for both the object and the error if a row is unable to be matched by the underlying datastore. TODO: Implement LIMIT so that we can improve this.

func (*ORM) RetrieveMany

func (o *ORM) RetrieveMany(ctx context.Context, table string, queryVals map[string]interface{}) (object.Array, error)

RetrieveMany function will fleshen a top-level object structure, given some primary keys

func (*ORM) RetrieveManyFromCustomSQL

func (o *ORM) RetrieveManyFromCustomSQL(ctx context.Context, table string, sqlStr string, columnNames []string, bindArgs []interface{}) (object.Array, error)

RetrieveManyFromCustomSQL will fleshen an object structure, given a custom SQL string. It must still be told the column names and the binding arguments in addition to the SQL string, so that it can dynamically map the column types accordingly to the destination object. (Mainly, so we know the array length..)

func (*ORM) RetrieveManyTx

func (o *ORM) RetrieveManyTx(ctx context.Context, tx *sql.Tx, table string, queryVals map[string]interface{}) (object.Array, error)

RetrieveManyTx function will fleshen a top-level object structure, given some primary keys. And it's transactional!

func (*ORM) RetrieveTx

func (o *ORM) RetrieveTx(ctx context.Context, tx *sql.Tx, table string, queryVals map[string]interface{}) (*object.Object, error)

RetrieveTx function will fleshen an object structure, given some primary keys. Technically, we call RetrieveMany internally. Since we do not have LIMIT implemented yet, it's just a cheap implementation that returns the zeroeth value. Nil will be returned for both the object and the error if a row is unable to be matched by the underlying datastore. TODO: Implement LIMIT so that we can improve this.

func (*ORM) RetrieveWithChildren

func (o *ORM) RetrieveWithChildren(ctx context.Context, table string, pkValues map[string]interface{}) (*object.Object, error)

RetrieveWithChildren function will fleshen an *entire* object structure, given some primary keys By entire, we mean that we also retrieve any relevant children objects. However, we do not call RetrieveWithChildren when fleshening the children structures -- when retrieving the children, we do a single-level retrieve, ignoring any child structures that may be configured at two levels of depth.

func (*ORM) Save

func (o *ORM) Save(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)

Save function will INSERT or UPDATE a record. It does not attempt to save any of the children. If given a transaction, it will use that to attempt to insert the data.

func (*ORM) SaveAll

func (o *ORM) SaveAll(ctx context.Context, obj *object.Object) (int64, error)

SaveAll will attempt to save an entire nested object structure inside of a single transaction. It begins the transaction, attempts to recursively save the object and all of it's children, and any of the children's children, and then will finally rollback/commit as necessary.

func (*ORM) SaveAllInsideTx

func (o *ORM) SaveAllInsideTx(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)

SaveAllInsideTx will attempt to save an entire nested object structure inside of a single transaction.

func (*ORM) SaveButErrorIfInsert

func (o *ORM) SaveButErrorIfInsert(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)

SaveButErrorIfInsert function will UPDATE a record and error if it appears that an INSERT should have been performed. This could be necessary in situations where an INSERT would compromise the integrity of the data. If given a transaction, it will use that to attempt to insert the data.

func (*ORM) SaveButErrorIfUpdate

func (o *ORM) SaveButErrorIfUpdate(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)

SaveButErrorIfUpdate function will INSERT or UPDATE a record. It does not attempt to save any of the children. If given a transaction, it will use that to attempt to insert the data.

func (*ORM) Transact

func (o *ORM) Transact(ctx context.Context, txFunc TxFuncType, opts *sql.TxOptions) error

Transact is meant to group operations into transactions, simplify error handling, and recover from any panics. See: http://stackoverflow.com/questions/16184238/database-sql-tx-detecting-commit-or-rollback Please note this function has been changed from the above post to use contexts

func (*ORM) TransactRethrow

func (o *ORM) TransactRethrow(ctx context.Context, txFunc TxFuncType, opts *sql.TxOptions) error

func (*ORM) Update

func (o *ORM) Update(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)

Update function will UPDATE a record ...

func (*ORM) UseTracing

func (o *ORM) UseTracing() bool

type TxFuncType

type TxFuncType func(*sql.Tx) error

Jump to

Keyboard shortcuts

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