Commit a891c1f5 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[FIX] insert db query

parent 705f1e2d
...@@ -235,6 +235,7 @@ insertDocs :: ( FlowCmdM env err m ...@@ -235,6 +235,7 @@ insertDocs :: ( FlowCmdM env err m
insertDocs uId cId hs = do insertDocs uId cId hs = do
let docs = map addUniqId hs let docs = map addUniqId hs
newIds <- insertDb uId cId docs newIds <- insertDb uId cId docs
printDebug "newIds" newIds
let let
newIds' = map reId newIds newIds' = map reId newIds
documentsWithId = mergeData (toInserted newIds) (Map.fromList $ map viewUniqId' docs) documentsWithId = mergeData (toInserted newIds) (Map.fromList $ map viewUniqId' docs)
......
...@@ -114,7 +114,8 @@ class InsertDb a ...@@ -114,7 +114,8 @@ class InsertDb a
instance InsertDb HyperdataDocument instance InsertDb HyperdataDocument
where where
insertDb' u p h = [ toField $ nodeTypeId NodeDocument insertDb' u p h = [ toField ("" :: Text)
, toField $ nodeTypeId NodeDocument
, toField u , toField u
, toField p , toField p
, toField $ maybe "No Title" (DT.take 255) (_hd_title h) , toField $ maybe "No Title" (DT.take 255) (_hd_title h)
...@@ -124,7 +125,8 @@ instance InsertDb HyperdataDocument ...@@ -124,7 +125,8 @@ instance InsertDb HyperdataDocument
instance InsertDb HyperdataContact instance InsertDb HyperdataContact
where where
insertDb' u p h = [ toField $ nodeTypeId NodeContact insertDb' u p h = [ toField ("" :: Text)
, toField $ nodeTypeId NodeContact
, toField u , toField u
, toField p , toField p
, toField $ maybe "Contact" (DT.take 255) (Just "Name") -- (_hc_name h) , toField $ maybe "Contact" (DT.take 255) (Just "Name") -- (_hc_name h)
...@@ -146,30 +148,29 @@ insertDocuments_Debug uId pId hs = formatPGSQuery queryInsert (Only $ Values fie ...@@ -146,30 +148,29 @@ insertDocuments_Debug uId pId hs = formatPGSQuery queryInsert (Only $ Values fie
-- | Input Tables: types of the tables -- | Input Tables: types of the tables
inputSqlTypes :: [Text] inputSqlTypes :: [Text]
inputSqlTypes = map DT.pack ["int4","int4","int4","text","date","jsonb"] inputSqlTypes = map DT.pack ["text", "int4","int4","int4","text","date","jsonb"]
-- | SQL query to insert documents inside the database -- | SQL query to insert documents inside the database
queryInsert :: Query queryInsert :: Query
queryInsert = [sql| queryInsert = [sql|
WITH input_rows(typename,user_id,parent_id,name,date,hyperdata) AS (?) WITH input_rows(hash_id,typename,user_id,parent_id,name,date,hyperdata) AS (?)
, ins AS ( , ins AS (
INSERT INTO nodes (typename,user_id,parent_id,name,date,hyperdata) INSERT INTO nodes (hash_id, typename,user_id,parent_id,name,date,hyperdata)
SELECT * FROM input_rows SELECT * FROM input_rows
ON CONFLICT ((hyperdata ->> 'uniqIdBdd')) DO NOTHING -- on unique index -- this does not return the ids ON CONFLICT (hash_id) DO NOTHING -- on unique index -- this does not return the ids
-- ON CONFLICT ((hyperdata ->> 'uniqIdBdd')) DO UPDATE SET user_id=EXCLUDED.user_id -- on unique index RETURNING id,hash_id
RETURNING id,hyperdata
) )
SELECT true AS source -- true for 'newly inserted' SELECT true AS source -- true for 'newly inserted'
, id , id
, hyperdata ->> 'uniqIdBdd' as doi , hash_id
FROM ins FROM ins
UNION ALL UNION ALL
SELECT false AS source -- false for 'not inserted' SELECT false AS source -- false for 'not inserted'
, c.id , n.id
, hyperdata ->> 'uniqIdBdd' as doi , hash_id
FROM input_rows FROM input_rows
JOIN nodes c USING (hyperdata); -- columns of unique index JOIN nodes n USING (hash_id); -- columns of unique index
|] |]
------------------------------------------------------------------------ ------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment