2 Module : Gargantext.Database.Query.Table.Ngrams
3 Description : Deal with in Gargantext Database.
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE Arrows #-}
13 {-# LANGUAGE QuasiQuotes #-}
14 {-# LANGUAGE TemplateHaskell #-}
16 module Gargantext.Database.Query.Table.Ngrams
17 ( module Gargantext.Database.Schema.Ngrams
24 import Control.Lens ((^.))
25 import Data.HashMap.Strict (HashMap)
26 import Data.ByteString.Internal (ByteString)
27 import Data.Map (Map, fromList)
28 import Data.Text (Text)
29 import qualified Database.PostgreSQL.Simple as PGS
30 import qualified Data.HashMap.Strict as HashMap
32 import Gargantext.Core.Types
33 import Gargantext.Database.Prelude (runOpaQuery, Cmd)
34 import Gargantext.Database.Prelude (runPGSQuery, formatPGSQuery)
35 import Gargantext.Database.Query.Table.NodeNodeNgrams
36 import Gargantext.Database.Schema.Ngrams
37 import Gargantext.Database.Schema.Prelude
38 import Gargantext.Database.Types
39 import Gargantext.Prelude
41 queryNgramsTable :: Query NgramsRead
42 queryNgramsTable = queryTable ngramsTable
44 selectNgramsByDoc :: [ListId] -> DocId -> NgramsType -> Cmd err [Text]
45 selectNgramsByDoc lIds dId nt = runOpaQuery (query lIds dId nt)
48 join :: Query (NgramsRead, NodeNodeNgramsReadNull)
49 join = leftJoin queryNgramsTable queryNodeNodeNgramsTable on1
51 on1 (ng,nnng) = ng^.ngrams_id .== nnng^.nnng_ngrams_id
53 query cIds' dId' nt' = proc () -> do
54 (ng,nnng) <- join -< ()
55 restrict -< foldl (\b cId -> ((toNullable $ pgNodeId cId) .== nnng^.nnng_node1_id) .|| b) (pgBool True) cIds'
56 restrict -< (toNullable $ pgNodeId dId') .== nnng^.nnng_node2_id
57 restrict -< (toNullable $ pgNgramsType nt') .== nnng^.nnng_ngramsType
58 returnA -< ng^.ngrams_terms
61 _postNgrams :: CorpusId -> DocId -> [Text] -> Cmd err Int
62 _postNgrams = undefined
64 _dbGetNgramsDb :: Cmd err [NgramsDB]
65 _dbGetNgramsDb = runOpaQuery queryNgramsTable
68 -- TODO-ACCESS: access must not be checked here but when insertNgrams is called.
69 insertNgrams :: [Ngrams] -> Cmd err (HashMap Text NgramsId)
70 insertNgrams ns = HashMap.fromList <$> map (\(Indexed i t) -> (t, i)) <$> (insertNgrams' ns)
72 -- TODO-ACCESS: access must not be checked here but when insertNgrams' is called.
73 insertNgrams' :: [Ngrams] -> Cmd err [Indexed Int Text]
74 insertNgrams' ns = runPGSQuery queryInsertNgrams (PGS.Only $ Values fields ns)
76 fields = map (\t -> QualifiedIdentifier Nothing t) ["text", "int4"]
78 _insertNgrams_Debug :: [(Text, Size)] -> Cmd err ByteString
79 _insertNgrams_Debug ns = formatPGSQuery queryInsertNgrams (PGS.Only $ Values fields ns)
81 fields = map (\t -> QualifiedIdentifier Nothing t) ["text", "int4"]
83 ----------------------
84 queryInsertNgrams :: PGS.Query
85 queryInsertNgrams = [sql|
86 WITH input_rows(terms,n) AS (?)
88 INSERT INTO ngrams (terms,n)
89 SELECT * FROM input_rows
90 ON CONFLICT (terms) DO NOTHING -- unique index created here
99 JOIN ngrams c USING (terms); -- columns of unique index