2 Module : Gargantext.Database.Query.Table.Node.Children
3 Description : Main requests of Node to the database
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 {-# LANGUAGE Arrows #-}
16 module Gargantext.Database.Query.Table.Node.Children
19 import Control.Arrow (returnA)
21 import Gargantext.Core
22 import Gargantext.Core.Types
23 import Gargantext.Core.Types.Query (Limit, Offset)
24 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument, HyperdataContact)
25 import Gargantext.Database.Prelude
26 import Gargantext.Database.Query.Filter
28 import Gargantext.Database.Schema.Node
29 import Gargantext.Database.Schema.Context
30 import Gargantext.Database.Schema.NodeContext
31 import Gargantext.Database.Query.Table.NodeContext
34 import Gargantext.Prelude
38 -- TODO getAllTableDocuments
39 getAllDocuments :: HasDBid NodeType => ParentId -> Cmd err (TableResult (Node HyperdataDocument))
40 getAllDocuments pId = getAllChildren pId (Proxy :: Proxy HyperdataDocument)
43 -- TODO getAllTableContacts
44 getAllContacts :: HasDBid NodeType => ParentId -> Cmd err (TableResult (Node HyperdataContact))
45 getAllContacts pId = getAllChildren pId (Proxy :: Proxy HyperdataContact)
48 getAllChildren :: (JSONB a, HasDBid NodeType)
52 -> Cmd err (NodeTableResult a)
53 getAllChildren pId p maybeNodeType = getChildren pId p maybeNodeType Nothing Nothing
56 getChildren :: (JSONB a, HasDBid NodeType)
62 -> Cmd err (NodeTableResult a)
63 getChildren pId p t@(Just NodeDocument) maybeOffset maybeLimit = getChildrenContext pId p t maybeOffset maybeLimit
64 getChildren pId p t@(Just NodeContact ) maybeOffset maybeLimit = getChildrenContext pId p t maybeOffset maybeLimit
65 getChildren a b c d e = getChildrenNode a b c d e
68 getChildrenNode :: (JSONB a, HasDBid NodeType)
74 -> Cmd err (NodeTableResult a)
75 getChildrenNode pId _ maybeNodeType maybeOffset maybeLimit = do
76 -- printDebug "getChildrenNode" (pId, maybeNodeType)
77 let query = selectChildrenNode pId maybeNodeType
81 $ orderBy (asc _node_id)
83 docCount <- runCountOpaQuery query
84 pure $ TableResult { tr_docs = docs, tr_count = docCount }
87 selectChildrenNode :: HasDBid NodeType
91 selectChildrenNode parentId maybeNodeType = proc () -> do
92 row@(Node _ _ typeName _ parent_id _ _ _) <- queryNodeTable -< ()
93 let nodeType = maybe 0 toDBid maybeNodeType
94 restrict -< typeName .== sqlInt4 nodeType
95 restrict -< parent_id .== (pgNodeId parentId)
99 getChildrenContext :: (JSONB a, HasDBid NodeType)
105 -> Cmd err (NodeTableResult a)
106 getChildrenContext pId _ maybeNodeType maybeOffset maybeLimit = do
107 -- printDebug "getChildrenContext" (pId, maybeNodeType)
108 let query = selectChildren' pId maybeNodeType
112 $ offset' maybeOffset
113 $ orderBy (asc _context_id)
116 docCount <- runCountOpaQuery query
117 pure $ TableResult { tr_docs = map context2node docs, tr_count = docCount }
120 selectChildren' :: HasDBid NodeType
123 -> Select ContextRead
124 selectChildren' parentId maybeNodeType = proc () -> do
125 row@(Context cid _ typeName _ _ _ _ _) <- queryContextTable -< ()
126 (NodeContext _ nid cid' _ _) <- queryNodeContextTable -< ()
128 let nodeType = maybe 0 toDBid maybeNodeType
129 restrict -< typeName .== sqlInt4 nodeType
131 restrict -< nid .== pgNodeId parentId
132 restrict -< cid .== cid'