2022-02-08 12:24:34 +03:00
|
|
|
-- | Postgres Translate Column
|
|
|
|
--
|
|
|
|
-- Translate column values to Postgres-specific SQL expressions.
|
2020-12-03 15:21:27 +03:00
|
|
|
module Hasura.Backends.Postgres.Translate.Column
|
2021-09-24 01:56:37 +03:00
|
|
|
( toTxtValue,
|
|
|
|
toJSONableExp,
|
|
|
|
)
|
|
|
|
where
|
2020-12-03 15:21:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.DML
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
import Hasura.Backends.Postgres.SQL.Value
|
|
|
|
import Hasura.Backends.Postgres.Types.Column
|
2022-07-19 09:55:42 +03:00
|
|
|
import Hasura.GraphQL.Schema.NamingCase
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Prelude
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.RQL.Types.Column
|
2023-04-24 18:17:15 +03:00
|
|
|
import Hasura.RQL.Types.Schema.Options qualified as Options
|
2020-12-03 15:21:27 +03:00
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
toTxtValue :: ColumnValue ('Postgres pgKind) -> SQLExp
|
2021-09-24 01:56:37 +03:00
|
|
|
toTxtValue ColumnValue {..} =
|
2022-04-27 18:36:02 +03:00
|
|
|
withScalarTypeAnn ty . withConstructorFn ty $ txtEncoder cvValue
|
2020-12-03 15:21:27 +03:00
|
|
|
where
|
|
|
|
ty = unsafePGColumnToBackend cvType
|
2021-09-09 14:54:19 +03:00
|
|
|
|
|
|
|
-- | Formats each columns to appropriate SQL expression
|
2022-07-19 09:55:42 +03:00
|
|
|
toJSONableExp :: Options.StringifyNumbers -> ColumnType ('Postgres pgKind) -> Bool -> Maybe NamingCase -> SQLExp -> SQLExp
|
|
|
|
toJSONableExp stringifyNum colType asText tCase expression
|
2022-07-14 20:57:28 +03:00
|
|
|
-- If it's a numeric column greater than a 32-bit integer, we have to stringify it as JSON spec doesn't support >32-bit integers
|
|
|
|
| asText || (isScalarColumnWhere isBigNum colType && (case stringifyNum of Options.StringifyNumbers -> True; Options.Don'tStringifyNumbers -> False)) =
|
2022-11-02 23:53:23 +03:00
|
|
|
expression `SETyAnn` textTypeAnn
|
2021-09-09 14:54:19 +03:00
|
|
|
-- If the column is either a `Geometry` or `Geography` then apply the `ST_AsGeoJSON` function to convert it into GeoJSON format
|
|
|
|
| isScalarColumnWhere isGeoType colType =
|
2022-11-02 23:53:23 +03:00
|
|
|
SEFnApp
|
|
|
|
"ST_AsGeoJSON"
|
|
|
|
[ expression,
|
|
|
|
SEUnsafe "15", -- max decimal digits
|
|
|
|
SEUnsafe "4" -- to print out crs
|
|
|
|
]
|
|
|
|
Nothing
|
|
|
|
`SETyAnn` jsonTypeAnn
|
2022-07-19 09:55:42 +03:00
|
|
|
| isEnumColumn colType && any isGraphqlCase tCase = applyUppercase expression
|
2021-09-09 14:54:19 +03:00
|
|
|
| otherwise = expression
|