2 Module : Gargantext.Database.Node.Document.Add
3 Description : Importing context of texts (documents)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Add Documents/Contact to a Corpus/Annuaire.
13 ------------------------------------------------------------------------
14 {-# LANGUAGE DeriveDataTypeable #-}
15 {-# LANGUAGE DeriveGeneric #-}
16 {-# LANGUAGE FlexibleContexts #-}
17 {-# LANGUAGE FlexibleInstances #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE NoImplicitPrelude #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE QuasiQuotes #-}
22 {-# LANGUAGE RankNTypes #-}
23 {-# LANGUAGE TypeSynonymInstances #-}
25 ------------------------------------------------------------------------
26 module Gargantext.Database.Query.Node.Document.Add
29 import Data.ByteString.Internal (ByteString)
30 import Data.Typeable (Typeable)
31 import Database.PostgreSQL.Simple (Query, Only(..))
32 import Database.PostgreSQL.Simple.SqlQQ
33 import Database.PostgreSQL.Simple.ToField (toField)
34 import Database.PostgreSQL.Simple.ToRow (ToRow(..))
35 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
37 import Data.Text (Text)
39 import Gargantext.Database.Utils (Cmd, runPGSQuery, formatPGSQuery)
40 import Gargantext.Database.Types.Node
41 import Gargantext.Prelude
43 import GHC.Generics (Generic)
44 ---------------------------------------------------------------------------
46 add :: ParentId -> [NodeId] -> Cmd err [Only Int]
47 add pId ns = runPGSQuery queryAdd (Only $ Values fields inputData)
49 fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
50 inputData = prepare pId ns
52 add_debug :: ParentId -> [NodeId] -> Cmd err ByteString
53 add_debug pId ns = formatPGSQuery queryAdd (Only $ Values fields inputData)
55 fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
56 inputData = prepare pId ns
59 -- | Input Tables: types of the tables
60 inputSqlTypes :: [Text]
61 inputSqlTypes = ["int4","int4","int4"]
63 -- | SQL query to add documents
64 -- TODO return id of added documents only
67 WITH input_rows(node1_id,node2_id,category) AS (?)
68 INSERT INTO nodes_nodes (node1_id, node2_id,category)
69 SELECT * FROM input_rows
70 ON CONFLICT (node1_id, node2_id) DO NOTHING -- on unique index
75 prepare :: ParentId -> [NodeId] -> [InputData]
76 prepare pId ns = map (\nId -> InputData pId nId) ns
78 ------------------------------------------------------------------------
81 data InputData = InputData { inNode1_id :: NodeId
82 , inNode2_id :: NodeId
83 } deriving (Show, Generic, Typeable)
85 instance ToRow InputData where
86 toRow inputData = [ toField (inNode1_id inputData)
87 , toField (inNode2_id inputData)