module Database.PostgreSQL.Simple.SqlQQ (sql) where
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Data.Char
sql :: QuasiQuoter
sql = QuasiQuoter
{ quotePat = error "Database.PostgreSQL.Simple.SqlQQ.sql:\
\ quasiquoter used in pattern context"
, quoteType = error "Database.PostgreSQL.Simple.SqlQQ.sql:\
\ quasiquoter used in type context"
, quoteExp = sqlExp
, quoteDec = error "Database.PostgreSQL.Simple.SqlQQ.sql:\
\ quasiquoter used in declaration context"
}
sqlExp :: String -> Q Exp
sqlExp = stringE . outstring . dropSpace
where
dropSpace = dropWhile isSpace
outstring ('\'':xs) = '\'' : instring xs
outstring (x:xs) | isSpace x = case dropSpace xs of
[] -> []
ys -> ' ' : outstring ys
| otherwise = x : outstring xs
outstring [] = []
instring ('\'':'\'':xs) = '\'':'\'': instring xs
instring ('\'':xs) = '\'': outstring xs
instring (x:xs) = x : instring xs
instring [] = error "Database.PostgreSQL.Simple.SqlQQ.sql:\
\ string literal not terminated"