2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
{-
|
|
|
|
Simple command line tool to experiment with simple-sql-parser
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
|
|
|
|
parse: parse sql from file, stdin or from command line
|
|
|
|
lex: lex sql same
|
|
|
|
indent: parse then pretty print sql
|
2024-01-10 08:40:24 +01:00
|
|
|
|
|
|
|
TODO: this is supposed to be a simple example, but it's a total mess
|
|
|
|
write some simple helpers so it's all in text?
|
|
|
|
|
2024-01-09 01:07:47 +01:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TupleSections #-}
|
2024-01-10 08:40:24 +01:00
|
|
|
import System.Environment (getArgs)
|
|
|
|
import Control.Monad (forM_, when)
|
|
|
|
import Data.Maybe (isJust)
|
|
|
|
import System.Exit (exitFailure)
|
|
|
|
import Data.List (intercalate)
|
|
|
|
import Text.Show.Pretty (ppShow)
|
2024-01-09 01:07:47 +01:00
|
|
|
--import Control.Applicative
|
|
|
|
|
2024-01-10 08:40:24 +01:00
|
|
|
import qualified Data.Text as T
|
|
|
|
|
2024-01-09 01:07:47 +01:00
|
|
|
import Language.SQL.SimpleSQL.Pretty
|
2024-01-10 08:40:24 +01:00
|
|
|
(prettyStatements)
|
2024-01-09 01:07:47 +01:00
|
|
|
import Language.SQL.SimpleSQL.Parse
|
2024-01-10 08:40:24 +01:00
|
|
|
(parseStatements
|
|
|
|
,prettyError)
|
|
|
|
import qualified Language.SQL.SimpleSQL.Lex as L
|
|
|
|
import Language.SQL.SimpleSQL.Dialect (ansi2011)
|
2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
args <- getArgs
|
|
|
|
case args of
|
|
|
|
[] -> do
|
|
|
|
showHelp $ Just "no command given"
|
|
|
|
(c:as) -> do
|
|
|
|
let cmd = lookup c commands
|
|
|
|
maybe (showHelp (Just "command not recognised"))
|
|
|
|
(\(_,cmd') -> cmd' as)
|
|
|
|
cmd
|
|
|
|
|
|
|
|
commands :: [(String, (String,[String] -> IO ()))]
|
|
|
|
commands =
|
|
|
|
[("help", helpCommand)
|
|
|
|
,("parse", parseCommand)
|
|
|
|
,("lex", lexCommand)
|
2024-01-26 16:28:15 +01:00
|
|
|
,("format", formatCommand)]
|
2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
showHelp :: Maybe String -> IO ()
|
|
|
|
showHelp msg = do
|
|
|
|
maybe (return ()) (\e -> putStrLn $ "Error: " ++ e) msg
|
2024-01-26 16:28:15 +01:00
|
|
|
putStrLn "Usage:\n SimpleSQLParserTool command args"
|
2024-01-09 01:07:47 +01:00
|
|
|
forM_ commands $ \(c, (h,_)) -> do
|
|
|
|
putStrLn $ c ++ "\t" ++ h
|
|
|
|
when (isJust msg) $ exitFailure
|
|
|
|
|
|
|
|
helpCommand :: (String,[String] -> IO ())
|
|
|
|
helpCommand =
|
|
|
|
("show help for this progam", \_ -> showHelp Nothing)
|
|
|
|
|
|
|
|
getInput :: [String] -> IO (FilePath,String)
|
|
|
|
getInput as =
|
|
|
|
case as of
|
|
|
|
["-"] -> ("",) <$> getContents
|
|
|
|
("-c":as') -> return ("", unwords as')
|
|
|
|
[filename] -> (filename,) <$> readFile filename
|
|
|
|
_ -> showHelp (Just "arguments not recognised") >> error ""
|
|
|
|
|
|
|
|
parseCommand :: (String,[String] -> IO ())
|
|
|
|
parseCommand =
|
|
|
|
("parse SQL from file/stdin/command line (use -c to parse from command line)"
|
|
|
|
,\args -> do
|
|
|
|
(f,src) <- getInput args
|
2024-01-10 08:40:24 +01:00
|
|
|
either (error . T.unpack . prettyError)
|
2024-01-09 01:07:47 +01:00
|
|
|
(putStrLn . ppShow)
|
2024-01-10 08:40:24 +01:00
|
|
|
$ parseStatements ansi2011 (T.pack f) Nothing (T.pack src)
|
2024-01-09 01:07:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
lexCommand :: (String,[String] -> IO ())
|
|
|
|
lexCommand =
|
|
|
|
("lex SQL from file/stdin/command line (use -c to parse from command line)"
|
|
|
|
,\args -> do
|
|
|
|
(f,src) <- getInput args
|
2024-01-10 08:40:24 +01:00
|
|
|
either (error . T.unpack . L.prettyError)
|
2024-01-09 01:07:47 +01:00
|
|
|
(putStrLn . intercalate ",\n" . map show)
|
2024-01-10 08:40:24 +01:00
|
|
|
$ L.lexSQL ansi2011 (T.pack f) Nothing (T.pack src)
|
2024-01-09 01:07:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-26 16:28:15 +01:00
|
|
|
formatCommand :: (String,[String] -> IO ())
|
|
|
|
formatCommand =
|
2024-01-09 01:07:47 +01:00
|
|
|
("parse then pretty print SQL from file/stdin/command line (use -c to parse from command line)"
|
|
|
|
,\args -> do
|
|
|
|
(f,src) <- getInput args
|
2024-01-10 08:40:24 +01:00
|
|
|
either (error . T.unpack . prettyError)
|
|
|
|
(putStrLn . T.unpack . prettyStatements ansi2011)
|
|
|
|
$ parseStatements ansi2011 (T.pack f) Nothing (T.pack src)
|
2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
)
|