Exemple #1
0
        public void GetValue()
        {
            var readState = new RecordReadState();
            var type = new SqlBit(readState, CompressionContext.NoCompression);

            // No bytes read - length is one
            Assert.AreEqual(1, type.FixedLength);

            // Load byte and check length is 0
            readState.LoadBitByte(0xD2);
            Assert.AreEqual(0, type.FixedLength);

            Assert.IsFalse((bool)type.GetValue(new byte[0]));
            Assert.IsTrue((bool)type.GetValue(new byte[0]));
            Assert.IsFalse((bool)type.GetValue(new byte[0]));
            Assert.IsFalse((bool)type.GetValue(new byte[0]));
            Assert.IsTrue((bool)type.GetValue(new byte[0]));
            Assert.IsFalse((bool)type.GetValue(new byte[0]));
            Assert.IsTrue((bool)type.GetValue(new byte[0]));

            // One bit left - length should still be 0
            Assert.AreEqual(0, type.FixedLength);

            Assert.IsTrue((bool)type.GetValue(new byte[0]));

            // All bits consumed - length should be 1
            Assert.AreEqual(1, type.FixedLength);
        }
        public void General()
        {
            var state = new RecordReadState();

            // No bits available
            Assert.IsTrue(state.AllBitsConsumed);

            state.LoadBitByte(0xD2); // 11010010

            // Bits available
            Assert.IsFalse(state.AllBitsConsumed);

            // Reading bit values
            Assert.IsFalse(state.GetNextBit());
            Assert.IsTrue(state.GetNextBit());
            Assert.IsFalse(state.GetNextBit());
            Assert.IsFalse(state.GetNextBit());
            Assert.IsTrue(state.GetNextBit());
            Assert.IsFalse(state.GetNextBit());
            Assert.IsTrue(state.GetNextBit());

            // One bit left
            Assert.IsFalse(state.AllBitsConsumed);

            Assert.IsTrue(state.GetNextBit());

            // Bits exhausted, ready for next byte
            Assert.IsTrue(state.AllBitsConsumed);
        }
        internal IEnumerable<Row> GetEntities(Row schema, CompressionContext compression)
        {
            for (int i = 0; i < Records.Length; i++)
            {
                var record = Records[i];

                short fixedOffset = 0;
                short variableColumnIndex = 0;
                int columnIndex = 0;
                var readState = new RecordReadState();
                var dataRow = schema.NewRow();

                foreach (DataColumn col in dataRow.Columns)
                {
                    var sqlType = SqlTypeFactory.Create(col, readState, compression);
                    object columnValue = null;

                    if (sqlType.IsVariableLength)
                    {
                        if (!record.HasNullBitmap || !record.NullBitmap[columnIndex])
                        {
                            // If a nullable varlength column does not have a value, it may be not even appear in the varlength column array if it's at the tail
                            if (record.VariableLengthColumnData.Count <= variableColumnIndex)
                                columnValue = sqlType.GetValue(new byte[] { });
                            else
                                columnValue = sqlType.GetValue(record.VariableLengthColumnData[variableColumnIndex].GetBytes().ToArray());
                        }

                        variableColumnIndex++;
                    }
                    else
                    {
                        // Must cache type FixedLength as it may change after getting a value (e.g. SqlBit)
                        short fixedLength = sqlType.FixedLength.Value;

                        if (!record.HasNullBitmap || !record.NullBitmap[columnIndex])
                            columnValue = sqlType.GetValue(record.FixedLengthData.Skip(fixedOffset).Take(fixedLength).ToArray());

                        fixedOffset += fixedLength;
                    }

                    columnIndex++;
                    dataRow[col] = columnValue;
                }

                yield return dataRow;
            }
        }
Exemple #4
0
 public SqlBit(RecordReadState readState, CompressionContext compression)
     : base(compression)
 {
     this.readState = readState;
 }
Exemple #5
0
        public static ISqlType Create(DataColumn column, RecordReadState readState, CompressionContext compression)
        {
            switch(column.Type)
            {
                case ColumnType.Binary:
                    return new SqlBinary((short)column.VariableFixedLength, compression);

                case ColumnType.BigInt:
                    return new SqlBigInt(compression);

                case ColumnType.Bit:
                    return new SqlBit(readState, compression);

                case ColumnType.Char:
                    return new SqlChar((short)column.VariableFixedLength, compression);

                case ColumnType.DateTime:
                    return new SqlDateTime(compression);

                case ColumnType.Decimal:
                    return new SqlDecimal(column.Precision, column.Scale, compression);

                case ColumnType.Image:
                    return new SqlImage(compression);

                case ColumnType.Int:
                    return new SqlInt(compression);

                case ColumnType.Money:
                    return new SqlMoney(compression);

                case ColumnType.NChar:
                    return new SqlNChar((short)column.VariableFixedLength, compression);

                case ColumnType.NText:
                    return new SqlNText(compression);

                case ColumnType.NVarchar:
                    return new SqlNVarchar(compression);

                case ColumnType.RID:
                    return new SqlRID(compression);

                case ColumnType.SmallDatetime:
                    return new SqlSmallDateTime(compression);

                case ColumnType.SmallInt:
                    return new SqlSmallInt(compression);

                case ColumnType.SmallMoney:
                    return new SqlSmallMoney(compression);

                case ColumnType.Text:
                    return new SqlText(compression);

                case ColumnType.TinyInt:
                    return new SqlTinyInt(compression);

                case ColumnType.UniqueIdentifier:
                    return new SqlUniqueIdentifier(compression);

                case ColumnType.Uniquifier:
                    return new SqlUniquifier(compression);

                case ColumnType.VarBinary:
                    return new SqlVarBinary(compression);

                case ColumnType.Varchar:
                    return new SqlVarchar(compression);

                case ColumnType.Variant:
                    return new SqlVariant(compression);
            }

            throw new ArgumentException("Unsupported type: " + column);
        }