graphql-engine/server/src-lib/Hasura/Backends/Postgres/Translate/Mutation.hs
Samir Talwar 342391f39d Upgrade Ormolu to v0.5.
This upgrades the version of Ormolu required by the HGE repository to v0.5.0.1, and reformats all code accordingly.

Ormolu v0.5 reformats code that uses infix operators. This is mostly useful, adding newlines and indentation to make it clear which operators are applied first, but in some cases, it's unpleasant. To make this easier on the eyes, I had to do the following:

* Add a few fixity declarations (search for `infix`)
* Add parentheses to make precedence clear, allowing Ormolu to keep everything on one line
* Rename `relevantEq` to `(==~)` in #6651 and set it to `infix 4`
* Add a few _.ormolu_ files (thanks to @hallettj for helping me get started), mostly for Autodocodec operators that don't have explicit fixity declarations

In general, I think these changes are quite reasonable. They mostly affect indentation.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6675
GitOrigin-RevId: cd47d87f1d089fb0bc9dcbbe7798dbceedcd7d83
2022-11-02 20:55:13 +00:00

73 lines
2.5 KiB
Haskell

-- | Postgres Translate Mutation
--
-- Provide a combinator for generating a Postgres SQL SELECT statement for the
-- selected columns in mutation queries.
--
-- See 'Hasura.Backends.Postgres.Execute.Mutation' and note
-- [Prepared statements in Mutations]
module Hasura.Backends.Postgres.Translate.Mutation
( mkSelectExpFromColumnValues,
)
where
import Data.HashMap.Strict qualified as Map
import Data.Text.Extended
import Hasura.Backends.Postgres.SQL.DML qualified as S
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Backends.Postgres.SQL.Value
import Hasura.Backends.Postgres.Types.Column
import Hasura.Base.Error
import Hasura.Prelude
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Table
import Hasura.SQL.Backend
import Hasura.SQL.Types
-- | Note:- Using sorted columns is necessary to enable casting the rows returned by VALUES expression to table type.
-- For example, let's consider the table, `CREATE TABLE test (id serial primary key, name text not null, age int)`.
-- The generated values expression should be in order of columns;
-- `SELECT ("row"::table).* VALUES (1, 'Robert', 23) AS "row"`.
mkSelectExpFromColumnValues ::
forall pgKind m.
MonadError QErr m =>
QualifiedTable ->
[ColumnInfo ('Postgres pgKind)] ->
[ColumnValues ('Postgres pgKind) TxtEncodedVal] ->
m S.Select
mkSelectExpFromColumnValues qt allCols = \case
[] -> return selNoRows
colVals -> do
tuples <- mapM mkTupsFromColVal colVals
let fromItem = S.FIValues (S.ValuesExp tuples) rowAlias Nothing
return
S.mkSelect
{ S.selExtr = [extractor],
S.selFrom = Just $ S.FromExp [fromItem]
}
where
rowAlias = S.mkTableAlias "row"
rowIdentifier = S.tableAliasToIdentifier rowAlias
extractor = S.selectStar' $ S.QualifiedIdentifier rowIdentifier $ Just $ S.TypeAnn $ toSQLTxt qt
sortedCols = sortCols allCols
mkTupsFromColVal colVal =
fmap S.TupleExp $
forM sortedCols $ \ci -> do
let pgCol = ciColumn ci
val <-
onNothing (Map.lookup pgCol colVal) $
throw500 $
"column " <> pgCol <<> " not found in returning values"
pure $ txtEncodedToSQLExp (ciType ci) val
selNoRows =
S.mkSelect
{ S.selExtr = [S.selectStar],
S.selFrom = Just $ S.mkSimpleFromExp qt,
S.selWhere = Just $ S.WhereFrag $ S.BELit False
}
txtEncodedToSQLExp colTy = \case
TENull -> S.SENull
TELit textValue ->
withScalarTypeAnn (unsafePGColumnToBackend colTy) $ S.SELit textValue