1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE FunctionalDependencies #-}
5 {-# LANGUAGE Arrows #-}
7 module Data.Gargantext.Database.NodeNgram where
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
18 import qualified Opaleye as O
19 import Opaleye (Column, PGBool, PGInt4, PGText, PGTimestamptz, PGFloat8
21 , QueryRunnerColumnDefault, queryRunnerColumnDefault
22 , fieldQueryRunnerColumn
26 import Data.Gargantext.Database.Private (infoGargandb)
27 import Data.Gargantext.Database.Instances
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
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))
40 type NodeNgram = NodeNgramPoly (Maybe Int) Int Int (Maybe Double)
42 $(makeAdaptorAndInstance "pNodeNgram" ''NodeNgramPoly)
43 $(makeLensesWith abbreviatedFields ''NodeNgramPoly)
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"
55 queryNodeNgramTable :: Query NodeNgramRead
56 queryNodeNgramTable = O.queryTable nodeNgramTable
59 -- | not optimized (get all ngrams without filters)
60 nodeNgrams :: IO [NodeNgram]
62 conn <- PGS.connect infoGargandb
63 O.runQuery conn queryNodeNgramTable