Skip to content

Instantly share code, notes, and snippets.

@erikd
Created July 22, 2014 05:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikd/cbe422eeaeeff29c6e33 to your computer and use it in GitHub Desktop.
Save erikd/cbe422eeaeeff29c6e33 to your computer and use it in GitHub Desktop.
Pretty print Persistent debug SQL statements
#!/usr/bin/runghc -Wall
-- Trivial little script to pretty-print safe SQL from Yesod's logging mechanism.
-- It can be run simply using:
-- prety-print-sql.hs <SQL statement string>
import System.Environment (getArgs, getProgName)
import System.Exit (exitSuccess)
main :: IO ()
main = do
text <- getArgs
case text of
[] -> usageExit
_ -> do
putStrLn ""
putStrLn . sanitize $ unwords text
putStrLn ""
sanitize :: String -> String
sanitize ('\\' : 'n' : xs) = '\n' : sanitize xs
sanitize ('S':'E':'L':'E':'C':'T' : xs) = "\nSELECT" ++ sanitize xs
sanitize (' ' : 'I':'N':'N':'E':'R': xs) = "\n INNER" ++ sanitize xs
sanitize (' ' : 'A':'N':'D' : xs) = "\n AND" ++ sanitize xs
sanitize (' ' : '[' : xs) = "\n [" ++ sanitize xs
sanitize (x : xs) = x : sanitize xs
sanitize [] = []
usageExit :: IO a
usageExit = do
pname <- getProgName
mapM_ putStr
[ pname, " : Pretty print an escaped Yesod SQL statement to stdout.\n"
, "\n"
, "Usage :\n"
, " ", pname, " <SQL statement string>"
, "\n\n"
]
exitSuccess
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment