Skip to content

Commit

Permalink
Add a SQL quasi-quoter.
Browse files Browse the repository at this point in the history
  • Loading branch information
fredefox committed May 15, 2019
1 parent 2e1619f commit ee3bba3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
45 changes: 45 additions & 0 deletions Database/MySQL/Simple/QQ.hs
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions mysql-simple.cabal
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ee3bba3

Please sign in to comment.