1 {-# LANGUAGE TemplateHaskell #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE FunctionalDependencies #-}
5 {-# LANGUAGE Arrows #-}
6 {-# OPTIONS_GHC -fno-warn-orphans #-}
9 module Gargantext.Database.NodeNgram where
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
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
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))
31 type NodeNgram = NodeNgramPoly (Maybe Int) Int Int (Maybe Double)
33 $(makeAdaptorAndInstance "pNodeNgram" ''NodeNgramPoly)
34 $(makeLensesWith abbreviatedFields ''NodeNgramPoly)
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"
46 queryNodeNgramTable :: Query NodeNgramRead
47 queryNodeNgramTable = queryTable nodeNgramTable
50 instance QueryRunnerColumnDefault PGInt4 (Maybe Int) where
51 queryRunnerColumnDefault = fieldQueryRunnerColumn
53 instance QueryRunnerColumnDefault PGFloat8 (Maybe Double) where
54 queryRunnerColumnDefault = fieldQueryRunnerColumn
57 -- | not optimized (get all ngrams without filters)
58 nodeNgrams :: PGS.Connection -> IO [NodeNgram]
59 nodeNgrams conn = runQuery conn queryNodeNgramTable