-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
version 0.0.0.6: 'PutRelation' and 'setNode' (#14)
- Loading branch information
Showing
7 changed files
with
103 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE QuasiQuotes #-} | ||
|
||
module Database.Bolt.Extras.Query.Set | ||
( | ||
setNode | ||
) where | ||
|
||
import Control.Monad.IO.Class (MonadIO) | ||
import Data.Text (Text, append, intercalate) | ||
import Database.Bolt (BoltActionT, | ||
RecordValue (..), at, | ||
exact, query) | ||
import Database.Bolt.Extras.Persisted (BoltId, fromInt) | ||
import Database.Bolt.Extras.Query.Cypher (ToCypher (..)) | ||
import Database.Bolt.Extras.Query.Get (NodeGetter, condIdAsText, | ||
nodeAsText) | ||
import Database.Bolt.Extras.Template.Types (Property) | ||
import NeatInterpolation (text) | ||
|
||
-- | 'setNode' updates properties for the node, | ||
-- corresponding to the given 'NodeGetter'. | ||
-- | ||
setNode :: (MonadIO m) => NodeGetter -> [Property] -> BoltActionT m BoltId | ||
setNode nodeGetter props = do | ||
let nodeGetterT = nodeAsText (varQ, nodeGetter) | ||
let condId = condIdAsText (varQ, nodeGetter) | ||
|
||
let newProperties = intercalate ", " $ fmap formPropertySet props | ||
|
||
let getQuery = [text|MATCH $nodeGetterT | ||
WHERE $condId | ||
SET $newProperties | ||
RETURN ID($varQ) as $varQ|] | ||
|
||
record <- head <$> query getQuery | ||
nodeIdentity' <- record `at` varQ >>= exact | ||
pure $ fromInt nodeIdentity' | ||
|
||
where | ||
varQ = "n" | ||
|
||
formPropertyName :: Text -> Text | ||
formPropertyName n = varQ `append` "." `append` n | ||
|
||
formPropertySet :: Property -> Text | ||
formPropertySet (name, prop) = formPropertyName name `append` "=" `append` toCypher prop |