Beispiel #1
0
        public static void SendRpc(this TdsPackageWriter writer, SqlCollations defaultCollation, FormattableString sql, long sqlConnectionId)
        {
            writer.NewPackage(TdsEnums.MT_RPC);
            writer.WriteMarsHeader(sqlConnectionId);

            writer.WriteInt16(0xffff);
            writer.WriteInt16(TdsEnums.RPC_PROCID_EXECUTESQL);

            // Options
            writer.WriteInt16(TdsEnums.RPC_PARAM_DEFAULT);

            // Stream out parameters
            var parameters = CreateParameters(sql);

            foreach (var parameter in parameters)
            {
                // parameters can be unnamed
                var param  = parameter;
                var value  = parameter.Value;
                var isNull = value == null;

                writer.WriteByteLenString(param.Name);

                // Write parameter status
                writer.WriteByte(parameter.Status);
                var p  = parameter;
                var mt = p.MetaData;
                WriteTdsTypeInfo(writer, mt, p.Size, p.IsNull, defaultCollation, p.Scale);
                WriteValue(writer, value, mt, isNull);
            }

            writer.SendLastMessage();
        }
 public static void SendTransactionCommit(this TdsPackageWriter writer, long sqlTransactionId)
 {
     WriteTransactionHeader(writer, TdsEnums.TransactionManagerRequestType.Commit, sqlTransactionId);
     writer.WriteByte(0); // No xact name
     writer.WriteByte(0); // No flags
     writer.SendLastMessage();
 }
        public static void SendExecuteBatch(this TdsPackageWriter tdsPackageWriter, string text, long sqlConnectionId)
        {
            tdsPackageWriter.NewPackage(TdsEnums.MT_SQL);

            tdsPackageWriter.WriteMarsHeader(sqlConnectionId);

            tdsPackageWriter.WriteUnicodeString(text);
            tdsPackageWriter.SendLastMessage();
        }
        public static void SendTransactionBegin(this TdsPackageWriter writer, TdsEnums.TransactionManagerIsolationLevel isoLevel)
        {
            var transactionName = "";

            WriteTransactionHeader(writer, TdsEnums.TransactionManagerRequestType.Begin, 0);
            writer.WriteByte((byte)isoLevel);
            writer.WriteByte((byte)(transactionName.Length * 2)); // Write number of bytes (unicode string).
            writer.WriteUnicodeString(transactionName);
            writer.SendLastMessage();
        }
        public static void SendTransactionRollback(this TdsPackageWriter writer, long sqlTransactionId)
        {
            var transactionName = "";

            WriteTransactionHeader(writer, TdsEnums.TransactionManagerRequestType.Rollback, sqlTransactionId);

            writer.WriteByte((byte)(transactionName.Length * 2)); // Write number of bytes (unicode string).
            writer.WriteUnicodeString(transactionName);
            writer.WriteByte(0);                                  // No flags
            writer.SendLastMessage();
        }
Beispiel #6
0
        public static void SendPreLoginHandshake(this TdsPackageWriter writer, string instanceName, bool marsOn)
        {
            // PreLoginHandshake buffer consists of:
            // 1) Standard header, with type = MT_PRELOGIN
            // 2) Consecutive 5 bytes for each option, (1 byte length, 2 byte offset, 2 byte buffer length)
            // 3) Consecutive data blocks for each option

            // NOTE: packet data needs to be big endian - not the standard little endian used by
            // the rest of the parser.

            writer.NewPackage(TdsEnums.MT_PRELOGIN);
            WriteHeader(writer, instanceName);
            WriteOptions(writer, instanceName, marsOn);

            // Write out last option - to let server know the second part of packet completed
            writer.SendLastMessage();
        }
        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);
            }
        }
Beispiel #8
0
 public static void SendSspi(this TdsPackageWriter writer, byte[] sspiData)
 {
     writer.NewPackage(TdsEnums.MT_SSPI);
     writer.WriteByteArray(sspiData);
     writer.SendLastMessage();
 }