refactor: rename RelationType to Cardinality

Remove Root type constructor
This commit is contained in:
steve-chavez
2019-11-05 12:55:48 -05:00
committed by Steve Chávez
parent db41fb454e
commit cb3d9ab625
3 changed files with 19 additions and 17 deletions
+9 -10
View File
@@ -110,9 +110,7 @@ addRelations schema allRelations parentNode (Node (query@Select{from=tbl}, (node
findRelation schema allRelations nodeName parentNodeTable relationDetail in
Node <$> newReadNode <*> (updateForest . hush $ Node <$> newReadNode <*> pure forest)
_ ->
let rn = (query, (nodeName, Just r, alias, Nothing, depth))
r = Relation t [] t [] Root Nothing Nothing Nothing
t = Table schema nodeName Nothing True in -- !!! TODO find another way to get the table from the query
let rn = (query, (nodeName, Nothing, alias, Nothing, depth)) in
Node rn <$> updateForest (Just $ Node rn forest)
where
updateForest :: Maybe ReadRequest -> Either ApiRequestError [ReadRequest]
@@ -201,13 +199,16 @@ findRelation schema allRelations nodeTableName parentNodeTableName relationDetai
addJoinConditions :: Maybe Alias -> ReadRequest -> Either ApiRequestError ReadRequest
addJoinConditions previousAlias (Node node@(query@Select{from=tbl}, nodeProps@(_, relation, _, _, depth)) forest) =
case relation of
Just Relation{relType=Root} -> Node node <$> updatedForest -- this is the root node
Just rel@Relation{relType=Parent} -> Node (augmentQuery rel, nodeProps) <$> updatedForest
Just rel@Relation{relType=Child} -> Node (augmentQuery rel, nodeProps) <$> updatedForest
Just rel@Relation{relType=Many, relLinkTable=(Just linkTable)} ->
let rq = augmentQuery rel in
Node (rq{implicitJoins=tableQi linkTable:implicitJoins rq}, nodeProps) <$> updatedForest
_ -> Left UnknownRelation
Just rel@Relation{relType=Many, relLinkTable=lTable} ->
case lTable of
Just linkTable ->
let rq = augmentQuery rel in
Node (rq{implicitJoins=tableQi linkTable:implicitJoins rq}, nodeProps) <$> updatedForest
Nothing ->
Left UnknownRelation
Nothing -> Node node <$> updatedForest
where
newAlias = case isSelfJoin <$> relation of
Just True
@@ -232,7 +233,6 @@ getJoinConditions previousAlias newAlias (Relation Table{tableSchema=tSchema, ta
Many ->
let ltN = maybe "" tableName lt in
zipWith (toJoinCondition tN ltN) cols (fromMaybe [] lc1) ++ zipWith (toJoinCondition ftN ltN) fCols (fromMaybe [] lc2)
Root -> witness
where
toJoinCondition :: Text -> Text -> Column -> Column -> JoinCondition
toJoinCondition tb ftb c fc =
@@ -354,7 +354,6 @@ returningCols rr@(Node _ forest) = returnings
Parent -> Just cols
Child -> Just cols
Many -> Just cols
_ -> Nothing
_ -> Nothing
) forest
-- However if the "client_id" is present, e.g. mutateRequest to /projects?select=client_id,name,clients(name)
+1 -3
View File
@@ -73,9 +73,7 @@ getJoinsSelects rr@(Node (_, (name, Just Relation{relType=relTyp,relTable=Table{
<> "FROM (" <> subquery <> ") " <> pgFmtIdent table
<> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) in
(j,sel:s)
--readRequestToQuery is called only after addJoinConditions which ensures the only posible relations are Child Parent Many
Root -> witness
getJoinsSelects _ _ = witness
getJoinsSelects (Node (_, (_, Nothing, _, _, _)) _) _ = ([], [])
mutateRequestToQuery :: MutateRequest -> SqlQuery
mutateRequestToQuery (Insert mainQi iCols onConflct putConditions returnings) =
+9 -4
View File
@@ -257,7 +257,12 @@ data QualifiedIdentifier = QualifiedIdentifier {
} deriving (Show, Eq, Ord)
data RelationType = Child | Parent | Many | Root deriving (Show, Eq)
-- | The relationship [cardinality](https://en.wikipedia.org/wiki/Cardinality_(data_modeling)).
-- | TODO: missing one-to-one
data Cardinality = Child -- ^ a.k.a. many-to-one
| Parent -- ^ a.k.a. one-to-many
| Many -- ^ a.k.a. many-to-many
deriving (Show, Eq)
{-|
The name 'Relation' here is used with the meaning
@@ -272,15 +277,15 @@ data Relation = Relation {
, relColumns :: [Column]
, relFTable :: Table
, relFColumns :: [Column]
, relType :: RelationType
-- The Link attrs are used when RelationType == Many
, relType :: Cardinality
-- The Link attrs are used when Cardinality == Many
, relLinkTable :: Maybe Table
, relLinkCols1 :: Maybe [Column]
, relLinkCols2 :: Maybe [Column]
} deriving (Show, Eq)
isSelfJoin :: Relation -> Bool
isSelfJoin r = relType r /= Root && relTable r == relFTable r
isSelfJoin r = relTable r == relFTable r
data PayloadJSON =
-- | Cached attributes of a JSON payload