diff --git a/Database/MySQL/Simple/QQ.hs b/Database/MySQL/Simple/QQ.hs new file mode 100644 index 0000000..9312f49 --- /dev/null +++ b/Database/MySQL/Simple/QQ.hs @@ -0,0 +1,45 @@ +{-# LANGUAGE TemplateHaskell, TypeApplications #-} +module Database.MySQL.Simple.QQ + ( sql + ) where + +import Prelude +import Language.Haskell.TH (Exp, Q, appE, stringE) +import Language.Haskell.TH.Quote (QuasiQuoter (..)) +import Database.MySQL.Simple (Query, Query) +import Text.Printf (printf) + +-- | A quasi-quoter for SQL expressions. +-- +-- The quasi-quoter does not do any sort of parsing of the SQL. It's +-- simply a convenience for writing multi-line SQL statements. So in stead of: +-- +-- > query +-- > = "select * " +-- > <> "from users " +-- > <> "where email is null;" +-- +-- You could write +-- +-- > query = [sql| +-- > select * +-- > from users +-- > where email is null; +-- > |] +-- +-- Note the quasi-quoter is only valid in expression contexts. +-- +-- @since 0.4.7 +sql :: QuasiQuoter +sql = QuasiQuoter + { quotePat = err "pattern" + , quoteType = error "Database.MySQL.Simple.QQ.sql: quasiquoter used in type context" + , quoteDec = error "Database.MySQL.Simple.QQ.sql: quasiquoter used in declaration context" + , quoteExp = quote + } + where + err :: String -> a + err ctxt = error (printf "Database.MySQL.Simple.QQ.sql: quasiquoter used in %s context" ctxt) + +quote :: String -> Q Exp +quote = appE [| fromString @Query |] . stringE diff --git a/mysql-simple.cabal b/mysql-simple.cabal index 8c5222e..ba8b9da 100644 --- a/mysql-simple.cabal +++ b/mysql-simple.cabal @@ -1,5 +1,5 @@ name: mysql-simple -version: 0.4.6 +version: 0.4.7 homepage: https://github.com/paul-rouse/mysql-simple bug-reports: https://github.com/paul-rouse/mysql-simple/issues synopsis: A mid-level MySQL client library. @@ -36,6 +36,7 @@ library exposed-modules: Database.MySQL.Simple Database.MySQL.Simple.Param + Database.MySQL.Simple.QQ Database.MySQL.Simple.QueryParams Database.MySQL.Simple.QueryResults Database.MySQL.Simple.Result @@ -57,7 +58,8 @@ library pcre-light, old-locale, text >= 0.11.0.2, - time + time, + template-haskell if !impl(ghc >= 8.0) build-depends: semigroups >= 0.11 && < 0.19