I want to create a function with Haskell values baked into it, e.g.
let admin = "my-admin" -- e.g. from config file
in
[sql|
CREATE FUNCTION check_admin() RETURNS void
AS $$
IF current_user != ^{escapeText admin} THEN
RAISE EXCEPTION "only admins allowed"
END IF;
$$ LANGUAGE plpgsql;
|]
Current workaround:
escapeText :: Text -> Query
escapeText s = fromString $ tag <> Text.unpack s <> tag
where
-- unlikely to show up in string
tag = "$" <> show (hash s) <> "$"
Ideally there would be a polymorphic escapeLiteral that would be similar to Lift for template haskell, injecting a Haskell value of some type directly into the query. Not as safe as #{...}, but works in places where query parameters don't work.
class EscapeLiteral a where
escapeLiteral :: a -> Query
instance EscapeLiteral Text where ...
instance EscapeLiteral Int where ...
instance EscapeLiteral a => EscapeLiteral [a] where ...
It would also be nice to have an explicit unsafeSqlString function instead of calling fromString
I want to create a function with Haskell values baked into it, e.g.
Current workaround:
Ideally there would be a polymorphic
escapeLiteralthat would be similar toLiftfor template haskell, injecting a Haskell value of some type directly into the query. Not as safe as#{...}, but works in places where query parameters don't work.It would also be nice to have an explicit
unsafeSqlStringfunction instead of callingfromString