@@ -24,12 +24,12 @@ type Node struct {
2424 Where []g.Map // 条件
2525 RowKey string // 主键
2626
27- structure Structure
27+ structure * db. Structure
2828
2929 keyNode map [string ]* Node
3030}
3131
32- func newNode (key string , req []g.Map , structure Structure ) Node {
32+ func newNode (key string , req []g.Map , structure * db. Structure ) Node {
3333 return Node {
3434 Key : key , req : req , structure : structure ,
3535 }
@@ -72,7 +72,7 @@ func (n *Node) parse(ctx context.Context, method string) error {
7272
7373 key := n .Key
7474 if strings .HasSuffix (key , consts .ListKeySuffix ) {
75- key = key [0 : len (key )- 2 ] // todo 提取util, 获取非数组的key
75+ key = key [0 : len (key )- 2 ]
7676 }
7777 access , err := db .GetAccess (key , true )
7878
@@ -216,6 +216,7 @@ func (n *Node) checkReq() error {
216216 return nil
217217}
218218
219+ // reqUpdate 处理 Update/Insert等
219220func (n * Node ) reqUpdate () error {
220221
221222 for i , _ := range n .req {
@@ -224,11 +225,11 @@ func (n *Node) reqUpdate() error {
224225 if strings .HasSuffix (key , consts .FunctionsKeySuffix ) {
225226 functionName , paramKeys := functions .ParseFunctionsStr (updateVal .(string ))
226227 var param = g.Map {}
227- for _ , key := range paramKeys {
228- if key == "$req" {
229- param [key ] = n .Data [i ]
228+ for _ , paramKey := range paramKeys {
229+ if paramKey == consts . FunctionOriReqParam {
230+ param [paramKey ] = n .Data [i ]
230231 } else {
231- param [key ] = n.Data [i ][key ]
232+ param [paramKey ] = n.Data [i ][paramKey ]
232233 }
233234 }
234235 k := key [0 : len (key )- 2 ]
@@ -250,10 +251,20 @@ func (n *Node) reqUpdate() error {
250251 }
251252 }
252253
254+ }
255+
256+ return nil
257+ }
258+
259+ // reqUpdate 处理 Update/Insert等 (事务内)
260+ func (n * Node ) reqUpdateBeforeDo () error {
261+
262+ for i , _ := range n .req {
263+
253264 for k , v := range n .Data [i ] {
254- if strings .HasSuffix (k , "@" ) {
265+ if strings .HasSuffix (k , consts . RefKeySuffix ) {
255266 refNodeKey , refCol := parseRefCol (v .(string ))
256- if strings .HasSuffix (refNodeKey , "[]" ) { // 双列表
267+ if strings .HasSuffix (refNodeKey , consts . ListKeySuffix ) { // 双列表
257268 n.Data [i ][k ] = n .keyNode [refNodeKey ].Data [i ][config .GetDbFieldStyle ()(n .ctx , n .TableName , refCol )]
258269 } else {
259270 n.Data [i ][k ] = n .keyNode [refNodeKey ].Data [0 ][config .GetDbFieldStyle ()(n .ctx , n .TableName , refCol )]
@@ -265,18 +276,19 @@ func (n *Node) reqUpdate() error {
265276 return nil
266277}
267278
268- func (n * Node ) do (ctx context.Context , method string , i int ) (ret g.Map , err error ) {
279+ func (n * Node ) do (ctx context.Context , method string , dataIndex int ) (ret g.Map , err error ) {
269280
270- // todo 此处运行会导致事务时长与hook时长相关,特别是hook中运行了io类型的操作, 故需要调整到事务外去执行, 且如果事务失败, 则不执行after, 可以改成增加error
271281 for _ , hook := range hooks {
272- if hook .Before != nil {
273- err : = hook .Before (n , method )
282+ if hook .BeforeDo != nil {
283+ err = hook .BeforeDo (n , method )
274284 if err != nil {
275285 return nil , err
276286 }
277287 }
278288 }
279289
290+ var count int64
291+
280292 switch method {
281293 case consts .MethodPost :
282294
@@ -295,7 +307,9 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
295307
296308 }
297309
298- id , count , err := db .Insert (ctx , n .TableName , n .Data )
310+ var id int64
311+
312+ id , count , err = db .Insert (ctx , n .TableName , n .Data )
299313 if err != nil {
300314 return nil , err
301315 }
@@ -317,7 +331,7 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
317331 }
318332
319333 case consts .MethodPut :
320- count , err : = db .Update (ctx , n .TableName , n .Data [i ], n .Where [i ])
334+ count , err = db .Update (ctx , n .TableName , n .Data [dataIndex ], n .Where [dataIndex ])
321335 if err != nil {
322336 return nil , err
323337 }
@@ -327,7 +341,7 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
327341 "count" : count ,
328342 }
329343 case consts .MethodDelete :
330- count , err : = db .Delete (ctx , n .TableName , n .Where [i ])
344+ count , err = db .Delete (ctx , n .TableName , n .Where [dataIndex ])
331345 if err != nil {
332346 return nil , err
333347 }
@@ -343,8 +357,8 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
343357 }
344358
345359 for _ , hook := range hooks {
346- if hook .After != nil {
347- err : = hook .After (n , method )
360+ if hook .AfterDo != nil {
361+ err = hook .AfterDo (n , method )
348362 if err != nil {
349363 return nil , err
350364 }
@@ -356,14 +370,11 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
356370
357371func (n * Node ) execute (ctx context.Context , method string ) (g.Map , error ) {
358372
359- // 参数替换
360- err := n .reqUpdate () // todo 处理放到事务外, 减短事务时长
373+ err := n .reqUpdateBeforeDo ()
361374 if err != nil {
362375 return nil , err
363376 }
364377
365- // 执行操作
366-
367378 if method == consts .MethodPost { // 新增时可以合并新增
368379 ret , err := n .do (ctx , method , 0 )
369380 if err != nil {
@@ -372,12 +383,11 @@ func (n *Node) execute(ctx context.Context, method string) (g.Map, error) {
372383 return ret , nil
373384 } else {
374385 for i , _ := range n .req {
375- _ , err = n .do (ctx , method , i )
386+ _ , err : = n .do (ctx , method , i )
376387 if err != nil {
377388 return nil , err
378389 }
379390 }
380-
381391 }
382392
383393 return g.Map {
0 commit comments