Documentation ¶
Overview ¶
Adapt tmeta to github.com/gocraft/dbr for easy query building.
Index ¶
- Variables
- type Builder
- func (b *Builder) DeleteByID(o interface{}, ids ...interface{}) (*dbr.DeleteStmt, error)
- func (b *Builder) DeleteRelationNotIn(o interface{}, relationName string) (*dbr.DeleteStmt, error)
- func (b *Builder) ExecContextOK(ctx context.Context, execContexter ExecContexter) error
- func (b *Builder) ExecOK(execer Execer) error
- func (b *Builder) Insert(o interface{}) (*dbr.InsertStmt, error)
- func (b *Builder) InsertRelationIgnore(o interface{}, relationName string) (*dbr.InsertStmt, error)
- func (b *Builder) MustDeleteByID(o interface{}, ids ...interface{}) *dbr.DeleteStmt
- func (b *Builder) MustDeleteRelationNotIn(o interface{}, relationName string) *dbr.DeleteStmt
- func (b *Builder) MustInsert(o interface{}) *dbr.InsertStmt
- func (b *Builder) MustInsertRelationIgnore(o interface{}, relationName string) *dbr.InsertStmt
- func (b *Builder) MustSelect(o interface{}) *dbr.SelectStmt
- func (b *Builder) MustSelectByID(o interface{}, ids ...interface{}) *dbr.SelectStmt
- func (b *Builder) MustSelectRelation(o interface{}, relationName string) (stmt *dbr.SelectStmt)
- func (b *Builder) MustSelectRelationPtr(o interface{}, relationName string) (stmt *dbr.SelectStmt, fieldPtr interface{})
- func (b *Builder) MustUpdateByID(o interface{}) *dbr.UpdateStmt
- func (b *Builder) ResultOK(res sql.Result, err error) error
- func (b *Builder) ResultWithInsertID(o interface{}, res sql.Result, err error) error
- func (b *Builder) ResultWithOneUpdate(res sql.Result, err error) error
- func (b *Builder) Select(o interface{}) (*dbr.SelectStmt, error)
- func (b *Builder) SelectByID(o interface{}, ids ...interface{}) (*dbr.SelectStmt, error)
- func (b *Builder) SelectRelation(o interface{}, relationName string) (stmt *dbr.SelectStmt, reterr error)
- func (b *Builder) SelectRelationPtr(o interface{}, relationName string) (stmt *dbr.SelectStmt, fieldPtr interface{}, reterr error)
- func (b *Builder) UpdateByID(o interface{}) (*dbr.UpdateStmt, error)
- type CreateTimeToucher
- type ExecContexter
- type Execer
- type IDAssigner
- type Session
- type UpdateTimeToucher
- type VersionIncrementer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrTypeNotRegistered is returned when the type of a variable does not correspond to a Meta entry. ErrTypeNotRegistered = errors.New("tmetadbr: type not registered") // ErrUpdateFailed is used to indicate the record could not be found or an optimistic locking update failure (version changed since last read) ErrUpdateFailed = &errorWithCode{code: 409, msg: "tmetadbr: update failed (not found or version conflict)"} )
Functions ¶
This section is empty.
Types ¶
type Builder ¶
Builder is used to do query building. It has a Session (can be a *dbr.Session or *dbr.Tx) and *tmeta.Meta.
func (*Builder) DeleteByID ¶
func (b *Builder) DeleteByID(o interface{}, ids ...interface{}) (*dbr.DeleteStmt, error)
DeleteByID make a delete statement with a where clause by the primary key. If len(ids)>0 then those values are included as the SQL where clause. Otherwise the primary keys are extracted from the object provided and, if optimistic locking is enabled for this type, the version number is included in the SQL where clause also.
func (*Builder) DeleteRelationNotIn ¶
func (b *Builder) DeleteRelationNotIn(o interface{}, relationName string) (*dbr.DeleteStmt, error)
DeleteRelationNotIn will make a delete statement for the records corresponding to the IDs indicated by the given relation. The relation must be of type BelongsToManyIDs.
func (*Builder) ExecContextOK ¶
func (b *Builder) ExecContextOK(ctx context.Context, execContexter ExecContexter) error
ExecContextOK is an alias for ExecContext and discard result, just return the error. If execer is nil then it's a no-op and nil error is returned.
func (*Builder) ExecOK ¶
ExecOK is an alias for Exec and discard result, just return the error. If execer is nil then it's a no-op and nil error is returned.
func (*Builder) Insert ¶
func (b *Builder) Insert(o interface{}) (*dbr.InsertStmt, error)
Insert generates an insert statement for the object(s) provided. Slice is supported. It also calls CreateTimeTouch on the object(s) if possible.
func (*Builder) InsertRelationIgnore ¶
func (b *Builder) InsertRelationIgnore(o interface{}, relationName string) (*dbr.InsertStmt, error)
InsertRelationIgnore will make an insert statement for the records indicated by the given relation. The relation must be of type BelongsToManyIDs. Will use InsertBySQL to generate a db-specific "insert with ignore" statement (unfortunately Sqlite3, MySQL and Postgres each require different syntaxes to achieve the same behavior). Note: (nil,nil) is a valid return in cases where the relation is an empty set, indicating that no insert is necessary.
func (*Builder) MustDeleteByID ¶
func (b *Builder) MustDeleteByID(o interface{}, ids ...interface{}) *dbr.DeleteStmt
MustDeleteByID is the same as DeleteByID but panics on error.
func (*Builder) MustDeleteRelationNotIn ¶
func (b *Builder) MustDeleteRelationNotIn(o interface{}, relationName string) *dbr.DeleteStmt
MustDeleteRelationNotIn is the same as DeleteRelationNotIn but panics on error.
func (*Builder) MustInsert ¶
func (b *Builder) MustInsert(o interface{}) *dbr.InsertStmt
MustInsert is the same as Insert but panics on error.
func (*Builder) MustInsertRelationIgnore ¶
func (b *Builder) MustInsertRelationIgnore(o interface{}, relationName string) *dbr.InsertStmt
MustInsertRelationIgnore is the same as InsertRelationIgnore but panics on error.
func (*Builder) MustSelect ¶
func (b *Builder) MustSelect(o interface{}) *dbr.SelectStmt
MustSelect is the same as Select but panics on error.
func (*Builder) MustSelectByID ¶
func (b *Builder) MustSelectByID(o interface{}, ids ...interface{}) *dbr.SelectStmt
MustSelectByID is the same as SelectByID but panics on error.
func (*Builder) MustSelectRelation ¶
func (b *Builder) MustSelectRelation(o interface{}, relationName string) (stmt *dbr.SelectStmt)
MustSelectRelation is the same as SelectRelation but will panic on error.
func (*Builder) MustSelectRelationPtr ¶
func (b *Builder) MustSelectRelationPtr(o interface{}, relationName string) (stmt *dbr.SelectStmt, fieldPtr interface{})
MustSelectRelationPtr is the same as SelectRelationPtr but will panic on error.
func (*Builder) MustUpdateByID ¶
func (b *Builder) MustUpdateByID(o interface{}) *dbr.UpdateStmt
MustUpdateByID is the same as UpdateByID but panics on error.
func (*Builder) ResultOK ¶
ResultOK accepts a result and an error and just returns the error. This is just a helper to more easily check "did this query succeed" when you don't care about rows affected or last insert ID.
func (*Builder) ResultWithInsertID ¶
ResultWithInsertID is a helper to handle the result of an insert. If err is non-nil it will be returned. If the object provided has an auto increment primary key, then LastInsertId is used used to populate it. TODO: This should be refactored so it can be used on one line, if possible. It doesn't provide the same convenience that ResultWithOneUpdate does due to needing the extra argument.
func (*Builder) ResultWithOneUpdate ¶
ResultWithOneUpdate is a helper to handle the result of an update. If err is non-nil it will be returned. The RowsAffected value is checked an if not equal to 1 then ErrUpdateFailed is returned. (This call also works for inserts if you don't need the last insert ID, in which case use ResultWithInsertID).
Note for MySQL: RowsAffected can return different values based on the connection string provided, possibly leading to unexpected behavior in cases where an update succeeds but no actual data is changed (i.e. updating a record without any version column or timestamps and all fields the same). See https://github.com/go-sql-driver/mysql#clientfoundrows and consider setting clientFoundRows=true to avoid this problem.
func (*Builder) Select ¶
func (b *Builder) Select(o interface{}) (*dbr.SelectStmt, error)
Select will build a select statement with the field list of the type provided from the appropriate table. If a slice is provided, the table is derived from the slice's element type.
func (*Builder) SelectByID ¶
func (b *Builder) SelectByID(o interface{}, ids ...interface{}) (*dbr.SelectStmt, error)
SelectByID will build a select statement on the appropriate table with a where clause matching the given primary keys. If ids is non-zero len it will be used as the pk values otherwise the pk values will be extracted from the object provided.
func (*Builder) SelectRelation ¶
func (b *Builder) SelectRelation(o interface{}, relationName string) (stmt *dbr.SelectStmt, reterr error)
SelectRelation is the same as SelectRelationPtr but does not return the field pointer.
func (*Builder) SelectRelationPtr ¶
func (b *Builder) SelectRelationPtr(o interface{}, relationName string) (stmt *dbr.SelectStmt, fieldPtr interface{}, reterr error)
SelectRelationPtr returns a select statement that will select the relation into the appropriate field, based on the name of that relation. The second return value is a pointer to the field which can be passed to stmt.Load() to populate the correct field. The object provided must not be a slice.
func (*Builder) UpdateByID ¶
func (b *Builder) UpdateByID(o interface{}) (*dbr.UpdateStmt, error)
UpdateByID creates an update statement for a record using it's primary key, taking into account the update time (if UpdateTimeToucher is supported), version field (if SQLVersionField is not empty). If using a version field, its value should be the same as it was selected with and this method will attempt to increment it by one.
type CreateTimeToucher ¶
type CreateTimeToucher interface {
CreateTimeTouch()
}
CreateTimeToucher can be implemented by objects to be notified when their create time should be set to "now".
type ExecContexter ¶
Execer interface for database things that can be ExecContext()ed
type IDAssigner ¶ added in v0.2.0
type IDAssigner interface {
IDAssign()
}
IDAssigner can be implemented by objects to provide a means to "set the ID to a new value if not already done".
type Session ¶
type Session interface { InsertInto(table string) *dbr.InsertStmt Select(column ...string) *dbr.SelectStmt Update(table string) *dbr.UpdateStmt DeleteFrom(table string) *dbr.DeleteStmt InsertBySql(query string, value ...interface{}) *dbr.InsertStmt SelectBySql(query string, value ...interface{}) *dbr.SelectStmt UpdateBySql(query string, value ...interface{}) *dbr.UpdateStmt DeleteBySql(query string, value ...interface{}) *dbr.DeleteStmt }
Session is implemented by *dbr.Session and *dbr.Tx
type UpdateTimeToucher ¶
type UpdateTimeToucher interface {
UpdateTimeTouch()
}
UpdateTimeToucher can be implemented by objects to be notified when their update time should be set to "now".
type VersionIncrementer ¶ added in v0.2.0
type VersionIncrementer interface {
VersionIncrement()
}
VersionIncrementer can be implemented by objects to update their version field to the next value.