Created
July 22, 2014 05:44
-
-
Save erikd/cbe422eeaeeff29c6e33 to your computer and use it in GitHub Desktop.
Pretty print Persistent debug SQL statements
This file contains hidden or 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
#!/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