]> Git — Sourcephile - gargantext.git/blob - src/Data/Gargantext/Database/NodeNgram.hs
First commit to start with.
[gargantext.git] / src / Data / Gargantext / Database / NodeNgram.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE FunctionalDependencies #-}
5 {-# LANGUAGE Arrows #-}
6
7 module Data.Gargantext.Database.NodeNgram 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 )
25
26 import Data.Gargantext.Database.Private (infoGargandb)
27 import Data.Gargantext.Database.Instances
28
29 data NodeNgramPoly id node_id ngram_id weight
30 = NodeNgram { nodeNgram_NodeNgramId :: id
31 , nodeNgram_NodeNgramNodeId :: node_id
32 , nodeNgram_NodeNgramNgramId :: ngram_id
33 , nodeNgram_NodeNgramWeight :: weight
34 } deriving (Show)
35
36 type NodeNgramWrite = NodeNgramPoly (Maybe (Column PGInt4)) (Column PGInt4) (Column PGInt4) (Maybe (Column PGFloat8))
37 type NodeNgramRead = NodeNgramPoly (Column PGInt4) (Column PGInt4) (Column PGInt4) ((Column PGFloat8))
38
39
40 type NodeNgram = NodeNgramPoly (Maybe Int) Int Int (Maybe Double)
41
42 $(makeAdaptorAndInstance "pNodeNgram" ''NodeNgramPoly)
43 $(makeLensesWith abbreviatedFields ''NodeNgramPoly)
44
45
46 nodeNgramTable :: O.Table NodeNgramWrite NodeNgramRead
47 nodeNgramTable = O.Table "nodes_ngrams" (pNodeNgram NodeNgram { nodeNgram_NodeNgramId = O.optional "id"
48 , nodeNgram_NodeNgramNodeId = O.required "node_id"
49 , nodeNgram_NodeNgramNgramId = O.required "ngram_id"
50 , nodeNgram_NodeNgramWeight = O.optional "weight"
51 }
52 )
53
54
55 queryNodeNgramTable :: Query NodeNgramRead
56 queryNodeNgramTable = O.queryTable nodeNgramTable
57
58
59 -- | not optimized (get all ngrams without filters)
60 nodeNgrams :: IO [NodeNgram]
61 nodeNgrams = do
62 conn <- PGS.connect infoGargandb
63 O.runQuery conn queryNodeNgramTable
64