]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Admin/Trigger/NodeNodeNgrams.hs
change export
[gargantext.git] / src / Gargantext / Database / Admin / Trigger / NodeNodeNgrams.hs
1 {-|
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
8 Portability : POSIX
9
10 Triggers on NodeNodeNgrams table.
11
12 -}
13
14 {-# LANGUAGE QuasiQuotes #-}
15
16 module Gargantext.Database.Admin.Trigger.NodeNodeNgrams
17 where
18
19 import Database.PostgreSQL.Simple.SqlQQ (sql)
20 import Gargantext.Core.Types.Main (listTypeId, ListType(CandidateTerm))
21 import Gargantext.Database.Admin.Config (nodeTypeId)
22 import Gargantext.Database.Admin.Types.Node -- (ListId, CorpusId, NodeId)
23 import Gargantext.Database.Prelude (Cmd, execPGSQuery)
24 import Gargantext.Prelude
25 import qualified Database.PostgreSQL.Simple as DPS
26
27 triggerCountInsert :: Cmd err Int64
28 triggerCountInsert = execPGSQuery query (nodeTypeId NodeDocument, nodeTypeId NodeList)
29 where
30 query :: DPS.Query
31 query = [sql|
32 CREATE OR REPLACE FUNCTION set_ngrams_global_count() RETURNS trigger AS $$
33 BEGIN
34 IF pg_trigger_depth() <> 1 THEN
35 RETURN NEW;
36 END IF;
37 IF TG_OP = 'INSERT' THEN
38 INSERT INTO node_node_ngrams (node1_id, node2_id, ngrams_id, ngrams_type, weight)
39 select n.parent_id, n.id, new1.ngrams_id, new1.ngrams_type, count(*) from NEW as new1
40 INNER JOIN nodes n ON n.id = new1.node1_id
41 INNER JOIN nodes n2 ON n2.id = new1.node2_id
42 WHERE n2.typename = ? -- not mandatory
43 AND n.typename = ? -- not mandatory
44 AND n.parent_id <> n2.id -- not mandatory
45 GROUP BY n.parent_id, n.id, new1.ngrams_id, new1.ngrams_type
46 ON CONFLICT (node1_id, node2_id, ngrams_id, ngrams_type)
47 DO UPDATE set weight = node_node_ngrams.weight + excluded.weight
48 ;
49 END IF;
50
51 RETURN NULL;
52 END
53 $$ LANGUAGE plpgsql;
54
55 -- DROP trigger trigger_count_insert on node_node_ngrams;
56
57 CREATE TRIGGER trigger_count_insert AFTER INSERT on node_node_ngrams
58 REFERENCING NEW TABLE AS NEW
59 FOR EACH STATEMENT
60 EXECUTE PROCEDURE set_ngrams_global_count();
61 |]
62
63 triggerCountInsert2 :: Cmd err Int64
64 triggerCountInsert2 = execPGSQuery query ( nodeTypeId NodeCorpus
65 , nodeTypeId NodeDocument
66 , nodeTypeId NodeList
67 )
68 where
69 query :: DPS.Query
70 query = [sql|
71 CREATE OR REPLACE FUNCTION set_ngrams_global_count2() RETURNS trigger AS $$
72 BEGIN
73 IF pg_trigger_depth() <> 1 THEN
74 RETURN NEW;
75 END IF;
76 IF TG_OP = 'INSERT' THEN
77 INSERT INTO node_node_ngrams2 (node_id, nodengrams_id, weight)
78 SELECT corpus.id, nng.id, count(*) from NEW as new1
79 INNER JOIN node_ngrams nng ON nng.id = new1.nodengrams_id
80 INNER JOIN nodes list ON list.id = nng.node_id
81 INNER JOIN nodes_nodes nn ON nn.node2_id = new1.node_id
82 INNER JOIN nodes corpus ON corpus.id = nn.node1_id
83 INNER JOIN nodes doc ON doc.id = nn.node2_id
84 WHERE corpus.typename = ? -- 30 -- corpus
85 AND doc.typename = ? -- 4 -- maybe not mandatory
86 AND list.typename = ? -- 5 -- list
87 GROUP BY corpus.id, nng.id
88
89 ON CONFLICT (node_id, nodengrams_id)
90 DO UPDATE set weight = node_node_ngrams2.weight + excluded.weight
91 ;
92 END IF;
93
94 RETURN NULL;
95 END
96 $$ LANGUAGE plpgsql;
97
98 -- DROP trigger trigger_count_insert2 on node_node_ngrams2;
99
100 CREATE TRIGGER trigger_count_insert2 AFTER INSERT on node_node_ngrams2
101 REFERENCING NEW TABLE AS NEW
102 FOR EACH STATEMENT
103 EXECUTE PROCEDURE set_ngrams_global_count2();
104 |]
105
106 -- TODO add the groups
107 triggerCoocInsert :: Cmd err Int64
108 triggerCoocInsert = execPGSQuery query ( nodeTypeId NodeCorpus
109 , nodeTypeId NodeDocument
110 , nodeTypeId NodeList
111 , listTypeId CandidateTerm
112 , listTypeId CandidateTerm
113 )
114 where
115 query :: DPS.Query
116 query = [sql|
117 CREATE OR REPLACE FUNCTION set_cooc() RETURNS trigger AS $$
118 BEGIN
119 IF pg_trigger_depth() <> 1 THEN
120 RETURN NEW;
121 END IF;
122 IF TG_OP = 'INSERT' THEN
123 INSERT INTO node_nodengrams_nodengrams (node_id, node_ngrams1_id, node_ngrams2_id, weight)
124 WITH input(corpus_id, nn1, nn2, weight) AS (
125 SELECT corpus.id, nng1.id, nng2.id, count(*) from NEW as new1
126 INNER JOIN node_ngrams nng1 ON nng1.id = new1.nodengrams_id
127 INNER JOIN nodes list ON list.id = nng1.node_id
128 INNER JOIN nodes_nodes nn ON nn.node2_id = new1.node_id
129 INNER JOIN nodes corpus ON corpus.id = nn.node1_id
130 INNER JOIN nodes doc ON doc.id = nn.node2_id
131
132 INNER JOIN node_node_ngrams2 nnng2 ON nnng2.node_id = doc.id
133 INNER JOIN node_ngrams nng2 ON nng2.id = nnng2.nodengrams_id
134
135 WHERE corpus.typename = ? -- 30 -- corpus
136 AND doc.typename = ? -- 4 -- maybe not mandatory
137 AND list.typename = ? -- 5 -- list
138 AND nng2.node_id = list.id
139 AND nng1.id < nng2.id
140 AND nng1.node_subtype >= ?
141 AND nng2.node_subtype >= ?
142 GROUP BY corpus.id, nng1.id, nng2.id
143 )
144 SELECT * from input where weight > 1
145
146 ON CONFLICT (node_id, node_ngrams1_id, node_ngrams2_id)
147 DO UPDATE set weight = node_nodengrams_nodengrams.weight + excluded.weight
148 ;
149 END IF;
150
151 RETURN NULL;
152 END
153 $$ LANGUAGE plpgsql;
154
155 -- DROP trigger trigger_cooc on node_node_ngrams2;
156
157 CREATE TRIGGER trigger_cooc_insert AFTER INSERT on node_node_ngrams2
158 REFERENCING NEW TABLE AS NEW
159 FOR EACH STATEMENT
160 EXECUTE PROCEDURE set_cooc();
161 |]
162