2 Module : Gargantext.Database.Admin.Trigger.Nodes
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 Nodes table.
14 {-# LANGUAGE QuasiQuotes #-}
16 module Gargantext.Database.Admin.Trigger.Nodes
19 import Data.Text (Text)
20 import Database.PostgreSQL.Simple.SqlQQ (sql)
21 import qualified Database.PostgreSQL.Simple as DPS
23 import Gargantext.Database.Admin.Config (nodeTypeId)
24 import Gargantext.Database.Admin.Types.Node -- (ListId, CorpusId, NodeId)
25 import Gargantext.Database.Prelude (Cmd, execPGSQuery)
26 import Gargantext.Prelude
29 triggerSearchUpdate :: Cmd err Int64
30 triggerSearchUpdate = execPGSQuery query ( nodeTypeId NodeDocument
31 , nodeTypeId NodeDocument
32 , nodeTypeId NodeContact
37 -- DROP TRIGGER search_update_trigger on nodes;
38 CREATE OR REPLACE FUNCTION public.search_update()
41 IF new.typename = ? AND new.hyperdata @> '{"language_iso2":"EN"}' THEN
42 new.search := to_tsvector( 'english' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
44 ELSIF new.typename = ? AND new.hyperdata @> '{"language_iso2":"FR"}' THEN
45 new.search := to_tsvector( 'french' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
47 ELSIF new.typename = ? THEN
48 new.search := to_tsvector( 'french' , (new.hyperdata ->> 'prenom')
49 || ' ' || (new.hyperdata ->> 'nom')
50 || ' ' || (new.hyperdata ->> 'fonction')
53 new.search := to_tsvector( 'english' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
59 ALTER FUNCTION public.search_update() OWNER TO gargantua;
61 CREATE TRIGGER search_update_trigger
62 BEFORE INSERT OR UPDATE
64 EXECUTE PROCEDURE search_update();
66 -- Initialize index with already existing data
67 UPDATE nodes SET hyperdata = hyperdata;
73 triggerUpdateHash :: Secret -> Cmd err Int64
74 triggerUpdateHash secret = execPGSQuery query ( nodeTypeId NodeDocument
75 , nodeTypeId NodeContact
78 , nodeTypeId NodeDocument
79 , nodeTypeId NodeContact
87 CREATE OR REPLACE FUNCTION hash_insert_nodes()
92 IF NEW.typename = ? OR NEW.typename = ?
93 THEN NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.parent_id, NEW.hyperdata), 'sha256');
94 ELSE NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.id, NEW.hyperdata), 'sha256');
102 CREATE OR REPLACE FUNCTION hash_update_nodes()
103 RETURNS trigger AS $$
105 IF NEW.typename = ? OR NEW.typename = ?
106 THEN NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.parent_id, NEW.hyperdata), 'sha256');
107 ELSE NEW.hash_id = digest(CONCAT(?, NEW.typename, NEW.name, NEW.id, NEW.hyperdata), 'sha256');
114 CREATE TRIGGER nodes_hash_insert BEFORE INSERT ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_insert_nodes();
115 CREATE TRIGGER nodes_hash_update BEFORE UPDATE ON nodes FOR EACH ROW EXECUTE PROCEDURE hash_update_nodes();