]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Triggers/Nodes.hs
Merge branch 'dev' into dev-graph-explorer-gexf
[gargantext.git] / src / Gargantext / Database / Triggers / Nodes.hs
1 {-|
2 Module : Gargantext.Database.Triggers.Nodes
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 Nodes table.
11
12 -}
13
14 {-# LANGUAGE FlexibleContexts #-}
15 {-# LANGUAGE QuasiQuotes #-}
16 {-# LANGUAGE NoImplicitPrelude #-}
17 {-# LANGUAGE OverloadedStrings #-}
18 {-# LANGUAGE RankNTypes #-}
19
20 module Gargantext.Database.Triggers.Nodes
21 where
22
23 import Database.PostgreSQL.Simple.SqlQQ (sql)
24 -- import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
25 import Gargantext.Database.Config (nodeTypeId)
26 import Gargantext.Database.Types.Node -- (ListId, CorpusId, NodeId)
27 import Gargantext.Database.Utils (Cmd, execPGSQuery)
28 import Gargantext.Prelude
29 import qualified Database.PostgreSQL.Simple as DPS
30
31
32 triggerSearchUpdate :: Cmd err Int64
33 triggerSearchUpdate = execPGSQuery query ( nodeTypeId NodeDocument
34 , nodeTypeId NodeDocument
35 , nodeTypeId NodeContact
36 )
37 where
38 query :: DPS.Query
39 query = [sql|
40 CREATE OR REPLACE FUNCTION public.search_update()
41 RETURNS trigger AS $$
42 begin
43 IF new.typename = ? AND new.hyperdata @> '{"language_iso2":"EN"}' THEN
44 new.search := to_tsvector( 'english' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
45
46 ELSIF new.typename = ? AND new.hyperdata @> '{"language_iso2":"FR"}' THEN
47 new.search := to_tsvector( 'french' , (new.hyperdata ->> 'title') || ' ' || (new.hyperdata ->> 'abstract'));
48
49 ELSIF new.typename = ? THEN
50 new.search := to_tsvector( 'french' , (new.hyperdata ->> 'prenom')
51 || ' ' || (new.hyperdata ->> 'nom')
52 || ' ' || (new.hyperdata ->> 'fonction')
53 );
54 ELSE
55 new.search := to_tsvector( 'english' , new.name);
56 END IF;
57 return new;
58 end
59 $$ LANGUAGE plpgsql;
60
61 ALTER FUNCTION public.search_update() OWNER TO gargantua;
62
63 CREATE TRIGGER search_update_trigger
64 BEFORE INSERT OR UPDATE
65 ON nodes FOR EACH ROW
66 EXECUTE PROCEDURE search_update();
67
68 -- Initialize index with already existing data
69 UPDATE nodes SET hyperdata = hyperdata;
70
71 |]
72