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.Database.Admin.Types.Hyperdata (HyperdataDocument, HyperdataContact)
24 import Gargantext.Database.Prelude
25 import Gargantext.Database.Query.Filter
27 import Gargantext.Database.Schema.Node
28 import Gargantext.Database.Schema.Context
29 import Gargantext.Database.Schema.NodeContext
30 import Gargantext.Database.Query.Table.NodeContext
33 import Gargantext.Prelude
37 -- TODO getAllTableDocuments
38 getAllDocuments :: HasDBid NodeType => ParentId -> Cmd err (TableResult (Node HyperdataDocument))
39 getAllDocuments pId = getAllChildren pId (Proxy :: Proxy HyperdataDocument)
42 -- TODO getAllTableContacts
43 getAllContacts :: HasDBid NodeType => ParentId -> Cmd err (TableResult (Node HyperdataContact))
44 getAllContacts pId = getAllChildren pId (Proxy :: Proxy HyperdataContact)
47 getAllChildren :: (JSONB a, HasDBid NodeType)
51 -> Cmd err (NodeTableResult a)
52 getAllChildren pId p maybeNodeType = getChildren pId p maybeNodeType Nothing Nothing
55 getChildren :: (JSONB a, HasDBid NodeType)
61 -> Cmd err (NodeTableResult a)
62 getChildren pId p t@(Just NodeDocument) maybeOffset maybeLimit = getChildrenContext pId p t maybeOffset maybeLimit
63 getChildren pId p t@(Just NodeContact ) maybeOffset maybeLimit = getChildrenContext pId p t maybeOffset maybeLimit
64 getChildren a b c d e = getChildrenNode a b c d e
67 getChildrenNode :: (JSONB a, HasDBid NodeType)
73 -> Cmd err (NodeTableResult a)
74 getChildrenNode pId _ maybeNodeType maybeOffset maybeLimit = do
75 printDebug "getChildrenNode" (pId, maybeNodeType)
76 let query = selectChildrenNode pId maybeNodeType
80 $ orderBy (asc _node_id)
82 docCount <- runCountOpaQuery query
83 pure $ TableResult { tr_docs = docs, tr_count = docCount }
86 selectChildrenNode :: HasDBid NodeType
90 selectChildrenNode parentId maybeNodeType = proc () -> do
91 row@(Node _ _ typeName _ parent_id _ _ _) <- queryNodeTable -< ()
92 let nodeType = maybe 0 toDBid maybeNodeType
93 restrict -< typeName .== sqlInt4 nodeType
94 restrict -< parent_id .== (pgNodeId parentId)
98 getChildrenContext :: (JSONB a, HasDBid NodeType)
104 -> Cmd err (NodeTableResult a)
105 getChildrenContext pId _ maybeNodeType maybeOffset maybeLimit = do
106 printDebug "getChildrenContext" (pId, maybeNodeType)
107 let query = selectChildren' pId maybeNodeType
111 $ offset' maybeOffset
112 $ orderBy (asc _context_id)
115 docCount <- runCountOpaQuery query
116 pure $ TableResult { tr_docs = map context2node docs, tr_count = docCount }
119 selectChildren' :: HasDBid NodeType
122 -> Select ContextRead
123 selectChildren' parentId maybeNodeType = proc () -> do
124 row@(Context cid _ typeName _ _ _ _ _) <- queryContextTable -< ()
125 (NodeContext nid cid' _ _) <- queryNodeContextTable -< ()
127 let nodeType = maybe 0 toDBid maybeNodeType
128 restrict -< typeName .== sqlInt4 nodeType
130 restrict -< nid .== pgNodeId parentId
131 restrict -< cid .== cid'