2020-12-03 15:21:27 +03:00
|
|
|
module Hasura.Backends.Postgres.Translate.Column
|
|
|
|
( toTxtValue
|
2021-09-09 14:54:19 +03:00
|
|
|
, toJSONableExp
|
2020-12-03 15:21:27 +03:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import Hasura.Backends.Postgres.SQL.DML
|
2021-09-09 14:54:19 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
2020-12-03 15:21:27 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Value
|
2021-03-25 20:50:08 +03:00
|
|
|
import Hasura.Backends.Postgres.Types.Column
|
2021-02-14 09:07:52 +03:00
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.SQL.Backend
|
2020-12-03 15:21:27 +03:00
|
|
|
|
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
toTxtValue :: ColumnValue ('Postgres pgKind) -> SQLExp
|
2020-12-03 15:21:27 +03:00
|
|
|
toTxtValue ColumnValue{..} =
|
|
|
|
withTyAnn ty . withConstructorFn ty $ txtEncoder cvValue
|
|
|
|
where
|
|
|
|
ty = unsafePGColumnToBackend cvType
|
2021-09-09 14:54:19 +03:00
|
|
|
|
|
|
|
-- | Formats each columns to appropriate SQL expression
|
|
|
|
toJSONableExp :: Bool -> ColumnType ('Postgres pgKind) -> Bool -> SQLExp -> SQLExp
|
|
|
|
toJSONableExp stringifyNum colType asText expression
|
|
|
|
-- If its 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 && stringifyNum) =
|
|
|
|
expression `SETyAnn` textTypeAnn
|
|
|
|
-- If the column is either a `Geometry` or `Geography` then apply the `ST_AsGeoJSON` function to convert it into GeoJSON format
|
|
|
|
| isScalarColumnWhere isGeoType colType =
|
|
|
|
SEFnApp "ST_AsGeoJSON"
|
|
|
|
[ expression
|
|
|
|
, SEUnsafe "15" -- max decimal digits
|
|
|
|
, SEUnsafe "4" -- to print out crs
|
|
|
|
] Nothing
|
|
|
|
`SETyAnn` jsonTypeAnn
|
|
|
|
| otherwise = expression
|