From 644acc7ee9c90ec867f8ad9b2d5f14f8eb853409 Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Wed, 30 Dec 2015 12:34:35 +0530 Subject: [PATCH] Moves out foreign key id coalesce value as a setting. --- app/Ringo/ArgParser.hs | 5 +++++ src/Ringo/Generator/Populate/Fact.hs | 22 +++++++++++----------- src/Ringo/Types.hs | 2 ++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app/Ringo/ArgParser.hs b/app/Ringo/ArgParser.hs index 79e5dbe..c7fa4be 100644 --- a/app/Ringo/ArgParser.hs +++ b/app/Ringo/ArgParser.hs @@ -66,6 +66,11 @@ settingsParser = let Settings {..} = defSettings <*> minorOption "dimensions-json-file" settingDimensionJSONFileName "Name of the output dimensions json file" + <*> option auto (long "foreign-key-id-coalesce-val" + <> hidden + <> value settingForeignKeyIdCoalesceValue + <> showDefault + <> help "Value to coalease missing foriegn key ids to, in fact tables") where minorOption longDesc defValue helpTxt = Text.pack <$> strOption (long longDesc diff --git a/src/Ringo/Generator/Populate/Fact.hs b/src/Ringo/Generator/Populate/Fact.hs index 0447c83..324f0c1 100644 --- a/src/Ringo/Generator/Populate/Fact.hs +++ b/src/Ringo/Generator/Populate/Fact.hs @@ -46,12 +46,12 @@ $$ LANGUAGE 'plpgsql' IMMUTABLE|] data FactTablePopulateSelectSQL = FactTablePopulateSelectSQL - { ftpsSelectCols :: ![(Text, Text)] - , ftpsSelectTable :: !Text - , ftpsJoinClauses :: ![Text] - , ftpsWhereClauses :: ![Text] - , ftpsGroupByCols :: ![Text] - } deriving (Show, Eq) + { ftpsSelectCols :: ![(Text, Text)] + , ftpsSelectTable :: !Text + , ftpsJoinClauses :: ![Text] + , ftpsWhereClauses :: ![Text] + , ftpsGroupByCols :: ![Text] + } deriving (Show, Eq) factTableUpdateSQL :: Fact -> Text -> FactTablePopulateSelectSQL -> Reader Env [Text] factTableUpdateSQL fact groupByColPrefix populateSelectSQL@FactTablePopulateSelectSQL {..} = do @@ -116,6 +116,11 @@ factTablePopulateSQL popMode fact = do fTable = fromJust . findTable fTableName $ tables dimIdColName = settingDimTableIdColumnName + coalesceFKId col = + if "coalesce" `Text.isPrefixOf` col + then col + else "coalesce((" <> col <> "), " <> Text.pack (show settingForeignKeyIdCoalesceValue) <> ")" + timeUnitColumnInsertSQL cName = let colName = timeUnitColumnName dimIdColName cName settingTimeUnit in ( colName @@ -203,11 +208,6 @@ factTablePopulateSQL popMode fact = do <$> listToMaybe [ colPairs | ForeignKey tName colPairs <- tableConstraints table , tName == oTableName ] - coalesceFKId col = - if "coalesce" `Text.isPrefixOf` col - then col - else "coalesce((" <> col <> "), -1)" -- TODO extract this out to settings - toSelectSQL :: FactTablePopulateSelectSQL -> Text toSelectSQL FactTablePopulateSelectSQL {..} = "SELECT \n" <> joinColumnNames (map (uncurry asName) ftpsSelectCols) diff --git a/src/Ringo/Types.hs b/src/Ringo/Types.hs index 100b642..5f55f8f 100644 --- a/src/Ringo/Types.hs +++ b/src/Ringo/Types.hs @@ -82,6 +82,7 @@ data Settings = Settings , settingDependenciesJSONFileName :: !Text , settingFactsJSONFileName :: !Text , settingDimensionJSONFileName :: !Text + , settingForeignKeyIdCoalesceValue :: !Int } deriving (Eq, Show) defSettings :: Settings @@ -99,6 +100,7 @@ defSettings = Settings , settingDependenciesJSONFileName = "dependencies.json" , settingFactsJSONFileName = "facts.json" , settingDimensionJSONFileName = "dimensions.json" + , settingForeignKeyIdCoalesceValue = -1 } data ValidationError = MissingTable !TableName