@@ -29,14 +29,16 @@ import Data.Char
2929--
3030-- This quasiquoter attempts to mimimize whitespace; otherwise the
3131-- above query would consist of approximately half whitespace when sent
32−-- to the database backend.
32+-- to the database backend. It also recognizes and strips out standard
33+-- sql comments "--".
3334--
3435-- The implementation of the whitespace reducer is currently incomplete.
3536-- Thus it can mess up your syntax in cases where whitespace should be
3637-- preserved as-is. It does preserve whitespace inside standard SQL string
3738-- literals. But it can get confused by the non-standard PostgreSQL string
3839-- literal syntax (which is the default setting in PostgreSQL 8 and below),
39−-- the extended escape string syntax, and other similar constructs.
40+-- the extended escape string syntax, quoted identifiers, and other similar
41+-- constructs.
4042--
4143-- Of course, this caveat only applies to text written inside the SQL
4244-- quasiquoter; whitespace reduction is a compile-time computation and
@@ -58,19 +60,28 @@ sql = QuasiQuoter
5860 }
5961
6062sqlExp :: String -> Q Exp
61−sqlExp = stringE . outstring . dropSpace
63+sqlExp = stringE . minimizeSpace
64+
65+minimizeSpace :: String -> String
66+minimizeSpace = drop 1 . reduceSpace
6267 where
63− dropSpace = dropWhile isSpace
68+ needsReduced [] = False
69+ needsReduced ('-':'-':_) = True
70+ needsReduced (x:_) = isSpace x
71+
72+ reduceSpace xs =
73+ case dropWhile isSpace xs of
74+ [] -> []
75+ ('-':'-':ys) -> reduceSpace (dropWhile (/= '\n') ys)
76+ ys -> ' ' : insql ys
6477
65− outstring ('\'':xs) = '\'' : instring xs
66− outstring (x:xs) | isSpace x = case dropSpace xs of
67− [] -> []
68− ys -> ' ' : outstring ys
69− | otherwise = x : outstring xs
70− outstring [] = []
78+ insql ('\'':xs) = '\'' : instring xs
79+ insql xs | needsReduced xs = reduceSpace xs
80+ insql (x:xs) = x : insql xs
81+ insql [] = []
7182
7283 instring ('\'':'\'':xs) = '\'':'\'': instring xs
73− instring ('\'':xs) = '\'': outstring xs
84+ instring ('\'':xs) = '\'': insql xs
7485 instring (x:xs) = x : instring xs
7586 instring [] = error "Database.PostgreSQL.Simple.SqlQQ.sql:\
7687 \ string literal not terminated"