]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Query/Table/NodeContext_NodeContext.hs
[FIX] Order 2
[gargantext.git] / src / Gargantext / Database / Query / Table / NodeContext_NodeContext.hs
1 {-|
2 Module : Gargantext.Database.Select.Table.NodeContext_NodeContext
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9 -}
10
11 {-# LANGUAGE Arrows #-}
12 {-# LANGUAGE QuasiQuotes #-}
13 {-# LANGUAGE TemplateHaskell #-}
14
15 module Gargantext.Database.Query.Table.NodeContext_NodeContext
16 ( module Gargantext.Database.Schema.NodeContext_NodeContext
17 -- , query_NodeContext_NodeContext_Table
18 , insertNodeContext_NodeContext
19 )
20 where
21
22 import Data.Text (Text)
23 import Gargantext.Core.Types
24 import Gargantext.Database.Prelude
25 import Gargantext.Database.Schema.NodeContext_NodeContext
26 import Gargantext.Database.Schema.Prelude hiding (sum)
27 import Gargantext.Prelude
28 import qualified Database.PostgreSQL.Simple as PGS
29
30 {-
31 queryNodeContext_NodeContextTable :: Select NodeContext_NodeContextRead
32 queryNodeContext_NodeContextTable = selectTable nodeContext_NodeContextTable
33 -}
34
35 insertNodeContext_NodeContext :: [(CorpusId, DocId, AnnuaireId, ContactId)] -> Cmd err [Int]
36 insertNodeContext_NodeContext contexts = do
37 let
38 fields = map (\t -> QualifiedIdentifier Nothing t) $ snd fields_name
39 fields_name :: ( [Text], [Text])
40 fields_name = ( ["corpus_id", "doc_id", "annuaire_id", "contact_id"]
41 , ["int4" , "int4" , "int4" , "int4" ]
42 )
43 result <- map (\(PGS.Only a) -> a) <$> runPGSQuery queryInsert (PGS.Only $ Values fields contexts)
44 pure [sum result]
45
46 queryInsert :: PGS.Query
47 queryInsert = [sql|
48 WITH input(corpus_id, doc_id, annuaire_id, contact_id) AS (?)
49 INSERT into nodescontexts_nodescontexts (nodescontexts1, nodescontexts2)
50 SELECT context1.id, context2.id FROM input
51 INNER JOIN nodes_contexts context1 ON context1.node_id = input.corpus_id
52 INNER JOIN nodes_contexts context2 ON context2.node_id = input.annuaire_id
53 WHERE context1.context_id = input.doc_id
54 AND context2.context_id = input.contact_id
55 ON CONFLICT (nodescontexts1, nodescontexts2) DO Nothing
56 RETURNING 1
57 |]