diff --git a/src/PostgREST/Plan/Types.hs b/src/PostgREST/Plan/Types.hs index 1c3b722a6f..8e4a41f156 100644 --- a/src/PostgREST/Plan/Types.hs +++ b/src/PostgREST/Plan/Types.hs @@ -1,14 +1,11 @@ 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 (..)) @@ -16,16 +13,12 @@ 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 diff --git a/src/PostgREST/Query/QueryBuilder.hs b/src/PostgREST/Query/QueryBuilder.hs index d861c4424d..9618495a42 100644 --- a/src/PostgREST/Query/QueryBuilder.hs +++ b/src/PostgREST/Query/QueryBuilder.hs @@ -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) @@ -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) diff --git a/src/PostgREST/Query/SqlFragment.hs b/src/PostgREST/Query/SqlFragment.hs index 1ec41ffa05..8252fd22b4 100644 --- a/src/PostgREST/Query/SqlFragment.hs +++ b/src/PostgREST/Query/SqlFragment.hs @@ -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 =