/
tran_entity.go
74 lines (67 loc) · 1.74 KB
/
tran_entity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package dal
import "github.com/antlinker/go-dal/utils"
// TranOperate 操作
type TranOperate byte
const (
// TA 新增
TA TranOperate = 1 << iota
// TU 更新
TU
// TD 删除
TD
)
// NewTranAEntity 创建新增实体
// fieldsValue 数据类型(map[string]interface{} or map[string]string or struct)
// 如果fieldsValue为struct类型,只保留非零值字段
func NewTranAEntity(table string, fieldsValue interface{}) TranEntityResult {
var result TranEntityResult
entity := TranEntity{
Table: table,
Operate: TA,
}
var fields map[string]interface{}
err := utils.NewDecoder(fieldsValue).Decode(&fields)
if err != nil {
result.Error = err
}
entity.FieldsValue = fields
result.Entity = entity
return result
}
// NewTranUEntity 创建更新实体
// fieldsValue 数据类型(map[string]interface{} or map[string]string or struct)
// 如果fieldsValue为struct类型,只保留非零值字段
func NewTranUEntity(table string, fieldsValue interface{}, cond QueryCondition) TranEntityResult {
var result TranEntityResult
entity := TranEntity{
Table: table,
Operate: TU,
Condition: cond,
}
var fields map[string]interface{}
err := utils.NewDecoder(fieldsValue).Decode(&fields)
if err != nil {
result.Error = err
return result
}
entity.FieldsValue = fields
result.Entity = entity
return result
}
// NewTranUEntity 创建删除实体
func NewTranDEntity(table string, cond QueryCondition) TranEntityResult {
var result TranEntityResult
result.Entity = TranEntity{
Table: table,
Operate: TD,
Condition: cond,
}
return result
}
// TranEntity 提供事务性操作结构体
type TranEntity struct {
Table string
Operate TranOperate
FieldsValue map[string]interface{}
Condition QueryCondition
}