-
Notifications
You must be signed in to change notification settings - Fork 2
/
trigger.ts
545 lines (503 loc) · 21.8 KB
/
trigger.ts
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import { sql } from '@deep-foundation/hasura/sql';
const call = (strOrFn: string | ((action: string) => string), action: string): string => {
if (typeof(strOrFn) === 'function') return strOrFn(action);
return strOrFn;
};
export interface IOptions {
mpTableName?: string;
graphTableName?: string;
id_field?: string;
to_field?: string;
from_field?: string;
id_type?: string;
iteratorInsertDeclare?: string;
iteratorInsertBegin?: string;
iteratorInsertEnd?: string;
iteratorDeleteArgumentSend?: string;
iteratorDeleteArgumentGet?: string;
iteratorDeleteDeclare?: string;
iteratorDeleteBegin?: string;
iteratorDeleteEnd?: string;
groupInsert?: string;
groupDelete?: string;
additionalFields?: string | ((action: string) => string);
additionalData?: string | ((action: string) => string);
isAllowSpreadFromCurrent?: string;
isAllowSpreadCurrentTo?: string;
isAllowSpreadToCurrent?: string;
isAllowSpreadCurrentFrom?: string;
isAllowSpreadToInCurrent?: string;
isAllowSpreadCurrentFromOut?: string;
isAllowSpreadFromOutCurrent?: string;
isAllowSpreadCurrentToIn?: string;
postfix?: string;
}
const wrapAnd = (code) => code ? 'AND '+code : '';
export const Trigger = ({
mpTableName = 'links__mp',
graphTableName = 'links',
id_field = 'id',
to_field = 'to_id',
from_field = 'from_id',
id_type = 'integer',
iteratorInsertDeclare = '',
iteratorInsertBegin = '',
iteratorInsertEnd = '',
iteratorDeleteArgumentSend = '',
iteratorDeleteArgumentGet = '',
iteratorDeleteDeclare = '',
iteratorDeleteBegin = '',
iteratorDeleteEnd = '',
groupInsert = '',
groupDelete = '',
additionalFields = '',
additionalData = '',
isAllowSpreadFromCurrent = '',
isAllowSpreadCurrentTo = '',
isAllowSpreadToCurrent = 'FALSE',
isAllowSpreadCurrentFrom = 'FALSE',
isAllowSpreadToInCurrent = '',
isAllowSpreadCurrentFromOut = '',
isAllowSpreadFromOutCurrent = 'FALSE',
isAllowSpreadCurrentToIn = 'FALSE',
postfix = '',
}: IOptions) => {
const result = {
downFunctionInsertNode: () => sql`
DROP FUNCTION IF EXISTS ${mpTableName+postfix}__insert_link__function CASCADE;
DROP FUNCTION IF EXISTS ${mpTableName+postfix}__insert_link__function_core CASCADE;
`,
upFunctionInsertNode: () => sql`CREATE OR REPLACE FUNCTION ${mpTableName+postfix}__insert_link__function_core(NEW RECORD, groupid ${id_type} DEFAULT 0)
RETURNS VOID AS $trigger$
DECLARE
insertCategory TEXT;
incomingFlow RECORD;
outcomingFlow RECORD;
outcomingFlowDown RECORD;
currentFlow RECORD;
positionId TEXT;
CURRENT RECORD;
${iteratorInsertDeclare}
BEGIN
${result.upFunctionInsertNodeCore().toString()}
END;
$trigger$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION ${mpTableName+postfix}__insert_link__function()
RETURNS TRIGGER AS $trigger$
BEGIN
PERFORM ${mpTableName+postfix}__insert_link__function_core(NEW);
RETURN NEW;
END;
$trigger$ LANGUAGE plpgsql;`,
upFunctionInsertNodeCore: () => sql`
CURRENT:=NEW;
SELECT gen_random_uuid() INTO insertCategory;
${iteratorInsertBegin}
FOR incomingFlow
IN (
SELECT flowItem.*
FROM "${graphTableName}" as flowLink, "${mpTableName}" as flowItem
WHERE
(
-- select: FROM to CURRENT
flowLink."${id_field}" = NEW."${from_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadFromCurrent)}
) OR (
-- select: TO to CURRENT
flowLink."${id_field}" = NEW."${to_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadToCurrent)}
) OR (
-- select: IN to CURRENT
flowLink."${to_field}" = NEW."${id_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadToInCurrent)}
) OR (
-- select: OUT to CURRENT
flowLink."${from_field}" = NEW."${id_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadFromOutCurrent)}
)
)
LOOP
-- CURRENT recursion UP?
IF EXISTS (
SELECT * FROM "${mpTableName}" AS spreadingFlows WHERE
spreadingFlows."position_id" = incomingFlow."position_id" AND
spreadingFlows."item_id" = incomingFlow."item_id" AND
spreadingFlows."path_item_id" = NEW."${id_field}"
) THEN
RAISE EXCEPTION 'recursion detected for link #% in incomingFlow mp #%', NEW."${id_field}", incomingFlow."id";
END IF;
SELECT gen_random_uuid() INTO positionId;
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id","insert_category","group_id"${call(additionalFields, 'fromIn')})
SELECT
NEW."${id_field}",
fromInItemPath."path_item_id",
fromInItemPath."path_item_depth",
fromInItemPath."root_id",
positionId,
insertCategory,
${groupInsert}
${call(additionalData, `(SELECT concat('fromIn ',NEW."${id_field}",' ', incomingFlow."id"))`)}
FROM "${mpTableName}" AS fromInItemPath
WHERE
fromInItemPath."item_id" = incomingFlow."item_id" AND
fromInItemPath."position_id" = incomingFlow."position_id";
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id","insert_category","group_id"${call(additionalFields, 'current')})
VALUES
(NEW."${id_field}",NEW."${id_field}",incomingFlow."path_item_depth" + 1,incomingFlow."root_id",positionId,insertCategory,${groupInsert}${call(additionalData, `(SELECT concat('current ',NEW."${id_field}"))`)});
END LOOP;
-- CURRENT has ALL spreaded mp rows now
-- IF NOT PARENT FLOWS - INSERT ITEM ROOT
IF (
SELECT COUNT("id") = 0
FROM "${mpTableName}"
WHERE
"item_id" = NEW."${id_field}" AND
"group_id" = ${groupInsert}
LIMIT 1
)
THEN
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id","insert_category","group_id"${call(additionalFields, 'root')})
VALUES
(NEW."${id_field}",NEW."${id_field}",0,NEW."${id_field}",gen_random_uuid(),insertCategory,${groupInsert}${call(additionalData, `(SELECT concat('root ',NEW."${id_field}"))`)});
END IF;
FOR currentFlow
IN (
SELECT currentFlowItem.*
FROM "${mpTableName}" as currentFlowItem
WHERE
currentFlowItem."item_id" = NEW."${id_field}" AND
currentFlowItem."path_item_id" = NEW."${id_field}" AND
currentFlowItem."group_id" = ${groupInsert}
)
LOOP
FOR outcomingFlow
IN (
SELECT
DISTINCT ON (flowItem."item_id") "item_id",flowItem."path_item_id", flowItem."path_item_depth", flowItem."group_id", flowItem."position_id"
FROM "${graphTableName}" as flowLink, "${mpTableName}" as flowItem
WHERE
(
-- select: CURRENT to TO.
flowLink."${id_field}" = NEW."${to_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadCurrentTo)}
) OR (
-- select: CURRENT to FROM.
flowLink."${id_field}" = NEW."${from_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadCurrentFrom)}
) OR (
-- select: CURRENT to OUT
flowLink."${from_field}" = NEW."${id_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadCurrentFromOut)}
) OR (
-- select: CURRENT to IN
flowLink."${to_field}" = NEW."${id_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadCurrentToIn)}
) AND
flowItem."insert_category" != insertCategory
)
LOOP
FOR outcomingFlowDown
IN (
SELECT
DISTINCT ON (toOutFlowDownItems."item_id") "item_id",toOutFlowDownItems."path_item_id", toOutFlowDownItems."path_item_depth", toOutFlowDownItems."group_id", toOutFlowDownItems."position_id", toOutFlowDownItems."id"
FROM "${mpTableName}" AS toOutFlowDownItems
WHERE
toOutFlowDownItems."group_id" = outcomingFlow."group_id" AND
toOutFlowDownItems."path_item_id" = outcomingFlow."path_item_id" AND
toOutFlowDownItems."insert_category" != insertCategory AND
toOutFlowDownItems."id" IN (
SELECT toOutFlowDownPath."id"
FROM
"${mpTableName}" AS toOutFlowDownPath,
"${mpTableName}" AS toOutFlowPath
WHERE
toOutFlowPath."position_id" = outcomingFlow."position_id" AND
toOutFlowPath."item_id" = outcomingFlow."item_id" AND
toOutFlowPath."path_item_depth" <= outcomingFlow."path_item_depth" AND
toOutFlowDownPath."position_id" = toOutFlowDownItems."position_id" AND
toOutFlowDownPath."item_id" = toOutFlowDownItems."item_id" AND
toOutFlowDownPath."path_item_depth" <= outcomingFlow."path_item_depth" AND
toOutFlowPath."path_item_id" = toOutFlowDownPath."path_item_id" AND
toOutFlowPath."path_item_depth" = toOutFlowDownPath."path_item_depth"
)
)
LOOP
-- UP recursion DOWN?
IF EXISTS (
SELECT * FROM "${mpTableName}" AS spreadingFlows WHERE
spreadingFlows."position_id" = currentFlow."position_id" AND
spreadingFlows."item_id" = currentFlow."item_id" AND
spreadingFlows."group_id" = currentFlow."group_id" AND
spreadingFlows."path_item_id" = outcomingFlowDown."item_id"
) THEN
RAISE EXCEPTION 'recursion detected for link #% in spreadingFlow mp #% outcomingFlowDown mp #%', NEW."${id_field}", currentFlow."id", outcomingFlowDown."id";
END IF;
SELECT gen_random_uuid() INTO positionId;
-- add prev flows current to to/out flow
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id","insert_category","group_id"${call(additionalFields, 'currentFlow')})
SELECT
outcomingFlowDown."item_id",
spreadingFlows."path_item_id",
spreadingFlows."path_item_depth",
spreadingFlows."root_id",
positionId,
insertCategory,
${groupInsert}
${call(additionalData, `(SELECT concat('currentFlow ',NEW."${id_field}"))`)}
FROM "${mpTableName}" AS spreadingFlows
WHERE
spreadingFlows."position_id" = currentFlow."position_id" AND
spreadingFlows."item_id" = currentFlow."item_id" AND
spreadingFlows."group_id" = currentFlow."group_id";
-- clone exists flows of to/out
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id","insert_category","group_id"${call(additionalFields, 'toOut')})
SELECT
toOutItemsI."item_id",
toOutItemsI."path_item_id",
toOutItemsI."path_item_depth" + currentFlow."path_item_depth" + 1 - outcomingFlow."path_item_depth",
currentFlow."root_id",
positionId,
insertCategory,
${groupInsert}
${call(additionalData, `(SELECT concat('toOut ',NEW."${id_field}"))`)}
FROM "${mpTableName}" AS toOutItemsI
WHERE
toOutItemsI."item_id" = outcomingFlowDown."item_id" AND
toOutItemsI."position_id" = outcomingFlowDown."position_id" AND
toOutItemsI."path_item_depth" >= outcomingFlow."path_item_depth" AND
toOutItemsI."group_id" = outcomingFlowDown."group_id";
END LOOP;
END LOOP;
END LOOP;
-- delete trash roots
DELETE FROM "${mpTableName}"
WHERE
"group_id" = ${groupInsert} AND
"root_id" IN (
SELECT flowItem."item_id"
FROM "${graphTableName}" as flowLink, "${mpTableName}" as flowItem
WHERE
(
-- select: CURRENT to TO.
flowLink."${id_field}" = NEW."${to_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadCurrentTo)}
) OR (
-- select: CURRENT to FROM.
flowLink."${id_field}" = NEW."${from_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadCurrentFrom)}
) OR (
-- select: CURRENT to OUT
flowLink."${from_field}" = NEW."${id_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadCurrentFromOut)}
) OR (
-- select: CURRENT to IN
flowLink."${to_field}" = NEW."${id_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupInsert}
${wrapAnd(isAllowSpreadCurrentToIn)}
)
);
${iteratorInsertEnd}`,
downFunctionDeleteNode: () => sql`
DROP FUNCTION IF EXISTS ${mpTableName+postfix}__delete_link__function CASCADE;
DROP FUNCTION IF EXISTS ${mpTableName+postfix}__delete_link__function_core CASCADE;
`,
upFunctionDeleteNode: () => sql`CREATE OR REPLACE FUNCTION ${mpTableName+postfix}__delete_link__function_core(OLD RECORD${iteratorDeleteArgumentGet ? `,${iteratorDeleteArgumentGet}` : ''})
RETURNS VOID AS $trigger$
DECLARE
linkFlow RECORD;
toOutItems RECORD;
inFromFlow RECORD;
CURRENT RECORD;
BEGIN
${result.upFunctionDeleteNodeCore().toString()}
END;
$trigger$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION ${mpTableName+postfix}__delete_link__function()
RETURNS TRIGGER AS $trigger$
DECLARE
${iteratorDeleteDeclare}
BEGIN
${iteratorDeleteBegin}
PERFORM ${mpTableName+postfix}__delete_link__function_core(OLD${iteratorDeleteArgumentSend ? `,${iteratorDeleteArgumentSend}` : ''});
${iteratorDeleteEnd}
RETURN OLD;
END;
$trigger$ LANGUAGE plpgsql;`,
upFunctionDeleteNodeCore: () => sql`
CURRENT:=OLD;
FOR toOutItems
IN (
SELECT flowItem.*
FROM "${graphTableName}" as flowLink, "${mpTableName}" as flowItem
WHERE
(
-- select: CURRENT to TO.
flowLink."${id_field}" = OLD."${to_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupDelete}
${wrapAnd(isAllowSpreadCurrentTo)}
) OR (
-- select: CURRENT to FROM.
flowLink."${id_field}" = OLD."${from_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupDelete}
${wrapAnd(isAllowSpreadCurrentFrom)}
) OR (
-- select: CURRENT to OUT
flowLink."${from_field}" = OLD."${id_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupDelete}
${wrapAnd(isAllowSpreadCurrentFromOut)}
) OR (
-- select: CURRENT to IN
flowLink."${to_field}" = OLD."${id_field}" AND
flowItem."item_id" = flowLink."${id_field}" AND
flowItem."path_item_id" = flowLink."${id_field}" AND
flowItem."group_id" = ${groupDelete}
${wrapAnd(isAllowSpreadCurrentToIn)}
)
)
LOOP
IF (
(
SELECT COUNT(*) FROM "${mpTableName}" WHERE
"item_id" = "path_item_id" AND "item_id" = OLD."${id_field}" AND
"group_id" = ${groupDelete}
) = (
SELECT COUNT(*) FROM "${mpTableName}" WHERE
"item_id" = "path_item_id" AND "item_id" = toOutItems."item_id" AND
"group_id" = ${groupDelete}
)
) THEN
SELECT inFromItems.* INTO inFromFlow
FROM "${mpTableName}" as inFromItems
WHERE
"item_id" = toOutItems."path_item_id" AND
"path_item_id" = toOutItems."path_item_id" AND
"group_id" = ${groupDelete} LIMIT 1;
UPDATE "${mpTableName}"
SET
"path_item_depth" = "path_item_depth" - (inFromFlow."path_item_depth"),
"root_id" = toOutItems."path_item_id"
WHERE
"id" IN (
SELECT toUpdate."id"
FROM "${mpTableName}" as toOutDown, "${mpTableName}" as toUpdate
WHERE
toOutDown."path_item_id" = inFromFlow."path_item_id" AND
toUpdate."position_id" = toOutDown."position_id"
);
DELETE FROM "${mpTableName}"
WHERE
"id" IN (
SELECT toDelete."id"
FROM "${mpTableName}" as toOutDown, "${mpTableName}" as toDelete
WHERE
toOutDown."path_item_id" = inFromFlow."path_item_id" AND
toOutDown."group_id" = ${groupDelete} AND
toDelete."position_id" = toOutDown."position_id" AND
toDelete."path_item_depth" < 0 AND
toDelete."group_id" = ${groupDelete}
);
ELSE
DELETE FROM "${mpTableName}"
WHERE
"id" IN (
SELECT toDelete."id"
FROM
"${mpTableName}" as toDelete,
"${mpTableName}" as childs
WHERE
childs."group_id" = ${groupDelete} AND
childs."path_item_id" = OLD."${id_field}" AND
toDelete."position_id" = childs."position_id"
);
END IF;
END LOOP;
-- DN
DELETE FROM "${mpTableName}"
WHERE "item_id" = OLD."${id_field}" AND "group_id" = ${groupDelete};`,
downTriggerDelete: () => sql`DROP TRIGGER IF EXISTS ${mpTableName+postfix}__delete_link__trigger ON "${graphTableName}" CASCADE;`,
upTriggerDelete: () => sql`CREATE TRIGGER ${mpTableName+postfix}__delete_link__trigger AFTER DELETE ON "${graphTableName}" FOR EACH ROW EXECUTE PROCEDURE ${mpTableName+postfix}__delete_link__function();`,
downTriggerInsert: () => sql`DROP TRIGGER IF EXISTS ${mpTableName+postfix}__insert_link__trigger ON "${graphTableName}" CASCADE;`,
upTriggerInsert: () => sql`CREATE TRIGGER ${mpTableName+postfix}__insert_link__trigger AFTER INSERT ON "${graphTableName}" FOR EACH ROW EXECUTE PROCEDURE ${mpTableName+postfix}__insert_link__function();`,
downTriggerUpdate: () => sql`DROP TRIGGER IF EXISTS ${mpTableName+postfix}__update_link__trigger ON "${graphTableName}" CASCADE;`,
upTriggerUpdate: () => sql`CREATE TRIGGER ${mpTableName+postfix}__update_link__trigger AFTER UPDATE ON "${graphTableName}" FOR EACH ROW EXECUTE PROCEDURE ${mpTableName+postfix}__update_link__function();`,
downFunctionUpdateNode: () => sql`
DROP FUNCTION IF EXISTS ${mpTableName+postfix}__update_link__function CASCADE;
DROP FUNCTION IF EXISTS ${mpTableName+postfix}__update_link__function_core CASCADE;
`,
upFunctionUpdateNode: () => sql`CREATE OR REPLACE FUNCTION ${mpTableName+postfix}__update_link__function_core(OLD RECORD, NEW RECORD, groupid ${id_type} DEFAULT 0)
RETURNS VOID AS $trigger$
DECLARE
insertCategory TEXT;
incomingFlow RECORD;
outcomingFlow RECORD;
outcomingFlowDown RECORD;
currentFlow RECORD;
positionId TEXT;
CURRENT RECORD;
linkFlow RECORD;
toOutItems RECORD;
inFromFlow RECORD;
${iteratorInsertDeclare}
BEGIN
${iteratorDeleteBegin}
${result.upFunctionDeleteNodeCore().toString()}
${iteratorDeleteEnd}
${result.upFunctionInsertNodeCore().toString()}
END;
$trigger$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION ${mpTableName+postfix}__update_link__function()
RETURNS TRIGGER AS $trigger$
BEGIN
PERFORM ${mpTableName+postfix}__update_link__function_core(OLD, NEW);
RETURN NEW;
END;
$trigger$ LANGUAGE plpgsql;`,
};
return result;
};