{-# 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)
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
(Int -> NodeSelector -> ShowS)
-> (NodeSelector -> String)
-> ([NodeSelector] -> ShowS)
-> Show NodeSelector
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
(NodeSelector -> NodeSelector -> Bool)
-> (NodeSelector -> NodeSelector -> Bool) -> Eq NodeSelector
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
(Int -> RelSelector -> ShowS)
-> (RelSelector -> String)
-> ([RelSelector] -> ShowS)
-> Show RelSelector
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
(RelSelector -> RelSelector -> Bool)
-> (RelSelector -> RelSelector -> Bool) -> Eq RelSelector
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
.: :: a -> Text -> a
(.:) = (Text -> a -> a) -> a -> Text -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> a -> a
forall a. SelectorLike a => Text -> a -> a
withLabel
infixl 9 .#
(.#) :: SelectorLike a => a -> [(Text, Value)] -> a
.# :: a -> [(Text, Value)] -> a
(.#) = (a -> (Text, Value) -> a) -> a -> [(Text, Value)] -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (((Text, Value) -> a -> a) -> a -> (Text, Value) -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Text, Value) -> a -> a
forall a. SelectorLike a => (Text, Value) -> a -> a
withProp)
(#) :: a -> (a -> b) -> b
# :: a -> (a -> b) -> b
(#) = ((a -> b) -> a -> b) -> a -> (a -> b) -> b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> a -> b
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
(Int -> PathPart -> ShowS)
-> (PathPart -> String) -> ([PathPart] -> ShowS) -> Show PathPart
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
(PathPart -> PathPart -> Bool)
-> (PathPart -> PathPart -> Bool) -> Eq PathPart
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
(Int -> PathSelector -> ShowS)
-> (PathSelector -> String)
-> ([PathSelector] -> ShowS)
-> Show PathSelector
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
(PathSelector -> PathSelector -> Bool)
-> (PathSelector -> PathSelector -> Bool) -> Eq PathSelector
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
(Int -> Selector -> ShowS)
-> (Selector -> String) -> ([Selector] -> ShowS) -> Show Selector
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
(Selector -> Selector -> Bool)
-> (Selector -> Selector -> Bool) -> Eq Selector
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
(Int -> Cond -> ShowS)
-> (Cond -> String) -> ([Cond] -> ShowS) -> Show Cond
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
(Cond -> Cond -> Bool) -> (Cond -> Cond -> Bool) -> Eq Cond
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
(Int -> Conds -> ShowS)
-> (Conds -> String) -> ([Conds] -> ShowS) -> Show Conds
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
(Conds -> Conds -> Bool) -> (Conds -> Conds -> Bool) -> Eq Conds
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
[Expr next] -> ShowS
Expr next -> String
(Int -> Expr next -> ShowS)
-> (Expr next -> String)
-> ([Expr next] -> ShowS)
-> Show (Expr next)
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
(Expr next -> Expr next -> Bool)
-> (Expr next -> Expr next -> Bool) -> Eq (Expr next)
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, a -> Expr b -> Expr a
(a -> b) -> Expr a -> Expr b
(forall a b. (a -> b) -> Expr a -> Expr b)
-> (forall a b. a -> Expr b -> Expr a) -> Functor Expr
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
<$ :: a -> Expr b -> Expr a
$c<$ :: forall a b. a -> Expr b -> Expr a
fmap :: (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 Maybe Text
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 Maybe Text
forall a. Maybe a
Nothing Text
"" [] []
defR :: RelSelector
defR :: RelSelector
defR = RelSelector
defaultRel
toNodeSelector :: Node -> NodeSelector
toNodeSelector :: 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 = ((Text, Value) -> Bool) -> [(Text, Value)] -> [(Text, Value)]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
/= () -> Value
N ()) (Value -> Bool)
-> ((Text, Value) -> Value) -> (Text, Value) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Value) -> Value
forall a b. (a, b) -> b
snd) (Map Text Value -> [(Text, Value)]
forall k a. Map k a -> [(k, a)]
toList Map Text Value
nodeProps)
}
toRelSelector :: URelationship -> RelSelector
toRelSelector :: 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 = Map Text Value -> [(Text, Value)]
forall k a. Map k a -> [(k, a)]
toList Map Text Value
urelProps
}