)
where
-import Control.Arrow (returnA)
import Control.Lens ((^.))
+import Data.HashMap.Strict (HashMap)
+import Data.ByteString.Internal (ByteString)
import Data.Text (Text)
-import Data.Map (Map, fromList)
+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.Admin.Types.Node (pgNodeId)
import Gargantext.Database.Prelude (runOpaQuery, Cmd)
-import Gargantext.Database.Schema.Ngrams
import Gargantext.Database.Prelude (runPGSQuery, formatPGSQuery)
import Gargantext.Database.Query.Table.NodeNodeNgrams
-import Gargantext.Prelude
+import Gargantext.Database.Schema.Ngrams
import Gargantext.Database.Schema.Prelude
-import Data.ByteString.Internal (ByteString)
-import qualified Database.PostgreSQL.Simple as PGS
+import Gargantext.Database.Types
+import Gargantext.Prelude
queryNgramsTable :: Query NgramsRead
queryNgramsTable = queryTable ngramsTable
-- TODO-ACCESS: access must not be checked here but when insertNgrams is called.
-insertNgrams :: [Ngrams] -> Cmd err (Map NgramsTerms NgramsId)
-insertNgrams ns = fromList <$> map (\(NgramIds i t) -> (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 [NgramIds]
+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"]
FROM input_rows
JOIN ngrams c USING (terms); -- columns of unique index
|]
-