@@ -11,6 +11,7 @@ module Database.Bolt.Extras.DSL.Internal.Instances () where
1111
1212import Control.Monad.Writer (execWriter , tell )
1313import Data.Function ((&) )
14+ import Data.String (IsString (.. ))
1415import Data.Proxy (Proxy (.. ))
1516import Data.Text (intercalate , pack )
1617import Database.Bolt.Extras (ToCypher (.. ),
@@ -127,14 +128,22 @@ instance ToCypher Selector where
127128instance ToCypher Selectors where
128129 toCypher = intercalate " , " . fmap toCypher
129130
131+ instance IsString Cond where
132+ fromString str = TC $ pack str
133+
130134instance 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+
136143instance 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
0 commit comments