2019-07-07 13:54:22 +02:00
|
|
|
|
|
|
|
-- Simple example to show parsing some SQL then pretty printing the AST
|
|
|
|
|
2024-01-10 13:29:21 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2019-07-07 13:54:22 +02:00
|
|
|
import System.Environment
|
|
|
|
import Text.Show.Pretty
|
|
|
|
import System.IO
|
|
|
|
|
2019-07-07 14:46:39 +02:00
|
|
|
import Language.SQL.SimpleSQL.Parse
|
|
|
|
(parseStatements
|
|
|
|
,ParseError
|
2024-01-10 13:29:21 +01:00
|
|
|
,prettyError
|
|
|
|
,ansi2011)
|
2019-07-07 13:54:22 +02:00
|
|
|
|
2024-01-10 13:29:21 +01:00
|
|
|
import Language.SQL.SimpleSQL.Syntax (Statement)
|
|
|
|
import qualified Data.Text as T
|
2019-07-07 13:54:22 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
args <- getArgs
|
|
|
|
case args of
|
|
|
|
[] -> do
|
|
|
|
-- read from stdin
|
|
|
|
c <- getContents
|
|
|
|
doIt c
|
|
|
|
["-s", sql] -> do
|
|
|
|
-- parse arg given
|
|
|
|
doIt sql
|
|
|
|
[f] ->
|
|
|
|
-- read file
|
|
|
|
withFile f ReadMode (\h -> do
|
|
|
|
x <- hGetContents h
|
|
|
|
doIt x)
|
|
|
|
_ -> do
|
|
|
|
putStrLn "use no arguments to stream sql from stdin, e.g.:\n\
|
|
|
|
\ cat some.sql | SimpleSQLParserExample\n\
|
|
|
|
\n\
|
|
|
|
\use -s to parse sql on command line, e.g.:\n\
|
|
|
|
\ SimpleSQLParserExample -s \"select * from t\"\n\
|
|
|
|
\use a single arg to parse a file, e.g.\n\
|
|
|
|
\ SimpleSQLParserExample some.sql"
|
|
|
|
|
|
|
|
doIt :: String -> IO ()
|
|
|
|
doIt src = do
|
2019-07-07 14:46:39 +02:00
|
|
|
let parsed :: Either ParseError [Statement]
|
2024-01-10 13:29:21 +01:00
|
|
|
parsed = parseStatements ansi2011 "" Nothing (T.pack src)
|
|
|
|
either (error . T.unpack . prettyError)
|
2019-07-07 13:54:22 +02:00
|
|
|
(putStrLn . ppShow)
|
|
|
|
parsed
|