2016-01-01 20:57:54 +05:30
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2016-07-05 21:59:20 +05:30
|
|
|
{-# LANGUAGE NoImplicitPrelude #-}
|
2016-06-22 16:52:04 +05:30
|
|
|
{-# LANGUAGE GADTs #-}
|
2016-02-03 16:00:39 +05:30
|
|
|
|
2016-07-06 17:23:46 +05:30
|
|
|
module Ringo.Generator.Populate.Dimension
|
|
|
|
( dimensionTablePopulationSQL
|
|
|
|
, dimensionTablePopulationStatement
|
|
|
|
) where
|
2015-12-29 17:11:15 +05:30
|
|
|
|
2016-07-05 21:59:20 +05:30
|
|
|
import Prelude.Compat
|
2016-06-22 17:10:14 +05:30
|
|
|
import Control.Monad.Reader (Reader, asks)
|
2016-01-04 01:32:36 +05:30
|
|
|
import Database.HsSqlPpp.Syntax (Statement, QueryExpr(..), Distinct(..), makeSelect, JoinType(..))
|
|
|
|
import Data.Maybe (fromJust)
|
|
|
|
import Data.Text (Text)
|
2015-12-29 17:11:15 +05:30
|
|
|
|
2015-12-30 12:21:41 +05:30
|
|
|
import Ringo.Extractor.Internal
|
2015-12-29 17:11:15 +05:30
|
|
|
import Ringo.Generator.Internal
|
2016-01-04 01:32:36 +05:30
|
|
|
import Ringo.Generator.Sql
|
2016-06-22 17:10:14 +05:30
|
|
|
import Ringo.Types.Internal
|
2015-12-29 17:11:15 +05:30
|
|
|
|
2016-07-05 23:09:42 +05:30
|
|
|
dimensionTablePopulationSQL :: TablePopulationMode -> Fact -> TableName -> Reader Env Text
|
|
|
|
dimensionTablePopulationSQL popMode fact dimTableName =
|
|
|
|
ppStatement <$> dimensionTablePopulationStatement popMode fact dimTableName
|
2016-01-04 01:32:36 +05:30
|
|
|
|
2016-07-05 23:09:42 +05:30
|
|
|
dimensionTablePopulationStatement :: TablePopulationMode -> Fact -> TableName -> Reader Env Statement
|
|
|
|
dimensionTablePopulationStatement popMode fact dimTableName = do
|
2015-12-30 19:57:38 +05:30
|
|
|
Settings {..} <- asks envSettings
|
|
|
|
tables <- asks envTables
|
|
|
|
defaults <- asks envTypeDefaults
|
|
|
|
let factTable = fromJust $ findTable (factTableName fact) tables
|
|
|
|
colMapping = dimColumnMapping settingDimPrefix fact dimTableName
|
2016-01-25 22:13:47 +05:30
|
|
|
selectCols = [ flip sia (nmc cName) $ coalesceColumn defaults (factTableName fact) col
|
2015-12-30 19:57:38 +05:30
|
|
|
| (_, cName) <- colMapping
|
|
|
|
, let col = fromJust . findColumn cName $ tableColumns factTable ]
|
2016-06-22 16:52:04 +05:30
|
|
|
timeCol = head ([ cName | FactColumn cName DimTime <- factColumns fact ] :: [ColumnName])
|
2016-01-04 01:32:36 +05:30
|
|
|
isNotNullC = parens . foldBinop "or" . map (postop "isnotnull" . ei . snd) $ colMapping
|
|
|
|
selectWhereC = Just . foldBinop "and" $
|
2016-01-15 14:43:11 +05:30
|
|
|
[ isNotNullC, binop "<" (ei timeCol) placeholder ] ++
|
|
|
|
[ binop ">=" (ei timeCol) placeholder | popMode == IncrementalPopulation ]
|
2016-01-04 01:32:36 +05:30
|
|
|
selectC = makeSelect
|
|
|
|
{ selDistinct = Distinct
|
|
|
|
, selSelectList = sl selectCols
|
|
|
|
, selTref = [tref $ factTableName fact]
|
|
|
|
, selWhere = selectWhereC
|
|
|
|
}
|
2015-12-30 19:57:38 +05:30
|
|
|
|
2016-01-04 01:32:36 +05:30
|
|
|
iTableName = suffixTableName popMode settingTableNameSuffixTemplate dimTableName
|
|
|
|
insertC = insert iTableName (map fst colMapping) $ case popMode of
|
|
|
|
FullPopulation -> selectC
|
|
|
|
IncrementalPopulation -> let alias = "x" in
|
|
|
|
makeSelect
|
|
|
|
{ selSelectList = sl [si $ qstar alias]
|
|
|
|
, selTref =
|
|
|
|
[ tjoin (subtrefa alias selectC) LeftOuter (tref dimTableName) . Just $
|
|
|
|
foldBinop "and" [ binop "=" (eqi dimTableName c1) (eqi alias c2) | (c1, c2) <- colMapping ] ]
|
|
|
|
, selWhere =
|
|
|
|
Just . foldBinop "and" . map (postop "isnull" . eqi dimTableName . fst) $ colMapping
|
|
|
|
}
|
2015-12-30 19:57:38 +05:30
|
|
|
|
2016-01-04 01:32:36 +05:30
|
|
|
return insertC
|