2016-01-01 20:57:54 +05:30
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2016-02-03 16:00:39 +05:30
|
|
|
{-# LANGUAGE GADTs #-}
|
2016-07-05 21:59:20 +05:30
|
|
|
|
2015-12-29 17:11:15 +05:30
|
|
|
module Ringo.Generator.Internal where
|
|
|
|
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
import qualified Data.Text as Text
|
|
|
|
|
2016-01-04 01:32:36 +05:30
|
|
|
import Database.HsSqlPpp.Syntax (ScalarExpr)
|
2016-07-05 21:00:20 +05:30
|
|
|
import Data.List (find)
|
2016-01-25 22:13:47 +05:30
|
|
|
import Data.Monoid ((<>))
|
|
|
|
import Data.Text (Text)
|
2015-12-29 17:11:15 +05:30
|
|
|
|
2016-01-04 01:32:36 +05:30
|
|
|
import Ringo.Generator.Sql
|
2015-12-29 17:11:15 +05:30
|
|
|
import Ringo.Types
|
|
|
|
|
2016-01-25 22:13:47 +05:30
|
|
|
coalesceColumn :: TypeDefaults -> TableName -> Column -> ScalarExpr
|
|
|
|
coalesceColumn defaults tName Column{..} =
|
2015-12-29 17:11:15 +05:30
|
|
|
if columnNullable == Null
|
2016-01-04 01:32:36 +05:30
|
|
|
then app "coalesce" [fqColName, num $ defVal columnType]
|
2015-12-29 17:11:15 +05:30
|
|
|
else fqColName
|
|
|
|
where
|
2016-01-04 01:32:36 +05:30
|
|
|
fqColName = eqi tName columnName
|
2015-12-29 17:11:15 +05:30
|
|
|
|
|
|
|
defVal colType =
|
2015-12-29 18:22:01 +05:30
|
|
|
maybe (error $ "Default value not known for column type: " ++ Text.unpack colType) snd
|
2015-12-29 17:11:15 +05:30
|
|
|
. find (\(k, _) -> k `Text.isPrefixOf` colType)
|
|
|
|
. Map.toList
|
|
|
|
$ defaults
|
2015-12-30 19:57:38 +05:30
|
|
|
|
|
|
|
suffixTableName :: TablePopulationMode -> Text -> TableName -> TableName
|
|
|
|
suffixTableName popMode suffix tableName = case popMode of
|
|
|
|
FullPopulation -> tableName <> suffix
|
|
|
|
IncrementalPopulation -> tableName
|