-
Notifications
You must be signed in to change notification settings - Fork 0
/
xq_inf_entity.go
191 lines (159 loc) · 7.63 KB
/
xq_inf_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package xqi
// 实体编辑器
type EntityEditor interface {
FieldEdit(field EntField, value FieldValue) FieldValue
}
// 实体插入数据监听器
type EntityInsertObserver interface {
// 插入数据之前调用,返回false时,取消插入
BeforeInsert(fields []FieldValue) bool
}
// 实体更新监听器
type EntityUpdateObserver interface {
// 调更前调用,返回false时取消该次更新
BeforeUpdate(fields []FieldValue) bool
}
type EntityDeleteObserver interface {
// 删除之前调用,返回false取消该次删除
BeforeDelete(where SqlCondition) bool
}
// 实体更新规则,如果实体实现该接口,ValidUpdate方法将先用该接口检查数据有效性,检查通过后才更新数据
type EntityUpdateRule interface {
// 返回字段验证规则,规格格式为: [字段名@]规格名[#提示消息]
UpdateRule(cate ...string) []string
}
// 实体插入数据规则检查,如果实体现实了该接口,ValidInsert方法将先调用该接口检查数据有效性,通过后才插入数据
type EntityInsertRule interface {
// 返回字段验证规则,规格格式为: [字段名@]规格名[#提示消息]
InsertRule(cate ...string) []string
}
// 实体更新规则,如果实体实现该接口,ValidUpdate方法将先用该接口检查数据有效性,检查通过后才更新数据
type EntityUpdateChecker interface {
// 检查数据是否有效,返回error != nil 将取消该次更新
CheckUpdateValid(values []FieldValue) ([]FieldValue, error)
}
// 实体插入数据规则检查,如果实体现实了该接口,ValidInsert方法将先调用该接口检查数据有效性,通过后才插入数据
type EntityInsertChecker interface {
// 检查数据是否有效,返回error != nil 取消该次插入
CheckInsertValid(values ...[]FieldValue) ([]FieldValue, error)
}
type BasicEntity interface {
SqlTable
// 关联的实体
ForeignTables() SqlTables
FieldByNameE(fieldName string) EntField
}
type EntityQuery interface {
Select(fields ...SqlField) EntitySelect
// 按指定规则字段返回查询结果
SelectR(rule string) EntitySelect
Open(fields ...SqlField) EntityQueryOpen
// 只查询返回指定规则中的字段
OpenR(rule string) EntityQueryOpen
// 以实体自身方式返回数据查询,返回满足条件的记录行数,从实体自定字段读取数据
OpenTop(count int, where SqlCondition, orders []SqlOrderField, fields ...SqlField) int
// 获取一条数据,如果无指定条件的数据返回false,如果出错会panic
OpenFirst(where SqlCondition, orders []SqlOrderField, fields ...SqlField) bool
// 根据id获取一条数据,如果实体未设置主键或查询出错会panic,如果指定主键值不存在,返回false,其返回值从实体实例字段中读取
OpenById(id interface{}, fields ...SqlField) bool
// 检查数据是否存在
Exists(where func(where SqlCondition, this interface{}) SqlCondition) (bool, error)
// Exists的简化版本,出错时panic
CheckExists(where ...SqlCondition) bool
// 检查条件获取一条记录,简单查询方式
First(where SqlCondition, orders []SqlOrderField, fields ...SqlField) (map[string]interface{}, error)
// 查找数据,简单查询方式
Find(where SqlCondition, orders []SqlOrderField, fields ...SqlField) ([]map[string]interface{}, error)
// 根据主键ID获取一条map数据数据, 出错将会panic
GetById(id interface{}, fields ...SqlField) map[string]interface{}
// 根据条件获取指定条数的数据,出错将会panic
GetTop(count int, where SqlCondition, orders []SqlOrderField, fields ...SqlField) []map[string]interface{}
}
type EntityInsertExecute interface {
Execute() (int, error)
}
type EntityInsert interface {
Execute() (int, error)
From(table SqlTable) EntityInsertExecute
}
type EntityUpdateExecute interface {
Execute() (int, error)
}
type EntityUpdate interface {
Join(table SqlTable, on func(joinEnt SqlTable, leftTables SqlTables) SqlCondition) EntityUpdate
LeftJoin(table SqlTable, on func(joinEnt SqlTable, leftTables SqlTables) SqlCondition) EntityUpdate
RightJoin(table SqlTable, on func(joinEnt SqlTable, leftTables SqlTables) SqlCondition) EntityUpdate
CrossJoin(table SqlTable, on func(joinEnt SqlTable, leftTables SqlTables) SqlCondition) EntityUpdate
Where(where ...SqlCondition) EntityUpdateExecute
}
type EntityCUD interface {
// 根据主键值删除
DeleteByKey(keyVal interface{}) (int, error)
// 根据条件删除
Delete(where ...SqlCondition) (int, error)
// 插入数据
Insert(values ...FieldValue) EntityInsert
// 使用map[string]interface数据源插入
InsertByMap(maps map[string]interface{}) (int, error)
// 分散式插入数据开始, 之后使用字字段的set设置值
BeginInsert() bool
// 提交插入数据
CommitInsert() (int, error)
// 更新
Update(values ...FieldValue) EntityUpdate
// 分散式更新开始, 使用字段的set相关方法设置值后更新
BeginUpdate() bool
// 提交更新
CommitUpdate(where ...SqlCondition) (int, error)
// 插入或更新,如果设置了主键则更新等于主键值的行,否则插入
SaveMap(data map[string]interface{}) (int, error)
// 插入或更新,如果设置了主键则更新等于主键值的行,否则插入
Save(fields ...FieldValue) (int, error)
// 在插入数据之前,先检查数据有效后才插入,实体需要实现EntityInsertChecker接口
ValidInsert(values []FieldValue, validRule ...string) (int, error)
// 在更新数据之前,先检查数据有效后才更新,实体需要实现EntityUpdateChecker接口
ValidUpdate(values []FieldValue, where SqlCondition, validRule ...string) (int, error)
// 使用参数数据源插入数据库,插入之前使用实体定义的插入规则过滤和检查数据有效性
InsertFromParams(params map[string]interface{}) (int, error)
// 使用参数数据源更新数据库,更新之前使用实体定义的更新规则过滤和检查数据有效性
UpdateFromParams(params map[string]interface{}, where ...SqlCondition) (int, error)
}
type EntityTrans interface {
BeginTrans() error
CommitTrans() error
RollbackTrans() error
}
type Entity interface {
BasicEntity
EntityTrans
// 继承的父类
Supper() Entity
// 设置实体继承类实例, 供构架内部调用
Implement(supper interface{})
// 实体构造方法
Constructor(attrs []XqAttribute, inherited ...interface{}) interface{}
// 主键字段
KeyField() EntField
LastSql() string
// 最后插入的id
LastInsertId() int64
EntityQuery
// 在特定数据库中进行操作
Database(dbName string) Entity
// 获取实体插入数据检查规则
GetInsertRules(cate ...string) []string
// 获取实体更新数据检查规则
GetUpdateRules(cate ...string) []string
// 使用更新规则过滤map参数
// @param validParams 默认为false,为true时过滤并检查数据有效性,否则不检查
FilterUpdateParams(params map[string]interface{}, validParams ...bool) (map[string]interface{}, error)
// 使用插入规则过滤map参数
// @param validParams 默认为false,为true时过滤并检查数据有效性,否则不检查
FilterInsertParams(params map[string]interface{}, validParams ...bool) (map[string]interface{}, error)
// 使用插入规则过滤map参数并转换成插入表达式中的值参数列表
// @param validParams 默认为false,为true时过滤并检查数据有效性,否则不检查
GetInsertValuesFromParams(params map[string]interface{}, validParams ...bool) ([]FieldValue, error)
// 使用更新规则过滤map参数并转换成更新表达式中的值参数列表
GetUpdateValuesFromParams(params map[string]interface{}, validParams ...bool) ([]FieldValue, error)
FieldFormat(defineName string) func(old interface{}) interface{}
}