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
+7 -8
View File
@@ -110,9 +110,7 @@ addRelations schema allRelations parentNode (Node (query@Select{from=tbl}, (node
findRelation schema allRelations nodeName parentNodeTable relationDetail in findRelation schema allRelations nodeName parentNodeTable relationDetail in
Node <$> newReadNode <*> (updateForest . hush $ Node <$> newReadNode <*> pure forest) Node <$> newReadNode <*> (updateForest . hush $ Node <$> newReadNode <*> pure forest)
_ -> _ ->
let rn = (query, (nodeName, Just r, alias, Nothing, depth)) let rn = (query, (nodeName, Nothing, alias, Nothing, depth)) in
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
Node rn <$> updateForest (Just $ Node rn forest) Node rn <$> updateForest (Just $ Node rn forest)
where where
updateForest :: Maybe ReadRequest -> Either ApiRequestError [ReadRequest] updateForest :: Maybe ReadRequest -> Either ApiRequestError [ReadRequest]
@@ -201,13 +199,16 @@ findRelation schema allRelations nodeTableName parentNodeTableName relationDetai
addJoinConditions :: Maybe Alias -> ReadRequest -> Either ApiRequestError ReadRequest addJoinConditions :: Maybe Alias -> ReadRequest -> Either ApiRequestError ReadRequest
addJoinConditions previousAlias (Node node@(query@Select{from=tbl}, nodeProps@(_, relation, _, _, depth)) forest) = addJoinConditions previousAlias (Node node@(query@Select{from=tbl}, nodeProps@(_, relation, _, _, depth)) forest) =
case relation of 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=Parent} -> Node (augmentQuery rel, nodeProps) <$> updatedForest
Just rel@Relation{relType=Child} -> Node (augmentQuery rel, nodeProps) <$> updatedForest Just rel@Relation{relType=Child} -> Node (augmentQuery rel, nodeProps) <$> updatedForest
Just rel@Relation{relType=Many, relLinkTable=(Just linkTable)} -> Just rel@Relation{relType=Many, relLinkTable=lTable} ->
case lTable of
Just linkTable ->
let rq = augmentQuery rel in let rq = augmentQuery rel in
Node (rq{implicitJoins=tableQi linkTable:implicitJoins rq}, nodeProps) <$> updatedForest Node (rq{implicitJoins=tableQi linkTable:implicitJoins rq}, nodeProps) <$> updatedForest
_ -> Left UnknownRelation Nothing ->
Left UnknownRelation
Nothing -> Node node <$> updatedForest
where where
newAlias = case isSelfJoin <$> relation of newAlias = case isSelfJoin <$> relation of
Just True Just True
@@ -232,7 +233,6 @@ getJoinConditions previousAlias newAlias (Relation Table{tableSchema=tSchema, ta
Many -> Many ->
let ltN = maybe "" tableName lt in let ltN = maybe "" tableName lt in
zipWith (toJoinCondition tN ltN) cols (fromMaybe [] lc1) ++ zipWith (toJoinCondition ftN ltN) fCols (fromMaybe [] lc2) zipWith (toJoinCondition tN ltN) cols (fromMaybe [] lc1) ++ zipWith (toJoinCondition ftN ltN) fCols (fromMaybe [] lc2)
Root -> witness
where where
toJoinCondition :: Text -> Text -> Column -> Column -> JoinCondition toJoinCondition :: Text -> Text -> Column -> Column -> JoinCondition
toJoinCondition tb ftb c fc = toJoinCondition tb ftb c fc =
@@ -355,7 +355,6 @@ returningCols rr@(Node _ forest) = returnings
Child -> Just cols Child -> Just cols
Many -> Just cols Many -> Just cols
_ -> Nothing _ -> Nothing
_ -> Nothing
) forest ) forest
-- However if the "client_id" is present, e.g. mutateRequest to /projects?select=client_id,name,clients(name) -- However if the "client_id" is present, e.g. mutateRequest to /projects?select=client_id,name,clients(name)
-- we would get `RETURNING client_id, name, client_id` and then we would produce the "column reference \"client_id\" is ambiguous" -- we would get `RETURNING client_id, name, client_id` and then we would produce the "column reference \"client_id\" is ambiguous"
+1 -3
View File
@@ -73,9 +73,7 @@ getJoinsSelects rr@(Node (_, (name, Just Relation{relType=relTyp,relTable=Table{
<> "FROM (" <> subquery <> ") " <> pgFmtIdent table <> "FROM (" <> subquery <> ") " <> pgFmtIdent table
<> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) in <> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) in
(j,sel:s) (j,sel:s)
--readRequestToQuery is called only after addJoinConditions which ensures the only posible relations are Child Parent Many getJoinsSelects (Node (_, (_, Nothing, _, _, _)) _) _ = ([], [])
Root -> witness
getJoinsSelects _ _ = witness
mutateRequestToQuery :: MutateRequest -> SqlQuery mutateRequestToQuery :: MutateRequest -> SqlQuery
mutateRequestToQuery (Insert mainQi iCols onConflct putConditions returnings) = mutateRequestToQuery (Insert mainQi iCols onConflct putConditions returnings) =
+9 -4
View File
@@ -257,7 +257,12 @@ data QualifiedIdentifier = QualifiedIdentifier {
} deriving (Show, Eq, Ord) } 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 The name 'Relation' here is used with the meaning
@@ -272,15 +277,15 @@ data Relation = Relation {
, relColumns :: [Column] , relColumns :: [Column]
, relFTable :: Table , relFTable :: Table
, relFColumns :: [Column] , relFColumns :: [Column]
, relType :: RelationType , relType :: Cardinality
-- The Link attrs are used when RelationType == Many -- The Link attrs are used when Cardinality == Many
, relLinkTable :: Maybe Table , relLinkTable :: Maybe Table
, relLinkCols1 :: Maybe [Column] , relLinkCols1 :: Maybe [Column]
, relLinkCols2 :: Maybe [Column] , relLinkCols2 :: Maybe [Column]
} deriving (Show, Eq) } deriving (Show, Eq)
isSelfJoin :: Relation -> Bool isSelfJoin :: Relation -> Bool
isSelfJoin r = relType r /= Root && relTable r == relFTable r isSelfJoin r = relTable r == relFTable r
data PayloadJSON = data PayloadJSON =
-- | Cached attributes of a JSON payload -- | Cached attributes of a JSON payload