2 Module : Gargantext.Database.Query.Table.Node
3 Description : Main Tools of Node to the database
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# OPTIONS_GHC -fno-warn-orphans #-}
13 {-# LANGUAGE Arrows #-}
14 {-# LANGUAGE ConstraintKinds #-}
15 {-# LANGUAGE FunctionalDependencies #-}
16 {-# LANGUAGE QuasiQuotes #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeFamilies #-}
20 module Gargantext.Database.Query.Table.Context
23 import Control.Arrow (returnA)
24 import Gargantext.Core
25 import Gargantext.Core.Types
26 import Gargantext.Database.Admin.Types.Hyperdata
27 import Gargantext.Database.Prelude
28 import Gargantext.Database.Query.Filter (limit', offset')
29 import Gargantext.Database.Query.Table.Node.Error
30 import Gargantext.Database.Schema.Context
31 import Gargantext.Prelude hiding (sum, head)
32 import Opaleye hiding (FromField)
33 import Prelude hiding (null, id, map, sum)
36 getContextWith :: (HasNodeError err, JSONB a)
37 => ContextId -> proxy a -> Cmd err (Node a)
38 getContextWith nId _ = do
39 maybeContext <- headMay <$> runOpaQuery (selectContext (pgNodeId nId))
41 Nothing -> nodeError (DoesNotExist nId)
42 Just r -> pure $ context2node r
44 queryContextSearchTable :: Select ContextSearchRead
45 queryContextSearchTable = selectTable contextTableSearch
47 selectContext :: Column SqlInt4 -> Select ContextRead
48 selectContext id' = proc () -> do
49 row <- queryContextTable -< ()
50 restrict -< _context_id row .== id'
53 runGetContexts :: Select ContextRead -> Cmd err [Context HyperdataAny]
54 runGetContexts = runOpaQuery
56 ------------------------------------------------------------------------
57 ------------------------------------------------------------------------
58 -- | order by publication date
59 -- Favorites (Bool), node_ngrams
60 selectContextsWith :: HasDBid NodeType
61 => ParentId -> Maybe NodeType
62 -> Maybe Offset -> Maybe Limit -> Select ContextRead
63 selectContextsWith parentId maybeContextType maybeOffset maybeLimit =
64 --offset' maybeOffset $ limit' maybeLimit $ orderBy (asc (hyperdataDocument_Publication_date . node_hyperdata)) $ selectNodesWith' parentId typeId
65 limit' maybeLimit $ offset' maybeOffset
66 $ orderBy (asc _context_id)
67 $ selectContextsWith' parentId maybeContextType
69 selectContextsWith' :: HasDBid NodeType
70 => ParentId -> Maybe NodeType -> Select ContextRead
71 selectContextsWith' parentId maybeContextType = proc () -> do
72 context' <- (proc () -> do
73 row@(Context _ _ typeId _ parentId' _ _ _) <- queryContextTable -< ()
74 restrict -< parentId' .== (pgNodeId parentId)
76 let typeId' = maybe 0 toDBid maybeContextType
78 restrict -< if typeId' > 0
79 then typeId .== (sqlInt4 (typeId' :: Int))
81 returnA -< row ) -< ()
85 ------------------------------------------------------------------------
86 getDocumentsV3WithParentId :: HasDBid NodeType => NodeId -> Cmd err [Context HyperdataDocumentV3]
87 getDocumentsV3WithParentId n = runOpaQuery $ selectContextsWith' n (Just NodeDocument)
89 -- TODO: merge with getDocumentsWithParentId by having a class IsHyperdataDocument
90 getDocumentsWithParentId :: HasDBid NodeType => NodeId -> Cmd err [Context HyperdataDocument]
91 getDocumentsWithParentId n = runOpaQuery $ selectContextsWith' n (Just NodeDocument)
93 ------------------------------------------------------------------------
94 selectContextsWithParentID :: NodeId -> Select ContextRead
95 selectContextsWithParentID n = proc () -> do
96 row@(Context _ _ _ _ parent_id _ _ _) <- queryContextTable -< ()
97 restrict -< parent_id .== (pgNodeId n)
101 ------------------------------------------------------------------------
103 -- runCmdReplEasy (getNodesWithType NodeList (Proxy :: Proxy HyperdataList))
104 getContextsWithType :: (HasNodeError err, JSONB a, HasDBid NodeType) => NodeType -> proxy a -> Cmd err [Context a]
105 getContextsWithType nt _ = runOpaQuery $ selectContextsWithType nt
107 selectContextsWithType :: HasDBid NodeType
108 => NodeType -> Select ContextRead
109 selectContextsWithType nt' = proc () -> do
110 row@(Context _ _ tn _ _ _ _ _) <- queryContextTable -< ()
111 restrict -< tn .== (sqlInt4 $ toDBid nt')
114 getContextsIdWithType :: (HasNodeError err, HasDBid NodeType) => NodeType -> Cmd err [ContextId]
115 getContextsIdWithType nt = do
116 ns <- runOpaQuery $ selectContextsIdWithType nt
119 selectContextsIdWithType :: HasDBid NodeType
120 => NodeType -> Select (Column SqlInt4)
121 selectContextsIdWithType nt = proc () -> do
122 row@(Context _ _ tn _ _ _ _ _) <- queryContextTable -< ()
123 restrict -< tn .== (sqlInt4 $ toDBid nt)
124 returnA -< _context_id row
126 ------------------------------------------------------------------------