import Control.Lens.Cons
import Control.Lens.Prism
import Data.Aeson (toJSON, encode, ToJSON)
-import Data.Maybe (maybe, fromMaybe)
+import Data.Maybe (fromMaybe)
import Data.Text (Text)
-- import Data.ByteString (ByteString)
import Data.Time.Segment (jour)
import Database.PostgreSQL.Simple.ToField (toField, Action{-, ToField-})
import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
import GHC.Generics (Generic)
-import Gargantext.Database.Admin.Config (nodeTypeId)
+import Gargantext.Core (HasDBid(toDBid))
import Gargantext.Database.Admin.Types.Hyperdata
import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Prelude (Cmd, runPGSQuery{-, formatPGSQuery-})
-- UserId : user who is inserting the documents
-- ParentId : folder ID which is parent of the inserted documents
-- Administrator of the database has to create a uniq index as following SQL command:
--- `create unique index on nodes (typename, parent_id, (hyperdata ->> 'uniqId'));`
-insertDb :: InsertDb a => UserId -> ParentId -> [a] -> Cmd err [ReturnId]
+-- `create unique index on contexts table (typename, parent_id, (hyperdata ->> 'uniqId'));`
+insertDb :: (InsertDb a, HasDBid NodeType) => UserId -> ParentId -> [a] -> Cmd err [ReturnId]
insertDb u p = runPGSQuery queryInsert . Only . Values fields . map (insertDb' u p)
where
fields = map (\t-> QualifiedIdentifier Nothing t) inputSqlTypes
class InsertDb a
where
- insertDb' :: UserId -> ParentId -> a -> [Action]
+ insertDb' :: HasDBid NodeType => UserId -> ParentId -> a -> [Action]
instance InsertDb HyperdataDocument
where
insertDb' u p h = [ toField ("" :: Text)
- , toField $ nodeTypeId NodeDocument
+ , toField $ toDBid NodeDocument
, toField u
, toField p
, toField $ maybe "No Title" (DT.take 255) (_hd_title h)
instance InsertDb HyperdataContact
where
insertDb' u p h = [ toField ("" :: Text)
- , toField $ nodeTypeId NodeContact
+ , toField $ toDBid NodeContact
, toField u
, toField p
, toField $ maybe "Contact" (DT.take 255) (Just "Name") -- (_hc_name h)
queryInsert = [sql|
WITH input_rows(hash_id,typename,user_id,parent_id,name,date,hyperdata) AS (?)
, ins AS (
- INSERT INTO nodes (hash_id, typename,user_id,parent_id,name,date,hyperdata)
+ INSERT INTO contexts (hash_id, typename,user_id,parent_id,name,date,hyperdata)
SELECT * FROM input_rows
ON CONFLICT (hash_id) DO NOTHING -- on unique index -- this does not return the ids
RETURNING id,hash_id
, n.id
, hash_id
FROM input_rows
- JOIN nodes n USING (hash_id); -- columns of unique index
+ JOIN contexts n USING (hash_id); -- columns of unique index
|]
------------------------------------------------------------------------
secret = "Database secret to change"
-instance (AddUniqId a, ToJSON a) => AddUniqId (Node a)
+instance (AddUniqId a, ToJSON a, HasDBid NodeType) => AddUniqId (Node a)
where
addUniqId (Node nid _ t u p n d h) = Node nid hashId t u p n d h
where
hashId = Just $ "\\x" <> (hash $ DT.concat params)
params = [ secret
- , cs $ show $ nodeTypeId NodeDocument
+ , cs $ show $ toDBid NodeDocument
, n
, cs $ show p
, cs $ encode h
where
hashId = "\\x" <> (hash $ DT.concat params)
params = [ secret
- , cs $ show $ nodeTypeId NodeDocument
+ , cs $ show $ toDBid NodeDocument
, n
, cs $ show p
, cs $ encode h
class ToNode a
where
-- TODO Maybe NodeId
- toNode :: UserId -> ParentId -> a -> Node a
+ toNode :: HasDBid NodeType => UserId -> ParentId -> a -> Node a
instance ToNode HyperdataDocument where
- toNode u p h = Node 0 Nothing (nodeTypeId NodeDocument) u (Just p) n date h
+ toNode u p h = Node 0 Nothing (toDBid NodeDocument) u (Just p) n date h
where
n = maybe "No Title" (DT.take 255) (_hd_title h)
date = jour y m d
- y = maybe 0 fromIntegral $ _hd_publication_year h
+ -- NOTE: There is no year '0' in postgres, there is year 1 AD and beofre that year 1 BC:
+ -- select '0001-01-01'::date, '0001-01-01'::date - '1 day'::interval;
+ -- 0001-01-01 0001-12-31 00:00:00 BC
+ y = maybe 1 fromIntegral $ _hd_publication_year h
m = fromMaybe 1 $ _hd_publication_month h
d = fromMaybe 1 $ _hd_publication_day h
--- TODO
+-- TODO better Node
instance ToNode HyperdataContact where
- toNode = undefined
+ toNode u p h = Node 0 Nothing (toDBid NodeContact) u (Just p) "Contact" date h
+ where
+ date = jour 2020 01 01