2021-02-23 20:37:27 +03:00
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2021-04-22 00:44:37 +03:00
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
2021-05-21 05:46:58 +03:00
|
|
|
|
module Hasura.Backends.Postgres.Instances.Schema
|
|
|
|
|
(
|
|
|
|
|
) where
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
2021-06-10 19:13:20 +03:00
|
|
|
|
import qualified Data.Aeson as J
|
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
|
import qualified Data.HashMap.Strict.Extended as M
|
|
|
|
|
import qualified Data.HashMap.Strict.InsOrd.Extended as OMap
|
|
|
|
|
import qualified Data.List.NonEmpty as NE
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
|
import qualified Language.GraphQL.Draft.Syntax as G
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
2021-04-08 11:25:11 +03:00
|
|
|
|
import Data.Has
|
2021-02-23 20:37:27 +03:00
|
|
|
|
import Data.Parser.JSONPath
|
|
|
|
|
import Data.Text.Extended
|
2021-04-22 00:44:37 +03:00
|
|
|
|
import Data.Typeable
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
2021-06-10 19:13:20 +03:00
|
|
|
|
import qualified Hasura.GraphQL.Parser as P
|
|
|
|
|
import qualified Hasura.GraphQL.Schema.Backend as BS
|
|
|
|
|
import qualified Hasura.GraphQL.Schema.Build as GSB
|
|
|
|
|
import qualified Hasura.RQL.IR.Select as IR
|
|
|
|
|
import qualified Hasura.RQL.IR.Update as IR
|
|
|
|
|
import qualified Hasura.SQL.AnyBackend as AB
|
2021-03-25 20:50:08 +03:00
|
|
|
|
|
2021-06-10 19:13:20 +03:00
|
|
|
|
import Hasura.Backends.Postgres.SQL.DML as PG hiding (CountType)
|
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types as PG hiding (FunctionName, TableName)
|
|
|
|
|
import Hasura.Backends.Postgres.SQL.Value as PG
|
2021-03-25 20:50:08 +03:00
|
|
|
|
import Hasura.Backends.Postgres.Types.BoolExp
|
|
|
|
|
import Hasura.Backends.Postgres.Types.Column
|
2021-05-11 18:18:31 +03:00
|
|
|
|
import Hasura.Base.Error
|
2021-06-10 19:13:20 +03:00
|
|
|
|
import Hasura.GraphQL.Parser hiding (EnumValueInfo, field)
|
|
|
|
|
import Hasura.GraphQL.Parser.Internal.Parser hiding (field)
|
|
|
|
|
import Hasura.GraphQL.Parser.Internal.TypeChecking
|
|
|
|
|
import Hasura.GraphQL.Schema.Backend (BackendSchema, ComparisonExp,
|
|
|
|
|
MonadBuildSchema)
|
2021-04-08 11:25:11 +03:00
|
|
|
|
import Hasura.GraphQL.Schema.BoolExp
|
2021-02-23 20:37:27 +03:00
|
|
|
|
import Hasura.GraphQL.Schema.Common
|
|
|
|
|
import Hasura.GraphQL.Schema.Select
|
|
|
|
|
import Hasura.GraphQL.Schema.Table
|
2021-06-11 06:26:50 +03:00
|
|
|
|
import Hasura.RQL.IR
|
2021-02-23 20:37:27 +03:00
|
|
|
|
import Hasura.RQL.Types
|
2021-04-22 00:44:37 +03:00
|
|
|
|
import Hasura.SQL.Tag
|
2021-02-23 20:37:27 +03:00
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
-- BackendSchema instance
|
|
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
|
-- | This class is an implementation detail of 'BackendSchema'.
|
|
|
|
|
-- Some functions of 'BackendSchema' differ across different Postgres "kinds",
|
|
|
|
|
-- or call to functions (such as those related to Relay) that have not been
|
|
|
|
|
-- generalized to all kinds of Postgres and still explicitly work on Vanilla
|
|
|
|
|
-- Postgres. This class alllows each "kind" to specify its own specific
|
|
|
|
|
-- implementation. All common code is directly part of `BackendSchema`.
|
|
|
|
|
class PostgresSchema (pgKind :: PostgresKind) where
|
|
|
|
|
pgkBuildTableRelayQueryFields
|
|
|
|
|
:: BS.MonadBuildSchema ('Postgres pgKind) r m n
|
|
|
|
|
=> SourceName
|
|
|
|
|
-> SourceConfig ('Postgres pgKind)
|
|
|
|
|
-> TableName ('Postgres pgKind)
|
|
|
|
|
-> TableInfo ('Postgres pgKind)
|
|
|
|
|
-> G.Name
|
|
|
|
|
-> NESeq (ColumnInfo ('Postgres pgKind))
|
|
|
|
|
-> SelPermInfo ('Postgres pgKind)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
|
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
|
2021-04-22 00:44:37 +03:00
|
|
|
|
pgkBuildFunctionRelayQueryFields
|
|
|
|
|
:: BS.MonadBuildSchema ('Postgres pgKind) r m n
|
|
|
|
|
=> SourceName
|
|
|
|
|
-> SourceConfig ('Postgres pgKind)
|
|
|
|
|
-> FunctionName ('Postgres pgKind)
|
|
|
|
|
-> FunctionInfo ('Postgres pgKind)
|
|
|
|
|
-> TableName ('Postgres pgKind)
|
|
|
|
|
-> NESeq (ColumnInfo ('Postgres pgKind))
|
|
|
|
|
-> SelPermInfo ('Postgres pgKind)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
|
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
|
2021-04-22 00:44:37 +03:00
|
|
|
|
pgkRelayExtension
|
2021-06-09 16:02:15 +03:00
|
|
|
|
:: Maybe (XRelay ('Postgres pgKind))
|
2021-04-22 00:44:37 +03:00
|
|
|
|
pgkNode
|
|
|
|
|
:: BS.MonadBuildSchema ('Postgres pgKind) r m n
|
|
|
|
|
=> m (Parser 'Output n
|
|
|
|
|
(HashMap
|
|
|
|
|
( TableName ('Postgres pgKind)
|
|
|
|
|
)
|
|
|
|
|
( SourceName
|
|
|
|
|
, SourceConfig ('Postgres pgKind)
|
|
|
|
|
, SelPermInfo ('Postgres pgKind)
|
|
|
|
|
, PrimaryKeyColumns ('Postgres pgKind)
|
|
|
|
|
, AnnotatedFields ('Postgres pgKind)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
instance PostgresSchema 'Vanilla where
|
|
|
|
|
pgkBuildTableRelayQueryFields = buildTableRelayQueryFields
|
|
|
|
|
pgkBuildFunctionRelayQueryFields = buildFunctionRelayQueryFields
|
2021-06-09 16:02:15 +03:00
|
|
|
|
pgkRelayExtension = Just ()
|
2021-04-22 00:44:37 +03:00
|
|
|
|
pgkNode = nodePG
|
|
|
|
|
|
2021-05-21 05:46:58 +03:00
|
|
|
|
instance PostgresSchema 'Citus where
|
2021-06-15 18:53:20 +03:00
|
|
|
|
pgkBuildTableRelayQueryFields _ _ _ _ _ _ _ = pure []
|
|
|
|
|
pgkBuildFunctionRelayQueryFields _ _ _ _ _ _ _ = pure []
|
2021-06-09 16:02:15 +03:00
|
|
|
|
pgkRelayExtension = Nothing
|
2021-05-21 05:46:58 +03:00
|
|
|
|
pgkNode = undefined
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- postgres schema
|
2021-04-22 00:44:37 +03:00
|
|
|
|
|
|
|
|
|
instance
|
|
|
|
|
( HasTag ('Postgres pgKind)
|
|
|
|
|
, Typeable ('Postgres pgKind)
|
2021-05-21 05:46:58 +03:00
|
|
|
|
, Backend ('Postgres pgKind)
|
2021-04-22 00:44:37 +03:00
|
|
|
|
, PostgresSchema pgKind
|
|
|
|
|
) => BackendSchema ('Postgres pgKind) where
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- top level parsers
|
|
|
|
|
buildTableQueryFields = GSB.buildTableQueryFields
|
2021-04-22 00:44:37 +03:00
|
|
|
|
buildTableRelayQueryFields = pgkBuildTableRelayQueryFields
|
2021-02-23 20:37:27 +03:00
|
|
|
|
buildTableInsertMutationFields = GSB.buildTableInsertMutationFields
|
|
|
|
|
buildTableUpdateMutationFields = GSB.buildTableUpdateMutationFields
|
|
|
|
|
buildTableDeleteMutationFields = GSB.buildTableDeleteMutationFields
|
|
|
|
|
buildFunctionQueryFields = GSB.buildFunctionQueryFields
|
2021-04-22 00:44:37 +03:00
|
|
|
|
buildFunctionRelayQueryFields = pgkBuildFunctionRelayQueryFields
|
2021-02-23 20:37:27 +03:00
|
|
|
|
buildFunctionMutationFields = GSB.buildFunctionMutationFields
|
2021-06-15 18:53:20 +03:00
|
|
|
|
|
|
|
|
|
-- table components
|
|
|
|
|
tableArguments = defaultTableArgs
|
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- backend extensions
|
2021-04-22 00:44:37 +03:00
|
|
|
|
relayExtension = pgkRelayExtension @pgKind
|
2021-06-09 16:02:15 +03:00
|
|
|
|
nodesAggExtension = Just ()
|
2021-06-15 18:53:20 +03:00
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- indivdual components
|
|
|
|
|
columnParser = columnParser
|
|
|
|
|
jsonPathArg = jsonPathArg
|
|
|
|
|
orderByOperators = orderByOperators
|
|
|
|
|
comparisonExps = comparisonExps
|
|
|
|
|
updateOperators = updateOperators
|
|
|
|
|
mkCountType = mkCountType
|
|
|
|
|
aggregateOrderByCountType = PG.PGInteger
|
|
|
|
|
computedField = computedFieldPG
|
2021-04-22 00:44:37 +03:00
|
|
|
|
node = pgkNode
|
2021-06-15 18:53:20 +03:00
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- SQL literals
|
|
|
|
|
columnDefaultValue = const PG.columnDefaultValue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
-- Top level parsers
|
|
|
|
|
|
|
|
|
|
buildTableRelayQueryFields
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: forall pgKind m n r
|
|
|
|
|
. MonadBuildSchema ('Postgres pgKind) r m n
|
2021-02-23 20:37:27 +03:00
|
|
|
|
=> SourceName
|
2021-04-22 00:44:37 +03:00
|
|
|
|
-> SourceConfig ('Postgres pgKind)
|
|
|
|
|
-> TableName ('Postgres pgKind)
|
|
|
|
|
-> TableInfo ('Postgres pgKind)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-> G.Name
|
2021-04-22 00:44:37 +03:00
|
|
|
|
-> NESeq (ColumnInfo ('Postgres pgKind))
|
|
|
|
|
-> SelPermInfo ('Postgres pgKind)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
|
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
|
2021-02-23 20:37:27 +03:00
|
|
|
|
buildTableRelayQueryFields sourceName sourceInfo tableName tableInfo gqlName pkeyColumns selPerms = do
|
|
|
|
|
let
|
2021-03-15 16:02:58 +03:00
|
|
|
|
mkRF = RFDB sourceName
|
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
|
. SourceConfigWith sourceInfo
|
|
|
|
|
. QDBR
|
2021-02-23 20:37:27 +03:00
|
|
|
|
fieldName = gqlName <> $$(G.litName "_connection")
|
|
|
|
|
fieldDesc = Just $ G.Description $ "fetch data from the table: " <>> tableName
|
2021-06-15 18:53:20 +03:00
|
|
|
|
fmap afold
|
|
|
|
|
$ optionalFieldParser (mkRF . QDBConnection)
|
|
|
|
|
$ selectTableConnection sourceName tableInfo fieldName fieldDesc pkeyColumns selPerms
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
|
|
buildFunctionRelayQueryFields
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: forall pgKind m n r
|
|
|
|
|
. MonadBuildSchema ('Postgres pgKind) r m n
|
2021-02-23 20:37:27 +03:00
|
|
|
|
=> SourceName
|
2021-04-22 00:44:37 +03:00
|
|
|
|
-> SourceConfig ('Postgres pgKind)
|
|
|
|
|
-> FunctionName ('Postgres pgKind)
|
|
|
|
|
-> FunctionInfo ('Postgres pgKind)
|
|
|
|
|
-> TableName ('Postgres pgKind)
|
|
|
|
|
-> NESeq (ColumnInfo ('Postgres pgKind))
|
|
|
|
|
-> SelPermInfo ('Postgres pgKind)
|
server: IR for DB-DB joins
### Description
This PR adds the required IR for DB to DB joins, based on @paf31 and @0x777 's `feature/db-to-db` branch.
To do so, it also refactors the IR to introduce a new type parameter, `r`, which is used to recursively constructs the `v` parameter of remote QueryDBs. When collecting remote joins, we replace `r` with `Const Void`, indicating at the type level that there cannot be any leftover remote join.
Furthermore, this PR refactors IR.Select for readability, moves some code from IR.Root to IR.Select to avoid having to deal with circular dependencies, and makes it compile by adding `error` in all new cases in the execution pipeline.
The diff doesn't make it clear, but most of Select.hs is actually unchanged. Declarations have just been reordered by topic, in the following order:
- type declarations
- instance declarations
- type aliases
- constructor functions
- traverse functions
https://github.com/hasura/graphql-engine-mono/pull/1580
Co-authored-by: Phil Freeman <630306+paf31@users.noreply.github.com>
GitOrigin-RevId: bbdcb4119cec8bb3fc32f1294f91b8dea0728721
2021-06-18 02:12:11 +03:00
|
|
|
|
-> m [FieldParser n (QueryRootField UnpreparedValue UnpreparedValue)]
|
2021-02-23 20:37:27 +03:00
|
|
|
|
buildFunctionRelayQueryFields sourceName sourceInfo functionName functionInfo tableName pkeyColumns selPerms = do
|
2021-04-22 00:44:37 +03:00
|
|
|
|
funcName <- functionGraphQLName @('Postgres pgKind) functionName `onLeft` throwError
|
2021-02-23 20:37:27 +03:00
|
|
|
|
let
|
2021-03-15 16:02:58 +03:00
|
|
|
|
mkRF = RFDB sourceName
|
|
|
|
|
. AB.mkAnyBackend
|
|
|
|
|
. SourceConfigWith sourceInfo
|
|
|
|
|
. QDBR
|
2021-02-23 20:37:27 +03:00
|
|
|
|
fieldName = funcName <> $$(G.litName "_connection")
|
|
|
|
|
fieldDesc = Just $ G.Description $ "execute function " <> functionName <<> " which returns " <>> tableName
|
2021-06-15 18:53:20 +03:00
|
|
|
|
fmap afold
|
|
|
|
|
$ optionalFieldParser (mkRF . QDBConnection)
|
|
|
|
|
$ selectFunctionConnection sourceName functionInfo fieldName fieldDesc pkeyColumns selPerms
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
-- Individual components
|
|
|
|
|
|
|
|
|
|
columnParser
|
|
|
|
|
:: (MonadSchema n m, MonadError QErr m)
|
2021-04-22 00:44:37 +03:00
|
|
|
|
=> ColumnType ('Postgres pgKind)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-> G.Nullability
|
2021-04-22 00:44:37 +03:00
|
|
|
|
-> m (Parser 'Both n (Opaque (ColumnValue ('Postgres pgKind))))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
columnParser columnType (G.Nullability isNullable) =
|
|
|
|
|
-- TODO(PDV): It might be worth memoizing this function even though it isn’t
|
|
|
|
|
-- recursive simply for performance reasons, since it’s likely to be hammered
|
|
|
|
|
-- during schema generation. Need to profile to see whether or not it’s a win.
|
|
|
|
|
opaque . fmap (ColumnValue columnType) <$> case columnType of
|
2021-05-13 23:12:52 +03:00
|
|
|
|
ColumnScalar scalarType -> possiblyNullable scalarType <$> do
|
|
|
|
|
-- We convert the value to JSON and use the FromJSON instance. This avoids
|
|
|
|
|
-- having two separate ways of parsing a value in the codebase, which
|
|
|
|
|
-- could lead to inconsistencies.
|
|
|
|
|
--
|
|
|
|
|
-- The mapping from postgres type to GraphQL scalar name is done by
|
|
|
|
|
-- 'mkScalarTypeName'. This is confusing, and we might want to fix it
|
|
|
|
|
-- later, as we will parse values differently here than how they'd be
|
|
|
|
|
-- parsed in other places using the same scalar name; for instance, we
|
|
|
|
|
-- will accept strings for postgres columns of type "Integer", despite the
|
|
|
|
|
-- fact that they will be represented as GraphQL ints, which otherwise do
|
|
|
|
|
-- not accept strings.
|
|
|
|
|
--
|
|
|
|
|
-- TODO: introduce new dedicated scalars for Postgres column types.
|
|
|
|
|
name <- mkScalarTypeName scalarType
|
|
|
|
|
let schemaType = P.NonNullable $ P.TNamed $ P.mkDefinition name Nothing P.TIScalar
|
|
|
|
|
pure $ Parser
|
|
|
|
|
{ pType = schemaType
|
|
|
|
|
, pParser = valueToJSON (P.toGraphQLType schemaType) >=> \case
|
|
|
|
|
J.Null -> parseError $ "unexpected null value for type " <>> name
|
|
|
|
|
value -> runAesonParser (parsePGValue scalarType) value
|
|
|
|
|
`onLeft` (parseErrorWith ParseFailed . qeError)
|
|
|
|
|
}
|
2021-02-23 20:37:27 +03:00
|
|
|
|
ColumnEnumReference (EnumReference tableName enumValues) ->
|
|
|
|
|
case nonEmpty (Map.toList enumValues) of
|
|
|
|
|
Just enumValuesList -> do
|
|
|
|
|
name <- qualifiedObjectToName tableName <&> (<> $$(G.litName "_enum"))
|
|
|
|
|
pure $ possiblyNullable PGText $ P.enum name Nothing (mkEnumValue <$> enumValuesList)
|
|
|
|
|
Nothing -> throw400 ValidationFailed "empty enum values"
|
|
|
|
|
where
|
|
|
|
|
-- Sadly, this combinator is not sound in general, so we can’t export it
|
|
|
|
|
-- for general-purpose use. If we did, someone could write this:
|
|
|
|
|
--
|
|
|
|
|
-- mkParameter <$> opaque do
|
|
|
|
|
-- n <- int
|
|
|
|
|
-- pure (mkIntColumnValue (n + 1))
|
|
|
|
|
--
|
|
|
|
|
-- Now we’d end up with a UVParameter that has a variable in it, so we’d
|
|
|
|
|
-- parameterize over it. But when we’d reuse the plan, we wouldn’t know to
|
|
|
|
|
-- increment the value by 1, so we’d use the wrong value!
|
|
|
|
|
--
|
|
|
|
|
-- We could theoretically solve this by retaining a reference to the parser
|
|
|
|
|
-- itself and re-parsing each new value, using the saved parser, which
|
|
|
|
|
-- would admittedly be neat. But it’s more complicated, and it isn’t clear
|
|
|
|
|
-- that it would actually be useful, so for now we don’t support it.
|
|
|
|
|
opaque :: MonadParse m => Parser 'Both m a -> Parser 'Both m (Opaque a)
|
|
|
|
|
opaque parser = parser
|
|
|
|
|
{ pParser = \case
|
|
|
|
|
P.GraphQLValue (G.VVariable var@Variable{ vInfo, vValue }) -> do
|
|
|
|
|
typeCheck False (P.toGraphQLType $ pType parser) var
|
|
|
|
|
P.mkOpaque (Just vInfo) <$> pParser parser (absurd <$> vValue)
|
|
|
|
|
value -> P.mkOpaque Nothing <$> pParser parser value
|
|
|
|
|
}
|
|
|
|
|
possiblyNullable scalarType
|
|
|
|
|
| isNullable = fmap (fromMaybe $ PGNull scalarType) . P.nullable
|
|
|
|
|
| otherwise = id
|
|
|
|
|
mkEnumValue :: (EnumValue, EnumValueInfo) -> (P.Definition P.EnumValueInfo, PGScalarValue)
|
|
|
|
|
mkEnumValue (EnumValue value, EnumValueInfo description) =
|
|
|
|
|
( P.mkDefinition value (G.Description <$> description) P.EnumValueInfo
|
|
|
|
|
, PGValText $ G.unName value
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
jsonPathArg
|
|
|
|
|
:: MonadParse n
|
2021-04-22 00:44:37 +03:00
|
|
|
|
=> ColumnType ('Postgres pgKind)
|
|
|
|
|
-> InputFieldsParser n (Maybe (IR.ColumnOp ('Postgres pgKind)))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
jsonPathArg columnType
|
|
|
|
|
| isScalarColumnWhere PG.isJSONType columnType =
|
|
|
|
|
P.fieldOptional fieldName description P.string `P.bindFields` fmap join . traverse toColExp
|
|
|
|
|
| otherwise = pure Nothing
|
|
|
|
|
where
|
|
|
|
|
fieldName = $$(G.litName "path")
|
|
|
|
|
description = Just "JSON select path"
|
|
|
|
|
toColExp textValue = case parseJSONPath textValue of
|
|
|
|
|
Left err -> parseError $ T.pack $ "parse json path error: " ++ err
|
|
|
|
|
Right [] -> pure Nothing
|
|
|
|
|
Right jPaths -> pure $ Just $ IR.ColumnOp PG.jsonbPathOp $ PG.SEArray $ map elToColExp jPaths
|
|
|
|
|
elToColExp (Key k) = PG.SELit k
|
|
|
|
|
elToColExp (Index i) = PG.SELit $ tshow i
|
|
|
|
|
|
|
|
|
|
orderByOperators
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: NonEmpty (Definition P.EnumValueInfo, (BasicOrderType ('Postgres pgKind), NullsOrderType ('Postgres pgKind)))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
orderByOperators = NE.fromList
|
|
|
|
|
[ ( define $$(G.litName "asc") "in ascending order, nulls last"
|
|
|
|
|
, (PG.OTAsc, PG.NLast)
|
|
|
|
|
)
|
|
|
|
|
, ( define $$(G.litName "asc_nulls_first") "in ascending order, nulls first"
|
|
|
|
|
, (PG.OTAsc, PG.NFirst)
|
|
|
|
|
)
|
|
|
|
|
, ( define $$(G.litName "asc_nulls_last") "in ascending order, nulls last"
|
|
|
|
|
, (PG.OTAsc, PG.NLast)
|
|
|
|
|
)
|
|
|
|
|
, ( define $$(G.litName "desc") "in descending order, nulls first"
|
|
|
|
|
, (PG.OTDesc, PG.NFirst)
|
|
|
|
|
)
|
|
|
|
|
, ( define $$(G.litName "desc_nulls_first") "in descending order, nulls first"
|
|
|
|
|
, (PG.OTDesc, PG.NFirst)
|
|
|
|
|
)
|
|
|
|
|
, ( define $$(G.litName "desc_nulls_last") "in descending order, nulls last"
|
|
|
|
|
, (PG.OTDesc, PG.NLast)
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
where
|
|
|
|
|
define name desc = P.mkDefinition name (Just desc) P.EnumValueInfo
|
|
|
|
|
|
|
|
|
|
comparisonExps
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: forall pgKind m n r
|
|
|
|
|
. ( BackendSchema ('Postgres pgKind)
|
2021-04-08 11:25:11 +03:00
|
|
|
|
, MonadSchema n m
|
|
|
|
|
, MonadError QErr m
|
|
|
|
|
, MonadReader r m
|
|
|
|
|
, Has QueryContext r
|
|
|
|
|
)
|
2021-04-22 00:44:37 +03:00
|
|
|
|
=> ColumnType ('Postgres pgKind) -> m (Parser 'Input n [ComparisonExp ('Postgres pgKind)])
|
2021-02-23 20:37:27 +03:00
|
|
|
|
comparisonExps = P.memoize 'comparisonExps \columnType -> do
|
|
|
|
|
-- see Note [Columns in comparison expression are never nullable]
|
2021-04-08 11:25:11 +03:00
|
|
|
|
collapseIfNull <- asks $ qcDangerousBooleanCollapse . getter
|
|
|
|
|
|
|
|
|
|
-- parsers used for comparison arguments
|
|
|
|
|
geogInputParser <- geographyWithinDistanceInput
|
|
|
|
|
geomInputParser <- geometryWithinDistanceInput
|
|
|
|
|
ignInputParser <- intersectsGeomNbandInput
|
|
|
|
|
ingInputParser <- intersectsNbandGeomInput
|
2021-02-23 20:37:27 +03:00
|
|
|
|
typedParser <- columnParser columnType (G.Nullability False)
|
|
|
|
|
nullableTextParser <- columnParser (ColumnScalar PGText) (G.Nullability True)
|
|
|
|
|
textParser <- columnParser (ColumnScalar PGText) (G.Nullability False)
|
2021-02-25 14:05:51 +03:00
|
|
|
|
-- `lquery` represents a regular-expression-like pattern for matching `ltree` values.
|
|
|
|
|
lqueryParser <- columnParser (ColumnScalar PGLquery) (G.Nullability False)
|
|
|
|
|
-- `ltxtquery` represents a full-text-search-like pattern for matching `ltree` values.
|
|
|
|
|
ltxtqueryParser <- columnParser (ColumnScalar PGLtxtquery) (G.Nullability False)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
maybeCastParser <- castExp columnType
|
|
|
|
|
let name = P.getName typedParser <> $$(G.litName "_comparison_exp")
|
|
|
|
|
desc = G.Description $ "Boolean expression to compare columns of type "
|
|
|
|
|
<> P.getName typedParser
|
|
|
|
|
<<> ". All fields are combined with logical 'AND'."
|
2021-04-08 11:25:11 +03:00
|
|
|
|
textListParser = P.list textParser `P.bind` traverse P.openOpaque
|
2021-02-23 20:37:27 +03:00
|
|
|
|
columnListParser = P.list typedParser `P.bind` traverse P.openOpaque
|
2021-04-08 11:25:11 +03:00
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
pure $ P.object name (Just desc) $ fmap catMaybes $ sequenceA $ concat
|
|
|
|
|
[ flip (maybe []) maybeCastParser $ \castParser ->
|
|
|
|
|
[ P.fieldOptional $$(G.litName "_cast") Nothing (ACast <$> castParser)
|
|
|
|
|
]
|
2021-04-08 11:25:11 +03:00
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- Common ops for all types
|
2021-04-08 11:25:11 +03:00
|
|
|
|
, equalityOperators
|
|
|
|
|
collapseIfNull
|
|
|
|
|
(mkParameter <$> typedParser)
|
|
|
|
|
(mkListLiteral columnType <$> columnListParser)
|
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- Comparison ops for non Raster types
|
|
|
|
|
, guard (isScalarColumnWhere (/= PGRaster) columnType) *>
|
2021-04-08 11:25:11 +03:00
|
|
|
|
comparisonOperators
|
|
|
|
|
collapseIfNull
|
|
|
|
|
(mkParameter <$> typedParser)
|
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- Ops for Raster types
|
|
|
|
|
, guard (isScalarColumnWhere (== PGRaster) columnType) *>
|
2021-06-18 18:34:05 +03:00
|
|
|
|
[ mkBoolOperator collapseIfNull $$(G.litName "_st_intersects_rast")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
Nothing
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTIntersectsRast . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_intersects_nband_geom")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
Nothing
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTIntersectsNbandGeom <$> ingInputParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_intersects_geom_nband")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
Nothing
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTIntersectsGeomNband <$> ignInputParser)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
]
|
2021-04-08 11:25:11 +03:00
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- Ops for String like types
|
|
|
|
|
, guard (isScalarColumnWhere isStringType columnType) *>
|
2021-06-18 18:34:05 +03:00
|
|
|
|
[ mkBoolOperator collapseIfNull $$(G.litName "_like")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column match the given pattern")
|
|
|
|
|
(ALIKE . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_nlike")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column NOT match the given pattern")
|
|
|
|
|
(ANLIKE . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_ilike")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column match the given case-insensitive pattern")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AILIKE . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_nilike")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column NOT match the given case-insensitive pattern")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ANILIKE . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_similar")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column match the given SQL regular expression")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASIMILAR . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_nsimilar")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column NOT match the given SQL regular expression")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ANSIMILAR . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_regex")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column match the given POSIX regular expression, case sensitive")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AREGEX . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_iregex")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column match the given POSIX regular expression, case insensitive")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AIREGEX . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_nregex")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column NOT match the given POSIX regular expression, case sensitive")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ANREGEX . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_niregex")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column NOT match the given POSIX regular expression, case insensitive")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ANIREGEX . mkParameter <$> typedParser)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
]
|
2021-04-08 11:25:11 +03:00
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- Ops for JSONB type
|
|
|
|
|
, guard (isScalarColumnWhere (== PGJSONB) columnType) *>
|
2021-06-18 18:34:05 +03:00
|
|
|
|
[ mkBoolOperator collapseIfNull $$(G.litName "_contains")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column contain the given json value at the top level")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AContains . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_contained_in")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "is the column contained in the given json value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AContainedIn . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_has_key")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the string exist as a top-level key in the column")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AHasKey . mkParameter <$> nullableTextParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_has_keys_any")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "do any of these strings exist as top-level keys in the column")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AHasKeysAny . mkListLiteral (ColumnScalar PGText) <$> textListParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_has_keys_all")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "do all of these strings exist as top-level keys in the column")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AHasKeysAll . mkListLiteral (ColumnScalar PGText) <$> textListParser)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
]
|
2021-04-08 11:25:11 +03:00
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- Ops for Geography type
|
|
|
|
|
, guard (isScalarColumnWhere (== PGGeography) columnType) *>
|
2021-06-18 18:34:05 +03:00
|
|
|
|
[ mkBoolOperator collapseIfNull $$(G.litName "_st_intersects")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column spatially intersect the given geography value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTIntersects . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_d_within")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "is the column within a given distance from the given geography value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTDWithinGeog <$> geogInputParser)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
]
|
2021-04-08 11:25:11 +03:00
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
-- Ops for Geometry type
|
|
|
|
|
, guard (isScalarColumnWhere (== PGGeometry) columnType) *>
|
2021-06-18 18:34:05 +03:00
|
|
|
|
[ mkBoolOperator collapseIfNull $$(G.litName "_st_contains")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column contain the given geometry value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTContains . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_crosses")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column cross the given geometry value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTCrosses . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_equals")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "is the column equal to given geometry value (directionality is ignored)")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTEquals . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_overlaps")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column 'spatially overlap' (intersect but not completely contain) the given geometry value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTOverlaps . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_touches")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column have atleast one point in common with the given geometry value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTTouches . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_within")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "is the column contained in the given geometry value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTWithin . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_intersects")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "does the column spatially intersect the given geometry value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTIntersects . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_3d_intersects")
|
2021-04-14 16:02:16 +03:00
|
|
|
|
(Just "does the column spatially intersect the given geometry value in 3D")
|
|
|
|
|
(ABackendSpecific . AST3DIntersects . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_d_within")
|
2021-02-23 20:37:27 +03:00
|
|
|
|
(Just "is the column within a given distance from the given geometry value")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ASTDWithinGeom <$> geomInputParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_st_3d_d_within")
|
2021-04-14 16:02:16 +03:00
|
|
|
|
(Just "is the column within a given 3D distance from the given geometry value")
|
|
|
|
|
(ABackendSpecific . AST3DDWithinGeom <$> geomInputParser)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
]
|
2021-02-25 14:05:51 +03:00
|
|
|
|
|
|
|
|
|
-- Ops for Ltree type
|
|
|
|
|
, guard (isScalarColumnWhere (== PGLtree) columnType) *>
|
2021-06-18 18:34:05 +03:00
|
|
|
|
[ mkBoolOperator collapseIfNull $$(G.litName "_ancestor")
|
2021-02-25 14:05:51 +03:00
|
|
|
|
(Just "is the left argument an ancestor of right (or equal)?")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AAncestor . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_ancestor_any")
|
2021-02-25 14:05:51 +03:00
|
|
|
|
(Just "does array contain an ancestor of `ltree`?")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AAncestorAny . mkListLiteral columnType <$> columnListParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_descendant")
|
2021-02-25 14:05:51 +03:00
|
|
|
|
(Just "is the left argument a descendant of right (or equal)?")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ADescendant . mkParameter <$> typedParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_descendant_any")
|
2021-02-25 14:05:51 +03:00
|
|
|
|
(Just "does array contain a descendant of `ltree`?")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . ADescendantAny . mkListLiteral columnType <$> columnListParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_matches")
|
2021-02-25 14:05:51 +03:00
|
|
|
|
(Just "does `ltree` match `lquery`?")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AMatches . mkParameter <$> lqueryParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_matches_any")
|
2021-02-25 14:05:51 +03:00
|
|
|
|
(Just "does `ltree` match any `lquery` in array?")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AMatchesAny . mkListLiteral (ColumnScalar PGLquery) <$> textListParser)
|
2021-06-18 18:34:05 +03:00
|
|
|
|
, mkBoolOperator collapseIfNull $$(G.litName "_matches_fulltext")
|
2021-02-25 14:05:51 +03:00
|
|
|
|
(Just "does `ltree` match `ltxtquery`?")
|
2021-03-25 20:50:08 +03:00
|
|
|
|
(ABackendSpecific . AMatchesFulltext . mkParameter <$> ltxtqueryParser)
|
2021-02-25 14:05:51 +03:00
|
|
|
|
]
|
2021-02-23 20:37:27 +03:00
|
|
|
|
]
|
|
|
|
|
where
|
2021-04-22 00:44:37 +03:00
|
|
|
|
mkListLiteral :: ColumnType ('Postgres pgKind) -> [ColumnValue ('Postgres pgKind)] -> UnpreparedValue ('Postgres pgKind)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
mkListLiteral columnType columnValues = P.UVLiteral $ SETyAnn
|
|
|
|
|
(SEArray $ txtEncoder . cvValue <$> columnValues)
|
|
|
|
|
(mkTypeAnn $ CollectableTypeArray $ unsafePGColumnToBackend columnType)
|
|
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
|
castExp :: ColumnType ('Postgres pgKind) -> m (Maybe (Parser 'Input n (CastExp ('Postgres pgKind) (UnpreparedValue ('Postgres pgKind)))))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
castExp sourceType = do
|
|
|
|
|
let maybeScalars = case sourceType of
|
|
|
|
|
ColumnScalar PGGeography -> Just (PGGeography, PGGeometry)
|
|
|
|
|
ColumnScalar PGGeometry -> Just (PGGeometry, PGGeography)
|
|
|
|
|
_ -> Nothing
|
|
|
|
|
|
|
|
|
|
forM maybeScalars $ \(sourceScalar, targetScalar) -> do
|
|
|
|
|
sourceName <- mkScalarTypeName sourceScalar <&> (<> $$(G.litName "_cast_exp"))
|
|
|
|
|
targetName <- mkScalarTypeName targetScalar
|
|
|
|
|
targetOpExps <- comparisonExps $ ColumnScalar targetScalar
|
|
|
|
|
let field = P.fieldOptional targetName Nothing $ (targetScalar, ) <$> targetOpExps
|
|
|
|
|
pure $ P.object sourceName Nothing $ M.fromList . maybeToList <$> field
|
|
|
|
|
|
|
|
|
|
geographyWithinDistanceInput
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: forall pgKind m n. (MonadSchema n m, MonadError QErr m)
|
|
|
|
|
=> m (Parser 'Input n (DWithinGeogOp (UnpreparedValue ('Postgres pgKind))))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
geographyWithinDistanceInput = do
|
|
|
|
|
geographyParser <- columnParser (ColumnScalar PGGeography) (G.Nullability False)
|
|
|
|
|
-- FIXME
|
|
|
|
|
-- It doesn't make sense for this value to be nullable; it only is for
|
|
|
|
|
-- backwards compatibility; if an explicit Null value is given, it will be
|
|
|
|
|
-- forwarded to the underlying SQL function, that in turns treat a null value
|
|
|
|
|
-- as an error. We can fix this by rejecting explicit null values, by marking
|
|
|
|
|
-- this field non-nullable in a future release.
|
|
|
|
|
booleanParser <- columnParser (ColumnScalar PGBoolean) (G.Nullability True)
|
|
|
|
|
floatParser <- columnParser (ColumnScalar PGFloat) (G.Nullability False)
|
|
|
|
|
pure $ P.object $$(G.litName "st_d_within_geography_input") Nothing $
|
|
|
|
|
DWithinGeogOp <$> (mkParameter <$> P.field $$(G.litName "distance") Nothing floatParser)
|
|
|
|
|
<*> (mkParameter <$> P.field $$(G.litName "from") Nothing geographyParser)
|
|
|
|
|
<*> (mkParameter <$> P.fieldWithDefault $$(G.litName "use_spheroid") Nothing (G.VBoolean True) booleanParser)
|
|
|
|
|
|
|
|
|
|
geometryWithinDistanceInput
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: forall pgKind m n. (MonadSchema n m, MonadError QErr m)
|
|
|
|
|
=> m (Parser 'Input n (DWithinGeomOp (UnpreparedValue ('Postgres pgKind))))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
geometryWithinDistanceInput = do
|
|
|
|
|
geometryParser <- columnParser (ColumnScalar PGGeometry) (G.Nullability False)
|
|
|
|
|
floatParser <- columnParser (ColumnScalar PGFloat) (G.Nullability False)
|
|
|
|
|
pure $ P.object $$(G.litName "st_d_within_input") Nothing $
|
|
|
|
|
DWithinGeomOp <$> (mkParameter <$> P.field $$(G.litName "distance") Nothing floatParser)
|
|
|
|
|
<*> (mkParameter <$> P.field $$(G.litName "from") Nothing geometryParser)
|
|
|
|
|
|
|
|
|
|
intersectsNbandGeomInput
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: forall pgKind m n. (MonadSchema n m, MonadError QErr m)
|
|
|
|
|
=> m (Parser 'Input n (STIntersectsNbandGeommin (UnpreparedValue ('Postgres pgKind))))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
intersectsNbandGeomInput = do
|
|
|
|
|
geometryParser <- columnParser (ColumnScalar PGGeometry) (G.Nullability False)
|
|
|
|
|
integerParser <- columnParser (ColumnScalar PGInteger) (G.Nullability False)
|
|
|
|
|
pure $ P.object $$(G.litName "st_intersects_nband_geom_input") Nothing $
|
|
|
|
|
STIntersectsNbandGeommin <$> (mkParameter <$> P.field $$(G.litName "nband") Nothing integerParser)
|
|
|
|
|
<*> (mkParameter <$> P.field $$(G.litName "geommin") Nothing geometryParser)
|
|
|
|
|
|
|
|
|
|
intersectsGeomNbandInput
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: forall pgKind m n. (MonadSchema n m, MonadError QErr m)
|
|
|
|
|
=> m (Parser 'Input n (STIntersectsGeomminNband (UnpreparedValue ('Postgres pgKind))))
|
2021-02-23 20:37:27 +03:00
|
|
|
|
intersectsGeomNbandInput = do
|
|
|
|
|
geometryParser <- columnParser (ColumnScalar PGGeometry) (G.Nullability False)
|
|
|
|
|
integerParser <- columnParser (ColumnScalar PGInteger) (G.Nullability False)
|
|
|
|
|
pure $ P.object $$(G.litName "st_intersects_geom_nband_input") Nothing $ STIntersectsGeomminNband
|
|
|
|
|
<$> ( mkParameter <$> P.field $$(G.litName "geommin") Nothing geometryParser)
|
|
|
|
|
<*> (fmap mkParameter <$> P.fieldOptional $$(G.litName "nband") Nothing integerParser)
|
|
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
|
mkCountType :: Maybe Bool -> Maybe [Column ('Postgres pgKind)] -> CountType ('Postgres pgKind)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
mkCountType _ Nothing = PG.CTStar
|
|
|
|
|
mkCountType (Just True) (Just cols) = PG.CTDistinct cols
|
|
|
|
|
mkCountType _ (Just cols) = PG.CTSimple cols
|
|
|
|
|
|
|
|
|
|
-- | Various update operators
|
|
|
|
|
updateOperators
|
2021-04-22 00:44:37 +03:00
|
|
|
|
:: forall pgKind m n r
|
|
|
|
|
. (BackendSchema ('Postgres pgKind), MonadSchema n m, MonadTableInfo r m)
|
2021-05-18 16:06:42 +03:00
|
|
|
|
=> TableInfo ('Postgres pgKind) -- ^ table info
|
2021-04-22 00:44:37 +03:00
|
|
|
|
-> UpdPermInfo ('Postgres pgKind) -- ^ update permissions of the table
|
|
|
|
|
-> m (Maybe (InputFieldsParser n [(Column ('Postgres pgKind), IR.UpdOpExpG (UnpreparedValue ('Postgres pgKind)))]))
|
2021-05-18 16:06:42 +03:00
|
|
|
|
updateOperators tableInfo updatePermissions = do
|
|
|
|
|
tableGQLName <- getTableGQLName tableInfo
|
|
|
|
|
columns <- tableUpdateColumns tableInfo updatePermissions
|
2021-02-23 20:37:27 +03:00
|
|
|
|
let numericCols = onlyNumCols columns
|
|
|
|
|
jsonCols = onlyJSONBCols columns
|
|
|
|
|
parsers <- catMaybes <$> sequenceA
|
|
|
|
|
[ updateOperator tableGQLName $$(G.litName "_set")
|
|
|
|
|
typedParser IR.UpdSet columns
|
|
|
|
|
"sets the columns of the filtered rows to the given values"
|
2021-05-18 16:06:42 +03:00
|
|
|
|
(G.Description $ "input type for updating data in table " <>> tableName)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
|
|
, updateOperator tableGQLName $$(G.litName "_inc")
|
|
|
|
|
typedParser IR.UpdInc numericCols
|
|
|
|
|
"increments the numeric columns with given value of the filtered values"
|
2021-05-18 16:06:42 +03:00
|
|
|
|
(G.Description $"input type for incrementing numeric columns in table " <>> tableName)
|
2021-02-23 20:37:27 +03:00
|
|
|
|
|
|
|
|
|
, let desc = "prepend existing jsonb value of filtered columns with new jsonb value"
|
|
|
|
|
in updateOperator tableGQLName $$(G.litName "_prepend")
|
|
|
|
|
typedParser IR.UpdPrepend jsonCols desc desc
|
|
|
|
|
|
|
|
|
|
, let desc = "append existing jsonb value of filtered columns with new jsonb value"
|
|
|
|
|
in updateOperator tableGQLName $$(G.litName "_append")
|
|
|
|
|
typedParser IR.UpdAppend jsonCols desc desc
|
|
|
|
|
|
|
|
|
|
, let desc = "delete key/value pair or string element. key/value pairs are matched based on their key value"
|
|
|
|
|
in updateOperator tableGQLName $$(G.litName "_delete_key")
|
|
|
|
|
nullableTextParser IR.UpdDeleteKey jsonCols desc desc
|
|
|
|
|
|
|
|
|
|
, let desc = "delete the array element with specified index (negative integers count from the end). "
|
|
|
|
|
<> "throws an error if top level container is not an array"
|
|
|
|
|
in updateOperator tableGQLName $$(G.litName "_delete_elem")
|
|
|
|
|
nonNullableIntParser IR.UpdDeleteElem jsonCols desc desc
|
|
|
|
|
|
|
|
|
|
, let desc = "delete the field or element with specified path (for JSON arrays, negative integers count from the end)"
|
|
|
|
|
in updateOperator tableGQLName $$(G.litName "_delete_at_path")
|
|
|
|
|
(fmap P.list . nonNullableTextParser) IR.UpdDeleteAtPath jsonCols desc desc
|
|
|
|
|
]
|
|
|
|
|
whenMaybe (not $ null parsers) do
|
|
|
|
|
let allowedOperators = fst <$> parsers
|
|
|
|
|
pure $ fmap catMaybes (sequenceA $ snd <$> parsers)
|
|
|
|
|
`P.bindFields` \opExps -> do
|
|
|
|
|
-- there needs to be at least one operator in the update, even if it is empty
|
|
|
|
|
let presetColumns = Map.toList $ IR.UpdSet . partialSQLExpToUnpreparedValue <$> upiSet updatePermissions
|
|
|
|
|
when (null opExps && null presetColumns) $ parseError $
|
|
|
|
|
"at least any one of " <> commaSeparated allowedOperators <> " is expected"
|
|
|
|
|
|
|
|
|
|
-- no column should appear twice
|
|
|
|
|
let flattenedExps = concat opExps
|
|
|
|
|
erroneousExps = OMap.filter ((>1) . length) $ OMap.groupTuples flattenedExps
|
|
|
|
|
unless (OMap.null erroneousExps) $ parseError $
|
|
|
|
|
"column found in multiple operators; " <>
|
|
|
|
|
T.intercalate ". " [ dquote columnName <> " in " <> commaSeparated (IR.updateOperatorText <$> ops)
|
|
|
|
|
| (columnName, ops) <- OMap.toList erroneousExps
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
pure $ presetColumns <> flattenedExps
|
|
|
|
|
where
|
2021-05-18 16:06:42 +03:00
|
|
|
|
tableName = tableInfoName tableInfo
|
2021-02-23 20:37:27 +03:00
|
|
|
|
typedParser columnInfo = fmap P.mkParameter <$> columnParser (pgiType columnInfo) (G.Nullability $ pgiIsNullable columnInfo)
|
|
|
|
|
nonNullableTextParser _ = fmap P.mkParameter <$> columnParser (ColumnScalar PGText) (G.Nullability False)
|
|
|
|
|
nullableTextParser _ = fmap P.mkParameter <$> columnParser (ColumnScalar PGText) (G.Nullability True)
|
|
|
|
|
nonNullableIntParser _ = fmap P.mkParameter <$> columnParser (ColumnScalar PGInteger) (G.Nullability False)
|
|
|
|
|
|
2021-03-25 20:50:08 +03:00
|
|
|
|
onlyJSONBCols = filter (isScalarColumnWhere (== PGJSONB) . pgiType)
|
|
|
|
|
|
2021-02-23 20:37:27 +03:00
|
|
|
|
updateOperator
|
|
|
|
|
:: G.Name
|
|
|
|
|
-> G.Name
|
|
|
|
|
-> (ColumnInfo b -> m (Parser 'Both n a))
|
|
|
|
|
-> (a -> IR.UpdOpExpG (UnpreparedValue b))
|
|
|
|
|
-> [ColumnInfo b]
|
|
|
|
|
-> G.Description
|
|
|
|
|
-> G.Description
|
|
|
|
|
-> m (Maybe (Text, InputFieldsParser n (Maybe [(Column b, IR.UpdOpExpG (UnpreparedValue b))])))
|
|
|
|
|
updateOperator tableGQLName opName mkParser updOpExp columns opDesc objDesc =
|
|
|
|
|
whenMaybe (not $ null columns) do
|
|
|
|
|
fields <- for columns \columnInfo -> do
|
|
|
|
|
let fieldName = pgiName columnInfo
|
|
|
|
|
fieldDesc = pgiDescription columnInfo
|
|
|
|
|
fieldParser <- mkParser columnInfo
|
|
|
|
|
pure $ P.fieldOptional fieldName fieldDesc fieldParser
|
|
|
|
|
`mapField` \value -> (pgiColumn columnInfo, updOpExp value)
|
|
|
|
|
let objName = tableGQLName <> opName <> $$(G.litName "_input")
|
|
|
|
|
pure $ (G.unName opName,)
|
|
|
|
|
$ P.fieldOptional opName (Just opDesc)
|
|
|
|
|
$ P.object objName (Just objDesc)
|
|
|
|
|
$ catMaybes <$> sequenceA fields
|