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,
|
|
|
|
)
|
|
|
|
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
|
|
|
|
import Hasura.RQL.IR.Update
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
2020-10-29 19:58:13 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
mkUpdateCTE ::
|
|
|
|
Backend ('Postgres pgKind) =>
|
2021-11-25 00:39:42 +03:00
|
|
|
AnnotatedUpdate ('Postgres pgKind) ->
|
2021-09-24 01:56:37 +03:00
|
|
|
S.CTE
|
2021-11-25 00:39:42 +03:00
|
|
|
mkUpdateCTE (AnnotatedUpdateG tn (permFltr, wc) chk (BackendUpdate opExps) _ columnsInfo) =
|
2020-10-29 19:58:13 +03:00
|
|
|
S.CTEUpdate update
|
|
|
|
where
|
|
|
|
update =
|
|
|
|
S.SQLUpdate tn setExp Nothing tableFltr
|
|
|
|
. Just
|
|
|
|
. S.RetExp
|
2021-09-24 01:56:37 +03:00
|
|
|
$ [ S.selectStar,
|
|
|
|
asCheckErrorExtractor $ insertCheckConstraint checkExpr
|
2020-10-29 19:58:13 +03:00
|
|
|
]
|
2021-11-18 21:02:58 +03:00
|
|
|
setExp = S.SetExp $ map (expandOperator columnsInfo) (Map.toList opExps)
|
2020-10-29 19:58:13 +03:00
|
|
|
tableFltr = Just $ S.WhereFrag tableFltrExpr
|
|
|
|
tableFltrExpr = toSQLBoolExp (S.QualTable tn) $ andAnnBoolExps permFltr wc
|
|
|
|
checkExpr = toSQLBoolExp (S.QualTable tn) chk
|
|
|
|
|
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
|