Documentation ¶
Overview ¶
Package orm NOTES
Package orm NOTES
Index ¶
- Variables
- func GetNamedSelectColumns(table interface{}) (expr string, err error)
- func RearrangeSQLDataWithOption(data interface{}, opts *FieldOption) (expr string, toUpdate map[string]interface{}, err error)
- func RecursiveGetDBTags(table interface{}) ([]string, error)
- func RecursiveGetTaggedFieldValues(v interface{}) (map[string]interface{}, error)
- type DoOrm
- type DoOrmWithTransaction
- type FieldOption
- type Interface
Constants ¶
This section is empty.
Variables ¶
var ( // ErrRecordNotFound returns a "record not found error". // Occurs only when attempting to query the database with a struct, // querying with a slice won't return this error. ErrRecordNotFound = sql.ErrNoRows // ErrDeadLock concurrent exec deadlock, error message returned by db. ErrDeadLock = "Error 1213: Deadlock found when trying to get lock" )
var ( // EndBracketsReg .... EndBracketsReg = regexp.MustCompile(`\([^()]*\)\s*$`) )
Functions ¶
func GetNamedSelectColumns ¶
GetNamedSelectColumns get 'all' the named field with 'db' tag and embedded if it's an embedded struct. Use table.App as an example, the returned expr should as follows: id, biz_id, name as 'spec.name', memo as 'spec.memo' Note: define the embedded columns in the table.go manually. Deprecated: GetNamedSelectColumns will panic if there is a nil value.
func RearrangeSQLDataWithOption ¶
func RearrangeSQLDataWithOption(data interface{}, opts *FieldOption) ( expr string, toUpdate map[string]interface{}, err error)
RearrangeSQLDataWithOption parse a *struct into a sql expression, and returned with the update sql expression and the to be updated data.
- the input FieldOption only works for the returned 'expr', not controls the returned 'toUpdate', so the returned 'toUpdate' contains all the flatted tagged 'db' field and value.
- Obviously, a data field need to be updated if the field value is not blank(as is not "ZERO"),
- If the field is defined in the blank options deliberately, then update it to blank value as required.
- see the test case to know the exact data returned.
func RecursiveGetDBTags ¶
RecursiveGetDBTags get a table's all the db tag recursively.
func RecursiveGetTaggedFieldValues ¶
RecursiveGetTaggedFieldValues get all the tagged db kv in the struct to a flat map except ptr and struct tag. Note:
- if the embedded tag is same, then it will be overlapped.
- use this function carefully, it not supports all the type, such as array, slice, map is not supported.
- see the test case to know the output data example.
Types ¶
type DoOrm ¶
type DoOrm interface { Get(ctx context.Context, dest interface{}, expr string, args ...interface{}) error Select(ctx context.Context, dest interface{}, expr string, args ...interface{}) error Count(ctx context.Context, expr string, args ...interface{}) (uint32, error) Delete(ctx context.Context, expr string, args ...interface{}) (int64, error) Update(ctx context.Context, expr string, args interface{}) (int64, error) Insert(ctx context.Context, expr string, data interface{}) error BulkInsert(ctx context.Context, expr string, args interface{}) error Exec(ctx context.Context, expr string) (int64, error) }
DoOrm defines all the orm method.
type DoOrmWithTransaction ¶
type DoOrmWithTransaction interface { Insert(ctx context.Context, expr string, args interface{}) error BulkInsert(ctx context.Context, expr string, args interface{}) error Delete(ctx context.Context, expr string, args ...interface{}) error Update(ctx context.Context, expr string, args interface{}) (int64, error) }
DoOrmWithTransaction defines all the orm method with transaction.
type FieldOption ¶
type FieldOption struct {
// contains filtered or unexported fields
}
FieldOption is to define which field need to be:
- updated to blank(as is ZERO) value.
- be ignored, which means not be updated even its value is not blank(as is not ZERO).
NOTE: 1. A field can not in the blanked and ignore fields at the same time. if a field does, then it will be ignored without being updated. 2. The map's key is the structs' 'db' tag of that field.
func NewFieldOptions ¶
func NewFieldOptions() *FieldOption
NewFieldOptions create a blank option instances for add keys to be updated when update data.
func (*FieldOption) AddBlankedFields ¶
func (f *FieldOption) AddBlankedFields(fields ...string) *FieldOption
AddBlankedFields add fields to be updated to blank values.
func (*FieldOption) AddIgnoredFields ¶
func (f *FieldOption) AddIgnoredFields(fields ...string) *FieldOption
AddIgnoredFields add fields which do not need to be updated even it do has a value.
func (*FieldOption) NeedBlanked ¶
func (f *FieldOption) NeedBlanked(field string) bool
NeedBlanked check if this field need to be updated with blank
func (*FieldOption) NeedIgnored ¶
func (f *FieldOption) NeedIgnored(field string) bool
NeedIgnored check if this field does not need to be updated.