diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 44f4d43e7..1470011f2 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -63,6 +63,7 @@ import PostgREST.Config (AppConfig (..), import PostgREST.Config.PgVersion (PgVersion (..)) import PostgREST.ContentType (ContentType (..)) import PostgREST.DbStructure (DbStructure (..), + findTable, tablePKCols) import PostgREST.DbStructure.Identifiers (FieldName, QualifiedIdentifier (..), @@ -410,7 +411,7 @@ handleDelete identifier context@(RequestContext _ _ ApiRequest{..} _) = do handleInfo :: Monad m => QualifiedIdentifier -> RequestContext -> Handler m Wai.Response handleInfo identifier RequestContext{..} = - case find tableMatches $ dbTables ctxDbStructure of + case findTable (qiSchema identifier) (qiName identifier) $ dbTables ctxDbStructure of Just table -> return $ Wai.responseLBS HTTP.status200 [allOrigins, allowH table] mempty Nothing -> @@ -426,9 +427,6 @@ handleInfo identifier RequestContext{..} = ++ ["PATCH" | tableUpdatable table] ++ ["DELETE" | tableDeletable table] ) - tableMatches table = - tableName table == qiName identifier - && tableSchema table == qiSchema identifier hasPK = not $ null $ tablePKCols ctxDbStructure (qiSchema identifier) (qiName identifier) diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index 24754d8fa..fcb08b9de 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -23,6 +23,7 @@ module PostgREST.DbStructure , queryDbStructure , accessibleTables , accessibleProcs + , findTable , schemaDescription , tableCols , tablePKCols @@ -76,7 +77,10 @@ tableCols dbs tSchema tName = filter (\Column{colTable=Table{tableSchema=s, tabl -- TODO Table could hold references to all its PrimaryKeys tablePKCols :: DbStructure -> Schema -> TableName -> [Text] -tablePKCols dbs tSchema tName = pkName <$> filter (\pk -> tSchema == (tableSchema . pkTable) pk && tName == (tableName . pkTable) pk) (dbPrimaryKeys dbs) +tablePKCols dbs tSchema tName = pkName <$> filter (\pk -> tSchema == (tableSchema . pkTable) pk && tName == (tableName . pkTable) pk) (dbPrimaryKeys dbs) + +findTable :: Schema -> TableName -> [Table] -> Maybe Table +findTable tSchema tName tbls = find (\tbl -> tableName tbl == tName && tableSchema tbl == tSchema) tbls -- | The source table column a view column refers to type SourceColumn = (Column, ViewColumn) @@ -669,10 +673,9 @@ relFromRow :: [Table] -> [Column] -> (Text, Text, Text, [Text], Text, Text, [Tex relFromRow allTabs allCols (rs, rt, cn, rcs, frs, frt, frcs) = Relationship <$> table <*> cols <*> tableF <*> colsF <*> pure (M2O cn) where - findTable s t = find (\tbl -> tableSchema tbl == s && tableName tbl == t) allTabs findCol s t c = find (\col -> tableSchema (colTable col) == s && tableName (colTable col) == t && colName col == c) allCols - table = findTable rs rt - tableF = findTable frs frt + table = findTable rs rt allTabs + tableF = findTable frs frt allTabs cols = mapM (findCol rs rt) rcs colsF = mapM (findCol frs frt) frcs