server/mssql: fix parsing of text column values

GitOrigin-RevId: c4692cdc254af074f70cdceb78c7dc6211e7e084
This commit is contained in:
Antoine Leblanc 2021-03-05 18:32:16 +00:00 committed by hasura-bot
parent 6d04f587b4
commit 5a1f71b3bc
3 changed files with 54 additions and 23 deletions

View File

@ -15,6 +15,7 @@
- server/mssql: fix malformed JSON answer on empty tables
- server/mssql: fix runtime errors when selecting geography/geometry columns
- server/mssql: supports connection pooling to sql server
- server/mssql: fix text values erroneously being parsed as varchar
- server: improve errors messages for inconsistent sources
- console: add relationship tab for mssql tables (#677)
- build: fix the packaging of static console assets (fix #6610)

View File

@ -9,6 +9,7 @@ import qualified Data.List.NonEmpty as NE
import qualified Database.ODBC.SQLServer as ODBC
import qualified Language.GraphQL.Draft.Syntax as G
import Data.Text.Encoding (encodeUtf8)
import Data.Text.Extended
import qualified Hasura.Backends.MSSQL.Types as MSSQL
@ -176,15 +177,30 @@ msColumnParser
msColumnParser columnType (G.Nullability isNullable) =
opaque . fmap (ColumnValue columnType) <$> case columnType of
ColumnScalar scalarType -> possiblyNullable scalarType <$> case scalarType of
MSSQL.WcharType -> pure (ODBC.TextValue <$> P.string)
MSSQL.WvarcharType -> pure (ODBC.TextValue <$> P.string)
MSSQL.WtextType -> pure (ODBC.TextValue <$> P.string)
-- bytestring
MSSQL.CharType -> pure $ ODBC.ByteStringValue . encodeUtf8 <$> P.string
MSSQL.VarcharType -> pure $ ODBC.ByteStringValue . encodeUtf8 <$> P.string
-- text
MSSQL.FloatType -> pure (ODBC.DoubleValue <$> P.float)
-- integer types
MSSQL.IntegerType -> pure (ODBC.IntValue . fromIntegral <$> P.int)
-- boolean type
MSSQL.BitType -> pure (ODBC.BoolValue <$> P.boolean)
MSSQL.WcharType -> pure $ ODBC.TextValue <$> P.string
MSSQL.WvarcharType -> pure $ ODBC.TextValue <$> P.string
MSSQL.WtextType -> pure $ ODBC.TextValue <$> P.string
MSSQL.TextType -> pure $ ODBC.TextValue <$> P.string
-- integer
MSSQL.IntegerType -> pure $ ODBC.IntValue . fromIntegral <$> P.int
MSSQL.SmallintType -> pure $ ODBC.IntValue . fromIntegral <$> P.int
MSSQL.BigintType -> pure $ ODBC.IntValue . fromIntegral <$> P.int
MSSQL.TinyintType -> pure $ ODBC.IntValue . fromIntegral <$> P.int
-- float
MSSQL.NumericType -> pure $ ODBC.DoubleValue <$> P.float
MSSQL.DecimalType -> pure $ ODBC.DoubleValue <$> P.float
MSSQL.FloatType -> pure $ ODBC.DoubleValue <$> P.float
MSSQL.RealType -> pure $ ODBC.DoubleValue <$> P.float
-- boolean
MSSQL.BitType -> pure $ ODBC.BoolValue <$> P.boolean
_ -> do
name <- mkMSSQLScalarTypeName scalarType
let schemaType = P.NonNullable $ P.TNamed $ P.mkDefinition name Nothing P.TIScalar

View File

@ -7,10 +7,11 @@ module Hasura.Backends.MSSQL.Types.Internal where
import Hasura.Prelude
import qualified Data.Aeson as J
import Data.Text.Encoding (encodeUtf8)
import qualified Database.ODBC.SQLServer as ODBC
import qualified Language.GraphQL.Draft.Syntax as G
import Data.Text.Encoding (encodeUtf8)
import Hasura.RQL.Types.Error
import Hasura.SQL.Backend
@ -302,32 +303,45 @@ scalarTypeDBName = \case
parseScalarValue :: ScalarType -> J.Value -> Either QErr Value
parseScalarValue scalarType jValue = case scalarType of
-- bytestring
CharType -> ODBC.ByteStringValue . encodeUtf8 <$> parseJValue jValue
VarcharType -> ODBC.ByteStringValue . encodeUtf8 <$> parseJValue jValue
TextType -> ODBC.ByteStringValue . encodeUtf8 <$> parseJValue jValue
NumericType -> ODBC.FloatValue <$> parseJValue jValue
DecimalType -> ODBC.FloatValue <$> parseJValue jValue
IntegerType -> ODBC.IntValue <$> parseJValue jValue
SmallintType -> ODBC.IntValue <$> parseJValue jValue
FloatType -> ODBC.FloatValue <$> parseJValue jValue
RealType -> ODBC.FloatValue <$> parseJValue jValue
DateType -> ODBC.DayValue <$> parseJValue jValue
Ss_time2Type -> ODBC.TimeOfDayValue <$> parseJValue jValue
-- text
TextType -> ODBC.TextValue <$> parseJValue jValue
WcharType -> ODBC.TextValue <$> parseJValue jValue
WvarcharType -> ODBC.TextValue <$> parseJValue jValue
WtextType -> ODBC.TextValue <$> parseJValue jValue
TimestampType -> ODBC.LocalTimeValue <$> parseJValue jValue
BinaryType -> ODBC.BinaryValue . ODBC.Binary . txtToBs <$> parseJValue jValue
VarbinaryType -> ODBC.BinaryValue . ODBC.Binary . txtToBs <$> parseJValue jValue
-- integer
IntegerType -> ODBC.IntValue <$> parseJValue jValue
SmallintType -> ODBC.IntValue <$> parseJValue jValue
BigintType -> ODBC.IntValue <$> parseJValue jValue
TinyintType -> ODBC.IntValue <$> parseJValue jValue
BitType -> ODBC.ByteValue <$> parseJValue jValue
GuidType -> ODBC.TextValue <$> parseJValue jValue
-- float
NumericType -> ODBC.FloatValue <$> parseJValue jValue
DecimalType -> ODBC.FloatValue <$> parseJValue jValue
FloatType -> ODBC.FloatValue <$> parseJValue jValue
RealType -> ODBC.FloatValue <$> parseJValue jValue
-- boolean
BitType -> ODBC.ByteValue <$> parseJValue jValue
-- geo
-- TODO: We'll need to wrap this with geography::STGeomFromText
GeographyType -> ODBC.TextValue <$> parseJValue jValue
GeometryType -> ODBC.TextValue <$> parseJValue jValue
-- misc
BinaryType -> ODBC.BinaryValue . ODBC.Binary . txtToBs <$> parseJValue jValue
VarbinaryType -> ODBC.BinaryValue . ODBC.Binary . txtToBs <$> parseJValue jValue
Ss_time2Type -> ODBC.TimeOfDayValue <$> parseJValue jValue
TimestampType -> ODBC.LocalTimeValue <$> parseJValue jValue
DateType -> ODBC.DayValue <$> parseJValue jValue
GuidType -> ODBC.TextValue <$> parseJValue jValue
-- the input format for types that aren't explicitly supported is a string
UnknownType _ -> ODBC.TextValue <$> parseJValue jValue
where