-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger.ts
342 lines (312 loc) · 11.3 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
import { sql } from '@deepcase/hasura/sql';
export interface IOptions {
mpTableName?: string;
graphTableName?: string;
id_field?: string;
to_field?: string;
from_field?: string;
id_type?: string;
}
export const Trigger = ({
mpTableName = 'nodes__mp',
graphTableName = 'nodes',
id_field = 'id',
to_field = 'to_id',
from_field = 'from_id',
id_type = 'integer',
}: IOptions) => ({
downFunctionIsRoot: () => sql`DROP FUNCTION IF EXISTS ${mpTableName}__is_root;`,
upFunctionIsRoot: () => sql`CREATE OR REPLACE FUNCTION ${mpTableName}__is_root(node_id ${id_type}) RETURNS boolean AS $$
DECLARE
DECLARE result BOOLEAN;
BEGIN
SELECT COUNT("id") >= 1
INTO result
FROM
"${mpTableName}"
WHERE
"item_id" = node_id AND
"path_item_id" = node_id AND
"path_item_depth" = 0
LIMIT 1;
RETURN result;
END;
$$ LANGUAGE plpgsql;`,
downFunctionInsertNode: () => sql`DROP FUNCTION IF EXISTS ${mpTableName}__insert_node__function;`,
upFunctionInsertNode: () => sql`CREATE OR REPLACE FUNCTION ${mpTableName}__insert_node__function()
RETURNS TRIGGER AS $trigger$
DECLARE
fromFlow RECORD;
toFlow RECORD;
positionId TEXT;
BEGIN
IF ((NEW."${from_field}" IS NOT NULL AND NEW."${from_field}" != 0) OR (NEW."${to_field}" IS NOT NULL AND NEW."${to_field}" != 0))
THEN
-- IL
-- ILTR
IF (SELECT * FROM ${mpTableName}__is_root(NEW."${to_field}"))
THEN
-- ILS
FOR fromFlow
IN (
-- find all .from flows
SELECT fromFlowItem.*
FROM "${mpTableName}" as fromFlowItem
WHERE
fromFlowItem."item_id" = NEW."${from_field}" AND
fromFlowItem."path_item_id" = NEW."${from_field}"
)
LOOP
SELECT gen_random_uuid() INTO positionId;
-- spread to link
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
SELECT
NEW."${id_field}",
fromItemPath."path_item_id",
fromItemPath."path_item_depth",
fromItemPath."root_id",
positionId
FROM "${mpTableName}" AS fromItemPath
WHERE
fromItemPath."item_id" = fromFlow."item_id" AND
fromItemPath."root_id" = fromFlow."root_id";
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
VALUES
(NEW."${id_field}", NEW."${id_field}", fromFlow."path_item_depth" + 1, fromFlow."root_id", positionId);
FOR toFlow
IN (
-- find all .to and down nodes
SELECT toFlowItem.*
FROM "${mpTableName}" as toFlowItem
WHERE
toFlowItem."root_id" = NEW."${to_field}" AND
toFlowItem."path_item_depth" = 0
)
LOOP
SELECT gen_random_uuid() INTO positionId;
-- toFlow."item_id"
-- clone root flow path with move depth
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
SELECT
toItemPath."item_id",
toItemPath."path_item_id",
toItemPath."path_item_depth" + fromFlow."path_item_depth" + 2,
fromFlow."root_id",
positionId
FROM "${mpTableName}" AS toItemPath
WHERE
toItemPath."position_id" = toFlow."position_id";
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
VALUES
(toFlow."item_id", NEW."${id_field}", fromFlow."path_item_depth" + 1, fromFlow."root_id", positionId);
-- fill path in moved area
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
SELECT
toFlow."item_id",
fromItemPath."path_item_id",
fromItemPath."path_item_depth",
fromItemPath."root_id",
positionId
FROM "${mpTableName}" AS fromItemPath
WHERE
fromItemPath."item_id" = fromFlow."item_id" AND
fromItemPath."root_id" = fromFlow."root_id";
END LOOP;
-- ILTFD
DELETE FROM "${mpTableName}"
WHERE "root_id" = NEW."${to_field}";
END LOOP;
ELSE
-- ILTRF
-- ILS
FOR fromFlow
IN (
-- find all .from flows
SELECT fromFlowItem.*
FROM "${mpTableName}" as fromFlowItem
WHERE
fromFlowItem."item_id" = NEW."${from_field}" AND
fromFlowItem."path_item_id" = NEW."${from_field}"
)
LOOP
SELECT gen_random_uuid() INTO positionId;
-- spread to link
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
SELECT
NEW."${id_field}",
fromItemPath."path_item_id",
fromItemPath."path_item_depth",
fromItemPath."root_id",
positionId
FROM "${mpTableName}" AS fromItemPath
WHERE
fromItemPath."item_id" = fromFlow."item_id" AND
fromItemPath."root_id" = fromFlow."root_id";
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
VALUES
(NEW."${id_field}", NEW."${id_field}", fromFlow."path_item_depth" + 1, fromFlow."root_id", positionId);
FOR toFlow
IN (
-- ILTD ONLY DIFFERENCE!!!
-- find all nodes of link flows next
SELECT
DISTINCT ON (nodesFlowPath."item_id") nodesFlowPath."item_id",
nodesFlowPath."id",
nodesFlowPath."path_item_id",
nodesFlowPath."path_item_depth",
nodesFlowPath."root_id",
nodesFlowPath."position_id"
FROM "${mpTableName}" as nodesFlowPath
WHERE
nodesFlowPath."path_item_id" = NEW."${to_field}"
)
LOOP
SELECT gen_random_uuid() INTO positionId;
-- toFlow."item_id"
-- clone root flow path with move depth
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
SELECT
toItemPath."item_id",
toItemPath."path_item_id",
toItemPath."path_item_depth" + fromFlow."path_item_depth" + 2,
fromFlow."root_id",
positionId
FROM "${mpTableName}" AS toItemPath
WHERE
toItemPath."position_id" = toFlow."position_id" AND
toItemPath."path_item_depth" >= toFlow."path_item_depth";
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
VALUES
(toFlow."item_id", NEW."${id_field}", fromFlow."path_item_depth" + 1, fromFlow."root_id", positionId);
-- fill path in moved area
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
SELECT
toFlow."item_id",
fromItemPath."path_item_id",
fromItemPath."path_item_depth",
fromItemPath."root_id",
positionId
FROM "${mpTableName}" AS fromItemPath
WHERE
fromItemPath."item_id" = fromFlow."item_id" AND
fromItemPath."root_id" = fromFlow."root_id";
END LOOP;
END LOOP;
END IF;
ELSE
-- IN
-- INR
-- INRR
INSERT INTO "${mpTableName}"
("item_id","path_item_id","path_item_depth","root_id","position_id")
VALUES
(NEW."${id_field}",NEW."${id_field}",0,NEW."${id_field}",gen_random_uuid());
END IF;
RETURN NEW;
END;
$trigger$ LANGUAGE plpgsql;`,
downFunctionWillRoot: () => sql`DROP FUNCTION IF EXISTS ${mpTableName}__will_root;`,
upFunctionWillRoot: () => sql`CREATE OR REPLACE FUNCTION ${mpTableName}__will_root(node_id ${id_type}, link_id ${id_type}) RETURNS boolean AS $$
DECLARE
DECLARE result BOOLEAN;
BEGIN
SELECT COUNT("${id_field}") = 0
INTO result
FROM
"${graphTableName}"
WHERE
"${to_field}" = node_id AND
"id" != link_id
LIMIT 1;
RETURN result;
END;
$$ LANGUAGE plpgsql;`,
downFunctionDeleteNode: () => sql`DROP FUNCTION IF EXISTS ${mpTableName}__delete_node__function;`,
upFunctionDeleteNode: () => sql`CREATE OR REPLACE FUNCTION ${mpTableName}__delete_node__function()
RETURNS TRIGGER AS $trigger$
DECLARE
linkFlow RECORD;
nodesFlow RECORD;
BEGIN
IF ((OLD."${from_field}" IS NOT NULL AND OLD."${from_field}" != 0) OR (OLD."${to_field}" IS NOT NULL AND OLD."${to_field}" != 0))
THEN
-- DL
IF (SELECT * FROM ${mpTableName}__will_root(OLD."${to_field}", OLD."id"))
THEN
-- DLWRF
FOR nodesFlow
IN (
-- find all nodes of link flows next
SELECT
DISTINCT ON (nodesFlowPath."item_id") nodesFlowPath."item_id",
nodesFlowPath."id",
nodesFlowPath."path_item_id",
nodesFlowPath."path_item_depth",
nodesFlowPath."root_id",
nodesFlowPath."position_id"
FROM "${mpTableName}" as nodesFlowPath
WHERE
nodesFlowPath."path_item_id" = OLD."id"
)
LOOP
DELETE FROM "${mpTableName}"
WHERE
"position_id" = nodesFlow."position_id" AND
"path_item_depth" <= nodesFlow."path_item_depth";
UPDATE "${mpTableName}"
SET
"path_item_depth" = "path_item_depth" - (nodesFlow."path_item_depth" + 1),
"root_id" = OLD."${to_field}"
WHERE "position_id" = nodesFlow."position_id";
END LOOP;
-- DLWRF
FOR linkFlow
IN (
-- find all path items of link next
SELECT linkFlowPath.*
FROM "${mpTableName}" as linkFlowPath
WHERE
linkFlowPath."path_item_id" = OLD."id"
)
LOOP
DELETE FROM "${mpTableName}"
WHERE "position_id" = linkFlow."position_id";
END LOOP;
ELSE
-- DLWRF
FOR linkFlow
IN (
-- find all path items of link next
SELECT linkFlowPath.*
FROM "${mpTableName}" as linkFlowPath
WHERE
linkFlowPath."path_item_id" = OLD."id"
)
LOOP
DELETE FROM "${mpTableName}"
WHERE "position_id" = linkFlow."position_id";
END LOOP;
END IF;
ELSE
END IF;
-- DN
DELETE FROM "${mpTableName}"
WHERE "item_id" = OLD."id";
RETURN OLD;
END;
$trigger$ LANGUAGE plpgsql;`,
downTriggerDelete: () => sql`DROP TRIGGER IF EXISTS ${mpTableName}__delete_node__trigger ON "${graphTableName}";`,
upTriggerDelete: () => sql`CREATE TRIGGER ${mpTableName}__delete_node__trigger AFTER DELETE ON "${graphTableName}" FOR EACH ROW EXECUTE PROCEDURE ${mpTableName}__delete_node__function();`,
downTriggerInsert: () => sql`DROP TRIGGER IF EXISTS ${mpTableName}__insert_node__trigger ON "${graphTableName}";`,
upTriggerInsert: () => sql`CREATE TRIGGER ${mpTableName}__insert_node__trigger AFTER INSERT ON "${graphTableName}" FOR EACH ROW EXECUTE PROCEDURE ${mpTableName}__insert_node__function();`,
});