2015-12-09 17:11:57 +05:30
|
|
|
module Main where
|
|
|
|
|
2015-12-21 15:30:23 +05:30
|
|
|
import qualified Data.ByteString.Lazy as BS
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
import qualified Data.Text as Text
|
2015-12-17 20:02:13 +05:30
|
|
|
|
2015-12-21 15:30:23 +05:30
|
|
|
import Data.Aeson (encode)
|
2015-12-17 23:17:00 +05:30
|
|
|
import Data.Char (toLower)
|
|
|
|
import Data.List (nub)
|
|
|
|
import Data.Monoid ((<>))
|
|
|
|
import Control.Monad (forM_)
|
|
|
|
import System.Directory (createDirectoryIfMissing)
|
|
|
|
import System.FilePath ((</>), (<.>))
|
|
|
|
import System.Exit (exitFailure, exitSuccess)
|
2015-12-17 20:02:13 +05:30
|
|
|
|
2015-12-09 17:11:57 +05:30
|
|
|
import Ringo
|
2015-12-17 20:02:13 +05:30
|
|
|
import Ringo.ArgParser
|
|
|
|
import Ringo.InputParser
|
2015-12-09 17:11:57 +05:30
|
|
|
|
2015-12-19 11:55:08 +05:30
|
|
|
data SQLType = Create | FullRefresh | IncRefresh deriving (Eq, Show)
|
2015-12-17 23:17:00 +05:30
|
|
|
|
2015-12-09 17:11:57 +05:30
|
|
|
main :: IO ()
|
2015-12-17 20:02:13 +05:30
|
|
|
main = do
|
|
|
|
ProgArgs {..} <- parseArgs
|
|
|
|
result <- parseInput progInputFile
|
|
|
|
case result of
|
2015-12-28 19:28:35 +05:30
|
|
|
Left err -> putStrLn err >> exitFailure
|
|
|
|
Right (tables, facts, defaults) -> do
|
|
|
|
let env = Env tables facts progSettings defaults
|
2015-12-17 20:02:13 +05:30
|
|
|
let errors = nub $ concatMap (validateTable env) tables ++ concatMap (validateFact env) facts
|
|
|
|
if not $ null errors
|
2015-12-21 15:30:23 +05:30
|
|
|
then mapM_ print errors >> exitFailure
|
|
|
|
else writeFiles progOutputDir env >> exitSuccess
|
|
|
|
|
|
|
|
writeFiles :: FilePath -> Env -> IO ()
|
|
|
|
writeFiles outputDir env@Env{..} = do
|
|
|
|
let Settings{..} = envSettings
|
|
|
|
forM_ sqls $ \(sqlType, table, sql) -> do
|
|
|
|
let dirName = outputDir </> map toLower (show sqlType)
|
|
|
|
createDirectoryIfMissing True dirName
|
|
|
|
writeFile (dirName </> Text.unpack table <.> "sql") sql
|
|
|
|
|
|
|
|
BS.writeFile (outputDir </> Text.unpack settingDependenciesJSONFileName)
|
|
|
|
. encode
|
|
|
|
. foldl (\acc -> Map.union acc . extractDependencies env) Map.empty
|
|
|
|
$ envFacts
|
|
|
|
|
2015-12-29 15:19:55 +05:30
|
|
|
BS.writeFile (outputDir </> Text.unpack settingDimensionJSONFileName) . encode $
|
2015-12-21 15:30:23 +05:30
|
|
|
[ tableName table | (_, tabs) <- dimTables, table <- tabs , table `notElem` envTables ]
|
|
|
|
|
2015-12-29 15:19:55 +05:30
|
|
|
BS.writeFile (outputDir </> Text.unpack settingFactsJSONFileName) . encode $
|
2015-12-21 15:30:23 +05:30
|
|
|
[ tableName table | (_, table) <- factTables ]
|
|
|
|
|
2015-12-17 23:17:00 +05:30
|
|
|
where
|
2015-12-19 11:55:08 +05:30
|
|
|
dimTables = [ (fact, extractDimensionTables env fact) | fact <- envFacts ]
|
|
|
|
factTables = [ (fact, extractFactTable env fact) | fact <- envFacts ]
|
2015-12-17 20:02:13 +05:30
|
|
|
|
2016-01-06 19:00:27 +05:30
|
|
|
dimTableDefnSQLs = [ (Create, tableName table, unlines . map sqlStr $ dimensionTableDefnSQL env table)
|
2015-12-19 11:55:08 +05:30
|
|
|
| (_, tabs) <- dimTables
|
|
|
|
, table <- tabs
|
2015-12-18 01:00:32 +05:30
|
|
|
, table `notElem` envTables ]
|
2015-12-19 11:55:08 +05:30
|
|
|
|
|
|
|
factTableDefnSQLs = [ (Create , tableName table, unlines . map sqlStr $ factTableDefnSQL env fact table)
|
2015-12-18 01:00:32 +05:30
|
|
|
| (fact, table) <- factTables ]
|
2015-12-17 20:02:13 +05:30
|
|
|
|
2015-12-20 18:25:14 +05:30
|
|
|
dimTablePopulateSQLs typ gen =
|
|
|
|
[ (typ , tableName table, sqlStr $ gen env fact (tableName table))
|
|
|
|
| (fact, tabs) <- dimTables
|
|
|
|
, table <- tabs
|
|
|
|
, table `notElem` envTables ]
|
2015-12-17 23:17:00 +05:30
|
|
|
|
2015-12-22 19:46:37 +05:30
|
|
|
factTablePopulateSQLs typ gen = [ (typ, tableName table, unlines . map sqlStr $ gen env fact)
|
2015-12-20 18:25:14 +05:30
|
|
|
| (fact, table) <- factTables ]
|
2015-12-17 23:17:00 +05:30
|
|
|
|
|
|
|
sqls = concat [ dimTableDefnSQLs
|
|
|
|
, factTableDefnSQLs
|
2015-12-20 18:25:14 +05:30
|
|
|
, dimTablePopulateSQLs FullRefresh $ dimensionTablePopulateSQL FullPopulation
|
|
|
|
, dimTablePopulateSQLs IncRefresh $ dimensionTablePopulateSQL IncrementalPopulation
|
|
|
|
, factTablePopulateSQLs FullRefresh $ factTablePopulateSQL FullPopulation
|
|
|
|
, factTablePopulateSQLs IncRefresh $ factTablePopulateSQL IncrementalPopulation
|
2015-12-17 23:17:00 +05:30
|
|
|
]
|
2015-12-17 20:02:13 +05:30
|
|
|
|
2015-12-18 01:00:32 +05:30
|
|
|
sqlStr s = Text.unpack $ s <> ";\n"
|