2 Module : Gargantext.Database.Triggers.NodesNodes
3 Description : Triggers configuration
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Triggers on NodesNodes table.
14 {-# LANGUAGE QuasiQuotes #-}
16 module Gargantext.Database.Admin.Trigger.NodesNodes
19 import Database.PostgreSQL.Simple.SqlQQ (sql)
20 -- import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
21 import Gargantext.Database.Admin.Config (nodeTypeId)
22 import Gargantext.Database.Admin.Types.Node -- (ListId, CorpusId, NodeId)
23 import Gargantext.Core.Types.Main (listTypeId, ListType(CandidateTerm))
24 import Gargantext.Database.Prelude (Cmd, execPGSQuery)
25 import Gargantext.Prelude
26 import qualified Database.PostgreSQL.Simple as DPS
28 type MasterListId = ListId
30 triggerDeleteCount :: MasterListId -> Cmd err Int64
31 triggerDeleteCount lId = execPGSQuery query (lId, nodeTypeId NodeList)
35 CREATE OR REPLACE FUNCTION set_delete_count() RETURNS trigger AS $$
37 UPDATE node_node_ngrams SET weight = weight - d.delete_count
38 FROM (SELECT old1.node1_id as node1_id, lists.id as node2_id, nnn.ngrams_id as ngrams_id, nnn.ngrams_type as ngrams_type, count(*) as delete_count FROM OLD as old1
39 INNER JOIN nodes doc ON doc.id = old1.node2_id
40 INNER JOIN nodes lists ON lists.parent_id = old1.node1_id
41 INNER JOIN node_node_ngrams nnn ON nnn.node2_id = doc.id
42 WHERE nnn.node1_id in (?, lists.id)
43 AND lists.typename = ?
44 GROUP BY old1.node1_id, lists.id, nnn.ngrams_id, nnn.ngrams_type
46 WHERE node_node_ngrams.node1_id = d.node1_id
47 AND node_node_ngrams.node2_id = d.node2_id
48 AND node_node_ngrams.ngrams_id = d.ngrams_id
49 AND node_node_ngrams.ngrams_type = d.ngrams_type
55 -- DROP trigger trigger_delete_count on nodes_nodes;
56 CREATE TRIGGER trigger_delete_count AFTER DELETE on nodes_nodes
57 REFERENCING OLD TABLE AS OLD
59 EXECUTE PROCEDURE set_delete_count();
62 triggerInsertCount :: MasterListId -> Cmd err Int64
63 triggerInsertCount lId = execPGSQuery query (lId, nodeTypeId NodeList)
67 CREATE OR REPLACE FUNCTION set_insert_count() RETURNS trigger AS $$
69 INSERT INTO node_node_ngrams (node1_id, node2_id, ngrams_id, ngrams_type, weight)
70 SELECT new1.node1_id , lists.id, nnn.ngrams_id, nnn.ngrams_type, count(*) as weight from NEW as new1
71 INNER JOIN nodes doc ON doc.id = new1.node2_id
72 INNER JOIN nodes lists ON lists.parent_id = new1.node1_id
73 INNER JOIN node_node_ngrams nnn ON nnn.node2_id = doc.id
74 WHERE nnn.node1_id in (?, lists.id)
75 AND lists.typename = ?
76 GROUP BY new1.node1_id, lists.id, nnn.ngrams_id, nnn.ngrams_type
77 ON CONFLICT (node1_id, node2_id, ngrams_id, ngrams_type)
78 DO UPDATE set weight = node_node_ngrams.weight + excluded.weight
84 -- DROP trigger trigger_insert_count on nodes_nodes;
85 CREATE TRIGGER trigger_insert_count AFTER INSERT on nodes_nodes
86 REFERENCING NEW TABLE AS NEW
88 EXECUTE PROCEDURE set_insert_count();
91 triggerUpdateAdd :: MasterListId -> Cmd err Int64
92 triggerUpdateAdd lId = execPGSQuery query (lId, nodeTypeId NodeList)
96 CREATE OR REPLACE FUNCTION set_update_ngrams_add() RETURNS trigger AS $$
98 UPDATE node_node_ngrams nnn0 SET weight = weight + d.fix_count
99 FROM (SELECT new1.node1_id as node1_id, lists.id as node2_id, nnn.ngrams_id as ngrams_id, nnn.ngrams_type as ngrams_type, count(*) as fix_count
101 INNER JOIN nodes lists ON new1.node1_id = lists.parent_id
102 INNER JOIN node_node_ngrams nnn ON new1.node2_id = nnn.node2_id
103 WHERE nnn.node1_id in (?, lists.id) -- (masterList_id, userLists)
104 AND lists.typename = ?
105 GROUP BY new1.node1_id, lists.id, nnn.ngrams_id, nnn.ngrams_type
107 WHERE nnn0.node1_id = d.node1_id
108 AND nnn0.node2_id = d.node2_id
109 AND nnn0.ngrams_id = d.ngrams_id
110 AND nnn0.ngrams_type = d.ngrams_type
116 -- DROP trigger trigger_count_update_add on nodes_nodes;
117 CREATE TRIGGER trigger_count_update_add AFTER UPDATE on nodes_nodes
118 REFERENCING OLD TABLE AS OLD NEW TABLE AS NEW
120 WHEN (OLD.category <= 0 AND NEW.category >= 1)
121 EXECUTE PROCEDURE set_update_ngrams_add();
124 triggerUpdateDel :: MasterListId -> Cmd err Int64
125 triggerUpdateDel lId = execPGSQuery query (lId, nodeTypeId NodeList)
129 CREATE OR REPLACE FUNCTION set_update_ngrams_count_del() RETURNS trigger AS $$
131 UPDATE node_node_ngrams nnn0 SET weight = weight - d.fix_count
132 FROM (SELECT new1.node1_id as node1_id, lists.id as node2_id, nnn.ngrams_id as ngrams_id, nnn.ngrams_type as ngrams_type, count(*) as fix_count
134 INNER JOIN nodes lists ON new1.node1_id = lists.parent_id
135 INNER JOIN node_node_ngrams nnn ON new1.node2_id = nnn.node2_id
136 WHERE nnn.node1_id in (?, lists.id) -- (masterList_id, userLists)
137 AND lists.typename = ?
138 GROUP BY new1.node1_id, lists.id, nnn.ngrams_id, nnn.ngrams_type
140 WHERE nnn0.node1_id = d.node1_id
141 AND nnn0.node2_id = d.node2_id
142 AND nnn0.ngrams_id = d.ngrams_id
143 AND nnn0.ngrams_type = d.ngrams_type
149 -- DROP trigger trigger_count_delete2 on nodes_nodes;
150 CREATE TRIGGER trigger_count_delete2 AFTER UPDATE on nodes_nodes
151 REFERENCING OLD TABLE AS OLD NEW TABLE AS NEW
153 WHEN (OLD.category >= 1 AND NEW.category <= 0)
154 EXECUTE PROCEDURE set_update_ngrams_count_del();
159 triggerCoocInsert :: MasterListId -> Cmd err Int64
160 triggerCoocInsert lid = execPGSQuery query ( lid
161 -- , nodeTypeId NodeCorpus
162 -- , nodeTypeId NodeDocument
163 -- , nodeTypeId NodeList
164 , listTypeId CandidateTerm
165 , listTypeId CandidateTerm
170 CREATE OR REPLACE FUNCTION nodes_nodes_set_cooc() RETURNS trigger AS $$
172 IF pg_trigger_depth() <> 1 THEN
175 IF TG_OP = 'INSERT' THEN
176 INSERT INTO node_nodengrams_nodengrams (node_id, node_ngrams1_id, node_ngrams2_id, weight)
177 WITH input(corpus_id, nn1, nn2, weight) AS (
178 SELECT new1.node1_id, nn1.id, nn2.id, count(*) from NEW as new1
179 INNER JOIN node_ngrams nn1
180 ON nn1.node_id = ? -- COALESCE(?,?) --(masterList, userList)
181 INNER JOIN node_ngrams nn2
182 ON nn2.node_id = nn1.node_id
184 INNER JOIN node_node_ngrams2 nnn1
185 ON nnn1.node_id = new1.node2_id
187 INNER JOIN node_node_ngrams2 nnn2
188 ON nnn2.node_id = new1.node2_id
190 WHERE nnn1.nodengrams_id = nn1.id
191 AND nnn2.nodengrams_id = nn2.id
193 AND nn1.node_subtype >= ?
194 AND nn2.node_subtype >= ?
195 GROUP BY new1.node1_id, nn1.id, nn2.id
197 SELECT * from input where weight >= 1
199 ON CONFLICT (node_id, node_ngrams1_id, node_ngrams2_id)
200 DO UPDATE set weight = node_nodengrams_nodengrams.weight + excluded.weight
208 -- DROP trigger trigger_cooc on node_node_ngrams2;
210 CREATE TRIGGER trigger_cooc_insert AFTER INSERT on nodes_nodes
211 REFERENCING NEW TABLE AS NEW
213 EXECUTE PROCEDURE nodes_nodes_set_cooc();