{-|
Module      : Gargantext.Database.Schema.Node
Description : Main requests of Node to the database
Copyright   : (c) CNRS, 2017-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX
-}


{-# OPTIONS_GHC -fno-warn-orphans        #-}

{-# LANGUAGE Arrows                 #-}
{-# LANGUAGE ConstraintKinds        #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TemplateHaskell        #-}
{-# LANGUAGE TypeFamilies           #-}

module Gargantext.Database.Schema.Node where

import Control.Lens hiding (elements, (&))
import Gargantext.Database.Schema.Prelude
import Prelude hiding (null, id, map, sum)

------------------------------------------------------------------------
-- Main polymorphic Node definition
data NodePoly id
              hash_id
              typename
              user_id
              parent_id
              name
              date
              hyperdata  =
     Node { NodePoly id hash_id typename user_id parent_id name date hyperdata
-> id
_node_id        :: !id
          , NodePoly id hash_id typename user_id parent_id name date hyperdata
-> hash_id
_node_hash_id   :: !hash_id
          , NodePoly id hash_id typename user_id parent_id name date hyperdata
-> typename
_node_typename  :: !typename

          , NodePoly id hash_id typename user_id parent_id name date hyperdata
-> user_id
_node_user_id    :: !user_id
          , NodePoly id hash_id typename user_id parent_id name date hyperdata
-> parent_id
_node_parent_id  :: !parent_id

          , NodePoly id hash_id typename user_id parent_id name date hyperdata
-> name
_node_name      :: !name
          , NodePoly id hash_id typename user_id parent_id name date hyperdata
-> date
_node_date      :: !date

          , NodePoly id hash_id typename user_id parent_id name date hyperdata
-> hyperdata
_node_hyperdata :: !hyperdata
          } deriving (Int
-> NodePoly
     id hash_id typename user_id parent_id name date hyperdata
-> ShowS
[NodePoly
   id hash_id typename user_id parent_id name date hyperdata]
-> ShowS
NodePoly id hash_id typename user_id parent_id name date hyperdata
-> String
(Int
 -> NodePoly
      id hash_id typename user_id parent_id name date hyperdata
 -> ShowS)
-> (NodePoly
      id hash_id typename user_id parent_id name date hyperdata
    -> String)
-> ([NodePoly
       id hash_id typename user_id parent_id name date hyperdata]
    -> ShowS)
-> Show
     (NodePoly
        id hash_id typename user_id parent_id name date hyperdata)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall id hash_id typename user_id parent_id name date hyperdata.
(Show id, Show hash_id, Show typename, Show user_id,
 Show parent_id, Show name, Show date, Show hyperdata) =>
Int
-> NodePoly
     id hash_id typename user_id parent_id name date hyperdata
-> ShowS
forall id hash_id typename user_id parent_id name date hyperdata.
(Show id, Show hash_id, Show typename, Show user_id,
 Show parent_id, Show name, Show date, Show hyperdata) =>
[NodePoly
   id hash_id typename user_id parent_id name date hyperdata]
-> ShowS
forall id hash_id typename user_id parent_id name date hyperdata.
(Show id, Show hash_id, Show typename, Show user_id,
 Show parent_id, Show name, Show date, Show hyperdata) =>
NodePoly id hash_id typename user_id parent_id name date hyperdata
-> String
showList :: [NodePoly
   id hash_id typename user_id parent_id name date hyperdata]
-> ShowS
$cshowList :: forall id hash_id typename user_id parent_id name date hyperdata.
(Show id, Show hash_id, Show typename, Show user_id,
 Show parent_id, Show name, Show date, Show hyperdata) =>
[NodePoly
   id hash_id typename user_id parent_id name date hyperdata]
-> ShowS
show :: NodePoly id hash_id typename user_id parent_id name date hyperdata
-> String
$cshow :: forall id hash_id typename user_id parent_id name date hyperdata.
(Show id, Show hash_id, Show typename, Show user_id,
 Show parent_id, Show name, Show date, Show hyperdata) =>
NodePoly id hash_id typename user_id parent_id name date hyperdata
-> String
showsPrec :: Int
-> NodePoly
     id hash_id typename user_id parent_id name date hyperdata
-> ShowS
$cshowsPrec :: forall id hash_id typename user_id parent_id name date hyperdata.
(Show id, Show hash_id, Show typename, Show user_id,
 Show parent_id, Show name, Show date, Show hyperdata) =>
Int
-> NodePoly
     id hash_id typename user_id parent_id name date hyperdata
-> ShowS
Show, (forall x.
 NodePoly id hash_id typename user_id parent_id name date hyperdata
 -> Rep
      (NodePoly
         id hash_id typename user_id parent_id name date hyperdata)
      x)
-> (forall x.
    Rep
      (NodePoly
         id hash_id typename user_id parent_id name date hyperdata)
      x
    -> NodePoly
         id hash_id typename user_id parent_id name date hyperdata)
-> Generic
     (NodePoly
        id hash_id typename user_id parent_id name date hyperdata)
forall x.
Rep
  (NodePoly
     id hash_id typename user_id parent_id name date hyperdata)
  x
-> NodePoly
     id hash_id typename user_id parent_id name date hyperdata
forall x.
NodePoly id hash_id typename user_id parent_id name date hyperdata
-> Rep
     (NodePoly
        id hash_id typename user_id parent_id name date hyperdata)
     x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall id hash_id typename user_id parent_id name date hyperdata x.
Rep
  (NodePoly
     id hash_id typename user_id parent_id name date hyperdata)
  x
-> NodePoly
     id hash_id typename user_id parent_id name date hyperdata
forall id hash_id typename user_id parent_id name date hyperdata x.
NodePoly id hash_id typename user_id parent_id name date hyperdata
-> Rep
     (NodePoly
        id hash_id typename user_id parent_id name date hyperdata)
     x
$cto :: forall id hash_id typename user_id parent_id name date hyperdata x.
Rep
  (NodePoly
     id hash_id typename user_id parent_id name date hyperdata)
  x
-> NodePoly
     id hash_id typename user_id parent_id name date hyperdata
$cfrom :: forall id hash_id typename user_id parent_id name date hyperdata x.
NodePoly id hash_id typename user_id parent_id name date hyperdata
-> Rep
     (NodePoly
        id hash_id typename user_id parent_id name date hyperdata)
     x
Generic)

------------------------------------------------------------------------
-- Automatic instances derivation
$(deriveJSON (unPrefix "_node_") ''NodePoly)
$(makeLenses ''NodePoly)

$(makeAdaptorAndInstance "pNode"   ''NodePoly)
$(makeLensesWith abbreviatedFields ''NodePoly)

nodeTable :: Table NodeWrite NodeRead
nodeTable :: Table NodeWrite NodeRead
nodeTable = String
-> TableFields NodeWrite NodeRead -> Table NodeWrite NodeRead
forall writeFields viewFields.
String
-> TableFields writeFields viewFields
-> Table writeFields viewFields
Table String
"nodes" (NodePoly
  (TableFields (Maybe (Column PGInt4)) (Column PGInt4))
  (TableFields (Maybe (Column PGText)) (Column PGText))
  (TableFields (Column PGInt4) (Column PGInt4))
  (TableFields (Column PGInt4) (Column PGInt4))
  (TableFields (Maybe (Column PGInt4)) (Column PGInt4))
  (TableFields (Column PGText) (Column PGText))
  (TableFields (Maybe (Column PGTimestamptz)) (Column PGTimestamptz))
  (TableFields (Column PGJsonb) (Column PGJsonb))
-> TableFields NodeWrite NodeRead
forall (p :: * -> * -> *) a1_0 a2_0 a3_0 a4_0 a5_0 a6_0 a7_0 a8_0
       a1_1 a2_1 a3_1 a4_1 a5_1 a6_1 a7_1 a8_1.
ProductProfunctor p =>
NodePoly
  (p a1_0 a1_1)
  (p a2_0 a2_1)
  (p a3_0 a3_1)
  (p a4_0 a4_1)
  (p a5_0 a5_1)
  (p a6_0 a6_1)
  (p a7_0 a7_1)
  (p a8_0 a8_1)
-> p (NodePoly a1_0 a2_0 a3_0 a4_0 a5_0 a6_0 a7_0 a8_0)
     (NodePoly a1_1 a2_1 a3_1 a4_1 a5_1 a6_1 a7_1 a8_1)
pNode Node :: forall id hash_id typename user_id parent_id name date hyperdata.
id
-> hash_id
-> typename
-> user_id
-> parent_id
-> name
-> date
-> hyperdata
-> NodePoly
     id hash_id typename user_id parent_id name date hyperdata
Node { _node_id :: TableFields (Maybe (Column PGInt4)) (Column PGInt4)
_node_id         = String -> TableFields (Maybe (Column PGInt4)) (Column PGInt4)
forall a. String -> TableFields (Maybe (Column a)) (Column a)
optionalTableField String
"id"
                                      , _node_hash_id :: TableFields (Maybe (Column PGText)) (Column PGText)
_node_hash_id    = String -> TableFields (Maybe (Column PGText)) (Column PGText)
forall a. String -> TableFields (Maybe (Column a)) (Column a)
optionalTableField String
"hash_id"
                                      , _node_typename :: TableFields (Column PGInt4) (Column PGInt4)
_node_typename   = String -> TableFields (Column PGInt4) (Column PGInt4)
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"typename"
                                      , _node_user_id :: TableFields (Column PGInt4) (Column PGInt4)
_node_user_id    = String -> TableFields (Column PGInt4) (Column PGInt4)
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"user_id"

                                      , _node_parent_id :: TableFields (Maybe (Column PGInt4)) (Column PGInt4)
_node_parent_id  = String -> TableFields (Maybe (Column PGInt4)) (Column PGInt4)
forall a. String -> TableFields (Maybe (Column a)) (Column a)
optionalTableField String
"parent_id"
                                      , _node_name :: TableFields (Column PGText) (Column PGText)
_node_name       = String -> TableFields (Column PGText) (Column PGText)
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"name"
                                      , _node_date :: TableFields (Maybe (Column PGTimestamptz)) (Column PGTimestamptz)
_node_date       = String
-> TableFields
     (Maybe (Column PGTimestamptz)) (Column PGTimestamptz)
forall a. String -> TableFields (Maybe (Column a)) (Column a)
optionalTableField String
"date"

                                      , _node_hyperdata :: TableFields (Column PGJsonb) (Column PGJsonb)
_node_hyperdata  = String -> TableFields (Column PGJsonb) (Column PGJsonb)
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"hyperdata"
                                      -- ignoring ts_vector field here
                                      }
                            )

queryNodeTable :: Query NodeRead
queryNodeTable :: Query NodeRead
queryNodeTable = Table NodeWrite NodeRead -> Query NodeRead
forall fields a.
Default Unpackspec fields fields =>
Table a fields -> Select fields
selectTable Table NodeWrite NodeRead
nodeTable
------------------------------------------------------------------------
type NodeWrite = NodePoly (Maybe (Column PGInt4)      )
                          (Maybe (Column PGText)      )
                                 (Column PGInt4)
                                 (Column PGInt4)
                          (Maybe (Column PGInt4)      )
                                 (Column PGText)
                          (Maybe (Column PGTimestamptz))
                                 (Column PGJsonb)

type NodeRead = NodePoly (Column PGInt4        )
                         (Column PGText        )
                         (Column PGInt4        )
                         (Column PGInt4        )
                         (Column PGInt4        )
                         (Column PGText        )
                         (Column PGTimestamptz )
                         (Column PGJsonb       )

type NodeReadNull = NodePoly (Column (Nullable PGInt4))
                             (Column (Nullable PGText))
                             (Column (Nullable PGInt4))
                             (Column (Nullable PGInt4))
                             (Column (Nullable PGInt4))
                             (Column (Nullable PGText))
                             (Column (Nullable PGTimestamptz))
                             (Column (Nullable PGJsonb))
------------------------------------------------------------------------
-- | Node(Read|Write)Search is slower than Node(Write|Read) use it
-- for full text search only

type NodeSearchWrite =
  NodePolySearch
    (Maybe  (Column  PGInt4)      )
    (Column  PGInt4               )
    (Column  PGInt4               )
    (Column (Nullable PGInt4)     )
    (Column PGText                )
    (Maybe  (Column PGTimestamptz))
    (Column  PGJsonb              )
    (Maybe  (Column PGTSVector)   )

type NodeSearchRead =
  NodePolySearch
    (Column  PGInt4           )
    (Column  PGInt4           )
    (Column  PGInt4           )
    (Column (Nullable PGInt4 ))
    (Column  PGText           )
    (Column  PGTimestamptz    )
    (Column  PGJsonb          )
    (Column  PGTSVector       )

type NodeSearchReadNull =
  NodePolySearch
    (Column (Nullable PGInt4)       )
    (Column (Nullable PGInt4)       )
    (Column (Nullable PGInt4)       )
    (Column (Nullable PGInt4)       )
    (Column (Nullable PGText)       )
    (Column (Nullable PGTimestamptz))
    (Column (Nullable PGJsonb)      )
    (Column (Nullable PGTSVector)   )


data NodePolySearch id
                    typename
                    user_id
                    parent_id
                    name
                    date
                    hyperdata
                    search =
     NodeSearch { NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> id
_ns_id           :: id
                , NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> typename
_ns_typename     :: typename
                , NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> user_id
_ns_user_id      :: user_id
           --   , nodeUniqId       :: shaId
                , NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> parent_id
_ns_parent_id    :: parent_id
                , NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> name
_ns_name         :: name
                , NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> date
_ns_date         :: date

                , NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> hyperdata
_ns_hyperdata    :: hyperdata
                , NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> search
_ns_search       :: search
                } deriving (Int
-> NodePolySearch
     id typename user_id parent_id name date hyperdata search
-> ShowS
[NodePolySearch
   id typename user_id parent_id name date hyperdata search]
-> ShowS
NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> String
(Int
 -> NodePolySearch
      id typename user_id parent_id name date hyperdata search
 -> ShowS)
-> (NodePolySearch
      id typename user_id parent_id name date hyperdata search
    -> String)
-> ([NodePolySearch
       id typename user_id parent_id name date hyperdata search]
    -> ShowS)
-> Show
     (NodePolySearch
        id typename user_id parent_id name date hyperdata search)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall id typename user_id parent_id name date hyperdata search.
(Show id, Show typename, Show user_id, Show parent_id, Show name,
 Show date, Show hyperdata, Show search) =>
Int
-> NodePolySearch
     id typename user_id parent_id name date hyperdata search
-> ShowS
forall id typename user_id parent_id name date hyperdata search.
(Show id, Show typename, Show user_id, Show parent_id, Show name,
 Show date, Show hyperdata, Show search) =>
[NodePolySearch
   id typename user_id parent_id name date hyperdata search]
-> ShowS
forall id typename user_id parent_id name date hyperdata search.
(Show id, Show typename, Show user_id, Show parent_id, Show name,
 Show date, Show hyperdata, Show search) =>
NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> String
showList :: [NodePolySearch
   id typename user_id parent_id name date hyperdata search]
-> ShowS
$cshowList :: forall id typename user_id parent_id name date hyperdata search.
(Show id, Show typename, Show user_id, Show parent_id, Show name,
 Show date, Show hyperdata, Show search) =>
[NodePolySearch
   id typename user_id parent_id name date hyperdata search]
-> ShowS
show :: NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> String
$cshow :: forall id typename user_id parent_id name date hyperdata search.
(Show id, Show typename, Show user_id, Show parent_id, Show name,
 Show date, Show hyperdata, Show search) =>
NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> String
showsPrec :: Int
-> NodePolySearch
     id typename user_id parent_id name date hyperdata search
-> ShowS
$cshowsPrec :: forall id typename user_id parent_id name date hyperdata search.
(Show id, Show typename, Show user_id, Show parent_id, Show name,
 Show date, Show hyperdata, Show search) =>
Int
-> NodePolySearch
     id typename user_id parent_id name date hyperdata search
-> ShowS
Show, (forall x.
 NodePolySearch
   id typename user_id parent_id name date hyperdata search
 -> Rep
      (NodePolySearch
         id typename user_id parent_id name date hyperdata search)
      x)
-> (forall x.
    Rep
      (NodePolySearch
         id typename user_id parent_id name date hyperdata search)
      x
    -> NodePolySearch
         id typename user_id parent_id name date hyperdata search)
-> Generic
     (NodePolySearch
        id typename user_id parent_id name date hyperdata search)
forall x.
Rep
  (NodePolySearch
     id typename user_id parent_id name date hyperdata search)
  x
-> NodePolySearch
     id typename user_id parent_id name date hyperdata search
forall x.
NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> Rep
     (NodePolySearch
        id typename user_id parent_id name date hyperdata search)
     x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall id typename user_id parent_id name date hyperdata search x.
Rep
  (NodePolySearch
     id typename user_id parent_id name date hyperdata search)
  x
-> NodePolySearch
     id typename user_id parent_id name date hyperdata search
forall id typename user_id parent_id name date hyperdata search x.
NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> Rep
     (NodePolySearch
        id typename user_id parent_id name date hyperdata search)
     x
$cto :: forall id typename user_id parent_id name date hyperdata search x.
Rep
  (NodePolySearch
     id typename user_id parent_id name date hyperdata search)
  x
-> NodePolySearch
     id typename user_id parent_id name date hyperdata search
$cfrom :: forall id typename user_id parent_id name date hyperdata search x.
NodePolySearch
  id typename user_id parent_id name date hyperdata search
-> Rep
     (NodePolySearch
        id typename user_id parent_id name date hyperdata search)
     x
Generic)

$(makeAdaptorAndInstance "pNodeSearch" ''NodePolySearch)
$(makeLensesWith abbreviatedFields ''NodePolySearch)
$(deriveJSON (unPrefix "_ns_")     ''NodePolySearch)
$(makeLenses ''NodePolySearch)

nodeTableSearch :: Table NodeSearchWrite NodeSearchRead
nodeTableSearch :: Table NodeSearchWrite NodeSearchRead
nodeTableSearch = String
-> TableFields NodeSearchWrite NodeSearchRead
-> Table NodeSearchWrite NodeSearchRead
forall writeFields viewFields.
String
-> TableFields writeFields viewFields
-> Table writeFields viewFields
Table String
"nodes" ( NodePolySearch
  (TableFields (Maybe (Column PGInt4)) (Column PGInt4))
  (TableFields (Column PGInt4) (Column PGInt4))
  (TableFields (Column PGInt4) (Column PGInt4))
  (TableFields (Column (Nullable PGInt4)) (Column (Nullable PGInt4)))
  (TableFields (Column PGText) (Column PGText))
  (TableFields (Maybe (Column PGTimestamptz)) (Column PGTimestamptz))
  (TableFields (Column PGJsonb) (Column PGJsonb))
  (TableFields (Maybe (Column PGTSVector)) (Column PGTSVector))
-> TableFields NodeSearchWrite NodeSearchRead
forall (p :: * -> * -> *) a1_0 a2_0 a3_0 a4_0 a5_0 a6_0 a7_0 a8_0
       a1_1 a2_1 a3_1 a4_1 a5_1 a6_1 a7_1 a8_1.
ProductProfunctor p =>
NodePolySearch
  (p a1_0 a1_1)
  (p a2_0 a2_1)
  (p a3_0 a3_1)
  (p a4_0 a4_1)
  (p a5_0 a5_1)
  (p a6_0 a6_1)
  (p a7_0 a7_1)
  (p a8_0 a8_1)
-> p (NodePolySearch a1_0 a2_0 a3_0 a4_0 a5_0 a6_0 a7_0 a8_0)
     (NodePolySearch a1_1 a2_1 a3_1 a4_1 a5_1 a6_1 a7_1 a8_1)
pNodeSearch
                                   NodeSearch :: forall id typename user_id parent_id name date hyperdata search.
id
-> typename
-> user_id
-> parent_id
-> name
-> date
-> hyperdata
-> search
-> NodePolySearch
     id typename user_id parent_id name date hyperdata search
NodeSearch { _ns_id :: TableFields (Maybe (Column PGInt4)) (Column PGInt4)
_ns_id           = String -> TableFields (Maybe (Column PGInt4)) (Column PGInt4)
forall a. String -> TableFields (Maybe (Column a)) (Column a)
optionalTableField String
"id"
                                              , _ns_typename :: TableFields (Column PGInt4) (Column PGInt4)
_ns_typename     = String -> TableFields (Column PGInt4) (Column PGInt4)
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"typename"
                                              , _ns_user_id :: TableFields (Column PGInt4) (Column PGInt4)
_ns_user_id      = String -> TableFields (Column PGInt4) (Column PGInt4)
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"user_id"

                                              , _ns_parent_id :: TableFields (Column (Nullable PGInt4)) (Column (Nullable PGInt4))
_ns_parent_id    = String
-> TableFields
     (Column (Nullable PGInt4)) (Column (Nullable PGInt4))
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"parent_id"
                                              , _ns_name :: TableFields (Column PGText) (Column PGText)
_ns_name         = String -> TableFields (Column PGText) (Column PGText)
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"name"
                                              , _ns_date :: TableFields (Maybe (Column PGTimestamptz)) (Column PGTimestamptz)
_ns_date         = String
-> TableFields
     (Maybe (Column PGTimestamptz)) (Column PGTimestamptz)
forall a. String -> TableFields (Maybe (Column a)) (Column a)
optionalTableField String
"date"

                                              , _ns_hyperdata :: TableFields (Column PGJsonb) (Column PGJsonb)
_ns_hyperdata    = String -> TableFields (Column PGJsonb) (Column PGJsonb)
forall a. String -> TableFields (Column a) (Column a)
requiredTableField String
"hyperdata"
                                              , _ns_search :: TableFields (Maybe (Column PGTSVector)) (Column PGTSVector)
_ns_search       = String
-> TableFields (Maybe (Column PGTSVector)) (Column PGTSVector)
forall a. String -> TableFields (Maybe (Column a)) (Column a)
optionalTableField String
"search"
                                              }
                                )
------------------------------------------------------------------------