Refactor some functions to use concatMap

This commit is contained in:
steve-chavez
2018-03-17 07:53:41 -05:00
committed by Steve Chávez
parent 349a5ae076
commit 243e692192
+12 -18
View File
@@ -47,7 +47,7 @@ getDbStructure schema pgVer = do
let rels = addManyToManyRelations . addParentRelations $ addViewRelations syns childRels let rels = addManyToManyRelations . addParentRelations $ addViewRelations syns childRels
cols' = addForeignKeys rels cols cols' = addForeignKeys rels cols
keys' = synonymousPrimaryKeys syns keys keys' = addViewPrimaryKeys syns keys
return DbStructure { return DbStructure {
dbTables = tabs dbTables = tabs
@@ -272,9 +272,8 @@ When having t1_view.c1 and a t2_view.c2 synonyms, we need to add a View to View
The logic for composite pks is similar just need to make sure all the Relation columns have synonyms. The logic for composite pks is similar just need to make sure all the Relation columns have synonyms.
-} -}
addViewRelations :: [Synonym] -> [Relation] -> [Relation] addViewRelations :: [Synonym] -> [Relation] -> [Relation]
addViewRelations _ [] = [] addViewRelations allSyns = concatMap (\rel ->
addViewRelations allSyns (rel:rels) = rel : case rel of
case rel of
Relation{relType=Child, relTable, relColumns, relFTable, relFColumns} -> Relation{relType=Child, relTable, relColumns, relFTable, relFColumns} ->
let colSynsGroupedByView :: [Column] -> [[Synonym]] let colSynsGroupedByView :: [Column] -> [[Synonym]]
@@ -296,15 +295,12 @@ addViewRelations allSyns (rel:rels) =
-- View View Relations -- View View Relations
[Relation (getView syns) (snd <$> syns) (getView fSyns) (snd <$> fSyns) Child Nothing Nothing Nothing [Relation (getView syns) (snd <$> syns) (getView fSyns) (snd <$> fSyns) Child Nothing Nothing Nothing
| syns <- colsSyns, fSyns <- fColsSyns, syns `allSynsOf` relColumns, fSyns `allSynsOf` relFColumns] ++ | syns <- colsSyns, fSyns <- fColsSyns, syns `allSynsOf` relColumns, fSyns `allSynsOf` relFColumns]
rel : addViewRelations allSyns rels _ -> [])
_ -> rel : addViewRelations allSyns rels
addParentRelations :: [Relation] -> [Relation] addParentRelations :: [Relation] -> [Relation]
addParentRelations [] = [] addParentRelations = concatMap (\rel@(Relation t c ft fc _ _ _ _) -> [rel, Relation ft fc t c Parent Nothing Nothing Nothing])
addParentRelations (rel@(Relation t c ft fc _ _ _ _):rels) = Relation ft fc t c Parent Nothing Nothing Nothing : rel : addParentRelations rels
addManyToManyRelations :: [Relation] -> [Relation] addManyToManyRelations :: [Relation] -> [Relation]
addManyToManyRelations rels = rels ++ addMirrorRelation (mapMaybe link2Relation links) addManyToManyRelations rels = rels ++ addMirrorRelation (mapMaybe link2Relation links)
@@ -317,8 +313,7 @@ addManyToManyRelations rels = rels ++ addMirrorRelation (mapMaybe link2Relation
combinations 0 _ = [ [] ] combinations 0 _ = [ [] ]
combinations n xs = [ y:ys | y:xs' <- tails xs combinations n xs = [ y:ys | y:xs' <- tails xs
, ys <- combinations (n-1) xs'] , ys <- combinations (n-1) xs']
addMirrorRelation [] = [] addMirrorRelation = concatMap (\rel@(Relation t c ft fc _ lt lc1 lc2) -> [rel, Relation ft fc t c Many lt lc2 lc1])
addMirrorRelation (rel@(Relation t c ft fc _ lt lc1 lc2):rels') = Relation ft fc t c Many lt lc2 lc1 : rel : addMirrorRelation rels'
link2Relation [ link2Relation [
Relation{relTable=lt, relColumns=lc1, relFTable=t, relFColumns=c}, Relation{relTable=lt, relColumns=lc1, relFTable=t, relFColumns=c},
Relation{ relColumns=lc2, relFTable=ft, relFColumns=fc} Relation{ relColumns=lc2, relFTable=ft, relFColumns=fc}
@@ -327,12 +322,11 @@ addManyToManyRelations rels = rels ++ addMirrorRelation (mapMaybe link2Relation
| otherwise = Nothing | otherwise = Nothing
link2Relation _ = Nothing link2Relation _ = Nothing
synonymousPrimaryKeys :: [Synonym] -> [PrimaryKey] -> [PrimaryKey] addViewPrimaryKeys :: [Synonym] -> [PrimaryKey] -> [PrimaryKey]
synonymousPrimaryKeys _ [] = [] addViewPrimaryKeys syns = concatMap (\pk ->
synonymousPrimaryKeys syns (key:keys) = key : newKeys ++ synonymousPrimaryKeys syns keys let viewPks = (\(_, viewCol) -> PrimaryKey{pkTable=colTable viewCol, pkName=colName viewCol}) <$>
where filter (\(col, _) -> colTable col == pkTable pk && colName col == pkName pk) syns in
keySyns = filter ((\c -> colTable c == pkTable key && colName c == pkName key) . fst) syns pk : viewPks)
newKeys = map ((\c -> PrimaryKey{pkTable=colTable c,pkName=colName c}) . snd) keySyns
allTables :: H.Query () [Table] allTables :: H.Query () [Table]
allTables = allTables =