/// <summary> /// Deflate the token /// </summary> /// <param name="destination">Stream to deflate token to</param> public override void Deflate(Stream destination) { // Write token identifier destination.WriteByte((byte)TDSTokenType.DoneProcedure); // Write status TDSUtilities.WriteUShort(destination, (ushort)Status); // Write command TDSUtilities.WriteUShort(destination, (ushort)Command); // Write row count TDSUtilities.WriteULong(destination, RowCount); }
/// <summary> /// Deflate the token /// </summary> /// <param name="destination">Stream to deflate token to</param> public override void Deflate(Stream destination) { // Allocate temporary cache MemoryStream cache = new MemoryStream(); // Write header type TDSUtilities.WriteUShort(cache, (ushort)TDSHeaderType.TransactionDescriptor); // Write transaction descriptor TDSUtilities.WriteULong(cache, TransactionDescriptor); // Write outstanding request count TDSUtilities.WriteUInt(cache, OutstandingRequestCount); // Write the header length including self into the destination TDSUtilities.WriteUInt(destination, (uint)(cache.Length + 4)); // Write cached header data cache.WriteTo(destination); }
public static void DeflateTypeVarByte(Stream destination, TDSDataType column, object data, Object dataTypeSpecific) { // Dispatch further reading based on the type switch (column) { case TDSDataType.Null: { // No data associated with it break; } case TDSDataType.DateTime2N: { DateTime date = (DateTime)data; uint days = (uint)(date - new DateTime(1, 1, 1)).TotalDays; var seconds = (int)(date - new DateTime(date.Year, date.Month, date.Day)).TotalSeconds; ulong scaledSeconds; switch ((byte)dataTypeSpecific) { case 1: case 2: destination.WriteByte(6); scaledSeconds = (ulong)(seconds * Math.Pow(10, 3)); destination.WriteByte((byte)scaledSeconds); destination.WriteByte((byte)(scaledSeconds >> 8)); destination.WriteByte((byte)(scaledSeconds >> 16)); break; case 3: case 4: destination.WriteByte(7); scaledSeconds = (ulong)(seconds * Math.Pow(10, 4)); destination.WriteByte((byte)scaledSeconds); destination.WriteByte((byte)(scaledSeconds >> 8)); destination.WriteByte((byte)(scaledSeconds >> 16)); destination.WriteByte((byte)(scaledSeconds >> 24)); break; case 5: case 6: case 7: destination.WriteByte(8); scaledSeconds = (ulong)(seconds * Math.Pow(10, 5)); destination.WriteByte((byte)scaledSeconds); destination.WriteByte((byte)(scaledSeconds >> 8)); destination.WriteByte((byte)(scaledSeconds >> 16)); destination.WriteByte((byte)(scaledSeconds >> 24)); destination.WriteByte((byte)(scaledSeconds >> 32)); break; } destination.WriteByte((byte)days); destination.WriteByte((byte)(days >> 8)); destination.WriteByte((byte)(days >> 16)); break; } case TDSDataType.Bit: { destination.WriteByte((byte)((bool)data ? 1 : 0)); break; } case TDSDataType.Int1: { // Bit, 1 byte data representation destination.WriteByte((byte)data); break; } case TDSDataType.Int2: { // SmallInt, 2 byte data representation TDSUtilities.WriteUShort(destination, unchecked ((ushort)((short)data))); break; } case TDSDataType.Int4: { // Int, 4 byte data representation TDSUtilities.WriteUInt(destination, unchecked ((uint)((int)data))); break; } case TDSDataType.Float8: { // Float (8 byte data representation) byte[] floatBytes = BitConverter.GetBytes((double)data); destination.Write(floatBytes, 0, floatBytes.Length); break; } case TDSDataType.Int8: { // BigInt (8 byte data representation) TDSUtilities.WriteULong(destination, unchecked ((ulong)((long)data))); break; } case TDSDataType.BitN: { // Check if data is available if (data == null) { // No data destination.WriteByte(0); } else { // One byte data destination.WriteByte(1); // Data destination.WriteByte((byte)((bool)data ? 1 : 0)); } break; } case TDSDataType.IntN: { // Check if data is available if (data == null) { // No data destination.WriteByte(0); } else if (data is byte) { // One-byte data destination.WriteByte(1); // Bit data destination.WriteByte((byte)data); } else if (data is short) { // One-byte data destination.WriteByte(2); // Short data TDSUtilities.WriteUShort(destination, unchecked ((ushort)(short)data)); } else if (data is int) { // One-byte data destination.WriteByte(4); // Integer data TDSUtilities.WriteUInt(destination, unchecked ((uint)(int)data)); } else if (data is long) { // One-byte data destination.WriteByte(8); // Long data TDSUtilities.WriteULong(destination, unchecked ((ulong)(long)data)); } else { // We don't know how to deflate this integer throw new InvalidDataException(string.Format("Unable to deflate integer of type {0}", data.GetType().FullName)); } break; } case TDSDataType.Guid: { // Check if data is available if (data == null) { // No data destination.WriteByte(0); } else { // Get bytes byte[] guidBytes = ((Guid)data).ToByteArray(); // One byte data length destination.WriteByte((byte)guidBytes.Length); // Data destination.Write(guidBytes, 0, guidBytes.Length); } break; } case TDSDataType.BigChar: case TDSDataType.BigVarChar: { // Check if data is available if (data == null) { // No data TDSUtilities.WriteUShort(destination, 0xFFFF); } else { // Get bytes byte[] textBytes = Encoding.ASCII.GetBytes((string)data); // One data length TDSUtilities.WriteUShort(destination, (ushort)textBytes.Length); // Data destination.Write(textBytes, 0, textBytes.Length); } break; } case TDSDataType.NVarChar: { // Check if data is available if (data == null) { // No data TDSUtilities.WriteUShort(destination, 0xFFFF); } else { // Get bytes byte[] textBytes = Encoding.Unicode.GetBytes((string)data); // One data length TDSUtilities.WriteUShort(destination, (ushort)textBytes.Length); // Data destination.Write(textBytes, 0, textBytes.Length); } break; } case TDSDataType.BigVarBinary: case TDSDataType.BigBinary: { // Check if data is available if (data == null) { // No data TDSUtilities.WriteUShort(destination, 0xFFFF); } else { // Get bytes byte[] bytes = (byte[])data; // One data length TDSUtilities.WriteUShort(destination, (ushort)bytes.Length); // Data destination.Write(bytes, 0, bytes.Length); } break; } case TDSDataType.Xml: { // Check if data is available if (data == null) { // No data TDSUtilities.WriteUShort(destination, 0xFFFF); } else { // Get bytes byte[] bytes = Encoding.UTF8.GetBytes((string)data); // One data length TDSUtilities.WriteUShort(destination, (ushort)bytes.Length); // Data destination.Write(bytes, 0, bytes.Length); } break; } default: { // We don't know this type throw new NotImplementedException(string.Format("Unrecognized data type {0} for deflation", column)); } } }
/// <summary> /// Deflate the column into the stream /// </summary> /// <param name="destination">Stream to deflate token to</param> /// <param name="column">Column metadata</param> /// <param name="data">Column value</param> protected virtual void DeflateColumn(Stream destination, TDSColumnData column, object data) { // Dispatch further reading based on the type switch (column.DataType) { case TDSDataType.Null: { // No data associated with it break; } case TDSDataType.Bit: { destination.WriteByte((byte)((bool)data ? 1 : 0)); break; } case TDSDataType.Int1: { // Bit, 1 byte data representation destination.WriteByte((byte)data); break; } case TDSDataType.Int2: { // SmallInt, 2 byte data representation TDSUtilities.WriteUShort(destination, unchecked ((ushort)((short)data))); break; } case TDSDataType.Int4: { // Int, 4 byte data representation TDSUtilities.WriteUInt(destination, unchecked ((uint)((int)data))); break; } case TDSDataType.Float8: { // Float (8 byte data representation) byte[] floatBytes = BitConverter.GetBytes((double)data); destination.Write(floatBytes, 0, floatBytes.Length); break; } case TDSDataType.Int8: { // BigInt (8 byte data representation) TDSUtilities.WriteULong(destination, unchecked ((ulong)((long)data))); break; } case TDSDataType.BitN: { // Check if data is available if (data == null) { // No data destination.WriteByte(0); } else { // One byte data destination.WriteByte(1); // Data destination.WriteByte((byte)((bool)data ? 1 : 0)); } break; } case TDSDataType.IntN: { // Check if data is available if (data == null) { // No data destination.WriteByte(0); } else if (data is byte) { // One-byte data destination.WriteByte(1); // Bit data destination.WriteByte((byte)data); } else if (data is short) { // One-byte data destination.WriteByte(2); // Short data TDSUtilities.WriteUShort(destination, unchecked ((ushort)(short)data)); } else if (data is int) { // One-byte data destination.WriteByte(4); // Integer data TDSUtilities.WriteUInt(destination, unchecked ((uint)(int)data)); } else if (data is long) { // One-byte data destination.WriteByte(8); // Long data TDSUtilities.WriteULong(destination, unchecked ((ulong)(long)data)); } else { // We don't know how to deflate this integer throw new InvalidDataException(string.Format("Unable to deflate integer of type {0}", data.GetType().FullName)); } break; } case TDSDataType.Guid: { // Check if data is available if (data == null) { // No data destination.WriteByte(0); } else { // Get bytes byte[] guidBytes = ((Guid)data).ToByteArray(); // One byte data length destination.WriteByte((byte)guidBytes.Length); // Data destination.Write(guidBytes, 0, guidBytes.Length); } break; } case TDSDataType.BigChar: case TDSDataType.BigVarChar: { // Check if data is available if (data == null) { // No data TDSUtilities.WriteUShort(destination, 0xFFFF); } else { // Get bytes byte[] textBytes = Encoding.ASCII.GetBytes((string)data); // One data length TDSUtilities.WriteUShort(destination, (ushort)textBytes.Length); // Data destination.Write(textBytes, 0, textBytes.Length); } break; } case TDSDataType.NVarChar: { // Check if data is available if (data == null) { // No data TDSUtilities.WriteUShort(destination, 0xFFFF); } else { // Get bytes byte[] textBytes = Encoding.Unicode.GetBytes((string)data); // One data length TDSUtilities.WriteUShort(destination, (ushort)textBytes.Length); // Data destination.Write(textBytes, 0, textBytes.Length); } break; } case TDSDataType.BigVarBinary: case TDSDataType.BigBinary: { // Check if data is available if (data == null) { // No data TDSUtilities.WriteUShort(destination, 0xFFFF); } else { // Get bytes byte[] bytes = (byte[])data; // One data length TDSUtilities.WriteUShort(destination, (ushort)bytes.Length); // Data destination.Write(bytes, 0, bytes.Length); } break; } default: { // We don't know this type throw new NotImplementedException(string.Format("Unrecognized data type {0} for deflation", column.DataType)); } } }