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.Text (Text)
28 import qualified Database.PostgreSQL.Simple as PGS
29 import qualified Data.List as List
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 = selectTable 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) (sqlBool 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)
72 then pure HashMap.empty
73 else HashMap.fromList <$> map (\(Indexed i t) -> (t, i)) <$> (insertNgrams' ns)
75 -- TODO-ACCESS: access must not be checked here but when insertNgrams' is called.
76 insertNgrams' :: [Ngrams] -> Cmd err [Indexed Int Text]
77 insertNgrams' ns = runPGSQuery queryInsertNgrams (PGS.Only $ Values fields ns)
79 fields = map (\t -> QualifiedIdentifier Nothing t) ["text", "int4"]
81 _insertNgrams_Debug :: [(Text, Size)] -> Cmd err ByteString
82 _insertNgrams_Debug ns = formatPGSQuery queryInsertNgrams (PGS.Only $ Values fields ns)
84 fields = map (\t -> QualifiedIdentifier Nothing t) ["text", "int4"]
86 ----------------------
87 queryInsertNgrams :: PGS.Query
88 queryInsertNgrams = [sql|
89 WITH input_rows(terms,n) AS (?)
91 INSERT INTO ngrams (terms,n)
92 SELECT * FROM input_rows
93 ON CONFLICT (terms) DO NOTHING -- unique index created here
102 JOIN ngrams c USING (terms); -- columns of unique index