graphql-engine/server/src-lib/Hasura/Backends/Postgres/Translate/Column.hs
Philip Lykke Carlsen b9ad721ea6 Reduce boolean blindness by promoting data type StringifyNumbers
I discovered and removed instances of Boolean Blindness about whether json numbers should be stringified or not.

Although quite far-reaching, this is a completely mechanical change and should have no observable impact outside the server code.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3763
GitOrigin-RevId: c588891afd8a6923a135c736f6581a43a2eddbc7
2022-02-23 20:19:24 +00:00

42 lines
1.5 KiB
Haskell

-- | Postgres Translate Column
--
-- Translate column values to Postgres-specific SQL expressions.
module Hasura.Backends.Postgres.Translate.Column
( toTxtValue,
toJSONableExp,
)
where
import Hasura.Backends.Postgres.SQL.DML
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Backends.Postgres.SQL.Value
import Hasura.Backends.Postgres.Types.Column
import Hasura.Prelude
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common (StringifyNumbers (..))
import Hasura.SQL.Backend
toTxtValue :: ColumnValue ('Postgres pgKind) -> SQLExp
toTxtValue ColumnValue {..} =
withTyAnn ty . withConstructorFn ty $ txtEncoder cvValue
where
ty = unsafePGColumnToBackend cvType
-- | Formats each columns to appropriate SQL expression
toJSONableExp :: StringifyNumbers -> 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 && (case stringifyNum of StringifyNumbers -> True; LeaveNumbersAlone -> False)) =
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