Skip to content

Commit

Permalink
version 0.0.0.12: remove query, escaping special characters (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheshirrrrrr authored and ozzzzz committed Oct 15, 2018
1 parent 2732e2c commit 1ffc1c2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.0.0.12] - 2018-10-15
### Added
- `REMOVE` query.
### Fixed
- Escaping special characters in text fields.

## [0.0.0.11] - 2018-06-19
### Changed
- Cabal fix.
Expand Down
2 changes: 1 addition & 1 deletion hasbolt-extras.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hasbolt-extras
version: 0.0.0.11
version: 0.0.0.12
synopsis: Extras for hasbolt library
description: Extras for hasbolt library
homepage: https://github.com/biocad/hasbolt-extras#readme
Expand Down
1 change: 1 addition & 0 deletions src/Database/Bolt/Extras/DSL/Internal/Executer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ execute (Where c n) = executeHelperC "WHERE " c n
execute (Set t n) = executeHelperT "SET " t n
execute (Delete t n) = executeHelperT "DELETE " t n
execute (DetachDelete t n) = executeHelperT "DETACH DELETE " t n
execute (Remove t n) = executeHelperT "REMOVE " t n
execute (Return t n) = executeHelperT "RETURN " t n
execute (Text t n) = tell [t] >> pure n

Expand Down
6 changes: 6 additions & 0 deletions src/Database/Bolt/Extras/DSL/Internal/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Database.Bolt.Extras.DSL.Internal.Language
, setF
, deleteF
, detachDeleteF
, removeF
, returnF
, textF
) where
Expand Down Expand Up @@ -57,6 +58,11 @@ deleteF txts = liftF (Delete txts ())
detachDeleteF :: [Text] -> Free Expr ()
detachDeleteF txts = liftF (DetachDelete txts ())

-- | Prepare 'REMOVE' query
--
removeF :: [Text] -> Free Expr ()
removeF txts = liftF (Remove txts ())

-- | Prepare 'RETURN' query
--
returnF :: [Text] -> Free Expr ()
Expand Down
1 change: 1 addition & 0 deletions src/Database/Bolt/Extras/DSL/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ data Expr next = Create Selectors next -- ^ CREATE query
| Set [Text] next -- ^ SET query
| Delete [Text] next -- ^ DELETE query
| DetachDelete [Text] next -- ^ DETACH DELETE query
| Remove [Text] next -- ^ REMOVE query
| Return [Text] next -- ^ RETURN query
| Text Text next -- ^ free text query
deriving (Show, Eq, Functor)
Expand Down
9 changes: 7 additions & 2 deletions src/Database/Bolt/Extras/Query/Cypher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ module Database.Bolt.Extras.Query.Cypher
-- This file contains some converation rules from 'Database.Bolt' types to `Cypher`.
-------------------------------------------------------------------------------------------------

import Data.Monoid ((<>))
import Data.Text as T (Text, concat, cons,
intercalate, pack, toUpper)
intercalate, pack, replace,
toUpper)
import Database.Bolt (Value (..))
import Database.Bolt.Extras.Template (Label, Property)
import Database.Bolt.Extras.Utils (currentLoc)
Expand All @@ -34,11 +36,14 @@ instance ToCypher Value where
toCypher (B bool) = toUpper . pack . show $ bool
toCypher (I int) = pack . show $ int
toCypher (F double) = pack . show $ double
toCypher (T t) = [text|"$t"|]
toCypher (T t) = "\"" <> escapeSpecSymbols t <> "\""
toCypher (L values) = let cvalues = T.intercalate "," $ map toCypher values
in [text|[$cvalues]|]
toCypher _ = error $ $currentLoc ++ "unacceptable Value type"

escapeSpecSymbols :: Text -> Text
escapeSpecSymbols = replace "\"" "\\\"" . replace "\\" "\\\\"

-- | Label with @name@ are formatted into @:name@
--
instance ToCypher Label where
Expand Down
14 changes: 8 additions & 6 deletions src/Database/Bolt/Extras/Query/Set.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module Database.Bolt.Extras.Query.Set
) where

import Control.Monad.IO.Class (MonadIO)
import Data.Text (Text, append, intercalate)
import Data.Monoid ((<>))
import Data.Text (Text, intercalate)
import Database.Bolt (BoltActionT,
RecordValue (..), at,
exact, query, Value (..))
RecordValue (..),
Value (..), 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,
Expand All @@ -26,7 +28,7 @@ setNode nodeGetter props = do
let nodeGetterT = nodeAsText (varQ, nodeGetter)
let condId = condIdAsText (varQ, nodeGetter)

let newProperties = intercalate "," $ fmap formPropertySet $ filter ((/= N ()) . snd) props
let newProperties = intercalate "," $ formPropertySet <$> filter ((/= N ()) . snd) props

let getQuery = [text|MATCH $nodeGetterT
WHERE $condId
Expand All @@ -41,7 +43,7 @@ setNode nodeGetter props = do
varQ = "n"

formPropertyName :: Text -> Text
formPropertyName n = varQ `append` "." `append` n
formPropertyName n = varQ <> "." <> n

formPropertySet :: Property -> Text
formPropertySet (name, prop) = formPropertyName name `append` "=" `append` toCypher prop
formPropertySet (name, prop) = formPropertyName name <> "=" <> toCypher prop

0 comments on commit 1ffc1c2

Please sign in to comment.