Documentation ¶
Overview ¶
Package dao is the data access object swiss army knife / "black box".
Index ¶
- Variables
- func GetDB(driver, dsn string) (*sql.DB, error)
- type HookFunction
- type ORM
- func (o *ORM) CallAfterCreateHookIfNeeded(obj *object.Object) error
- func (o *ORM) CallAfterDeleteHookIfNeeded(obj *object.Object) error
- func (o *ORM) CallAfterUpdateHookIfNeeded(obj *object.Object) error
- func (o *ORM) CallBeforeCreateHookIfNeeded(obj *object.Object) error
- func (o *ORM) CallBeforeDeleteHookIfNeeded(obj *object.Object) error
- func (o *ORM) CallBeforeUpdateHookIfNeeded(obj *object.Object) error
- func (o *ORM) CreateOrUpdate(ctx context.Context, obj *object.Object) (int64, *object.Object, error)
- func (o *ORM) CreateOrUpdateKV(ctx context.Context, typ string, queryKV map[string]interface{}, ...) (int64, *object.Object, error)
- func (o *ORM) CreateOrUpdateKVHookUpdate(ctx context.Context, tx *sql.Tx, typ string, ...) (int64, *object.Object, error)
- func (o *ORM) CreateOrUpdateKVTx(ctx context.Context, tx *sql.Tx, typ string, queryKV map[string]interface{}, ...) (int64, *object.Object, error)
- func (o *ORM) CreateOrUpdateTx(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, *object.Object, error)
- func (o *ORM) CreateTable(ctx context.Context, sch *schema.Schema, tableName string) error
- func (o *ORM) CreateTables(ctx context.Context) error
- func (o *ORM) Delete(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)
- func (o *ORM) DropTable(ctx context.Context, tableName string) error
- func (o *ORM) DropTables(ctx context.Context) error
- func (o *ORM) FindOrCreate(ctx context.Context, obj *object.Object) (int64, *object.Object, error)
- func (o *ORM) FindOrCreateKV(ctx context.Context, typ string, queryKV map[string]interface{}, ...) (int64, *object.Object, error)
- func (o *ORM) FindOrCreateKVTx(ctx context.Context, tx *sql.Tx, typ string, queryKV map[string]interface{}, ...) (int64, *object.Object, error)
- func (o *ORM) FindOrCreateTx(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, *object.Object, error)
- func (o *ORM) FleshenChildren(ctx context.Context, obj *object.Object) (*object.Object, error)
- func (o *ORM) GetLock(ctx context.Context, tx *sql.Tx, lockStr string) (bool, error)
- func (o *ORM) GetParentsViaChild(ctx context.Context, childObj *object.Object) (object.Array, error)
- func (o *ORM) GetSQLGenerator() *sg.SQLGenerator
- func (o *ORM) GetSchema() *schema.Schema
- func (o *ORM) Insert(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)
- func (o *ORM) ReleaseLock(ctx context.Context, tx *sql.Tx, lockStr string) (bool, error)
- func (o *ORM) Retrieve(ctx context.Context, table string, queryVals map[string]interface{}) (*object.Object, error)
- func (o *ORM) RetrieveMany(ctx context.Context, table string, queryVals map[string]interface{}) (object.Array, error)
- func (o *ORM) RetrieveManyFromCustomSQL(ctx context.Context, table string, sqlStr string, columnNames []string, ...) (object.Array, error)
- func (o *ORM) RetrieveManyTx(ctx context.Context, tx *sql.Tx, table string, ...) (object.Array, error)
- func (o *ORM) RetrieveTx(ctx context.Context, tx *sql.Tx, table string, ...) (*object.Object, error)
- func (o *ORM) RetrieveWithChildren(ctx context.Context, table string, pkValues map[string]interface{}) (*object.Object, error)
- func (o *ORM) Save(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)
- func (o *ORM) SaveAll(ctx context.Context, obj *object.Object) (int64, error)
- func (o *ORM) SaveAllInsideTx(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)
- func (o *ORM) SaveButErrorIfInsert(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)
- func (o *ORM) SaveButErrorIfUpdate(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)
- func (o *ORM) Transact(ctx context.Context, txFunc TxFuncType, opts *sql.TxOptions) error
- func (o *ORM) TransactRethrow(ctx context.Context, txFunc TxFuncType, opts *sql.TxOptions) error
- func (o *ORM) Update(ctx context.Context, tx *sql.Tx, obj *object.Object) (int64, error)
- func (o *ORM) UseTracing() bool
- type TxFuncType
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoResult = errors.New("dyndao: operation had no result")
)
Functions ¶
Types ¶
type HookFunction ¶
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 ¶
New is the ORM constructor. It expects a SQL generator, JSON/SQL Schema object, and database connection.
func (*ORM) CallAfterCreateHookIfNeeded ¶
func (*ORM) CallAfterDeleteHookIfNeeded ¶
func (*ORM) CallAfterUpdateHookIfNeeded ¶
func (*ORM) CallBeforeCreateHookIfNeeded ¶
Software trigger functions
func (*ORM) CallBeforeDeleteHookIfNeeded ¶
func (*ORM) CallBeforeUpdateHookIfNeeded ¶
func (*ORM) CreateOrUpdate ¶
func (*ORM) CreateOrUpdateKV ¶
func (*ORM) CreateOrUpdateKVHookUpdate ¶
func (*ORM) CreateOrUpdateKVTx ¶
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 ¶
CreateTable will execute a CreateTable operation for the specified table in a given schema.
func (*ORM) CreateTables ¶
CreateTables executes a CreateTable operation for every table specified in the schema.
func (*ORM) DropTable ¶
DropTable will execute a DropTable operation for the specified table in a given schema.
func (*ORM) DropTables ¶
DropTables executes a DropTable operation for every table specified in the schema.
func (*ORM) FindOrCreate ¶
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 ¶
FleshenChildren function accepts an object and resets it's children.
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) Insert ¶
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 (*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 ¶
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 ¶
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 ¶
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 ¶
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