Skip to content

Commit

Permalink
Simplify as per PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljungberg committed Nov 28, 2022
1 parent 0e16f43 commit 51c13dc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
13 changes: 3 additions & 10 deletions src/PostgREST/Plan/Types.hs
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
module PostgREST.Plan.Types
( TypedField(..)
, resolveField
, resolveTableField

) where

import qualified Data.HashMap.Strict.InsOrd as HMI

import PostgREST.ApiRequest.Types (Field, JsonPath)

import PostgREST.SchemaCache.Identifiers (FieldName)
import PostgREST.SchemaCache.Table (Column (..), Table (..))

import Protolude

-- | A TypedField is a field with sufficient information to be read from JSON with `json_to_recordset`.
data TypedField = TypedField
{ tfFieldName :: FieldName
, tfJsonPath :: JsonPath
, tfIRType :: Text
{ tfName :: FieldName
, tfIRType :: Text -- ^ The initial type of the field, before any casting.
} deriving (Eq)

resolveField :: Field -> Text -> TypedField
resolveField (fieldName, jsonPath) = TypedField fieldName jsonPath

resolveTableField :: Table -> FieldName -> Maybe TypedField
resolveTableField table fieldName =
case HMI.lookup fieldName (tableColumns table) of
Just column -> Just $ resolveField (colName column, []) (colNominalType column)
Just column -> Just $ TypedField (colName column) (colNominalType column)
Nothing -> Nothing
8 changes: 4 additions & 4 deletions src/PostgREST/Query/QueryBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ mutatePlanToQuery (Insert mainQi iCols body onConflct putConditions returnings _
MergeDuplicates ->
if null iCols
then "DO NOTHING"
else "DO UPDATE SET " <> BS.intercalate ", " ((pgFmtIdent . tfFieldName) <> const " = EXCLUDED." <> (pgFmtIdent . tfFieldName) <$> iCols)
else "DO UPDATE SET " <> BS.intercalate ", " ((pgFmtIdent . tfName) <> const " = EXCLUDED." <> (pgFmtIdent . tfName) <$> iCols)
) onConflct,
returningF mainQi returnings
])
where
cols = BS.intercalate ", " $ pgFmtIdent . tfFieldName <$> iCols
cols = BS.intercalate ", " $ pgFmtIdent . tfName <$> iCols

-- An update without a limit is always filtered with a WHERE
mutatePlanToQuery (Update mainQi uCols body logicForest range ordts returnings)
Expand Down Expand Up @@ -138,8 +138,8 @@ mutatePlanToQuery (Update mainQi uCols body logicForest range ordts returnings)
whereLogic = if null logicForest then mempty else " WHERE " <> intercalateSnippet " AND " (pgFmtLogicTree mainQi <$> logicForest)
mainTbl = SQL.sql (fromQi mainQi)
emptyBodyReturnedColumns = if null returnings then "NULL" else BS.intercalate ", " (pgFmtColumn (QualifiedIdentifier mempty $ qiName mainQi) <$> returnings)
nonRangeCols = BS.intercalate ", " (pgFmtIdent . tfFieldName <> const " = _." <> pgFmtIdent . tfFieldName <$> uCols)
rangeCols = BS.intercalate ", " ((\col -> pgFmtIdent (tfFieldName col) <> " = (SELECT " <> pgFmtIdent (tfFieldName col) <> " FROM pgrst_update_body) ") <$> uCols)
nonRangeCols = BS.intercalate ", " (pgFmtIdent . tfName <> const " = _." <> pgFmtIdent . tfName <$> uCols)
rangeCols = BS.intercalate ", " ((\col -> pgFmtIdent (tfName col) <> " = (SELECT " <> pgFmtIdent (tfName col) <> " FROM pgrst_update_body) ") <$> uCols)
(whereRangeIdF, rangeIdF) = mutRangeF mainQi (fst . otTerm <$> ordts)

mutatePlanToQuery (Delete mainQi logicForest range ordts returnings)
Expand Down
4 changes: 2 additions & 2 deletions src/PostgREST/Query/SqlFragment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ pgFmtSelectFromJson fields =
else SQL.sql ("FROM json_to_recordset (" <> selectBody <> ") AS _ " <> "(" <> typedCols <> ") ")
)
where
parsedCols = SQL.sql $ BS.intercalate ", " $ pgFmtIdent . tfFieldName <$> fields
typedCols = BS.intercalate ", " $ pgFmtIdent . tfFieldName <> const " " <> encodeUtf8 . tfIRType <$> fields
parsedCols = SQL.sql $ BS.intercalate ", " $ pgFmtIdent . tfName <$> fields
typedCols = BS.intercalate ", " $ pgFmtIdent . tfName <> const " " <> encodeUtf8 . tfIRType <$> fields

pgFmtOrderTerm :: QualifiedIdentifier -> OrderTerm -> SQL.Snippet
pgFmtOrderTerm qi ot =
Expand Down

0 comments on commit 51c13dc

Please sign in to comment.