2 Module : Gargantext.Database.Triggers.NodeNodeNgrams
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 NodeNodeNgrams table.
14 {-# LANGUAGE QuasiQuotes #-}
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE OverloadedStrings #-}
17 {-# LANGUAGE RankNTypes #-}
19 module Gargantext.Database.Triggers.NodeNodeNgrams
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 triggerCountInsert :: Cmd err Int64
31 triggerCountInsert = execPGSQuery query (nodeTypeId NodeDocument, nodeTypeId NodeList)
35 CREATE OR REPLACE FUNCTION set_ngrams_global_count() RETURNS trigger AS $$
37 IF pg_trigger_depth() <> 1 THEN
40 IF TG_OP = 'INSERT' THEN
41 INSERT INTO node_node_ngrams (node1_id, node2_id, ngrams_id, ngrams_type, weight)
42 select n.parent_id, n.id, new1.ngrams_id, new1.ngrams_type, count(*) from NEW as new1
43 INNER JOIN nodes n ON n.id = new1.node1_id
44 INNER JOIN nodes n2 ON n2.id = new1.node2_id
45 WHERE n2.typename = ? -- not mandatory
46 AND n.typename = ? -- not mandatory
47 AND n.parent_id <> n2.id -- not mandatory
48 GROUP BY n.parent_id, n.id, new1.ngrams_id, new1.ngrams_type
49 ON CONFLICT (node1_id, node2_id, ngrams_id, ngrams_type)
50 DO UPDATE set weight = node_node_ngrams.weight + excluded.weight
58 -- DROP trigger trigger_count_insert on node_node_ngrams;
60 CREATE TRIGGER trigger_count_insert AFTER INSERT on node_node_ngrams
61 REFERENCING NEW TABLE AS NEW
63 EXECUTE PROCEDURE set_ngrams_global_count();