2 Module : Gargantext.Database.Query.Table.NodeNode
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 {-# LANGUAGE Arrows #-}
17 {-# LANGUAGE FunctionalDependencies #-}
18 {-# LANGUAGE QuasiQuotes #-}
19 {-# LANGUAGE TemplateHaskell #-}
21 module Gargantext.Database.Query.Table.NodeNode
22 ( module Gargantext.Database.Schema.NodeNode
33 import Control.Arrow (returnA)
34 import Control.Lens (view, (^.))
35 import Data.Maybe (catMaybes)
36 import Data.Text (Text, splitOn)
37 import Database.PostgreSQL.Simple.SqlQQ (sql)
38 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
39 import Gargantext.Core.Types
40 import Gargantext.Database.Schema.NodeNode
41 import Gargantext.Database.Admin.Types.Node (pgNodeId)
42 import Gargantext.Database.Admin.Config (nodeTypeId)
43 import Gargantext.Database.Admin.Types.Node (CorpusId, DocId)
44 import Gargantext.Database.Prelude
45 import Gargantext.Database.Schema.Node
46 import Gargantext.Prelude
48 import qualified Database.PostgreSQL.Simple as PGS (Query, Only(..))
49 import qualified Opaleye as O
52 queryNodeNodeTable :: Query NodeNodeRead
53 queryNodeNodeTable = queryTable nodeNodeTable
55 -- | not optimized (get all ngrams without filters)
56 _nodesNodes :: Cmd err [NodeNode]
57 _nodesNodes = runOpaQuery queryNodeNodeTable
59 ------------------------------------------------------------------------
60 -- | Basic NodeNode tools
61 getNodeNode :: NodeId -> Cmd err [NodeNode]
62 getNodeNode n = runOpaQuery (selectNodeNode $ pgNodeId n)
64 selectNodeNode :: Column PGInt4 -> Query NodeNodeRead
65 selectNodeNode n' = proc () -> do
66 ns <- queryNodeNodeTable -< ()
67 restrict -< _nn_node1_id ns .== n'
70 -------------------------
71 insertNodeNode :: [NodeNode] -> Cmd err Int64
72 insertNodeNode ns = mkCmd $ \conn -> runInsert_ conn $ Insert nodeNodeTable ns' rCount Nothing
74 ns' :: [NodeNodeWrite]
75 ns' = map (\(NodeNode n1 n2 x y)
76 -> NodeNode (pgNodeId n1)
83 -- | Favorite management
84 _nodeNodeCategory :: CorpusId -> DocId -> Int -> Cmd err [Int]
85 _nodeNodeCategory cId dId c = map (\(PGS.Only a) -> a) <$> runPGSQuery favQuery (c,cId,dId)
88 favQuery = [sql|UPDATE nodes_nodes SET category = ?
89 WHERE node1_id = ? AND node2_id = ?
93 nodeNodesCategory :: [(CorpusId,DocId,Int)] -> Cmd err [Int]
94 nodeNodesCategory inputData = map (\(PGS.Only a) -> a)
95 <$> runPGSQuery catQuery (PGS.Only $ Values fields inputData)
97 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","int4","int4"]
99 catQuery = [sql| UPDATE nodes_nodes as nn0
100 SET category = nn1.category
101 FROM (?) as nn1(node1_id,node2_id,category)
102 WHERE nn0.node1_id = nn1.node1_id
103 AND nn0.node2_id = nn1.node2_id
104 RETURNING nn1.node2_id
107 ------------------------------------------------------------------------
108 -- | TODO use UTCTime fast
109 selectDocsDates :: CorpusId -> Cmd err [Text]
110 selectDocsDates cId = map (head' "selectDocsDates" . splitOn "-")
112 <$> map (view hyperdataDocument_publication_date)
115 selectDocs :: CorpusId -> Cmd err [HyperdataDocument]
116 selectDocs cId = runOpaQuery (queryDocs cId)
118 queryDocs :: CorpusId -> O.Query (Column PGJsonb)
119 queryDocs cId = proc () -> do
120 (n, nn) <- joinInCorpus -< ()
121 restrict -< nn^.nn_node1_id .== (toNullable $ pgNodeId cId)
122 restrict -< nn^.nn_category .>= (toNullable $ pgInt4 1)
123 restrict -< n^.node_typename .== (pgInt4 $ nodeTypeId NodeDocument)
124 returnA -< view (node_hyperdata) n
126 selectDocNodes :: CorpusId -> Cmd err [Node HyperdataDocument]
127 selectDocNodes cId = runOpaQuery (queryDocNodes cId)
129 queryDocNodes :: CorpusId -> O.Query NodeRead
130 queryDocNodes cId = proc () -> do
131 (n, nn) <- joinInCorpus -< ()
132 restrict -< nn^.nn_node1_id .== (toNullable $ pgNodeId cId)
133 restrict -< nn^.nn_category .>= (toNullable $ pgInt4 1)
134 restrict -< n^.node_typename .== (pgInt4 $ nodeTypeId NodeDocument)
137 joinInCorpus :: O.Query (NodeRead, NodeNodeReadNull)
138 joinInCorpus = leftJoin queryNodeTable queryNodeNodeTable cond
140 cond :: (NodeRead, NodeNodeRead) -> Column PGBool
141 cond (n, nn) = nn^.nn_node2_id .== (view node_id n)