2022-02-08 12:24:34 +03:00
|
|
|
-- | Postgres Translate Update
|
|
|
|
--
|
|
|
|
-- Translates IR update to Postgres-specific SQL UPDATE statements.
|
2020-10-29 19:58:13 +03:00
|
|
|
module Hasura.Backends.Postgres.Translate.Update
|
|
|
|
( mkUpdateCTE,
|
2022-07-18 18:15:34 +03:00
|
|
|
UpdateCTE (..),
|
2020-10-29 19:58:13 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2021-11-18 21:02:58 +03:00
|
|
|
import Data.HashMap.Strict qualified as Map
|
2020-11-12 12:25:48 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
2020-11-02 14:50:40 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.BoolExp
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Insert
|
2020-11-12 12:25:48 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Returning
|
2021-11-18 21:02:58 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Update
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.Prelude
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.IR.BoolExp
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.RQL.IR.Update
|
2023-01-10 04:54:40 +03:00
|
|
|
import Hasura.RQL.IR.Update.Batch
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.SQL.Backend
|
2020-12-01 18:50:18 +03:00
|
|
|
import Hasura.SQL.Types
|
2020-10-29 19:58:13 +03:00
|
|
|
|
2022-07-18 18:15:34 +03:00
|
|
|
data UpdateCTE
|
|
|
|
= -- | Used for /update_table/ and /update_table_by_pk/.
|
|
|
|
Update S.TopLevelCTE
|
|
|
|
| -- | Used for /update_table_many/.
|
|
|
|
MultiUpdate [S.TopLevelCTE]
|
2022-12-08 11:02:44 +03:00
|
|
|
deriving (Show)
|
2022-07-18 18:15:34 +03:00
|
|
|
|
|
|
|
-- | Create the update CTE.
|
2020-10-29 19:58:13 +03:00
|
|
|
mkUpdateCTE ::
|
2022-07-18 18:15:34 +03:00
|
|
|
forall pgKind.
|
2021-04-22 00:44:37 +03:00
|
|
|
Backend ('Postgres pgKind) =>
|
2021-11-25 00:39:42 +03:00
|
|
|
AnnotatedUpdate ('Postgres pgKind) ->
|
2022-07-18 18:15:34 +03:00
|
|
|
UpdateCTE
|
2023-01-10 04:54:40 +03:00
|
|
|
mkUpdateCTE (AnnotatedUpdateG tn permFltr chk updateVariant _ columnsInfo _tCase) =
|
|
|
|
case updateVariant of
|
|
|
|
SingleBatch update ->
|
|
|
|
Update $ translateUpdate update
|
|
|
|
MultipleBatches updates ->
|
|
|
|
MultiUpdate $ translateUpdate <$> updates
|
|
|
|
where
|
|
|
|
mkWhere :: AnnBoolExp ('Postgres pgKind) S.SQLExp -> Maybe S.WhereFrag
|
|
|
|
mkWhere =
|
|
|
|
Just
|
|
|
|
. S.WhereFrag
|
|
|
|
. S.simplifyBoolExp
|
|
|
|
. toSQLBoolExp (S.QualTable tn)
|
|
|
|
. andAnnBoolExps permFltr
|
|
|
|
|
|
|
|
checkConstraint :: Maybe S.RetExp
|
|
|
|
checkConstraint =
|
|
|
|
Just $
|
|
|
|
S.RetExp
|
|
|
|
[ S.selectStar,
|
|
|
|
asCheckErrorExtractor
|
|
|
|
. insertCheckConstraint
|
|
|
|
. toSQLBoolExp (S.QualTable tn)
|
|
|
|
$ chk
|
|
|
|
]
|
|
|
|
|
|
|
|
translateUpdate :: UpdateBatch ('Postgres pgKind) UpdateOpExpression S.SQLExp -> S.TopLevelCTE
|
|
|
|
translateUpdate UpdateBatch {..} =
|
|
|
|
S.CTEUpdate
|
|
|
|
S.SQLUpdate
|
|
|
|
{ upTable = tn,
|
|
|
|
upSet =
|
|
|
|
S.SetExp $ map (expandOperator columnsInfo) (Map.toList _ubOperations),
|
|
|
|
upFrom = Nothing,
|
|
|
|
upWhere = mkWhere _ubWhere,
|
|
|
|
upRet = checkConstraint
|
|
|
|
}
|
2020-10-29 19:58:13 +03:00
|
|
|
|
2021-11-25 00:39:42 +03:00
|
|
|
expandOperator :: [ColumnInfo ('Postgres pgKind)] -> (PGCol, UpdateOpExpression S.SQLExp) -> S.SetExpItem
|
2020-10-29 19:58:13 +03:00
|
|
|
expandOperator infos (column, op) = S.SetExpItem $
|
|
|
|
(column,) $ case op of
|
2021-11-25 00:39:42 +03:00
|
|
|
UpdateSet e -> e
|
|
|
|
UpdateInc e -> S.mkSQLOpExp S.incOp identifier (asNum e)
|
|
|
|
UpdateAppend e -> S.mkSQLOpExp S.jsonbConcatOp identifier (asJSON e)
|
|
|
|
UpdatePrepend e -> S.mkSQLOpExp S.jsonbConcatOp (asJSON e) identifier
|
|
|
|
UpdateDeleteKey e -> S.mkSQLOpExp S.jsonbDeleteOp identifier (asText e)
|
|
|
|
UpdateDeleteElem e -> S.mkSQLOpExp S.jsonbDeleteOp identifier (asInt e)
|
|
|
|
UpdateDeleteAtPath a -> S.mkSQLOpExp S.jsonbDeleteAtPathOp identifier (asArray a)
|
2020-10-29 19:58:13 +03:00
|
|
|
where
|
|
|
|
identifier = S.SEIdentifier $ toIdentifier column
|
|
|
|
asInt e = S.SETyAnn e S.intTypeAnn
|
|
|
|
asText e = S.SETyAnn e S.textTypeAnn
|
|
|
|
asJSON e = S.SETyAnn e S.jsonbTypeAnn
|
|
|
|
asArray a = S.SETyAnn (S.SEArray a) S.textArrTypeAnn
|
|
|
|
asNum e = S.SETyAnn e $
|
2022-01-19 11:37:50 +03:00
|
|
|
case find (\info -> ciColumn info == column) infos <&> ciType of
|
2020-12-01 18:50:18 +03:00
|
|
|
Just (ColumnScalar s) -> S.mkTypeAnn $ CollectableTypeScalar s
|
2020-11-25 17:18:58 +03:00
|
|
|
_ -> S.numericTypeAnn
|