private Task WriteUnterminatedSqlValue(object value, MetaType type, int actualLength, int codePageByteSize, int offset, TdsParserStateObject stateObj)
{
Debug.Assert(((type.NullableType == TdsEnums.SQLXMLTYPE) ||
(value is INullable && !((INullable)value).IsNull)),
"unexpected null SqlType!");
// parameters are always sent over as BIG or N types
switch (type.NullableType)
{
case TdsEnums.SQLFLTN:
if (type.FixedLength == 4)
WriteFloat(((SqlSingle)value).Value, stateObj);
else
{
Debug.Assert(type.FixedLength == 8, "Invalid length for SqlDouble type!");
WriteDouble(((SqlDouble)value).Value, stateObj);
}
break;
case TdsEnums.SQLBIGBINARY:
case TdsEnums.SQLBIGVARBINARY:
case TdsEnums.SQLIMAGE:
{
if (type.IsPlp)
{
WriteInt(actualLength, stateObj); // chunk length
}
if (value is SqlBinary)
{
return stateObj.WriteByteArray(((SqlBinary)value).Value, actualLength, offset, canAccumulate: false);
}
else
{
Debug.Assert(value is SqlBytes);
return stateObj.WriteByteArray(((SqlBytes)value).Value, actualLength, offset, canAccumulate: false);
}
}
case TdsEnums.SQLUNIQUEID:
{
byte[] b = ((SqlGuid)value).ToByteArray();
Debug.Assert((actualLength == b.Length) && (actualLength == 16), "Invalid length for guid type in com+ object");
stateObj.WriteByteArray(b, actualLength, 0);
break;
}
case TdsEnums.SQLBITN:
{
Debug.Assert(type.FixedLength == 1, "Invalid length for SqlBoolean type");
if (((SqlBoolean)value).Value == true)
stateObj.WriteByte(1);
else
stateObj.WriteByte(0);
break;
}
case TdsEnums.SQLINTN:
if (type.FixedLength == 1)
stateObj.WriteByte(((SqlByte)value).Value);
else
if (type.FixedLength == 2)
WriteShort(((SqlInt16)value).Value, stateObj);
else
if (type.FixedLength == 4)
WriteInt(((SqlInt32)value).Value, stateObj);
else
{
Debug.Assert(type.FixedLength == 8, "invalid length for SqlIntN type: " + type.FixedLength.ToString(CultureInfo.InvariantCulture));
WriteLong(((SqlInt64)value).Value, stateObj);
}
break;
case TdsEnums.SQLBIGCHAR:
case TdsEnums.SQLBIGVARCHAR:
case TdsEnums.SQLTEXT:
if (type.IsPlp)
{
WriteInt(codePageByteSize, stateObj); // chunk length
}
if (value is SqlChars)
{
String sch = new String(((SqlChars)value).Value);
return WriteEncodingChar(sch, actualLength, offset, _defaultEncoding, stateObj, canAccumulate: false);
}
else
{
Debug.Assert(value is SqlString);
return WriteEncodingChar(((SqlString)value).Value, actualLength, offset, _defaultEncoding, stateObj, canAccumulate: false);
}
case TdsEnums.SQLNCHAR:
case TdsEnums.SQLNVARCHAR:
case TdsEnums.SQLNTEXT:
case TdsEnums.SQLXMLTYPE:
if (type.IsPlp)
{
if (IsBOMNeeded(type, value))
{
WriteInt(actualLength + 2, stateObj); // chunk length
WriteShort(TdsEnums.XMLUNICODEBOM, stateObj);
}
else
{
WriteInt(actualLength, stateObj); // chunk length
}
}
// convert to cchars instead of cbytes
// Xml type is already converted to string through GetCoercedValue
if (actualLength != 0)
actualLength >>= 1;
if (value is SqlChars)
{
return WriteCharArray(((SqlChars)value).Value, actualLength, offset, stateObj, canAccumulate: false);
}
else
{
Debug.Assert(value is SqlString);
return WriteString(((SqlString)value).Value, actualLength, offset, stateObj, canAccumulate: false);
}
case TdsEnums.SQLNUMERICN:
Debug.Assert(type.FixedLength <= 17, "Decimal length cannot be greater than 17 bytes");
WriteSqlDecimal((SqlDecimal)value, stateObj);
break;
case TdsEnums.SQLDATETIMN:
SqlDateTime dt = (SqlDateTime)value;
if (type.FixedLength == 4)
{
if (0 > dt.DayTicks || dt.DayTicks > ushort.MaxValue)
throw SQL.SmallDateTimeOverflow(dt.ToString());
WriteShort(dt.DayTicks, stateObj);
WriteShort(dt.TimeTicks / SqlDateTime.SQLTicksPerMinute, stateObj);
}
else
{
WriteInt(dt.DayTicks, stateObj);
WriteInt(dt.TimeTicks, stateObj);
}
break;
case TdsEnums.SQLMONEYN:
{
WriteSqlMoney((SqlMoney)value, type.FixedLength, stateObj);
break;
}
case TdsEnums.SQLUDT:
throw ADP.DbTypeNotSupported(SqlDbType.Udt.ToString());
default:
Debug.Assert(false, "Unknown TdsType!" + type.NullableType.ToString("x2", (IFormatProvider)null));
break;
} // switch
// return point for accumulated writes, note: non-accumulated writes returned from their case statements
return null;
}