Skip to content

Support interpolating escaped string literals #37

Description

@brandonchinn178

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions