From cb3d9ab625a6c87a1786056564dd3f014fcb6d10 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Fri, 25 Oct 2019 12:36:11 -0500 Subject: [PATCH] refactor: rename RelationType to Cardinality Remove Root type constructor --- src/PostgREST/DbRequestBuilder.hs | 19 +++++++++---------- src/PostgREST/QueryBuilder.hs | 4 +--- src/PostgREST/Types.hs | 13 +++++++++---- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/PostgREST/DbRequestBuilder.hs b/src/PostgREST/DbRequestBuilder.hs index 3527cc3c3..c258cc20b 100644 --- a/src/PostgREST/DbRequestBuilder.hs +++ b/src/PostgREST/DbRequestBuilder.hs @@ -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) diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs index 0919c4d95..cfb3b649c 100644 --- a/src/PostgREST/QueryBuilder.hs +++ b/src/PostgREST/QueryBuilder.hs @@ -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) = diff --git a/src/PostgREST/Types.hs b/src/PostgREST/Types.hs index 20ceb0093..11ded2246 100644 --- a/src/PostgREST/Types.hs +++ b/src/PostgREST/Types.hs @@ -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