]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/NodeNgram.hs
Need to derive Arbitrary for ParseError and Eq for ZonedTime
[gargantext.git] / src / Gargantext / Database / NodeNgram.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE FunctionalDependencies #-}
5 {-# LANGUAGE Arrows #-}
6 {-# OPTIONS_GHC -fno-warn-orphans #-}
7
8
9 module Gargantext.Database.NodeNgram where
10
11 import Prelude
12 import Data.Maybe (Maybe)
13 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
14 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
15 import qualified Database.PostgreSQL.Simple as PGS
16
17 import Opaleye
18
19
20 data NodeNgramPoly id node_id ngram_id weight
21 = NodeNgram { nodeNgram_NodeNgramId :: id
22 , nodeNgram_NodeNgramNodeId :: node_id
23 , nodeNgram_NodeNgramNgramId :: ngram_id
24 , nodeNgram_NodeNgramWeight :: weight
25 } deriving (Show)
26
27 type NodeNgramWrite = NodeNgramPoly (Maybe (Column PGInt4)) (Column PGInt4) (Column PGInt4) (Maybe (Column PGFloat8))
28 type NodeNgramRead = NodeNgramPoly (Column PGInt4) (Column PGInt4) (Column PGInt4) ((Column PGFloat8))
29
30
31 type NodeNgram = NodeNgramPoly (Maybe Int) Int Int (Maybe Double)
32
33 $(makeAdaptorAndInstance "pNodeNgram" ''NodeNgramPoly)
34 $(makeLensesWith abbreviatedFields ''NodeNgramPoly)
35
36
37 nodeNgramTable :: Table NodeNgramWrite NodeNgramRead
38 nodeNgramTable = Table "nodes_ngrams" (pNodeNgram NodeNgram { nodeNgram_NodeNgramId = optional "id"
39 , nodeNgram_NodeNgramNodeId = required "node_id"
40 , nodeNgram_NodeNgramNgramId = required "ngram_id"
41 , nodeNgram_NodeNgramWeight = optional "weight"
42 }
43 )
44
45
46 queryNodeNgramTable :: Query NodeNgramRead
47 queryNodeNgramTable = queryTable nodeNgramTable
48
49
50 instance QueryRunnerColumnDefault PGInt4 (Maybe Int) where
51 queryRunnerColumnDefault = fieldQueryRunnerColumn
52
53 instance QueryRunnerColumnDefault PGFloat8 (Maybe Double) where
54 queryRunnerColumnDefault = fieldQueryRunnerColumn
55
56
57 -- | not optimized (get all ngrams without filters)
58 nodeNgrams :: PGS.Connection -> IO [NodeNgram]
59 nodeNgrams conn = runQuery conn queryNodeNgramTable