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