]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/NodeNode.hs
no unsafeIO
[gargantext.git] / src / Gargantext / Database / NodeNode.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE FunctionalDependencies #-}
5 {-# LANGUAGE Arrows #-}
6 {-# OPTIONS_GHC -fno-warn-orphans #-}
7
8 module Gargantext.Database.NodeNode where
9
10 import Prelude
11 import Data.Maybe (Maybe)
12 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
13 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
14 import qualified Database.PostgreSQL.Simple as PGS
15
16 import Opaleye
17
18
19 data NodeNodePoly node1_id node2_id score
20 = NodeNode { nodeNode_node1_id :: node1_id
21 , nodeNode_node2_id :: node2_id
22 , nodeNode_score :: score
23 } deriving (Show)
24
25 type NodeNodeWrite = NodeNodePoly (Column PGInt4) (Column PGInt4) (Maybe (Column PGFloat8))
26 type NodeNodeRead = NodeNodePoly (Column PGInt4) (Column PGInt4) (Column PGFloat8)
27
28
29 type NodeNode = NodeNodePoly Int Int (Maybe Double)
30
31 $(makeAdaptorAndInstance "pNodeNode" ''NodeNodePoly)
32 $(makeLensesWith abbreviatedFields ''NodeNodePoly)
33
34
35 nodeNodeTable :: Table NodeNodeWrite NodeNodeRead
36 nodeNodeTable = Table "nodes_nodes" (pNodeNode NodeNode { nodeNode_node1_id = required "node1_id"
37 , nodeNode_node2_id = required "node2_id"
38 , nodeNode_score = optional "score"
39 }
40 )
41
42
43 queryNodeNodeTable :: Query NodeNodeRead
44 queryNodeNodeTable = queryTable nodeNodeTable
45
46
47 -- | not optimized (get all ngrams without filters)
48 nodeNodes :: PGS.Connection -> IO [NodeNode]
49 nodeNodes conn = runQuery conn queryNodeNodeTable
50
51 instance QueryRunnerColumnDefault PGFloat8 (Maybe Double) where
52 queryRunnerColumnDefault = fieldQueryRunnerColumn