refactor: add findTable function

This commit is contained in:
steve-chavez
2022-03-26 15:29:13 +01:00
committed by Steve Chavez
parent 87ffe39746
commit 73dc2692b1
2 changed files with 9 additions and 8 deletions
+2 -4
View File
@@ -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)
+7 -4
View File
@@ -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