{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Database.Bolt.Extras.DSL.Internal.Types
(
NodeSelector (..)
, RelSelector (..)
, PathPart (..)
, PathSelector (..)
, Selector (..)
, Selectors
, Cond (..)
, Conds (..)
, Expr (..)
, SelectorLike (..)
, (.:)
, (.#)
, (#)
, (-:)
, (<-:)
, defaultNode
, defN
, defaultRel
, defR
, toNodeSelector
, toRelSelector
) where
import Data.Foldable (foldl')
import Data.Map.Strict (toList)
import Data.Text (Text)
import Database.Bolt (Node (..), URelationship (..), Value (..))
import Database.Bolt.Extras (BoltId)
import GHC.Stack (HasCallStack)
class SelectorLike a where
withIdentifier :: Text -> a -> a
withLabel :: Text -> a -> a
withProp :: (Text, Value) -> a -> a
withParam :: (Text, Text) -> a -> a
data NodeSelector = NodeSelector { NodeSelector -> Maybe Text
nodeIdentifier :: Maybe Text
, NodeSelector -> [Text]
nodeLabels :: [Text]
, NodeSelector -> [(Text, Value)]
nodeProperties :: [(Text, Value)]
, NodeSelector -> [(Text, Text)]
nodeParams :: [(Text, Text)]
}
deriving (Int -> NodeSelector -> ShowS
[NodeSelector] -> ShowS
NodeSelector -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NodeSelector] -> ShowS
$cshowList :: [NodeSelector] -> ShowS
show :: NodeSelector -> String
$cshow :: NodeSelector -> String
showsPrec :: Int -> NodeSelector -> ShowS
$cshowsPrec :: Int -> NodeSelector -> ShowS
Show, NodeSelector -> NodeSelector -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NodeSelector -> NodeSelector -> Bool
$c/= :: NodeSelector -> NodeSelector -> Bool
== :: NodeSelector -> NodeSelector -> Bool
$c== :: NodeSelector -> NodeSelector -> Bool
Eq)
data RelSelector = RelSelector { RelSelector -> Maybe Text
relIdentifier :: Maybe Text
, RelSelector -> Text
relLabel :: Text
, RelSelector -> [(Text, Value)]
relProperties :: [(Text, Value)]
, RelSelector -> [(Text, Text)]
relParams :: [(Text, Text)]
}
deriving (Int -> RelSelector -> ShowS
[RelSelector] -> ShowS
RelSelector -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RelSelector] -> ShowS
$cshowList :: [RelSelector] -> ShowS
show :: RelSelector -> String
$cshow :: RelSelector -> String
showsPrec :: Int -> RelSelector -> ShowS
$cshowsPrec :: Int -> RelSelector -> ShowS
Show, RelSelector -> RelSelector -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RelSelector -> RelSelector -> Bool
$c/= :: RelSelector -> RelSelector -> Bool
== :: RelSelector -> RelSelector -> Bool
$c== :: RelSelector -> RelSelector -> Bool
Eq)
infixl 9 .:
(.:) :: SelectorLike a => a -> Text -> a
.: :: forall a. SelectorLike a => a -> Text -> a
(.:) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. SelectorLike a => Text -> a -> a
withLabel
infixl 9 .#
(.#) :: SelectorLike a => a -> [(Text, Value)] -> a
.# :: forall a. SelectorLike a => a -> [(Text, Value)] -> a
(.#) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. SelectorLike a => (Text, Value) -> a -> a
withProp)
(#) :: a -> (a -> b) -> b
# :: forall a b. a -> (a -> b) -> b
(#) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> a -> b
($)
infixl 2 :!->:
infixl 2 :!-:
data PathPart = RelSelector :!->: NodeSelector
| RelSelector :!-: NodeSelector
deriving (Int -> PathPart -> ShowS
[PathPart] -> ShowS
PathPart -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PathPart] -> ShowS
$cshowList :: [PathPart] -> ShowS
show :: PathPart -> String
$cshow :: PathPart -> String
showsPrec :: Int -> PathPart -> ShowS
$cshowsPrec :: Int -> PathPart -> ShowS
Show, PathPart -> PathPart -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PathPart -> PathPart -> Bool
$c/= :: PathPart -> PathPart -> Bool
== :: PathPart -> PathPart -> Bool
$c== :: PathPart -> PathPart -> Bool
Eq)
infixl 1 :-!:
infixl 1 :<-!:
data PathSelector = PathSelector :-!: PathPart
| PathSelector :<-!: PathPart
| P NodeSelector
deriving (Int -> PathSelector -> ShowS
[PathSelector] -> ShowS
PathSelector -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PathSelector] -> ShowS
$cshowList :: [PathSelector] -> ShowS
show :: PathSelector -> String
$cshow :: PathSelector -> String
showsPrec :: Int -> PathSelector -> ShowS
$cshowsPrec :: Int -> PathSelector -> ShowS
Show, PathSelector -> PathSelector -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PathSelector -> PathSelector -> Bool
$c/= :: PathSelector -> PathSelector -> Bool
== :: PathSelector -> PathSelector -> Bool
$c== :: PathSelector -> PathSelector -> Bool
Eq)
infixl 1 -:
(-:) :: NodeSelector -> PathPart -> PathSelector
NodeSelector
ns -: :: NodeSelector -> PathPart -> PathSelector
-: PathPart
pp = NodeSelector -> PathSelector
P NodeSelector
ns PathSelector -> PathPart -> PathSelector
:-!: PathPart
pp
infixl 1 <-:
(<-:) :: NodeSelector -> PathPart -> PathSelector
NodeSelector
ns <-: :: NodeSelector -> PathPart -> PathSelector
<-: PathPart
pp = NodeSelector -> PathSelector
P NodeSelector
ns PathSelector -> PathPart -> PathSelector
:<-!: PathPart
pp
data Selector = PS PathSelector
| TS Text
deriving (Int -> Selector -> ShowS
[Selector] -> ShowS
Selector -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Selector] -> ShowS
$cshowList :: [Selector] -> ShowS
show :: Selector -> String
$cshow :: Selector -> String
showsPrec :: Int -> Selector -> ShowS
$cshowsPrec :: Int -> Selector -> ShowS
Show, Selector -> Selector -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Selector -> Selector -> Bool
$c/= :: Selector -> Selector -> Bool
== :: Selector -> Selector -> Bool
$c== :: Selector -> Selector -> Bool
Eq)
type Selectors = [Selector]
data Cond = ID Text BoltId
| IDs Text [BoltId]
| IN Text [Text]
| TC Text
deriving (Int -> Cond -> ShowS
[Cond] -> ShowS
Cond -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Cond] -> ShowS
$cshowList :: [Cond] -> ShowS
show :: Cond -> String
$cshow :: Cond -> String
showsPrec :: Int -> Cond -> ShowS
$cshowsPrec :: Int -> Cond -> ShowS
Show, Cond -> Cond -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Cond -> Cond -> Bool
$c/= :: Cond -> Cond -> Bool
== :: Cond -> Cond -> Bool
$c== :: Cond -> Cond -> Bool
Eq)
infixr 3 :&&:
infixr 2 :||:
data Conds = Conds :&&: Conds
| Conds :||: Conds
| C Cond
| Not Conds
deriving (Int -> Conds -> ShowS
[Conds] -> ShowS
Conds -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Conds] -> ShowS
$cshowList :: [Conds] -> ShowS
show :: Conds -> String
$cshow :: Conds -> String
showsPrec :: Int -> Conds -> ShowS
$cshowsPrec :: Int -> Conds -> ShowS
Show, Conds -> Conds -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Conds -> Conds -> Bool
$c/= :: Conds -> Conds -> Bool
== :: Conds -> Conds -> Bool
$c== :: Conds -> Conds -> Bool
Eq)
data Expr next = Create Selectors next
| Match Selectors next
| OptionalMatch Selectors next
| Merge Selectors next
| Where Conds next
| Set [Text] next
| Delete [Text] next
| DetachDelete [Text] next
| Remove [Text] next
| Return [Text] next
| With [Text] next
| Text Text next
deriving (Int -> Expr next -> ShowS
forall next. Show next => Int -> Expr next -> ShowS
forall next. Show next => [Expr next] -> ShowS
forall next. Show next => Expr next -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Expr next] -> ShowS
$cshowList :: forall next. Show next => [Expr next] -> ShowS
show :: Expr next -> String
$cshow :: forall next. Show next => Expr next -> String
showsPrec :: Int -> Expr next -> ShowS
$cshowsPrec :: forall next. Show next => Int -> Expr next -> ShowS
Show, Expr next -> Expr next -> Bool
forall next. Eq next => Expr next -> Expr next -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Expr next -> Expr next -> Bool
$c/= :: forall next. Eq next => Expr next -> Expr next -> Bool
== :: Expr next -> Expr next -> Bool
$c== :: forall next. Eq next => Expr next -> Expr next -> Bool
Eq, forall a b. a -> Expr b -> Expr a
forall a b. (a -> b) -> Expr a -> Expr b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Expr b -> Expr a
$c<$ :: forall a b. a -> Expr b -> Expr a
fmap :: forall a b. (a -> b) -> Expr a -> Expr b
$cfmap :: forall a b. (a -> b) -> Expr a -> Expr b
Functor)
defaultNode :: NodeSelector
defaultNode :: NodeSelector
defaultNode = Maybe Text
-> [Text] -> [(Text, Value)] -> [(Text, Text)] -> NodeSelector
NodeSelector forall a. Maybe a
Nothing [] [] []
defN :: NodeSelector
defN :: NodeSelector
defN = NodeSelector
defaultNode
defaultRel :: RelSelector
defaultRel :: RelSelector
defaultRel = Maybe Text
-> Text -> [(Text, Value)] -> [(Text, Text)] -> RelSelector
RelSelector forall a. Maybe a
Nothing Text
"" [] []
defR :: RelSelector
defR :: RelSelector
defR = RelSelector
defaultRel
toNodeSelector :: HasCallStack => Node -> NodeSelector
toNodeSelector :: HasCallStack => Node -> NodeSelector
toNodeSelector Node{Int
[Text]
Map Text Value
nodeIdentity :: Node -> Int
labels :: Node -> [Text]
nodeProps :: Node -> Map Text Value
nodeProps :: Map Text Value
labels :: [Text]
nodeIdentity :: Int
..} = NodeSelector
defaultNode { nodeLabels :: [Text]
nodeLabels = [Text]
labels
, nodeProperties :: [(Text, Value)]
nodeProperties = forall a. (a -> Bool) -> [a] -> [a]
filter ((forall a. Eq a => a -> a -> Bool
/= () -> Value
N ()) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) (forall k a. Map k a -> [(k, a)]
toList Map Text Value
nodeProps)
}
toRelSelector :: HasCallStack => URelationship -> RelSelector
toRelSelector :: HasCallStack => URelationship -> RelSelector
toRelSelector URelationship{Int
Text
Map Text Value
urelIdentity :: URelationship -> Int
urelType :: URelationship -> Text
urelProps :: URelationship -> Map Text Value
urelProps :: Map Text Value
urelType :: Text
urelIdentity :: Int
..} = RelSelector
defaultRel { relLabel :: Text
relLabel = Text
urelType
, relProperties :: [(Text, Value)]
relProperties = forall k a. Map k a -> [(k, a)]
toList Map Text Value
urelProps
}