2020-10-29 19:58:13 +03:00
|
|
|
module Hasura.Backends.Postgres.Translate.Update
|
|
|
|
( mkUpdateCTE
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
2020-11-12 12:25:48 +03:00
|
|
|
import qualified Hasura.Backends.Postgres.SQL.DML 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
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.RQL.IR.Update
|
|
|
|
import Hasura.RQL.Types
|
2020-12-01 18:50:18 +03:00
|
|
|
import Hasura.SQL.Types
|
2020-10-29 19:58:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
mkUpdateCTE
|
2021-04-22 00:44:37 +03:00
|
|
|
:: Backend ('Postgres pgKind)
|
|
|
|
=> AnnUpd ('Postgres pgKind)
|
|
|
|
-> S.CTE
|
2020-10-29 19:58:13 +03:00
|
|
|
mkUpdateCTE (AnnUpd tn opExps (permFltr, wc) chk _ columnsInfo) =
|
|
|
|
S.CTEUpdate update
|
|
|
|
where
|
|
|
|
update =
|
|
|
|
S.SQLUpdate tn setExp Nothing tableFltr
|
|
|
|
. Just
|
|
|
|
. S.RetExp
|
|
|
|
$ [ S.selectStar
|
2020-11-12 12:25:48 +03:00
|
|
|
, asCheckErrorExtractor $ insertCheckConstraint checkExpr
|
2020-10-29 19:58:13 +03:00
|
|
|
]
|
|
|
|
setExp = S.SetExp $ map (expandOperator columnsInfo) opExps
|
|
|
|
tableFltr = Just $ S.WhereFrag tableFltrExpr
|
|
|
|
tableFltrExpr = toSQLBoolExp (S.QualTable tn) $ andAnnBoolExps permFltr wc
|
|
|
|
checkExpr = toSQLBoolExp (S.QualTable tn) chk
|
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
expandOperator :: [ColumnInfo ('Postgres pgKind)] -> (PGCol, UpdOpExpG S.SQLExp) -> S.SetExpItem
|
2020-10-29 19:58:13 +03:00
|
|
|
expandOperator infos (column, op) = S.SetExpItem $ (column,) $ case op of
|
|
|
|
UpdSet e -> e
|
|
|
|
UpdInc e -> S.mkSQLOpExp S.incOp identifier (asNum e)
|
|
|
|
UpdAppend e -> S.mkSQLOpExp S.jsonbConcatOp identifier (asJSON e)
|
|
|
|
UpdPrepend e -> S.mkSQLOpExp S.jsonbConcatOp (asJSON e) identifier
|
|
|
|
UpdDeleteKey e -> S.mkSQLOpExp S.jsonbDeleteOp identifier (asText e)
|
|
|
|
UpdDeleteElem e -> S.mkSQLOpExp S.jsonbDeleteOp identifier (asInt e)
|
|
|
|
UpdDeleteAtPath a -> S.mkSQLOpExp S.jsonbDeleteAtPathOp identifier (asArray a)
|
|
|
|
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 $
|
|
|
|
case find (\info -> pgiColumn info == column) infos <&> pgiType 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
|