mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-18 13:02:11 +03:00
1afa4ac3cc
Co-authored-by: Vladimir Ciobanu <1017953+vladciobanu@users.noreply.github.com> Co-authored-by: Antoine Leblanc <1618949+nicuveo@users.noreply.github.com> Co-authored-by: Ikechukwu Eze <22247592+iykekings@users.noreply.github.com> Co-authored-by: Philip Lykke Carlsen <358550+plcplc@users.noreply.github.com> Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com> GitOrigin-RevId: 1b964fe5f5f50380172cb702b6a328fed782b6b7
63 lines
2.3 KiB
Haskell
63 lines
2.3 KiB
Haskell
module Hasura.Backends.Postgres.Translate.Mutation
|
|
( mkSelectExpFromColumnValues
|
|
) where
|
|
|
|
import Hasura.Prelude
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import Data.Text.Extended
|
|
|
|
import qualified Hasura.Backends.Postgres.SQL.DML 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.Base.Instances ()
|
|
import Hasura.RQL.Types
|
|
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) (S.Alias rowAlias) Nothing
|
|
return S.mkSelect
|
|
{ S.selExtr = [extractor]
|
|
, S.selFrom = Just $ S.FromExp [fromItem]
|
|
}
|
|
where
|
|
rowAlias = Identifier "row"
|
|
extractor = S.selectStar' $ S.QualifiedIdentifier rowAlias $ Just $ S.TypeAnn $ toSQLTxt qt
|
|
sortedCols = sortCols allCols
|
|
mkTupsFromColVal colVal =
|
|
fmap S.TupleExp $ forM sortedCols $ \ci -> do
|
|
let pgCol = pgiColumn ci
|
|
val <- onNothing (Map.lookup pgCol colVal) $
|
|
throw500 $ "column " <> pgCol <<> " not found in returning values"
|
|
pure $ txtEncodedToSQLExp (pgiType 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 ->
|
|
S.withTyAnn (unsafePGColumnToBackend colTy) $ S.SELit textValue
|