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.Config.PgVersion (PgVersion (..))
import PostgREST.ContentType (ContentType (..)) import PostgREST.ContentType (ContentType (..))
import PostgREST.DbStructure (DbStructure (..), import PostgREST.DbStructure (DbStructure (..),
findTable,
tablePKCols) tablePKCols)
import PostgREST.DbStructure.Identifiers (FieldName, import PostgREST.DbStructure.Identifiers (FieldName,
QualifiedIdentifier (..), QualifiedIdentifier (..),
@@ -410,7 +411,7 @@ handleDelete identifier context@(RequestContext _ _ ApiRequest{..} _) = do
handleInfo :: Monad m => QualifiedIdentifier -> RequestContext -> Handler m Wai.Response handleInfo :: Monad m => QualifiedIdentifier -> RequestContext -> Handler m Wai.Response
handleInfo identifier RequestContext{..} = handleInfo identifier RequestContext{..} =
case find tableMatches $ dbTables ctxDbStructure of case findTable (qiSchema identifier) (qiName identifier) $ dbTables ctxDbStructure of
Just table -> Just table ->
return $ Wai.responseLBS HTTP.status200 [allOrigins, allowH table] mempty return $ Wai.responseLBS HTTP.status200 [allOrigins, allowH table] mempty
Nothing -> Nothing ->
@@ -426,9 +427,6 @@ handleInfo identifier RequestContext{..} =
++ ["PATCH" | tableUpdatable table] ++ ["PATCH" | tableUpdatable table]
++ ["DELETE" | tableDeletable table] ++ ["DELETE" | tableDeletable table]
) )
tableMatches table =
tableName table == qiName identifier
&& tableSchema table == qiSchema identifier
hasPK = hasPK =
not $ null $ tablePKCols ctxDbStructure (qiSchema identifier) (qiName identifier) not $ null $ tablePKCols ctxDbStructure (qiSchema identifier) (qiName identifier)
+7 -4
View File
@@ -23,6 +23,7 @@ module PostgREST.DbStructure
, queryDbStructure , queryDbStructure
, accessibleTables , accessibleTables
, accessibleProcs , accessibleProcs
, findTable
, schemaDescription , schemaDescription
, tableCols , tableCols
, tablePKCols , 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 -- TODO Table could hold references to all its PrimaryKeys
tablePKCols :: DbStructure -> Schema -> TableName -> [Text] 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 -- | The source table column a view column refers to
type SourceColumn = (Column, ViewColumn) 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) = relFromRow allTabs allCols (rs, rt, cn, rcs, frs, frt, frcs) =
Relationship <$> table <*> cols <*> tableF <*> colsF <*> pure (M2O cn) Relationship <$> table <*> cols <*> tableF <*> colsF <*> pure (M2O cn)
where 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 findCol s t c = find (\col -> tableSchema (colTable col) == s && tableName (colTable col) == t && colName col == c) allCols
table = findTable rs rt table = findTable rs rt allTabs
tableF = findTable frs frt tableF = findTable frs frt allTabs
cols = mapM (findCol rs rt) rcs cols = mapM (findCol rs rt) rcs
colsF = mapM (findCol frs frt) frcs colsF = mapM (findCol frs frt) frcs