-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters