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
|
2021-09-24 01:56:37 +03:00
|
|
|
( mkUpdateCTE,
|
2022-07-18 18:15:34 +03:00
|
|
|
UpdateCTE (..),
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
|
|
|
where
|
2020-10-29 19:58:13 +03:00
|
|
|
|
2021-11-18 21:02:58 +03:00
|
|
|
import Data.HashMap.Strict qualified as Map
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
import Hasura.Backends.Postgres.Translate.BoolExp
|
|
|
|
import Hasura.Backends.Postgres.Translate.Insert
|
|
|
|
import Hasura.Backends.Postgres.Translate.Returning
|
2021-11-18 21:02:58 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Update
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Prelude
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.IR.BoolExp
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.IR.Update
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.SQL.Backend
|
2021-09-24 01:56:37 +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]
|
|
|
|
|
|
|
|
-- | Create the update CTE.
|
2021-09-24 01:56:37 +03:00
|
|
|
mkUpdateCTE ::
|
2022-07-18 18:15:34 +03:00
|
|
|
forall pgKind.
|
2021-09-24 01:56: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
|
2022-07-19 09:55:42 +03:00
|
|
|
mkUpdateCTE (AnnotatedUpdateG tn (permFltr, wc) chk backendUpdate _ columnsInfo _tCase) =
|
2022-07-18 18:15:34 +03:00
|
|
|
case backendUpdate of
|
|
|
|
BackendUpdate opExps ->
|
|
|
|
Update $ S.CTEUpdate update
|
|
|
|
where
|
|
|
|
update =
|
|
|
|
S.SQLUpdate
|
|
|
|
{ upTable = tn,
|
|
|
|
upSet =
|
|
|
|
S.SetExp $ map (expandOperator columnsInfo) (Map.toList opExps),
|
|
|
|
upFrom = Nothing,
|
|
|
|
upWhere =
|
|
|
|
Just
|
|
|
|
. S.WhereFrag
|
2022-08-06 00:40:41 +03:00
|
|
|
. S.simplifyBoolExp
|
2022-07-18 18:15:34 +03:00
|
|
|
. toSQLBoolExp (S.QualTable tn)
|
|
|
|
$ andAnnBoolExps permFltr wc,
|
|
|
|
upRet =
|
|
|
|
Just $
|
|
|
|
S.RetExp
|
|
|
|
[ S.selectStar,
|
|
|
|
asCheckErrorExtractor $
|
|
|
|
insertCheckConstraint $
|
|
|
|
toSQLBoolExp (S.QualTable tn) chk
|
|
|
|
]
|
|
|
|
}
|
|
|
|
BackendMultiRowUpdate updates ->
|
|
|
|
MultiUpdate $ translateUpdate <$> updates
|
|
|
|
where
|
|
|
|
translateUpdate :: MultiRowUpdate pgKind S.SQLExp -> S.TopLevelCTE
|
|
|
|
translateUpdate MultiRowUpdate {..} =
|
|
|
|
S.CTEUpdate
|
|
|
|
S.SQLUpdate
|
|
|
|
{ upTable = tn,
|
|
|
|
upSet =
|
|
|
|
S.SetExp $ map (expandOperator columnsInfo) (Map.toList mruExpression),
|
|
|
|
upFrom = Nothing,
|
|
|
|
upWhere =
|
2022-08-06 00:40:41 +03:00
|
|
|
Just
|
|
|
|
. S.WhereFrag
|
|
|
|
. S.simplifyBoolExp
|
|
|
|
$ toSQLBoolExp (S.QualTable tn) mruWhere,
|
2022-07-18 18:15:34 +03:00
|
|
|
upRet =
|
|
|
|
Just $
|
|
|
|
S.RetExp
|
|
|
|
[ S.selectStar,
|
|
|
|
asCheckErrorExtractor
|
|
|
|
. insertCheckConstraint
|
|
|
|
$ toSQLBoolExp (S.QualTable tn) chk
|
|
|
|
]
|
|
|
|
}
|
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
|
2021-09-24 01:56:37 +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
|
2021-09-24 01:56:37 +03:00
|
|
|
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
|
2021-09-24 01:56:37 +03:00
|
|
|
_ -> S.numericTypeAnn
|