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 #-}
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE OverloadedStrings #-}
17 {-# LANGUAGE RankNTypes #-}
19 module Gargantext.Database.Triggers.NodesNodes
22 import Database.PostgreSQL.Simple.SqlQQ (sql)
23 -- import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
24 import Gargantext.Database.Config (nodeTypeId)
25 import Gargantext.Database.Types.Node -- (ListId, CorpusId, NodeId)
26 import Gargantext.Database.Utils (Cmd, execPGSQuery)
27 import Gargantext.Prelude
28 import qualified Database.PostgreSQL.Simple as DPS
30 type MasterListId = ListId
32 triggerDeleteCount :: MasterListId -> Cmd err Int64
33 triggerDeleteCount lId = execPGSQuery query (lId, nodeTypeId NodeList)
37 CREATE OR REPLACE FUNCTION set_delete_count() RETURNS trigger AS $$
39 UPDATE node_node_ngrams SET weight = weight - d.delete_count
40 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
41 INNER JOIN nodes doc ON doc.id = old1.node2_id
42 INNER JOIN nodes lists ON lists.parent_id = old1.node1_id
43 INNER JOIN node_node_ngrams nnn ON nnn.node2_id = doc.id
44 WHERE nnn.node1_id in (?, lists.id)
45 AND lists.typename = ?
46 GROUP BY old1.node1_id, lists.id, nnn.ngrams_id, nnn.ngrams_type
48 WHERE node_node_ngrams.node1_id = d.node1_id
49 AND node_node_ngrams.node2_id = d.node2_id
50 AND node_node_ngrams.ngrams_id = d.ngrams_id
51 AND node_node_ngrams.ngrams_type = d.ngrams_type
57 -- DROP trigger trigger_delete_count on nodes_nodes;
58 CREATE TRIGGER trigger_delete_count AFTER DELETE on nodes_nodes
59 REFERENCING OLD TABLE AS OLD
61 EXECUTE PROCEDURE set_delete_count();
64 triggerInsertCount :: MasterListId -> Cmd err Int64
65 triggerInsertCount lId = execPGSQuery query (lId, nodeTypeId NodeList)
69 CREATE OR REPLACE FUNCTION set_insert_count() RETURNS trigger AS $$
71 INSERT INTO node_node_ngrams (node1_id, node2_id, ngrams_id, ngrams_type, weight)
72 SELECT new1.node1_id , lists.id, nnn.ngrams_id, nnn.ngrams_type, count(*) as weight from NEW as new1
73 INNER JOIN nodes doc ON doc.id = new1.node2_id
74 INNER JOIN nodes lists ON lists.parent_id = new1.node1_id
75 INNER JOIN node_node_ngrams nnn ON nnn.node2_id = doc.id
76 WHERE nnn.node1_id in (?, lists.id)
77 AND lists.typename = ?
78 GROUP BY new1.node1_id, lists.id, nnn.ngrams_id, nnn.ngrams_type
79 ON CONFLICT (node1_id, node2_id, ngrams_id, ngrams_type)
80 DO UPDATE set weight = node_node_ngrams.weight + excluded.weight
86 -- DROP trigger trigger_insert_count on nodes_nodes;
87 CREATE TRIGGER trigger_insert_count AFTER INSERT on nodes_nodes
88 REFERENCING NEW TABLE AS NEW
90 EXECUTE PROCEDURE set_insert_count();
93 triggerUpdateAdd :: MasterListId -> Cmd err Int64
94 triggerUpdateAdd lId = execPGSQuery query (lId, nodeTypeId NodeList)
98 CREATE OR REPLACE FUNCTION set_update_ngrams_add() RETURNS trigger AS $$
100 UPDATE node_node_ngrams nnn0 SET weight = weight + d.fix_count
101 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
103 INNER JOIN nodes lists ON new1.node1_id = lists.parent_id
104 INNER JOIN node_node_ngrams nnn ON new1.node2_id = nnn.node2_id
105 WHERE nnn.node1_id in (?, lists.id) -- (masterList_id, userLists)
106 AND lists.typename = ?
107 GROUP BY new1.node1_id, lists.id, nnn.ngrams_id, nnn.ngrams_type
109 WHERE nnn0.node1_id = d.node1_id
110 AND nnn0.node2_id = d.node2_id
111 AND nnn0.ngrams_id = d.ngrams_id
112 AND nnn0.ngrams_type = d.ngrams_type
118 -- DROP trigger trigger_count_update_add on nodes_nodes;
119 CREATE TRIGGER trigger_count_update_add AFTER UPDATE on nodes_nodes
120 REFERENCING OLD TABLE AS OLD NEW TABLE AS NEW
122 WHEN (OLD.category <= 0 AND NEW.category >= 1)
123 EXECUTE PROCEDURE set_update_ngrams_add();
126 triggerUpdateDel :: MasterListId -> Cmd err Int64
127 triggerUpdateDel lId = execPGSQuery query (lId, nodeTypeId NodeList)
131 CREATE OR REPLACE FUNCTION set_update_ngrams_count_del() RETURNS trigger AS $$
133 UPDATE node_node_ngrams nnn0 SET weight = weight - d.fix_count
134 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
136 INNER JOIN nodes lists ON new1.node1_id = lists.parent_id
137 INNER JOIN node_node_ngrams nnn ON new1.node2_id = nnn.node2_id
138 WHERE nnn.node1_id in (?, lists.id) -- (masterList_id, userLists)
139 AND lists.typename = ?
140 GROUP BY new1.node1_id, lists.id, nnn.ngrams_id, nnn.ngrams_type
142 WHERE nnn0.node1_id = d.node1_id
143 AND nnn0.node2_id = d.node2_id
144 AND nnn0.ngrams_id = d.ngrams_id
145 AND nnn0.ngrams_type = d.ngrams_type
151 -- DROP trigger trigger_count_delete2 on nodes_nodes;
152 CREATE TRIGGER trigger_count_delete2 AFTER UPDATE on nodes_nodes
153 REFERENCING OLD TABLE AS OLD NEW TABLE AS NEW
155 WHEN (OLD.category >= 1 AND NEW.category <= 0)
156 EXECUTE PROCEDURE set_update_ngrams_count_del();