示例#1
0
 public static Guid ReadGuid(BinaryReader reader)
 {
     var buf = new DriverRowData.ValueHolder16Bytes {Lo = reader.ReadInt64(), Hi = reader.ReadInt64()};
     return buf.AsGuid;
 }
        private object ReadCollection(DbType dbType, BinaryReader reader, StringBuilder stringBuilder)
        {
            var itemCount = RowData.Read7BitEncodedInt(reader);

            switch (dbType)
            {
                //case DbType.VarNumeric:
                //    break;
                case DbType.AnsiString:
                case DbType.String:
                case DbType.AnsiStringFixedLength:
                case DbType.StringFixedLength:
                case DbType.Xml:
                    {
                        var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

                        for (var x = 0; x < itemCount; x++)
                        {
                            var len = RowData.Read7BitEncodedInt(reader);
                            if (len > 0)
                            {
                                stringBuilder.Clear();
                                stringBuilder.EnsureCapacity(len);
                                for (var i = 0; i < len; i++)
                                {
                                    stringBuilder.Append((char) RowData.Read7BitEncodedInt(reader));
                                }
                                result.Add(stringBuilder.ToString());
                            }
                            else if (len >= -1)
                            {
                                // client supplies -1 to indicate that collection element is null
                                // ignore ReSharper's warning about notnull, no exception here

                                // ReSharper disable AssignNullToNotNullAttribute
                                result.Add(len == 0 ? string.Empty : null);
                                // ReSharper restore AssignNullToNotNullAttribute
                            }
                            else
                            {
                                throw new Exception("Invalid length value: " + len);
                            }
                        }
                        return result;
                    }

                case DbType.Binary:
                case DbType.Object:
                    {
                        var result = new HashSet<SizableArrayOfByte>(SizableArrayOfByte.DefaultComparer.Instance);

                        for (var x = 0; x < itemCount; x++)
                        {
                            var len = RowData.Read7BitEncodedInt(reader);
                            if (len >= 0)
                            {
                                var data = new SizableArrayOfByte();
                                data.SetLength(len);
                                var bytesRead = 0;
                                while (bytesRead < data.Length)
                                {
                                    var count = reader.Read(data.Data, bytesRead, data.Length - bytesRead);
                                    if (count == 0)
                                    {
                                        throw new DataException("Unexpected end of stream");
                                    }
                                    bytesRead += count;
                                }

                                result.Add(data);
                            }
                            else if (len == -1)
                            {
                                // client supplies -1 to indicate that collection element is null
                                // ignore ReSharper's warning about notnull, no exception here

                                // ReSharper disable AssignNullToNotNullAttribute
                                result.Add(null);
                                // ReSharper restore AssignNullToNotNullAttribute
                            }
                            else
                            {
                                throw new Exception("Invalid length value: " + len);
                            }
                        }
                        return result;
                    }

                case DbType.SByte:
                    {
                        var result = new HashSet<SByte>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add((sbyte)reader.ReadByte());
                        }
                        return result;
                    }

                case DbType.Byte:
                    {
                        var result = new HashSet<Byte>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadByte());
                        }
                        return result;
                    }

                case DbType.Boolean:
                    {
                        var result = new HashSet<bool>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadBoolean());
                        }
                        return result;
                    }

                case DbType.Decimal:
                case DbType.Currency:
                    {
                        var result = new HashSet<Decimal>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadDecimal());
                        }
                        return result;
                    }

                case DbType.Guid:
                    {
                        var result = new HashSet<Guid>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            var buf = new DriverRowData.ValueHolder16Bytes { Lo = reader.ReadInt64(), Hi = reader.ReadInt64() };
                            result.Add(buf.AsGuid);
                        }
                        return result;
                    }

                case DbType.DateTimeOffset:
                    {
                        var result = new HashSet<DateTimeOffset>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            var buf = new DriverRowData.ValueHolder16Bytes {Lo = reader.ReadInt64(), Hi = reader.ReadInt64()};
                            result.Add(buf.AsDateTimeOffset);
                        }
                        return result;
                    }

                case DbType.Int16:
                    {
                        var result = new HashSet<Int16>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadInt16());
                        }
                        return result;
                    }

                case DbType.UInt16:
                    {
                        var result = new HashSet<UInt16>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadUInt16());
                        }
                        return result;
                    }

                case DbType.Int32:
                    {
                        var result = new HashSet<Int32>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadInt32());
                        }
                        return result;
                    }

                case DbType.UInt32:
                    {
                        var result = new HashSet<UInt32>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadUInt32());
                        }
                        return result;
                    }

                case DbType.Single:
                    {
                        var result = new HashSet<Single>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadSingle());
                        }
                        return result;
                    }

                case DbType.Date:
                case DbType.DateTime:
                case DbType.DateTime2:
                    {
                        var result = new HashSet<DateTime>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(DateTime.FromBinary(reader.ReadInt64()));
                        }
                        return result;
                    }
                case DbType.Time:
                    {
                        var result = new HashSet<TimeSpan>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(new TimeSpan(reader.ReadInt64()));
                        }
                        return result;
                    }

                case DbType.Int64:
                    {
                        var result = new HashSet<Int64>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadInt64());
                        }
                        return result;
                    }

                case DbType.UInt64:
                    {
                        var result = new HashSet<UInt64>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadUInt64());
                        }
                        return result;
                    }

                case DbType.Double:
                    {
                        var result = new HashSet<Double>();
                        for (var x = 0; x < itemCount; x++)
                        {
                            result.Add(reader.ReadDouble());
                        }
                        return result;
                    }

                default:
                    throw new DataException("Invalid DbType: " + dbType);
            }
        }