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 DeriveGeneric #-}
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE QuasiQuotes #-}
17 {-# LANGUAGE DeriveDataTypeable #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE TypeSynonymInstances #-}
20 ------------------------------------------------------------------------
21 module Gargantext.Database.Node.Document.Add where
23 import Control.Lens (set)
25 import Data.Aeson (toJSON, Value)
26 import Data.ByteString.Internal (ByteString)
27 import Data.Maybe (maybe)
28 import Data.Typeable (Typeable)
29 import Database.PostgreSQL.Simple (Connection, FromRow, Query, formatQuery, query, Only(..))
30 import Database.PostgreSQL.Simple.FromRow (fromRow, field)
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)
37 import qualified Data.Text as DT (pack, unpack, concat)
38 import qualified Data.Digest.Pure.SHA as SHA (sha256, showDigest)
39 import qualified Data.ByteString.Lazy.Char8 as DC (pack)
41 import Gargantext.Database.Config (nodeTypeId)
42 import Gargantext.Database.Node (mkCmd, Cmd(..))
43 import Gargantext.Database.Types.Node
44 import Gargantext.Prelude
46 import GHC.Generics (Generic)
47 ---------------------------------------------------------------------------
51 add :: ParentId -> [NodeId] -> Cmd [Only Int]
52 add pId ns = mkCmd $ \c -> query c queryAdd (Only $ Values fields inputData)
54 fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
55 inputData = prepare pId ns
57 add_debug :: ParentId -> [NodeId] -> Cmd ByteString
58 add_debug pId ns = mkCmd $ \c -> formatQuery c queryAdd (Only $ Values fields inputData)
60 fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
61 inputData = prepare pId ns
65 -- | Input Tables: types of the tables
66 inputSqlTypes :: [Text]
67 inputSqlTypes = map DT.pack ["int4","int4"]
69 -- | SQL query to add documents
70 -- TODO return id of added documents only
73 WITH input_rows(node1_id,node2_id) AS (?)
74 INSERT INTO nodes_nodes (node1_id, node2_id)
75 SELECT * FROM input_rows
76 ON CONFLICT (node1_id, node2_id) DO NOTHING -- on unique index
81 prepare :: ParentId -> [NodeId] -> [InputData]
82 prepare pId ns = map (\nId -> InputData pId nId) ns
84 ------------------------------------------------------------------------
88 data InputData = InputData { inNode1_id :: NodeId
89 , inNode2_id :: NodeId
90 } deriving (Show, Generic, Typeable)
92 instance ToRow InputData where
93 toRow inputData = [ toField (inNode1_id inputData)
94 , toField (inNode2_id inputData)