refactor: Remove DbRequest type

This type only adds an extra constructor.
This commit is contained in:
steve-chavez
2019-09-28 13:45:18 -05:00
committed by Steve Chávez
parent 94f5894d7f
commit 0183d32c7f
3 changed files with 28 additions and 26 deletions
+7 -7
View File
@@ -57,8 +57,8 @@ import PostgREST.OpenAPI
import PostgREST.Parsers (pRequestColumns) import PostgREST.Parsers (pRequestColumns)
import PostgREST.QueryBuilder (limitedQuery, import PostgREST.QueryBuilder (limitedQuery,
requestToCallProcQuery, requestToCallProcQuery,
requestToCountQuery, readRequestToCountQuery,
requestToQuery) readRequestToQuery, mutateRequestToQuery)
import PostgREST.RangeQuery (allRange, contentRangeH, import PostgREST.RangeQuery (allRange, contentRangeH,
rangeStatusHeader) rangeStatusHeader)
import PostgREST.Statements (callProcStatement, import PostgREST.Statements (callProcStatement,
@@ -339,14 +339,14 @@ app dbStructure proc cols conf apiRequest =
topLevelRange = iTopLevelRange apiRequest topLevelRange = iTopLevelRange apiRequest
readReq tableName = readRequest schema tableName maxRows (dbRelations dbStructure) apiRequest readReq tableName = readRequest schema tableName maxRows (dbRelations dbStructure) apiRequest
fldNames tableName = fieldNames <$> readReq tableName fldNames tableName = fieldNames <$> readReq tableName
readDbRequest tableName = DbRead <$> readReq tableName readReqst tableName = readReq tableName
selectQuery tableName = requestToQuery schema False <$> readDbRequest tableName selectQuery tableName = readRequestToQuery schema False <$> readReqst tableName
countQuery tableName = requestToCountQuery schema <$> readDbRequest tableName countQuery tableName = readRequestToCountQuery schema <$> readReqst tableName
readSqlParts tableName = (,) <$> selectQuery tableName <*> countQuery tableName readSqlParts tableName = (,) <$> selectQuery tableName <*> countQuery tableName
mutationDbRequest s t = mutateRequest apiRequest t cols (tablePKCols dbStructure s t) =<< fldNames t mutationRequest s t = mutateRequest apiRequest t cols (tablePKCols dbStructure s t) =<< fldNames t
mutateSqlParts s t = mutateSqlParts s t =
(,) <$> selectQuery t (,) <$> selectQuery t
<*> (requestToQuery schema False . DbMutate <$> mutationDbRequest s t) <*> (mutateRequestToQuery schema <$> mutationRequest s t)
rawContentTypes = rawContentTypes =
(decodeContentType <$> configRawMediaTypes conf) `L.union` (decodeContentType <$> configRawMediaTypes conf) `L.union`
[ CTOctetStream, CTTextPlain ] [ CTOctetStream, CTTextPlain ]
+18 -15
View File
@@ -11,8 +11,9 @@ represent database objects (e.g. Relation, Schema) and SqlFragment
to produce SqlQuery type outputs. to produce SqlQuery type outputs.
-} -}
module PostgREST.QueryBuilder ( module PostgREST.QueryBuilder (
requestToQuery readRequestToQuery
, requestToCountQuery , mutateRequestToQuery
, readRequestToCountQuery
, requestToCallProcQuery , requestToCallProcQuery
, limitedQuery , limitedQuery
, setLocalQuery , setLocalQuery
@@ -33,8 +34,8 @@ import PostgREST.Types
import Protolude hiding (cast, intercalate, import Protolude hiding (cast, intercalate,
replace) replace)
requestToQuery :: Schema -> Bool -> DbRequest -> SqlQuery readRequestToQuery :: Schema -> Bool -> ReadRequest -> SqlQuery
requestToQuery schema isParent (DbRead (Node (Select colSelects tbl tblAlias implJoins logicForest joinConditions_ ordts range, _) forest)) = readRequestToQuery schema isParent (Node (Select colSelects tbl tblAlias implJoins logicForest joinConditions_ ordts range, _) forest) =
unwords [ unwords [
"SELECT " <> intercalate ", " (map (pgFmtSelectItem qi) colSelects ++ selects), "SELECT " <> intercalate ", " (map (pgFmtSelectItem qi) colSelects ++ selects),
"FROM " <> intercalate ", " (tabl : implJs), "FROM " <> intercalate ", " (tabl : implJs),
@@ -59,26 +60,29 @@ requestToQuery schema isParent (DbRead (Node (Select colSelects tbl tblAlias imp
<> "SELECT json_agg(" <> pgFmtIdent table <> ".*) " <> "SELECT json_agg(" <> pgFmtIdent table <> ".*) "
<> "FROM (" <> subquery <> ") " <> pgFmtIdent table <> "FROM (" <> subquery <> ") " <> pgFmtIdent table
<> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) <> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias)
where subquery = requestToQuery schema False (DbRead (Node n forst)) where subquery = readRequestToQuery schema False (Node n forst)
getQueryParts (Node n@(_, (name, Just Relation{relType=Parent,relTable=Table{tableName=table}}, alias, _, _)) forst) (j,s) = (joi:j,sel:s) getQueryParts (Node n@(_, (name, Just Relation{relType=Parent,relTable=Table{tableName=table}}, alias, _, _)) forst) (j,s) = (joi:j,sel:s)
where where
aliasOrName = fromMaybe name alias aliasOrName = fromMaybe name alias
localTableName = pgFmtIdent $ table <> "_" <> aliasOrName localTableName = pgFmtIdent $ table <> "_" <> aliasOrName
sel = "row_to_json(" <> localTableName <> ".*) AS " <> pgFmtIdent aliasOrName sel = "row_to_json(" <> localTableName <> ".*) AS " <> pgFmtIdent aliasOrName
joi = " LEFT JOIN LATERAL( " <> subquery <> " ) AS " <> localTableName <> " ON TRUE " joi = " LEFT JOIN LATERAL( " <> subquery <> " ) AS " <> localTableName <> " ON TRUE "
where subquery = requestToQuery schema True (DbRead (Node n forst)) where subquery = readRequestToQuery schema True (Node n forst)
getQueryParts (Node n@(_, (name, Just Relation{relType=Many,relTable=Table{tableName=table}}, alias, _, _)) forst) (j,s) = (j,sel:s) getQueryParts (Node n@(_, (name, Just Relation{relType=Many,relTable=Table{tableName=table}}, alias, _, _)) forst) (j,s) = (j,sel:s)
where where
sel = "COALESCE ((" sel = "COALESCE (("
<> "SELECT json_agg(" <> pgFmtIdent table <> ".*) " <> "SELECT json_agg(" <> pgFmtIdent table <> ".*) "
<> "FROM (" <> subquery <> ") " <> pgFmtIdent table <> "FROM (" <> subquery <> ") " <> pgFmtIdent table
<> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) <> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias)
where subquery = requestToQuery schema False (DbRead (Node n forst)) where subquery = readRequestToQuery schema False (Node n forst)
--the following is just to remove the warning --the following is just to remove the warning
--getQueryParts is not total but requestToQuery is called only after addJoinConditions which ensures the only --getQueryParts is not total but readRequestToQuery is called only after addJoinConditions which ensures the only
--posible relations are Child Parent Many --posible relations are Child Parent Many
getQueryParts _ _ = witness getQueryParts _ _ = witness
requestToQuery schema _ (DbMutate (Insert mainTbl iCols onConflct putConditions returnings)) =
mutateRequestToQuery :: Schema -> MutateRequest -> SqlQuery
mutateRequestToQuery schema (Insert mainTbl iCols onConflct putConditions returnings) =
unwords [ unwords [
"WITH " <> normalizedBody, "WITH " <> normalizedBody,
"INSERT INTO ", fromQi qi, if S.null iCols then " " else "(" <> cols <> ")", "INSERT INTO ", fromQi qi, if S.null iCols then " " else "(" <> cols <> ")",
@@ -100,7 +104,7 @@ requestToQuery schema _ (DbMutate (Insert mainTbl iCols onConflct putConditions
where where
qi = QualifiedIdentifier schema mainTbl qi = QualifiedIdentifier schema mainTbl
cols = intercalate ", " $ pgFmtIdent <$> S.toList iCols cols = intercalate ", " $ pgFmtIdent <$> S.toList iCols
requestToQuery schema _ (DbMutate (Update mainTbl uCols logicForest returnings)) = mutateRequestToQuery schema (Update mainTbl uCols logicForest returnings) =
if S.null uCols if S.null uCols
then "WITH " <> ignoredBody <> "SELECT null WHERE false" -- if there are no columns we cannot do UPDATE table SET {empty}, it'd be invalid syntax then "WITH " <> ignoredBody <> "SELECT null WHERE false" -- if there are no columns we cannot do UPDATE table SET {empty}, it'd be invalid syntax
else else
@@ -114,7 +118,7 @@ requestToQuery schema _ (DbMutate (Update mainTbl uCols logicForest returnings))
where where
qi = QualifiedIdentifier schema mainTbl qi = QualifiedIdentifier schema mainTbl
cols = intercalate ", " (pgFmtIdent <> const " = _." <> pgFmtIdent <$> S.toList uCols) cols = intercalate ", " (pgFmtIdent <> const " = _." <> pgFmtIdent <$> S.toList uCols)
requestToQuery schema _ (DbMutate (Delete mainTbl logicForest returnings)) = mutateRequestToQuery schema (Delete mainTbl logicForest returnings) =
unwords [ unwords [
"WITH " <> ignoredBody, "WITH " <> ignoredBody,
"DELETE FROM ", fromQi qi, "DELETE FROM ", fromQi qi,
@@ -168,13 +172,12 @@ requestToCallProcQuery qi pgArgs returnsScalar preferParams =
callIt = fromQi qi <> "(" <> args <> ")" callIt = fromQi qi <> "(" <> args <> ")"
-- | SQL query meant for COUNTing the root node of the DbRead Tree. -- | SQL query meant for COUNTing the root node of the Tree.
-- It only takes WHERE into account and doesn't include LIMIT/OFFSET because it would reduce the COUNT. -- It only takes WHERE into account and doesn't include LIMIT/OFFSET because it would reduce the COUNT.
-- SELECT 1 is done instead of SELECT * to prevent doing expensive operations(like functions based on the columns) -- SELECT 1 is done instead of SELECT * to prevent doing expensive operations(like functions based on the columns)
-- inside the FROM target. -- inside the FROM target.
requestToCountQuery :: Schema -> DbRequest -> SqlQuery readRequestToCountQuery :: Schema -> ReadRequest -> SqlQuery
requestToCountQuery _ (DbMutate _) = witness readRequestToCountQuery schema (Node (Select{where_=logicForest}, (mainTbl, _, _, _, _)) _) =
requestToCountQuery schema (DbRead (Node (Select{where_=logicForest}, (mainTbl, _, _, _, _)) _)) =
unwords [ unwords [
"SELECT 1", "SELECT 1",
"FROM " <> fromQi qi, "FROM " <> fromQi qi,
+3 -4
View File
@@ -428,13 +428,12 @@ data MutateQuery =
, returning :: [FieldName] , returning :: [FieldName]
} deriving (Show, Eq) } deriving (Show, Eq)
data DbRequest = DbRead ReadRequest | DbMutate MutateRequest
type ReadRequest = Tree ReadNode type ReadRequest = Tree ReadNode
type ReadNode = (ReadQuery, (NodeName, Maybe Relation, Maybe Alias, Maybe RelationDetail, Depth))
-- Depth of the ReadRequest tree
type Depth = Integer
type MutateRequest = MutateQuery type MutateRequest = MutateQuery
type ReadNode = (ReadQuery, (NodeName, Maybe Relation, Maybe Alias, Maybe RelationDetail, Depth))
type Depth = Integer
fieldNames :: ReadRequest -> [FieldName] fieldNames :: ReadRequest -> [FieldName]
fieldNames (Node (sel, _) forest) = fieldNames (Node (sel, _) forest) =
map (fst . view _1) (select sel) ++ map colName fks map (fst . view _1) (select sel) ++ map colName fks