Commit 5efcb182 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[TAB] Opaleye query for Document view (todo: date + title later).

parent 3c9f028c
......@@ -67,7 +67,9 @@ import Gargantext.Database.Config (nodeTypeId)
--instance FromJSON Facet
--instance ToJSON Facet
type FacetDoc = Facet NodeId UTCTime HyperdataDocument Bool Int
type Favorite = Bool
type FacetDoc = Facet NodeId UTCTime HyperdataDocument Favorite Int
type FacetSources = FacetDoc
type FacetAuthors = FacetDoc
type FacetTerms = FacetDoc
......@@ -108,7 +110,7 @@ $(makeLensesWith abbreviatedFields ''Facet)
type FacetDocRead = Facet (Column PGInt4 )
(Column PGTimestamptz)
(Column PGJsonb )
(Column PGBool )
(Column PGBool)
(Column PGInt4 )
-----------------------------------------------------------------------
......@@ -125,6 +127,46 @@ instance Arbitrary FacetChart where
-----------------------------------------------------------------------
data OrderBy = DateAsc | DateDesc
-- | TitleAsc | TitleDesc
| FavDesc | FavAsc -- | NgramCount
viewDocuments :: CorpusId -> NodeTypeId -> Query FacetDocRead
viewDocuments cId ntId = proc () -> do
n <- queryNodeTable -< ()
nn <- queryNodeNodeTable -< ()
restrict -< _node_id n .== nodeNode_node2_id nn
restrict -< nodeNode_node1_id nn .== (pgInt4 cId)
restrict -< _node_typename n .== (pgInt4 ntId)
returnA -< FacetDoc (_node_id n) (_node_date n) (_node_hyperdata n) (nodeNode_favorite nn) (pgInt4 1)
filterDocuments :: (PGOrd date, PGOrd favorite) =>
Maybe Gargantext.Core.Types.Offset
-> Maybe Gargantext.Core.Types.Limit
-> OrderBy
-> Select (Facet id (Column date) hyperdata (Column favorite) ngramCount)
-> Query (Facet id (Column date) hyperdata (Column favorite) ngramCount)
filterDocuments o l order q = limit' l $ offset' o $ orderBy ordering q
where
ordering = case order of
DateAsc -> asc facetDoc_created
DateDesc -> desc facetDoc_created
--TitleAsc -> asc facetDoc_hyperdata
--TitleDesc -> desc facetDoc_hyperdata
FavAsc -> asc facetDoc_favorite
FavDesc -> desc facetDoc_favorite
runViewDocuments :: CorpusId -> Maybe Offset -> Maybe Limit -> OrderBy -> Cmd [FacetDoc]
runViewDocuments cId o l order = mkCmd $ \c -> runQuery c ( filterDocuments o l order
$ viewDocuments cId ntId)
where
ntId = nodeTypeId NodeDocument
{-
getDocFacet :: Connection -> NodeType -> Int -> Maybe NodeType
-> Maybe Offset -> Maybe Limit
......
......@@ -64,14 +64,14 @@ add_debug pId ns = mkCmd $ \c -> formatQuery c queryAdd (Only $ Values fields in
-- | Input Tables: types of the tables
inputSqlTypes :: [Text]
inputSqlTypes = map DT.pack ["int4","int4"]
inputSqlTypes = map DT.pack ["int4","int4","bool"]
-- | SQL query to add documents
-- TODO return id of added documents only
queryAdd :: Query
queryAdd = [sql|
WITH input_rows(node1_id,node2_id) AS (?)
INSERT INTO nodes_nodes (node1_id, node2_id)
WITH input_rows(node1_id,node2_id, favorite) AS (?)
INSERT INTO nodes_nodes (node1_id, node2_id, favorite)
SELECT * FROM input_rows
ON CONFLICT (node1_id, node2_id) DO NOTHING -- on unique index
RETURNING 1
......@@ -79,7 +79,7 @@ queryAdd = [sql|
|]
prepare :: ParentId -> [NodeId] -> [InputData]
prepare pId ns = map (\nId -> InputData pId nId) ns
prepare pId ns = map (\nId -> InputData pId nId False) ns
------------------------------------------------------------------------
-- * Main Types used
......@@ -87,10 +87,12 @@ prepare pId ns = map (\nId -> InputData pId nId) ns
data InputData = InputData { inNode1_id :: NodeId
, inNode2_id :: NodeId
, inNode_fav :: Bool
} deriving (Show, Generic, Typeable)
instance ToRow InputData where
toRow inputData = [ toField (inNode1_id inputData)
, toField (inNode2_id inputData)
, toField (inNode_fav inputData)
]
......@@ -22,6 +22,7 @@ commentary with @some markup@.
module Gargantext.Database.NodeNode where
import Gargantext.Database.Node (Cmd(..), mkCmd)
import Gargantext.Prelude
import Data.Maybe (Maybe)
import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
......@@ -77,8 +78,8 @@ queryNodeNodeTable = queryTable nodeNodeTable
-- | not optimized (get all ngrams without filters)
nodeNodes :: PGS.Connection -> IO [NodeNode]
nodeNodes conn = runQuery conn queryNodeNodeTable
nodesNodes :: Cmd [NodeNode]
nodesNodes = mkCmd $ \c -> runQuery c queryNodeNodeTable
instance QueryRunnerColumnDefault (Nullable PGInt4) Int where
queryRunnerColumnDefault = fieldQueryRunnerColumn
......
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