Refactoring.

pull/1/head
Abhinav Sarkar 2015-12-30 12:21:41 +05:30
parent f00f158578
commit dc4b260ff6
9 changed files with 33 additions and 36 deletions

View File

@ -30,7 +30,7 @@ library
text >=1.2 && <1.3, text >=1.2 && <1.3,
containers >=0.5 && <0.6, containers >=0.5 && <0.6,
mtl >=2.1 && <2.3, mtl >=2.1 && <2.3,
raw-strings-qq >= 1.0 && <1.2 raw-strings-qq >=1.0 && <1.2
ghc-options: -Wall -fno-warn-unused-do-bind -funbox-strict-fields -fno-warn-orphans -O2 ghc-options: -Wall -fno-warn-unused-do-bind -funbox-strict-fields -fno-warn-orphans -O2
default-extensions: OverloadedStrings, RecordWildCards, ScopedTypeVariables, BangPatterns, default-extensions: OverloadedStrings, RecordWildCards, ScopedTypeVariables, BangPatterns,
TupleSections, CPP, NamedFieldPuns TupleSections, CPP, NamedFieldPuns

View File

@ -12,11 +12,19 @@ import Control.Monad.Reader (Reader, asks)
import Data.Function (on) import Data.Function (on)
import Data.Maybe (mapMaybe, fromMaybe, fromJust, catMaybes) import Data.Maybe (mapMaybe, fromMaybe, fromJust, catMaybes)
import Data.Monoid ((<>)) import Data.Monoid ((<>))
import Data.List (nub, nubBy) import Data.List (nub, nubBy, find)
import Data.Text (Text) import Data.Text (Text)
import Ringo.Types import Ringo.Types
import Ringo.Utils
findTable :: TableName -> [Table] -> Maybe Table
findTable tName = find ((== tName) . tableName)
findFact :: TableName -> [Fact] -> Maybe Fact
findFact fName = find ((== fName) . factName)
findColumn :: ColumnName -> [Column] -> Maybe Column
findColumn cName = find ((== cName) . columnName)
dimColumnName :: Text -> ColumnName -> ColumnName dimColumnName :: Text -> ColumnName -> ColumnName
dimColumnName dimName columnName = dimColumnName dimName columnName =

View File

@ -13,16 +13,16 @@ tableDefnSQL :: Table -> [Text]
tableDefnSQL Table {..} = tableDefnSQL Table {..} =
tableSQL : concatMap constraintDefnSQL tableConstraints tableSQL : concatMap constraintDefnSQL tableConstraints
where where
tableSQL = "CREATE TABLE " <> tableName <> " (\n"
<> (joinColumnNames . map columnDefnSQL $ tableColumns)
<> "\n)"
columnDefnSQL Column {..} = columnDefnSQL Column {..} =
columnName <> " " <> columnType <> " " <> nullableDefnSQL columnNullable columnName <> " " <> columnType <> " " <> nullableDefnSQL columnNullable
nullableDefnSQL Null = "NULL" nullableDefnSQL Null = "NULL"
nullableDefnSQL NotNull = "NOT NULL" nullableDefnSQL NotNull = "NOT NULL"
tableSQL = "CREATE TABLE " <> tableName <> " (\n"
<> (joinColumnNames . map columnDefnSQL $ tableColumns)
<> "\n)"
constraintDefnSQL constraint = constraintDefnSQL constraint =
let alterTableSQL = "ALTER TABLE ONLY " <> tableName <> " ADD " let alterTableSQL = "ALTER TABLE ONLY " <> tableName <> " ADD "
in case constraint of in case constraint of

View File

@ -19,7 +19,8 @@ fullColumnName tName cName = tName <> "." <> cName
dimColumnMapping :: Text -> Fact -> TableName -> [(ColumnName, ColumnName)] dimColumnMapping :: Text -> Fact -> TableName -> [(ColumnName, ColumnName)]
dimColumnMapping dimPrefix fact dimTableName = dimColumnMapping dimPrefix fact dimTableName =
[ (dimColumnName dName cName, cName) [ (dimColumnName dName cName, cName)
| DimVal dName cName <- factColumns fact , dimPrefix <> dName == dimTableName] | DimVal dName cName <- factColumns fact
, dimPrefix <> dName == dimTableName ]
coalesceColumn :: TypeDefaults -> TableName -> Column -> Text coalesceColumn :: TypeDefaults -> TableName -> Column -> Text
coalesceColumn defaults tName Column{..} = coalesceColumn defaults tName Column{..} =

View File

@ -12,9 +12,9 @@ import Data.Maybe (fromJust)
import Data.Monoid ((<>)) import Data.Monoid ((<>))
import Data.Text (Text) import Data.Text (Text)
import Ringo.Extractor.Internal
import Ringo.Generator.Internal import Ringo.Generator.Internal
import Ringo.Types import Ringo.Types
import Ringo.Utils
dimensionTablePopulateSQL :: TablePopulationMode -> Fact -> TableName -> Reader Env Text dimensionTablePopulateSQL :: TablePopulationMode -> Fact -> TableName -> Reader Env Text
dimensionTablePopulateSQL popMode fact dimTableName = do dimensionTablePopulateSQL popMode fact dimTableName = do

View File

@ -206,7 +206,7 @@ factTablePopulateSQL popMode fact = do
coalesceFKId col = coalesceFKId col =
if "coalesce" `Text.isPrefixOf` col if "coalesce" `Text.isPrefixOf` col
then col then col
else "coalesce((" <> col <> "), -1)" else "coalesce((" <> col <> "), -1)" -- TODO extract this out to settings
toSelectSQL :: FactTablePopulateSelectSQL -> Text toSelectSQL :: FactTablePopulateSelectSQL -> Text
toSelectSQL FactTablePopulateSelectSQL {..} = toSelectSQL FactTablePopulateSelectSQL {..} =

View File

@ -58,15 +58,15 @@ data FactColumn = DimTime !ColumnName
| FactCountDistinct !(Maybe ColumnName) !ColumnName | FactCountDistinct !(Maybe ColumnName) !ColumnName
deriving (Eq, Show) deriving (Eq, Show)
factColumnName :: FactColumn -> Maybe ColumnName factSourceColumnName :: FactColumn -> Maybe ColumnName
factColumnName (DimTime cName) = Just cName factSourceColumnName (DimTime cName) = Just cName
factColumnName (NoDimId cName) = Just cName factSourceColumnName (NoDimId cName) = Just cName
factColumnName (DimId _ cName) = Just cName factSourceColumnName (DimId _ cName) = Just cName
factColumnName (DimVal _ cName) = Just cName factSourceColumnName (DimVal _ cName) = Just cName
factColumnName (FactCount cName _) = cName factSourceColumnName (FactCount cName _) = cName
factColumnName (FactSum cName _) = Just cName factSourceColumnName (FactSum cName _) = Just cName
factColumnName (FactAverage cName _) = Just cName factSourceColumnName (FactAverage cName _) = Just cName
factColumnName (FactCountDistinct cName _) = cName factSourceColumnName (FactCountDistinct cName _) = cName
data Settings = Settings data Settings = Settings
{ settingDimPrefix :: !Text { settingDimPrefix :: !Text

View File

@ -3,18 +3,6 @@ module Ringo.Utils where
import qualified Control.Arrow as Arrow import qualified Control.Arrow as Arrow
import Data.Maybe (mapMaybe) import Data.Maybe (mapMaybe)
import Data.List (find)
import Ringo.Types
findTable :: TableName -> [Table] -> Maybe Table
findTable tName = find ((== tName) . tableName)
findFact :: TableName -> [Fact] -> Maybe Fact
findFact fName = find ((== fName) . factName)
findColumn :: ColumnName -> [Column] -> Maybe Column
findColumn cName = find ((== cName) . columnName)
for :: [a] -> (a -> b) -> [b] for :: [a] -> (a -> b) -> [b]
for = flip map for = flip map

View File

@ -12,10 +12,10 @@ import Control.Applicative ((<$>))
#endif #endif
import Control.Monad.Reader (Reader, asks) import Control.Monad.Reader (Reader, asks)
import Data.Maybe (isJust, fromJust)
import Data.Maybe (isJust, fromJust) import Ringo.Extractor.Internal
import Ringo.Types import Ringo.Types
import Ringo.Utils
checkTableForCol :: Table -> ColumnName -> [ValidationError] checkTableForCol :: Table -> ColumnName -> [ValidationError]
checkTableForCol tab colName = checkTableForCol tab colName =
@ -44,8 +44,8 @@ validateFact Fact {..} = do
case findTable factTableName tables of case findTable factTableName tables of
Nothing -> return [ MissingTable factTableName ] Nothing -> return [ MissingTable factTableName ]
Just table -> do Just table -> do
tableVs <- validateTable table tableVs <- validateTable table
parentVs <- concat <$> mapM checkFactParents factParentNames parentVs <- concat <$> mapM checkFactParents factParentNames
let colVs = concatMap (checkColumn tables table) factColumns let colVs = concatMap (checkColumn tables table) factColumns
timeVs = [ MissingTimeColumn factTableName timeVs = [ MissingTimeColumn factTableName
| null [ c | DimTime c <- factColumns ] ] | null [ c | DimTime c <- factColumns ] ]
@ -71,7 +71,7 @@ validateFact Fact {..} = do
Just pFact -> validateFact pFact Just pFact -> validateFact pFact
checkColumn tables table factCol = checkColumn tables table factCol =
maybe [] (checkTableForCol table) (factColumnName factCol) maybe [] (checkTableForCol table) (factSourceColumnName factCol)
++ checkColumnTable tables factCol ++ checkColumnTable tables factCol
checkColumnTable tables factCol = case factCol of checkColumnTable tables factCol = case factCol of