Skip to content

Commit c12b491

Browse files
authored
Merge pull request #56 from biocad/maksbotan/better-conditions
version 0.0.3.0: better Conds
2 parents d3f78a8 + 77f9cce commit c12b491

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [0.0.3.0] - 2023-08-17
10+
### Added
11+
- `IsString` instance for `Cond` and `Conds`.
12+
### Fixed
13+
- `:&&:` and `:||:` render to Cypher with correct operator precedence.
14+
915
## [0.0.2.2] - 2023-05-05
1016
### Changed
1117
- Add `HasCallStack` to a lot of unpacking functions.

hasbolt-extras.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: hasbolt-extras
2-
version: 0.0.2.2
2+
version: 0.0.3.0
33
synopsis: Extras for hasbolt library
44
description: Extras for hasbolt library
55
homepage: https://github.com/biocad/hasbolt-extras#readme

src/Database/Bolt/Extras/DSL/Internal/Instances.hs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Database.Bolt.Extras.DSL.Internal.Instances () where
1111

1212
import Control.Monad.Writer (execWriter, tell)
1313
import Data.Function ((&))
14+
import Data.String (IsString(..))
1415
import Data.Proxy (Proxy (..))
1516
import Data.Text (intercalate, pack)
1617
import Database.Bolt.Extras (ToCypher (..),
@@ -127,14 +128,22 @@ instance ToCypher Selector where
127128
instance ToCypher Selectors where
128129
toCypher = intercalate ", " . fmap toCypher
129130

131+
instance IsString Cond where
132+
fromString str = TC $ pack str
133+
130134
instance ToCypher Cond where
131135
toCypher (ID t bId) = pack $ printf "ID(%s)=%d" t (fromInt bId)
132136
toCypher (IDs t bIds) = pack $ printf "ID(%s) in [%s]" t (intercalate ", " $ fmap (pack . show) bIds)
133137
toCypher (IN t txts) = pack $ printf "%s in [%s]" t (intercalate ", " $ fmap (\s -> [text|"$s"|]) txts)
134138
toCypher (TC txt) = txt
135139

140+
instance IsString Conds where
141+
fromString str = C $ TC $ pack str
142+
136143
instance ToCypher Conds where
137-
toCypher (fcp :&&: scp) = toCypher fcp <> " AND " <> toCypher scp
138-
toCypher (fcp :||: scp) = toCypher fcp <> " OR " <> toCypher scp
139-
toCypher (Not cp) = "NOT " <> toCypher cp
144+
-- Adding "(" ")" to have correct precedence in Cypher
145+
-- "(a :&&: b) :||: c" should mean "(a AND b) OR c", not "a AND b OR c"
146+
toCypher (fcp :&&: scp) = "(" <> toCypher fcp <> ") AND (" <> toCypher scp <> ")"
147+
toCypher (fcp :||: scp) = "(" <> toCypher fcp <> ") OR (" <> toCypher scp <> ")"
148+
toCypher (Not cp) = "NOT (" <> toCypher cp <> ")"
140149
toCypher (C cp) = toCypher cp

src/Database/Bolt/Extras/DSL/Internal/Types.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ data Cond = ID Text BoltId -- ^ ID(txt) = boltId
133133

134134
infixr 3 :&&:
135135
infixr 2 :||:
136+
-- | Conditional expressions in Cypher.
137+
--
138+
-- Can be used with @OverloadedStrings@ to include atomic text conditions.
139+
--
140+
-- > toCypher $ ("x = 5" :&&: "y = 10") :||: Not "z = 20"
141+
-- > "((x = 5) AND (y = 10)) OR (NOT (z = 20))"
136142
data Conds = Conds :&&: Conds -- ^ 'condition' AND 'condition'
137143
| Conds :||: Conds -- ^ 'condition' OR 'condition'
138144
| C Cond -- ^ single 'condition'

0 commit comments

Comments
 (0)