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 #-}
24 ------------------------------------------------------------------------
25 module Gargantext.Database.Node.Document.Add where
28 import Data.ByteString.Internal (ByteString)
29 import Data.Typeable (Typeable)
30 import Database.PostgreSQL.Simple (Query, Only(..))
31 import Database.PostgreSQL.Simple.SqlQQ
32 import Database.PostgreSQL.Simple.ToField (toField)
33 import Database.PostgreSQL.Simple.ToRow (ToRow(..))
34 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
36 import Data.Text (Text)
38 import Gargantext.Database.Utils (Cmd, runPGSQuery, formatPGSQuery)
39 import Gargantext.Database.Types.Node
40 import Gargantext.Prelude
42 import GHC.Generics (Generic)
43 ---------------------------------------------------------------------------
45 add :: ParentId -> [NodeId] -> Cmd err [Only Int]
46 add pId ns = runPGSQuery queryAdd (Only $ Values fields inputData)
48 fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
49 inputData = prepare pId ns
51 add_debug :: ParentId -> [NodeId] -> Cmd err ByteString
52 add_debug pId ns = formatPGSQuery queryAdd (Only $ Values fields inputData)
54 fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
55 inputData = prepare pId ns
58 -- | Input Tables: types of the tables
59 inputSqlTypes :: [Text]
60 inputSqlTypes = ["int4","int4","int4"]
62 -- | SQL query to add documents
63 -- TODO return id of added documents only
66 WITH input_rows(node1_id,node2_id,category) AS (?)
67 INSERT INTO nodes_nodes (node1_id, node2_id,category)
68 SELECT * FROM input_rows
69 ON CONFLICT (node1_id, node2_id) DO NOTHING -- on unique index
74 prepare :: ParentId -> [NodeId] -> [InputData]
75 prepare pId ns = map (\nId -> InputData pId nId) ns
77 ------------------------------------------------------------------------
80 data InputData = InputData { inNode1_id :: NodeId
81 , inNode2_id :: NodeId
82 } deriving (Show, Generic, Typeable)
84 instance ToRow InputData where
85 toRow inputData = [ toField (inNode1_id inputData)
86 , toField (inNode2_id inputData)