where
import Control.Lens ((^.))
+import Data.HashMap.Strict (HashMap)
import Data.ByteString.Internal (ByteString)
-import Data.Map (Map, fromList)
import Data.Text (Text)
import qualified Database.PostgreSQL.Simple as PGS
+import qualified Data.List as List
+import qualified Data.HashMap.Strict as HashMap
import Gargantext.Core.Types
import Gargantext.Database.Prelude (runOpaQuery, Cmd)
import Gargantext.Prelude
queryNgramsTable :: Query NgramsRead
-queryNgramsTable = queryTable ngramsTable
+queryNgramsTable = selectTable ngramsTable
selectNgramsByDoc :: [ListId] -> DocId -> NgramsType -> Cmd err [Text]
selectNgramsByDoc lIds dId nt = runOpaQuery (query lIds dId nt)
-- TODO-ACCESS: access must not be checked here but when insertNgrams is called.
-insertNgrams :: [Ngrams] -> Cmd err (Map NgramsTerms NgramsId)
-insertNgrams ns = fromList <$> map (\(Indexed t i) -> (t, i)) <$> (insertNgrams' ns)
+insertNgrams :: [Ngrams] -> Cmd err (HashMap Text NgramsId)
+insertNgrams ns =
+ if List.null ns
+ then pure HashMap.empty
+ else HashMap.fromList <$> map (\(Indexed i t) -> (t, i)) <$> (insertNgrams' ns)
-- TODO-ACCESS: access must not be checked here but when insertNgrams' is called.
-insertNgrams' :: [Ngrams] -> Cmd err [Indexed Text]
+insertNgrams' :: [Ngrams] -> Cmd err [Indexed Int Text]
insertNgrams' ns = runPGSQuery queryInsertNgrams (PGS.Only $ Values fields ns)
where
fields = map (\t -> QualifiedIdentifier Nothing t) ["text", "int4"]
-_insertNgrams_Debug :: [(NgramsTerms, Size)] -> Cmd err ByteString
+_insertNgrams_Debug :: [(Text, Size)] -> Cmd err ByteString
_insertNgrams_Debug ns = formatPGSQuery queryInsertNgrams (PGS.Only $ Values fields ns)
where
fields = map (\t -> QualifiedIdentifier Nothing t) ["text", "int4"]
RETURNING id,terms
)
- SELECT terms, id
+ SELECT id, terms
FROM ins
UNION ALL
SELECT c.id, terms
FROM input_rows
JOIN ngrams c USING (terms); -- columns of unique index
|]
-
-