| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Hmssql.Types
Description
The Haskell types that stand for SQL Server's, and which SQL types each
decodes from and is sent as. The value types of hmssql's own
(Money, DateTimeOffset, RawValue, RawParam; Hmssql.SqlValue for a
value of any type) are
defined or re-exported here; the rest are the usual base, text,
bytestring, time, Decimal and uuid-types types.
Decoding: FromSqlField
A column's SQL type is checked against the Haskell type when the result
set's columns arrive, before any row is decoded. Maybe a decodes
NULL as Nothing for any a below; a NULL in a column decoded as a
bare type is a DecodeError.
| Haskell type | SQL types accepted |
|---|---|
Bool | bit |
Int, Int8, Int16, Int32,
Int64, Word8 | tinyint, smallint, int, bigint, bit; a value outside the
Haskell type's range is an error |
Integer | the integer types, and decimal or numeric with scale 0 |
Double | float, real |
Float | real (float(24)); float(53) is refused rather than rounded |
Decimal | decimal, numeric, money, smallmoney, the integer types |
Money | money, smallmoney |
UUID | uniqueidentifier |
Text, String, lazy
Text | nchar, nvarchar, ntext, xml, and char, varchar, text
in UTF-8 or a supported single-byte code page |
ByteString, lazy
ByteString | binary, varbinary, image, and the undecoded bytes of char,
varchar, text |
LocalTime | datetime, smalldatetime, datetime2, date (at midnight) |
UTCTime | the same, taken to be UTC, and datetimeoffset, converted to UTC |
DateTimeOffset, ZonedTime | datetimeoffset |
Day | date |
TimeOfDay | time |
SqlValue
| any type but a CLR user-defined type; a sql_variant as its base
type |
RawValue | any type, as the bytes on the wire |
rowversion is binary(8) on the wire, json is nvarchar(max) and
vector is varchar: each decodes as what it is sent as. A CLR
user-defined type (hierarchyid, geometry, geography) decodes only
as RawValue; CAST it or call .ToString() in the query instead.
Encoding: ToSqlField
A parameter's wire type is fixed by its Haskell type. The server
converts it to the column's type where it can, but sending nvarchar
against a varchar index, or datetime2 against a datetime column,
can turn a seek into a scan; RawParam chooses the wire type yourself.
Nothing is a NULL of the type the bare type would be sent as.
| Haskell type | Sent as |
|---|---|
Bool | bit |
Word8 | tinyint |
Int8, Int16 | smallint |
Int32 | int |
Int, Int64 | bigint |
Integer
| bigint, or decimal(38,0) beyond its range; 39 digits or more is
an EncodeError |
Double | float |
Float | real |
Decimal | decimal(p,s) with the value's own scale and just enough precision |
Money | money |
UUID | uniqueidentifier |
Text, String, lazy
Text | nvarchar(4000), or nvarchar(max) beyond 4000 UTF-16 code units
|
ByteString, lazy
ByteString | varbinary(8000), or varbinary(max) beyond 8000 bytes
|
LocalTime | datetime2(7) |
UTCTime | datetime2(7) holding the UTC clock time |
DateTimeOffset, ZonedTime | datetimeoffset(7); the offset must be within ±14:00 |
Day | date |
TimeOfDay | time(7) |
SqlValue
| the type its constructor names; SqlNull as an
nvarchar(max) NULL |
RawParam | the TypeInfo given, with the bytes given |
Date and time values are rounded to 100 ns as the server rounds them;
a value the server would reject (a year outside 1 through 9999, an
offset beyond ±14:00) is an EncodeError before anything
is sent. A NULL of Text, SqlParam or RawParam is nvarchar(max),
which the server converts to most types but not to binary or
sql_variant: send a typed NULL (Nothing :: Maybe Int32) there.
When a string's declared type matters
A string or binary parameter's declared type depends on the value's length, as the table shows, and on whether it is NULL. The declared type doesn't limit what can be sent, and it doesn't change the result of storing the value in a column or comparing it with one. It does matter in three cases:
- An expression typed by the parameter.
SELECT @p1returns the declared type, and concatenating bounded types stops at their limit without an error. With two values of 3000 characters,@p1 + @p2returns 4000 characters andCAST(@p1 AS nvarchar(max)) + @p2returns 6000. Binary values stop at 8000 bytes in the same way. Cast a parameter whose result can outgrow the limit. - A
sql_varianttarget. The server won't convert a(max)type tosql_variant, so a longer value or an untyped NULL fails with error 206. - Plan caching. The server caches a plan per declaration, so one statement can have one plan for short values and another for long values and NULLs. Values within one declaration share a plan whatever their lengths.
Synopsis
- newtype Money = Money (Fixed E4)
- data DateTimeOffset = DateTimeOffset {
- dtoUtcTime :: !UTCTime
- dtoOffsetMinutes :: !Int16
- dateTimeOffsetToZonedTime :: DateTimeOffset -> ZonedTime
- zonedTimeToDateTimeOffset :: ZonedTime -> DateTimeOffset
- newtype RawValue = RawValue ByteString
- newtype RawParam = RawParam (TypeInfo, RawBytes)
- data TypeInfo
- = TINull
- | TIBit
- | TIInt1
- | TIInt2
- | TIInt4
- | TIInt8
- | TIMoney4
- | TIMoney8
- | TIDateTime4
- | TIDateTime8
- | TIFlt4
- | TIFlt8
- | TIBitN
- | TIIntN !Word8
- | TIMoneyN !Word8
- | TIDateTimeN !Word8
- | TIFltN !Word8
- | TIGuid
- | TIDecimalN !Precision !Scale
- | TINumericN !Precision !Scale
- | TIDateN
- | TITimeN !Scale
- | TIDateTime2N !Scale
- | TIDateTimeOffsetN !Scale
- | TIBigChar !Word16 !Collation
- | TIBigVarChar !Word16 !Collation
- | TIText !Word32 !Collation
- | TINChar !Word16 !Collation
- | TINVarChar !Word16 !Collation
- | TINText !Word32 !Collation
- | TIBigBinary !Word16
- | TIBigVarBinary !Word16
- | TIImage !Word32
- | TIXml !(Maybe XmlSchema)
- | TISqlVariant !Word32
- | TIUdt !UdtInfo
- data Collation = Collation {
- collationInfo :: !Word32
- collationSortId :: !Word8
- data ColumnInfo = ColumnInfo {
- columnName :: !Text
- columnType :: !TypeInfo
- columnFlags :: !Word16
- columnUserType :: !Word32
- columnTableName :: !(Maybe [Text])
- columnIsNullable :: ColumnInfo -> Bool
- columnIsLong :: ColumnInfo -> Bool
- data ServerMessage = ServerMessage {}
hmssql's own value types
A money or smallmoney value: a fixed-point number with four
decimal places, exactly as the server stores it.
Instances
| Num Money Source # | |
| Fractional Money Source # | |
| Real Money Source # | |
Defined in Hmssql.Tds.Primitives Methods toRational :: Money -> Rational # | |
| RealFrac Money Source # | |
| Show Money Source # | |
| Eq Money Source # | |
| Ord Money Source # | |
| FromSqlField Money Source # |
|
Defined in Hmssql.Decoding Methods fieldDecoder :: FieldDecoder Money Source # fieldRowDecoder :: RowDecoder Money Source # nullableRowDecoder :: RowDecoder (Maybe Money) rowDecoderWith :: Nulls Money b -> RowDecoder b | |
| ToSqlField Money Source # |
|
Defined in Hmssql.Encoding | |
data DateTimeOffset Source #
A datetimeoffset value: an instant plus the UTC offset it was recorded with.
Constructors
| DateTimeOffset | |
Fields
| |
Instances
dateTimeOffsetToZonedTime :: DateTimeOffset -> ZonedTime Source #
The same instant as a ZonedTime in the value's offset.
zonedTimeToDateTimeOffset :: ZonedTime -> DateTimeOffset Source #
An offset beyond the range of Int16 is clamped to it rather than
wrapped (so it stays outside the ±14:00 a datetimeoffset allows, and
putDateTimeOffset rejects it).
The bytes as they are
The bytes of any column's value as the server sends them, including the CLR types hmssql doesn't otherwise decode.
Constructors
| RawValue ByteString |
Instances
| Show RawValue Source # | |
| Eq RawValue Source # | |
| FromSqlField RawValue Source # | |
Defined in Hmssql.Decoding Methods fieldDecoder :: FieldDecoder RawValue Source # fieldRowDecoder :: RowDecoder RawValue Source # nullableRowDecoder :: RowDecoder (Maybe RawValue) rowDecoderWith :: Nulls RawValue b -> RowDecoder b | |
Send a value with an explicit wire type.
Instances
| ToSqlField RawParam Source # | |
Defined in Hmssql.Encoding Methods fieldEncoder :: FieldEncoder RawParam Source # fieldParam :: RawParam -> FieldParams -> FieldParams | |
The wire's view of a type
A type as the wire describes it (MS-TDS 2.2.5.4 TYPE_INFO): the
token's type byte with its length, precision and scale, or collation,
as the server sends it in COLMETADATA and a client sends it for a
parameter. The wire has fewer types than T-SQL (int arrives as
TIIntN 4 or TIInt4; json and vector as character types):
Hmssql.SqlType names the declared type. RawParam takes one to send
a value with a wire type of your choosing.
Constructors
Instances
MS-TDS 2.2.5.1.2 Collation Rule Definition. Kept opaque: the LCID, flags and version share a DWORD, followed by the sort ID byte.
Constructors
| Collation | |
Fields
| |
data ColumnInfo Source #
A column of a result set, as its COLMETADATA describes it.
Constructors
| ColumnInfo | |
Fields
| |
Instances
| Show ColumnInfo Source # | |
Defined in Hmssql.Tds.Token Methods showsPrec :: Int -> ColumnInfo -> ShowS # show :: ColumnInfo -> String # showList :: [ColumnInfo] -> ShowS # | |
| Eq ColumnInfo Source # | |
Defined in Hmssql.Tds.Token | |
columnIsNullable :: ColumnInfo -> Bool Source #
Whether the column may hold NULL.
columnIsLong :: ColumnInfo -> Bool Source #
Whether a column's values may be of any length: a max type, xml, json, a
CLR type, or a legacy text, ntext or image, which arrive in pieces rather
than behind a length of at most 8,000 bytes. Such a column is the one to read
with a FoldingCell or streamField.
data ServerMessage Source #
A message from the server: an error (SqlServerError)
or an informational message (PRINT output, a low-severity message;
connectOnInfoMessage).
Constructors
| ServerMessage | |
Fields
| |
Instances
| Show ServerMessage Source # | |
Defined in Hmssql.Tds.Token Methods showsPrec :: Int -> ServerMessage -> ShowS # show :: ServerMessage -> String # showList :: [ServerMessage] -> ShowS # | |
| Eq ServerMessage Source # | |
Defined in Hmssql.Tds.Token Methods (==) :: ServerMessage -> ServerMessage -> Bool # (/=) :: ServerMessage -> ServerMessage -> Bool # | |