Exemplo n.º 1
0
        public static void SqlErrorAndInfo(this TdsPackageReader reader, byte token, int tokenLength)
        {
            var start = reader.GetReadPos();
            var error = new SqlInfoAndError
            {
                Number    = reader.ReadInt32(),
                State     = reader.ReadByte(),
                Class     = reader.ReadByte(),
                Message   = reader.ReadString(reader.ReadUInt16()),
                Server    = reader.ReadString(reader.ReadByte()),
                Procedure = reader.ReadString(reader.ReadByte())
            };
            var current = reader.GetReadPos();

            error.LineNumber = tokenLength - (current - start) > 2 ? reader.ReadInt32() : reader.ReadInt16();
            if (error.Class >= TdsEnums.MIN_ERROR_CLASS)
            {
                throw new Exception(error.Message);
            }
            reader.CurrentSession.Errors.Add(error);
        }
Exemplo n.º 2
0
        public static void EnvChange(this TdsPackageReader reader, int tokenLength, Action <int> transactionAction)
        {
            // There could be multiple environment change messages following this token.
            var processedLength = 0;

            var startpos = reader.GetReadPos();

            while (tokenLength > processedLength)
            {
                var type = reader.ReadByte();
                switch (type)
                {
                case TdsEnums.ENV_DATABASE:
                case TdsEnums.ENV_LANG:
                    reader.EnvLanguage();
                    break;

                case TdsEnums.ENV_CHARSET:
                    reader.EnvCharset();
                    break;

                case TdsEnums.ENV_PACKETSIZE:
                    reader.EnvPackageSize();
                    break;

                case TdsEnums.ENV_LOCALEID:
                    throw new NotImplementedException();

                case TdsEnums.ENV_COMPFLAGS:
                    throw new NotImplementedException();

                case TdsEnums.ENV_COLLATION:
                    reader.EnvCollation();
                    break;

                case TdsEnums.ENV_BEGINTRAN:
                case TdsEnums.ENV_COMMITTRAN:
                case TdsEnums.ENV_ROLLBACKTRAN:
                case TdsEnums.ENV_ENLISTDTC:
                case TdsEnums.ENV_DEFECTDTC:
                case TdsEnums.ENV_TRANSACTIONENDED:
                    if (transactionAction == null)
                    {
                        reader.EnvSqlTransaction();
                    }
                    else
                    {
                        transactionAction(type);
                    }
                    break;

                case TdsEnums.ENV_LOGSHIPNODE:
                    throw new NotImplementedException();

                case TdsEnums.ENV_PROMOTETRANSACTION:
                    throw new NotImplementedException();

                case TdsEnums.ENV_TRANSACTIONMANAGERADDRESS:
                case TdsEnums.ENV_SPRESETCONNECTIONACK:
                    throw new NotImplementedException();

                case TdsEnums.ENV_USERINSTANCE:
                    throw new NotImplementedException();

                case TdsEnums.ENV_ROUTING:
                    throw new NotImplementedException();

                default:
                    Debug.Assert(false, "Unknown environment change token: " + type);
                    break;
                }

                processedLength = reader.GetReadPos() - startpos;
            }
        }
Exemplo n.º 3
0
        private void TestIntN(object value, int tdsType, bool nulltest, byte precision = 0, byte scale = 0, bool isPlp = false)
        {
            var stream = new TestStream();
            var writer = new TdsPackageWriter(stream);
            var reader = new TdsPackageReader(stream);

            SetupColMetaData(reader, writer, tdsType, precision, scale, isPlp);
            var columwriter  = new TdsColumnWriter(writer);
            var columnReader = new TdsColumnReader(reader);

            object result;

            if (nulltest)
            {
                writer.NewPackage(TdsEnums.MT_RPC);
                ObjectWriter(columwriter, tdsType, value, nulltest);
                writer.SendLastMessage();
                result = ObjectReader(columnReader, value);
                Assert.Null(result);
            }

            writer.NewPackage(TdsEnums.MT_RPC);
            ObjectWriter(columwriter, tdsType, value, false);
            writer.SendLastMessage();
            result = ObjectReader(columnReader, value);
            switch (value)
            {
            case Money v:
                Assert.Equal((decimal)v, result);
                break;

            case Money4 v:
                Assert.Equal((decimal)v, result);
                break;

            case SqlDate v:
                Assert.Equal((DateTime)v, result);
                break;

            case SqlDateTime2 v:
                Assert.Equal((DateTime)v, result);
                break;

            case SqlDateTime4 v:
                Assert.Equal((DateTime)v, result);
                break;

            case SqlImage v:
                Assert.Equal((byte[])v, result);
                break;

            case SqlUnicode v:
                Assert.Equal((string)v, result);
                break;

            case SqlXml v:
                Assert.Equal((string)v, result);
                break;

            case SqlVariant v:
                switch (v.Value)
                {
                case bool b: Assert.Equal(b, result); break;

                case byte b: Assert.Equal(b, result); break;

                case short b: Assert.Equal(b, result); break;

                case int b: Assert.Equal(b, result); break;

                case long b: Assert.Equal(b, result); break;

                case float b: Assert.Equal(b, result); break;

                case double b: Assert.Equal(b, result); break;

                case DateTime b: Assert.Equal(b, result); break;

                case Guid b: Assert.Equal(b, result); break;

                case decimal b: Assert.Equal(b, result); break;

                case byte[] b: Assert.Equal(b, result); break;

                case string b: Assert.Equal(b, result); break;

                case TimeSpan b: Assert.Equal(b, result); break;

                case DateTimeOffset b: Assert.Equal(b, result); break;

                default:
                    Assert.False(true);
                    break;
                }

                break;

            default:
                Assert.Equal(value, result);
                break;
            }

            Assert.Equal(reader.GetReadEndPos(), reader.GetReadPos());
            if (!new[]
            {
                TdsEnums.SQLBIGBINARY,
                TdsEnums.SQLBIGVARBINARY,
                TdsEnums.SQLBIGVARCHAR,
                TdsEnums.SQLBIGCHAR,
                TdsEnums.SQLTEXT,
                TdsEnums.SQLNVARCHAR,
                TdsEnums.SQLNTEXT,
                TdsEnums.SQLNCHAR,
                TdsEnums.SQLXMLTYPE,
                TdsEnums.SQLIMAGE,
            }.Contains(tdsType) && !(value is SqlVariant v1 && (v1.Value is string s || v1.Value is byte[])))
            {
                Assert.InRange(reader.GetReadPos() - 8, 0, TdsEnums.MaxSizeSqlValue);
            }
        }