39 lines
1.2 KiB
Haskell
39 lines
1.2 KiB
Haskell
|
module Ringo.Generator.Internal where
|
||
|
|
||
|
import qualified Data.Map as Map
|
||
|
import qualified Data.Text as Text
|
||
|
|
||
|
import Data.List (find)
|
||
|
import Data.Maybe (fromMaybe)
|
||
|
import Data.Monoid ((<>))
|
||
|
import Data.Text (Text)
|
||
|
|
||
|
import Ringo.Extractor.Internal
|
||
|
import Ringo.Types
|
||
|
|
||
|
joinColumnNames :: [ColumnName] -> Text
|
||
|
joinColumnNames = Text.intercalate ",\n"
|
||
|
|
||
|
fullColumnName :: TableName -> ColumnName -> ColumnName
|
||
|
fullColumnName tName cName = tName <> "." <> cName
|
||
|
|
||
|
dimColumnMapping :: Text -> Fact -> TableName -> [(ColumnName, ColumnName)]
|
||
|
dimColumnMapping dimPrefix fact dimTableName =
|
||
|
[ (dimColumnName dName cName, cName)
|
||
|
| DimVal dName cName <- factColumns fact , dimPrefix <> dName == dimTableName]
|
||
|
|
||
|
coalesceColumn :: TypeDefaults -> TableName -> Column -> Text
|
||
|
coalesceColumn defaults tName Column{..} =
|
||
|
if columnNullable == Null
|
||
|
then "coalesce(" <> fqColName <> ", " <> defVal columnType <> ")"
|
||
|
else fqColName
|
||
|
where
|
||
|
fqColName = fullColumnName tName columnName
|
||
|
|
||
|
defVal colType =
|
||
|
fromMaybe (error $ "Default value not known for column type: " ++ Text.unpack colType)
|
||
|
. fmap snd
|
||
|
. find (\(k, _) -> k `Text.isPrefixOf` colType)
|
||
|
. Map.toList
|
||
|
$ defaults
|