]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Node/Document/Add.hs
[DBFLOW] /api/v1.0/node/{id}/table ok
[gargantext.git] / src / Gargantext / Database / Node / Document / Add.hs
1 {-|
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
8 Portability : POSIX
9
10 Add Documents/Contact to a Corpus/Annuaire.
11
12 -}
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
22
23
24 import Data.ByteString.Internal (ByteString)
25 import Data.Typeable (Typeable)
26 import Database.PostgreSQL.Simple (Query, formatQuery, query, Only(..))
27 import Database.PostgreSQL.Simple.SqlQQ
28 import Database.PostgreSQL.Simple.ToField (toField)
29 import Database.PostgreSQL.Simple.ToRow (ToRow(..))
30 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
31
32 import Data.Text (Text)
33 import qualified Data.Text as DT (pack)
34
35 import Gargantext.Database.Node (mkCmd, Cmd(..))
36 import Gargantext.Database.Types.Node
37 import Gargantext.Prelude
38
39 import GHC.Generics (Generic)
40 ---------------------------------------------------------------------------
41
42 type ParentId = Int
43
44 add :: ParentId -> [NodeId] -> Cmd [Only Int]
45 add pId ns = mkCmd $ \c -> query c queryAdd (Only $ Values fields inputData)
46 where
47 fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
48 inputData = prepare pId ns
49
50 add_debug :: ParentId -> [NodeId] -> Cmd ByteString
51 add_debug pId ns = mkCmd $ \c -> formatQuery c queryAdd (Only $ Values fields inputData)
52 where
53 fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
54 inputData = prepare pId ns
55
56
57
58 -- | Input Tables: types of the tables
59 inputSqlTypes :: [Text]
60 inputSqlTypes = map DT.pack ["int4","int4","bool","bool"]
61
62 -- | SQL query to add documents
63 -- TODO return id of added documents only
64 queryAdd :: Query
65 queryAdd = [sql|
66 WITH input_rows(node1_id,node2_id, favorite, delete) AS (?)
67 INSERT INTO nodes_nodes (node1_id, node2_id, favorite, delete)
68 SELECT * FROM input_rows
69 ON CONFLICT (node1_id, node2_id) DO NOTHING -- on unique index
70 RETURNING 1
71 ;
72 |]
73
74 prepare :: ParentId -> [NodeId] -> [InputData]
75 prepare pId ns = map (\nId -> InputData pId nId False False) ns
76
77 ------------------------------------------------------------------------
78 -- * Main Types used
79
80
81 data InputData = InputData { inNode1_id :: NodeId
82 , inNode2_id :: NodeId
83 , inNode_fav :: Bool
84 , inNode_del :: Bool
85 } deriving (Show, Generic, Typeable)
86
87 instance ToRow InputData where
88 toRow inputData = [ toField (inNode1_id inputData)
89 , toField (inNode2_id inputData)
90 , toField (inNode_fav inputData)
91 , toField (inNode_del inputData)
92 ]
93