refactor: clarify Plan module and comment

This commit is contained in:
steve-chavez
2025-09-02 16:26:15 -05:00
committed by Steve Chavez
parent 3e776213b7
commit 0844743c30
2 changed files with 29 additions and 10 deletions
+22 -5
View File
@@ -4,7 +4,8 @@ Description : PostgREST Request Planner
This module is in charge of building an intermediate This module is in charge of building an intermediate
representation between the HTTP request and the representation between the HTTP request and the
final resulting SQL query. final response, which may or not result in SQL execution
(computing OpenAPI or OPTIONS requests don't require database interaction)
A query tree is built in case of resource embedding. By inferring the A query tree is built in case of resource embedding. By inferring the
relationship between tables, join conditions are added for every embedded relationship between tables, join conditions are added for every embedded
@@ -90,6 +91,7 @@ import Protolude hiding (from)
-- Setup for doctests -- Setup for doctests
-- >>> import Data.Ranged.Ranges (fullRange) -- >>> import Data.Ranged.Ranges (fullRange)
-- Plan for reading or writing to the db
data CrudPlan data CrudPlan
= WrappedReadPlan = WrappedReadPlan
{ wrReadPlan :: ReadPlanTree { wrReadPlan :: ReadPlanTree
@@ -109,6 +111,7 @@ data CrudPlan
, crudQi :: QualifiedIdentifier , crudQi :: QualifiedIdentifier
} }
-- Plan for calling a function
data CallReadPlan = CallReadPlan { data CallReadPlan = CallReadPlan {
crReadPlan :: ReadPlanTree crReadPlan :: ReadPlanTree
, crCallPlan :: CallPlan , crCallPlan :: CallPlan
@@ -120,6 +123,7 @@ data CallReadPlan = CallReadPlan {
, crQi :: QualifiedIdentifier , crQi :: QualifiedIdentifier
} }
-- Plan for reading db object metadadta
data InspectPlan = InspectPlan { data InspectPlan = InspectPlan {
ipMedia :: MediaType ipMedia :: MediaType
, ipTxmode :: SQL.Mode , ipTxmode :: SQL.Mode
@@ -127,9 +131,22 @@ data InspectPlan = InspectPlan {
, ipSchema :: Schema , ipSchema :: Schema
} }
data DbActionPlan = DbCrud CrudPlan | DbCall CallReadPlan | MaybeDb InspectPlan -- A Plan may use the the database or not
data InfoPlan = RelInfoPlan QualifiedIdentifier | RoutineInfoPlan CallReadPlan | SchemaInfoPlan data ActionPlan
data ActionPlan = Db DbActionPlan | NoDb InfoPlan = Db DbActionPlan
| NoDb InfoPlan
-- A db plan can consist on read/write, rpc call or reading metadata (which may use the db or just use cached objects)
data DbActionPlan
= DbCrud CrudPlan
| DbCall CallReadPlan
| MayUseDb InspectPlan
-- Plans that don't use the database
data InfoPlan
= RelInfoPlan QualifiedIdentifier -- info about relation
| RoutineInfoPlan CallReadPlan -- info about function
| SchemaInfoPlan -- info about schema cache
actionPlan :: Action -> AppConfig -> ApiRequest -> SchemaCache -> Either Error ActionPlan actionPlan :: Action -> AppConfig -> ApiRequest -> SchemaCache -> Either Error ActionPlan
actionPlan act conf apiReq sCache = case act of actionPlan act conf apiReq sCache = case act of
@@ -147,7 +164,7 @@ dbActionPlan dbAct conf apiReq sCache = case dbAct of
ActRoutine identifier invMethod -> ActRoutine identifier invMethod ->
DbCall <$> callReadPlan identifier conf sCache apiReq invMethod DbCall <$> callReadPlan identifier conf sCache apiReq invMethod
ActSchemaRead tSchema headersOnly -> ActSchemaRead tSchema headersOnly ->
MaybeDb <$> inspectPlan apiReq headersOnly tSchema MayUseDb <$> inspectPlan apiReq headersOnly tSchema
wrappedReadPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest -> Bool -> Either Error CrudPlan wrappedReadPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest -> Bool -> Either Error CrudPlan
wrappedReadPlan identifier conf sCache apiRequest@ApiRequest{iPreferences=Preferences{..},..} headersOnly = do wrappedReadPlan identifier conf sCache apiRequest@ApiRequest{iPreferences=Preferences{..},..} headersOnly = do
+7 -5
View File
@@ -122,9 +122,9 @@ query config AuthResult{..} apiReq (Db plan) sCache =
mainActionQuery mainActionQuery
planTxMode :: DbActionPlan -> SQL.Mode planTxMode :: DbActionPlan -> SQL.Mode
planTxMode (DbCrud x) = pTxMode x planTxMode (DbCrud x) = pTxMode x
planTxMode (DbCall x) = crTxMode x planTxMode (DbCall x) = crTxMode x
planTxMode (MaybeDb x) = ipTxmode x planTxMode (MayUseDb x) = ipTxmode x
planIsoLvl :: AppConfig -> ByteString -> DbActionPlan -> SQL.IsolationLevel planIsoLvl :: AppConfig -> ByteString -> DbActionPlan -> SQL.IsolationLevel
planIsoLvl AppConfig{configRoleIsoLvl} role actPlan = case actPlan of planIsoLvl AppConfig{configRoleIsoLvl} role actPlan = case actPlan of
@@ -165,7 +165,9 @@ actionQuery (DbCrud plan@WrappedReadPlan{..}) conf@AppConfig{..} apiReq@ApiReque
actionQuery (DbCrud plan@MutateReadPlan{..}) conf@AppConfig{..} apiReq@ApiRequest{iPreferences=Preferences{..}} _ = actionQuery (DbCrud plan@MutateReadPlan{..}) conf@AppConfig{..} apiReq@ApiRequest{iPreferences=Preferences{..}} _ =
(mainActionQuery, mainSQLQuery) (mainActionQuery, mainSQLQuery)
where where
(isPut, isInsert, pkCols) = case mrMutatePlan of {Insert{where_,insPkCols} -> ((not . null) where_, True, insPkCols); _ -> (False,False, mempty);} (isPut, isInsert, pkCols) = case mrMutatePlan of
Insert{where_,insPkCols} -> ((not . null) where_, True, insPkCols)
_ -> (False,False, mempty);
result@(SQL.Statement mainSQLQuery _ _ _) = SQL.dynamicallyParameterized (Statements.prepareWrite result@(SQL.Statement mainSQLQuery _ _ _) = SQL.dynamicallyParameterized (Statements.prepareWrite
(QueryBuilder.readPlanToQuery mrReadPlan) (QueryBuilder.readPlanToQuery mrReadPlan)
(QueryBuilder.mutatePlanToQuery mrMutatePlan) (QueryBuilder.mutatePlanToQuery mrMutatePlan)
@@ -222,7 +224,7 @@ actionQuery (DbCall plan@CallReadPlan{..}) conf@AppConfig{..} apiReq@ApiRequest{
MTVndPlan{} -> planRow MTVndPlan{} -> planRow
_ -> fromMaybe (RSStandard (Just 0) 0 mempty mempty Nothing Nothing Nothing) <$> HD.rowMaybe (standardRow True) _ -> fromMaybe (RSStandard (Just 0) 0 mempty mempty Nothing Nothing Nothing) <$> HD.rowMaybe (standardRow True)
actionQuery (MaybeDb plan@InspectPlan{ipSchema=tSchema}) AppConfig{..} _ sCache = actionQuery (MayUseDb plan@InspectPlan{ipSchema=tSchema}) AppConfig{..} _ sCache =
(mainActionQuery, mempty) (mainActionQuery, mempty)
where where
mainActionQuery = lift $ mainActionQuery = lift $