{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Database.Bolt.Extras.Query.Cypher
(
ToCypher (..)
) where
import Data.Text as T (Text, concat, cons,
intercalate, pack, toUpper)
import Database.Bolt (Value (..))
import Database.Bolt.Extras.Template (Label, Property)
import Database.Bolt.Extras.Utils (currentLoc)
import NeatInterpolation (text)
class ToCypher a where
toCypher :: a -> Text
instance ToCypher Value where
toCypher (N ()) = ""
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 (L values) = let cvalues = T.intercalate "," $ map toCypher values
in [text|[$cvalues]|]
toCypher _ = error $ $currentLoc ++ "unacceptable Value type"
instance ToCypher Label where
toCypher = cons ':'
instance ToCypher [Label] where
toCypher = T.concat . map toCypher
instance ToCypher Property where
toCypher (propTitle, value) = T.concat [propTitle, pack ":", toCypher value]
instance ToCypher [Property] where
toCypher = T.intercalate "," . map toCypher