]> Git — Sourcephile - gargantext.git/blob - src/Data/Gargantext/Database/NodeNode.hs
First commit to start with.
[gargantext.git] / src / Data / Gargantext / Database / NodeNode.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE FunctionalDependencies #-}
5 {-# LANGUAGE Arrows #-}
6
7 module Data.Gargantext.Database.NodeNode where
8
9 import Prelude
10 import Data.Time (UTCTime)
11 import Data.Text (Text)
12 import Data.Maybe (Maybe)
13 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
14 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
15 import Control.Arrow (returnA)
16 import qualified Database.PostgreSQL.Simple as PGS
17
18 import qualified Opaleye as O
19 import Opaleye (Column, PGBool, PGInt4, PGText, PGTimestamptz, PGFloat8
20 , Table(Table), Query
21 , QueryRunnerColumnDefault, queryRunnerColumnDefault
22 , fieldQueryRunnerColumn
23 , (.==), (.>)
24 , required, optional
25 )
26
27 import Data.Gargantext.Database.Private (infoGargandb)
28 import Data.Gargantext.Database.Instances
29
30 data NodeNodePoly node1_id node2_id score
31 = NodeNode { nodeNode_node1_id :: node1_id
32 , nodeNode_node2_id :: node2_id
33 , nodeNode_score :: score
34 } deriving (Show)
35
36 type NodeNodeWrite = NodeNodePoly (Column PGInt4) (Column PGInt4) (Maybe (Column PGFloat8))
37 type NodeNodeRead = NodeNodePoly (Column PGInt4) (Column PGInt4) (Column PGFloat8)
38
39
40 type NodeNode = NodeNodePoly Int Int (Maybe Double)
41
42 $(makeAdaptorAndInstance "pNodeNode" ''NodeNodePoly)
43 $(makeLensesWith abbreviatedFields ''NodeNodePoly)
44
45
46 nodeNodeTable :: O.Table NodeNodeWrite NodeNodeRead
47 nodeNodeTable = O.Table "nodes_nodes" (pNodeNode NodeNode { nodeNode_node1_id = required "node1_id"
48 , nodeNode_node2_id = required "node2_id"
49 , nodeNode_score = optional "score"
50 }
51 )
52
53
54 queryNodeNodeTable :: Query NodeNodeRead
55 queryNodeNodeTable = O.queryTable nodeNodeTable
56
57
58 -- | not optimized (get all ngrams without filters)
59 nodeNodes :: IO [NodeNode]
60 nodeNodes = do
61 conn <- PGS.connect infoGargandb
62 O.runQuery conn queryNodeNodeTable
63