示例#1
0
        private static object GetMoneyValue(TdsResponseStream stream, TdsType type)
        {
            int len;

            if (type == TdsType.TDS_MONEY)
            {
                len = 8;
            }
            else if (type == TdsType.TDS_MONEYN)
            {
                len = stream.Read();
            }
            else
            {
                len = 4;
            }

            long?x = null;

            if (len == 4)
            {
                x = stream.ReadInt();
            }
            else if (len == 8)
            {
                int hi = stream.ReadInt();
                int lo = stream.ReadInt();
                x = lo | (hi << 32);
            }

            return(x.HasValue ? new decimal(x.Value) * 0.0001m : (decimal?)null);
        }
示例#2
0
        public static void ReadFormat(FormatToken.Format f, TdsResponseStream stream)
        {
            var t = _types[f.Type];

            if (t.FormatFormat == FormatFormat.LenOneByte || t.FormatFormat == FormatFormat.Decimal)
            {
                f.Len = stream.Read();
            }

            if (t.FormatFormat == FormatFormat.LenFourBytes || t.FormatFormat == FormatFormat.Image)
            {
                f.Len = stream.ReadInt();
            }

            if (t.FormatFormat == FormatFormat.Image)
            {
                int nameLen = stream.ReadShort();
                f.ObjectName = stream.ReadString(nameLen);
            }

            if (t.FormatFormat == FormatFormat.Decimal)
            {
                f.Pecision = stream.Read();
                f.Scale    = stream.Read();
            }

            int localLen = stream.Read();

            f.LocaleInfo = stream.ReadString(localLen);

            // todo: blob
        }
示例#3
0
        private TdsToken CreateReturnStatus(TokenDescriptor tokenDesc, TdsResponseStream stream, Encoding encoder, FormatToken lastFormat)
        {
            int value = stream.ReadInt();

            return(new ReturnStatusToken(tokenDesc.TokenType)
            {
                Value = value,
            });
        }
示例#4
0
        private static object GetDatetimeValue(TdsResponseStream stream, TdsType type)
        {
            int len;
            int dayTicks;
            int timeTicks;
            int minutes;

            if (type == TdsType.TDS_DATETIMEN)
            {
                len = stream.Read();
            }
            else if (type == TdsType.TDS_SHORTDATE)
            {
                len = 4;
            }
            else
            {
                len = 8;
            }

            switch (len)
            {
            case 8:
                dayTicks  = stream.ReadInt();
                timeTicks = stream.ReadInt();
                return(new System.Data.SqlTypes.SqlDateTime(dayTicks, timeTicks).Value);

            case 4:
                dayTicks = stream.ReadShort();
                minutes  = stream.ReadShort();
                return(new DateTime(1900, 1, 1).AddDays(dayTicks).AddMinutes(minutes));

            default:
            case 0:
                return(null);
            }
        }
示例#5
0
        private TdsToken CreateNotImplementedToken(TokenDescriptor tokenDesc, TdsResponseStream stream)
        {
            int len = 0;

            switch (tokenDesc.Len)
            {
            case TokenLength.OneByteLen:
                len = stream.Read();
                stream.SkipRead(len);
                len += 1;
                break;

            case TokenLength.TwoByteLen:
                len = stream.ReadShort();
                stream.SkipRead(len);
                len += 2;
                break;

            case TokenLength.FourByteLen:
                len = stream.ReadInt();
                stream.SkipRead(len);
                len += 4;
                break;

            case TokenLength.OneByte:
                len = 1;
                stream.SkipRead(len);
                break;

            case TokenLength.FourBytes:
                len = 4;
                stream.SkipRead(len);
                break;

            case TokenLength.EigthBytes:
                len = 8;
                stream.SkipRead(len);
                break;

            case TokenLength.Dependent:
            default:
                _logger?.LogError($"Unknown length of token of type {tokenDesc.TokenType}");
                throw new Exception($"Unknown length of token of type {tokenDesc.TokenType}");
            }

            _logger?.LogWarning($"Not implemented token {tokenDesc.TokenType} of len {len}");
            return(new UnknownToken(tokenDesc.TokenType, len));
        }
示例#6
0
        private TdsToken CreateDone(TokenDescriptor tokenDesc, TdsResponseStream stream, Encoding encoder, FormatToken lastFormat)
        {
            int status    = stream.ReadShort();
            int tranState = stream.ReadShort();
            int count     = stream.ReadInt();

            return(new DoneToken(tokenDesc.TokenType)
            {
                More = (status & 1) != 0,
                Error = (status & 2) != 0,
                InTransaction = (status & 4) != 0,
                Proc = (status & 8) != 0,
                Count = (status & 16) != 0,
                AttentionAck = (status & 32) != 0,
                //Event = (status & 64) != 0,

                TransactionState = (DoneToken.TransactionStatus)tranState,

                RowCount = (status & 16) != 0 ? count : (int?)null,
            });
        }
示例#7
0
        private TdsToken CreateTextMessage(TokenDescriptor tokenDesc, TdsResponseStream stream, Encoding encoder, FormatToken lastFormat)
        {
            int len       = stream.ReadShort();
            int msgNumber = stream.ReadInt();
            int state     = stream.Read();
            int severity  = stream.Read();

            int sqlStatusLen = stream.Read();
            var sqlStatus    = stream.ReadBytes(sqlStatusLen);

            int status    = stream.Read();
            int tranState = stream.ReadShort(); // discarded

            int msgLen = stream.ReadShort();
            var msg    = stream.ReadString(msgLen);

            int srvNameLen = stream.Read();
            var serverName = stream.ReadString(srvNameLen);

            int procNameLen = stream.Read();
            var procName    = stream.ReadString(procNameLen);

            int lineNumber = stream.ReadShort();

            return(new TextMessageToken(tokenDesc.TokenType)
            {
                MessageNumber = msgNumber,
                State = state,
                Severity = severity,
                ParamsFollows = status == 1,
                Message = msg,
                ServerName = serverName,
                ProcName = procName,
                LineNumber = lineNumber,
            });
        }
示例#8
0
        private TdsToken CreateFormat(TokenDescriptor tokenDesc, TdsResponseStream stream, Encoding encoder, FormatToken lastFormat)
        {
            int len;

            if (tokenDesc.Len == TokenLength.FourByteLen)
            {
                len = stream.ReadInt();
            }
            else
            {
                len = stream.ReadShort();
            }

            int numFormats = stream.ReadShort();

            var formats = new List <FormatToken.Format>();

            for (int i = 0; i < numFormats; i++)
            {
                var f = new FormatToken.Format
                {
                    Params = tokenDesc.TokenType == TokenType.TDS_PARAMFMT || tokenDesc.TokenType == TokenType.TDS_PARAMFMT2,
                };

                int nameLen = stream.Read();
                f.Name = stream.ReadString(nameLen);

                if (tokenDesc.TokenType == TokenType.TDS_ROWFMT2)
                {
                    int catalogNameLen = stream.Read();
                    f.CatalogName = stream.ReadString(catalogNameLen);
                    int schemaNameLen = stream.Read();
                    f.SchemaName = stream.ReadString(schemaNameLen);
                    int tableNameLen = stream.Read();
                    f.TableName = stream.ReadString(tableNameLen);
                    int columnNameLen = stream.Read();
                    f.ColumnName = stream.ReadString(columnNameLen);
                }

                if (tokenDesc.Len == TokenLength.FourByteLen)
                {
                    f.Status = stream.ReadInt();
                }
                else
                {
                    f.Status = stream.Read();
                }

                f.UserType = (UserDataType)stream.ReadInt();
                f.Type     = (TdsType)stream.Read();

                TypeReader.ReadFormat(f, stream);

                formats.Add(f);
            }

            return(new FormatToken(tokenDesc.TokenType)
            {
                Formats = formats,
            });
        }
示例#9
0
        public static object ReadData(FormatToken.Format f, TdsResponseStream stream, Encoding encoder)
        {
            int len;

            switch (f.Type)
            {
            case TdsType.TDS_INTN:
            case TdsType.TDS_UINTN:     // lets ignore unsigned for now
                switch (stream.Read())
                {
                case 1:
                    return(stream.Read());

                case 2:
                    return(stream.ReadShort());

                case 4:
                    return(stream.ReadInt());

                case 8:
                    return(stream.ReadLong());
                }
                break;

            case TdsType.TDS_INT1:
            case TdsType.TDS_UINT1:
                return(stream.Read());

            case TdsType.TDS_INT2:
            case TdsType.TDS_UINT2:
                return(stream.ReadShort());

            case TdsType.TDS_INT4:
            case TdsType.TDS_UINT4:
                return(stream.ReadInt());

            case TdsType.TDS_INT8:
            case TdsType.TDS_UINT8:
                return(stream.ReadLong());

            case TdsType.TDS_BIT:
                return(stream.Read() != 0);

            case TdsType.TDS_DATETIMEN:
            case TdsType.TDS_DATETIME:
            case TdsType.TDS_SHORTDATE:
                return(GetDatetimeValue(stream, f.Type));

            case TdsType.TDS_FLT4:
                return(BitConverter.Int32BitsToSingle(stream.ReadInt()));

            case TdsType.TDS_FLT8:
                return(BitConverter.Int64BitsToDouble(stream.ReadLong()));

            case TdsType.TDS_FLTN:
                len = stream.Read();

                if (len == 4)
                {
                    return(BitConverter.Int32BitsToSingle(stream.ReadInt()));
                }
                else if (len == 8)
                {
                    return(BitConverter.Int64BitsToDouble(stream.ReadLong()));
                }

                break;

            case TdsType.TDS_SHORTMONEY:
            case TdsType.TDS_MONEY:
            case TdsType.TDS_MONEYN:
                return(GetMoneyValue(stream, f.Type));

            case TdsType.TDS_DECN:
            case TdsType.TDS_NUMN:
                return(GetDecimalValue(stream, f.Type, f));

            case TdsType.TDS_LONGBINARY:
                len = stream.ReadInt();
                return(stream.ReadBytes(len));

            case TdsType.TDS_LONGCHAR:
                len = stream.ReadInt();
                if (len > 0)
                {
                    return(encoder.GetString(stream.ReadBytes(len)));
                }
                break;

            case TdsType.TDS_IMAGE:
                len = stream.Read();
                if (len > 0)
                {
                    var txtPtr    = stream.ReadBytes(len);
                    var timeStamp = stream.ReadBytes(8);
                    int dataLen   = stream.ReadInt();
                    return(stream.ReadBytes(dataLen));
                }
                break;

            case TdsType.TDS_TEXT:
                len = stream.Read();
                if (len > 0)
                {
                    var txtPtr    = stream.ReadBytes(len);
                    var timeStamp = stream.ReadBytes(8);
                    int dataLen   = stream.ReadInt();
                    return(encoder.GetString(stream.ReadBytes(dataLen)));
                }
                break;

            case TdsType.TDS_CHAR:
            case TdsType.TDS_VARCHAR:
                len = stream.Read();
                if (len > 0)
                {
                    return(encoder.GetString(stream.ReadBytes(len)));
                }
                break;

            case TdsType.TDS_BINARY:
            case TdsType.TDS_VARBINARY:
            case TdsType.TDS_BOUNDARY:
            case TdsType.TDS_SENSITIVITY:
                len = stream.Read();
                return(stream.ReadBytes(len));

            case TdsType.TDS_VOID:
            case TdsType.TDS_BLOB:
            default:
                throw new NotImplementedException();
            }

            return(null);
        }