36 lines
1.1 KiB
Haskell
36 lines
1.1 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
{-# LANGUAGE GADTs #-}
|
|
module Ringo.Generator.Internal where
|
|
|
|
import qualified Data.Map as Map
|
|
import qualified Data.Text as Text
|
|
|
|
import Database.HsSqlPpp.Syntax (ScalarExpr)
|
|
import Data.List (find, nub)
|
|
import Data.Monoid ((<>))
|
|
import Data.Text (Text)
|
|
|
|
import Ringo.Extractor.Internal
|
|
import Ringo.Generator.Sql
|
|
import Ringo.Types
|
|
|
|
coalesceColumn :: TypeDefaults -> TableName -> Column -> ScalarExpr
|
|
coalesceColumn defaults tName Column{..} =
|
|
if columnNullable == Null
|
|
then app "coalesce" [fqColName, num $ defVal columnType]
|
|
else fqColName
|
|
where
|
|
fqColName = eqi tName columnName
|
|
|
|
defVal colType =
|
|
maybe (error $ "Default value not known for column type: " ++ Text.unpack colType) snd
|
|
. find (\(k, _) -> k `Text.isPrefixOf` colType)
|
|
. Map.toList
|
|
$ defaults
|
|
|
|
suffixTableName :: TablePopulationMode -> Text -> TableName -> TableName
|
|
suffixTableName popMode suffix tableName = case popMode of
|
|
FullPopulation -> tableName <> suffix
|
|
IncrementalPopulation -> tableName
|