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.ByteString.Internal (ByteString)
26 import Data.Map (Map, fromList)
27 import Data.Text (Text)
28 import qualified Database.PostgreSQL.Simple as PGS
30 import Gargantext.Core.Types
31 import Gargantext.Database.Prelude (runOpaQuery, Cmd)
32 import Gargantext.Database.Prelude (runPGSQuery, formatPGSQuery)
33 import Gargantext.Database.Query.Table.NodeNodeNgrams
34 import Gargantext.Database.Schema.Ngrams
35 import Gargantext.Database.Schema.Prelude
36 import Gargantext.Prelude
38 queryNgramsTable :: Query NgramsRead
39 queryNgramsTable = queryTable ngramsTable
41 selectNgramsByDoc :: [ListId] -> DocId -> NgramsType -> Cmd err [Text]
42 selectNgramsByDoc lIds dId nt = runOpaQuery (query lIds dId nt)
45 join :: Query (NgramsRead, NodeNodeNgramsReadNull)
46 join = leftJoin queryNgramsTable queryNodeNodeNgramsTable on1
48 on1 (ng,nnng) = ng^.ngrams_id .== nnng^.nnng_ngrams_id
50 query cIds' dId' nt' = proc () -> do
51 (ng,nnng) <- join -< ()
52 restrict -< foldl (\b cId -> ((toNullable $ pgNodeId cId) .== nnng^.nnng_node1_id) .|| b) (pgBool True) cIds'
53 restrict -< (toNullable $ pgNodeId dId') .== nnng^.nnng_node2_id
54 restrict -< (toNullable $ pgNgramsType nt') .== nnng^.nnng_ngramsType
55 returnA -< ng^.ngrams_terms
58 _postNgrams :: CorpusId -> DocId -> [Text] -> Cmd err Int
59 _postNgrams = undefined
61 _dbGetNgramsDb :: Cmd err [NgramsDB]
62 _dbGetNgramsDb = runOpaQuery queryNgramsTable
65 -- TODO-ACCESS: access must not be checked here but when insertNgrams is called.
66 insertNgrams :: [Ngrams] -> Cmd err (Map NgramsTerms NgramsId)
67 insertNgrams ns = fromList <$> map (\(NgramIds i t) -> (t, i)) <$> (insertNgrams' ns)
69 -- TODO-ACCESS: access must not be checked here but when insertNgrams' is called.
70 insertNgrams' :: [Ngrams] -> Cmd err [NgramIds]
71 insertNgrams' ns = runPGSQuery queryInsertNgrams (PGS.Only $ Values fields ns)
73 fields = map (\t -> QualifiedIdentifier Nothing t) ["text", "int4"]
75 _insertNgrams_Debug :: [(NgramsTerms, Size)] -> Cmd err ByteString
76 _insertNgrams_Debug ns = formatPGSQuery queryInsertNgrams (PGS.Only $ Values fields ns)
78 fields = map (\t -> QualifiedIdentifier Nothing t) ["text", "int4"]
80 ----------------------
81 queryInsertNgrams :: PGS.Query
82 queryInsertNgrams = [sql|
83 WITH input_rows(terms,n) AS (?)
85 INSERT INTO ngrams (terms,n)
86 SELECT * FROM input_rows
87 ON CONFLICT (terms) DO NOTHING -- unique index created here
96 JOIN ngrams c USING (terms); -- columns of unique index