|
| 1 | +using System.Data; |
| 2 | +using System.Data.SqlTypes; |
| 3 | +using Microsoft.Data.SqlClient; |
| 4 | +using Microsoft.Data.SqlClient.Server; |
| 5 | +using Microsoft.SqlServer.Types; |
| 6 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 7 | + |
| 8 | +namespace SqlServerSimulator; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// <c>sql_variant</c> and CLR-UDT (<c>hierarchyid</c> / <c>geography</c> / |
| 12 | +/// <c>geometry</c>) columns inside a table-valued parameter. Once the RPC value |
| 13 | +/// decoders and the COLMETADATA-shaped column decoder share one implementation |
| 14 | +/// (<c>TdsWireValue</c>), a TVP column carrying a self-describing |
| 15 | +/// <c>sql_variant</c> body or a PLP-carried UDT value decodes through the same |
| 16 | +/// path an RPC <c>sql_variant</c> / UDT parameter uses. Wire shapes probe-captured |
| 17 | +/// against SQL Server 2025 + SqlClient 7.0.2 through a cleartext tee proxy |
| 18 | +/// (2026-07-19): the <c>sql_variant</c> column TYPE_INFO is <c>0x62</c> + a 4-byte |
| 19 | +/// max length with a 4-byte-total-length (0 = NULL) body value; the UDT column |
| 20 | +/// TYPE_INFO is <c>0xF0</c> + three B_VARCHARs (db / schema / type) with a PLP |
| 21 | +/// value — both the same forms as the matching RPC parameters. |
| 22 | +/// </summary> |
| 23 | +/// <remarks> |
| 24 | +/// These drive the TVP through the <c>sp_executesql</c> text path (the parameter |
| 25 | +/// binds as its own <c>@rows</c> table variable). Routing a LOB-backed TVP column |
| 26 | +/// (<c>geography</c> / <c>geometry</c>, like <c>nvarchar(max)</c>) through a |
| 27 | +/// stored-procedure READONLY parameter hits a pre-existing table-variable LOB-copy |
| 28 | +/// gap in proc-parameter binding, unrelated to the wire decode; see |
| 29 | +/// docs/claude/tds-endpoint.md. |
| 30 | +/// </remarks> |
| 31 | +[TestClass] |
| 32 | +public sealed class TvpVariantUdtColumnTests |
| 33 | +{ |
| 34 | + public TestContext TestContext { get; set; } = null!; |
| 35 | + |
| 36 | + // geometry POINT(3 4): the 22-byte single-point MS spatial binary |
| 37 | + // (SqlGeometry.STGeomFromText is unavailable on managed Types under Linux, so |
| 38 | + // geometry rides raw WKB bytes — the shape DacFx / bulk-import consumers send). |
| 39 | + private const string GeometryPointWkbHex = "00000000010C00000000000008400000000000001040"; |
| 40 | + |
| 41 | + private static async Task InsertViaTvp(SqlConnection connection, string typeName, IEnumerable<SqlDataRecord> rows, CancellationToken token) |
| 42 | + { |
| 43 | + await using var command = new SqlCommand("insert into dbo.sink select * from @rows", connection); |
| 44 | + var parameter = command.Parameters.AddWithValue("@rows", rows); |
| 45 | + parameter.SqlDbType = SqlDbType.Structured; |
| 46 | + parameter.TypeName = typeName; |
| 47 | + _ = await command.ExecuteNonQueryAsync(token); |
| 48 | + } |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public async Task SqlVariantColumn_PerBaseType_RoundTripsWithBaseType() |
| 52 | + { |
| 53 | + var simulation = new Simulation(); |
| 54 | + Wire.ExecInProc(simulation, "create type dbo.VarRows as table (id int, v sql_variant)"); |
| 55 | + Wire.ExecInProc(simulation, "create table dbo.sink (id int, v sql_variant)"); |
| 56 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 57 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 58 | + |
| 59 | + var metadata = new[] { new SqlMetaData("id", SqlDbType.Int), new SqlMetaData("v", SqlDbType.Variant) }; |
| 60 | + var rows = new List<SqlDataRecord>(); |
| 61 | + foreach (var (id, value) in new (int, object?)[] { (1, 4242), (2, "hello"), (3, 3.5d), (4, null) }) |
| 62 | + { |
| 63 | + var record = new SqlDataRecord(metadata); |
| 64 | + record.SetInt32(0, id); |
| 65 | + if (value is null) |
| 66 | + record.SetDBNull(1); |
| 67 | + else |
| 68 | + record.SetValue(1, value); |
| 69 | + rows.Add(record); |
| 70 | + } |
| 71 | + |
| 72 | + await InsertViaTvp(connection, "dbo.VarRows", rows, TestContext.CancellationToken); |
| 73 | + |
| 74 | + var read = Wire.Drain(await new SqlCommand( |
| 75 | + "select id, v, sql_variant_property(v, 'BaseType') from dbo.sink order by id", |
| 76 | + connection).ExecuteReaderAsync(TestContext.CancellationToken)); |
| 77 | + HasCount(4, read); |
| 78 | + AreEqual(4242, read[0][1]); |
| 79 | + AreEqual("int", read[0][2]); |
| 80 | + AreEqual("hello", read[1][1]); |
| 81 | + AreEqual("nvarchar", read[1][2]); |
| 82 | + AreEqual(3.5d, read[2][1]); |
| 83 | + AreEqual("float", read[2][2]); |
| 84 | + IsNull(read[3][1]); |
| 85 | + } |
| 86 | + |
| 87 | + [TestMethod] |
| 88 | + public async Task HierarchyIdColumn_RoundTripsToPathAndDataLength() |
| 89 | + { |
| 90 | + var simulation = new Simulation(); |
| 91 | + Wire.ExecInProc(simulation, "create type dbo.HierRows as table (id int, h hierarchyid)"); |
| 92 | + Wire.ExecInProc(simulation, "create table dbo.sink (id int, h hierarchyid)"); |
| 93 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 94 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 95 | + |
| 96 | + var metadata = new[] { new SqlMetaData("id", SqlDbType.Int), new SqlMetaData("h", SqlDbType.Udt, typeof(SqlHierarchyId), "hierarchyid") }; |
| 97 | + var record = new SqlDataRecord(metadata); |
| 98 | + record.SetInt32(0, 1); |
| 99 | + record.SetValue(1, SqlHierarchyId.Parse(new SqlString("/1/2/"))); |
| 100 | + |
| 101 | + await InsertViaTvp(connection, "dbo.HierRows", [record], TestContext.CancellationToken); |
| 102 | + |
| 103 | + await using var reader = await new SqlCommand("select h.ToString(), datalength(h) from dbo.sink", connection) |
| 104 | + .ExecuteReaderAsync(TestContext.CancellationToken); |
| 105 | + IsTrue(await reader.ReadAsync(TestContext.CancellationToken)); |
| 106 | + AreEqual("/1/2/", reader.GetString(0)); |
| 107 | + AreEqual(2, reader.GetInt32(1)); |
| 108 | + } |
| 109 | + |
| 110 | + [TestMethod] |
| 111 | + public async Task GeographyColumn_RoundTripsToWkt() |
| 112 | + { |
| 113 | + var simulation = new Simulation(); |
| 114 | + Wire.ExecInProc(simulation, "create type dbo.GeoRows as table (id int, g geography)"); |
| 115 | + Wire.ExecInProc(simulation, "create table dbo.sink (id int, g geography)"); |
| 116 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 117 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 118 | + |
| 119 | + var metadata = new[] { new SqlMetaData("id", SqlDbType.Int), new SqlMetaData("g", SqlDbType.Udt, typeof(SqlGeography), "geography") }; |
| 120 | + var record = new SqlDataRecord(metadata); |
| 121 | + record.SetInt32(0, 1); |
| 122 | + record.SetValue(1, SqlGeography.STGeomFromText(new SqlChars("POINT(-122.3 47.6)"), 4326)); |
| 123 | + |
| 124 | + await InsertViaTvp(connection, "dbo.GeoRows", [record], TestContext.CancellationToken); |
| 125 | + |
| 126 | + AreEqual("POINT (-122.3 47.6)", await new SqlCommand("select g.ToString() from dbo.sink", connection) |
| 127 | + .ExecuteScalarAsync(TestContext.CancellationToken)); |
| 128 | + } |
| 129 | + |
| 130 | + [TestMethod] |
| 131 | + public async Task GeometryColumn_RoundTripsToWkt() |
| 132 | + { |
| 133 | + var simulation = new Simulation(); |
| 134 | + Wire.ExecInProc(simulation, "create type dbo.GeomRows as table (id int, g geometry)"); |
| 135 | + Wire.ExecInProc(simulation, "create table dbo.sink (id int, g geometry)"); |
| 136 | + await using var listener = await simulation.ListenAsync(0, TestContext.CancellationToken); |
| 137 | + await using var connection = await Wire.OpenAsync(listener, TestContext.CancellationToken); |
| 138 | + |
| 139 | + var metadata = new[] { new SqlMetaData("id", SqlDbType.Int), new SqlMetaData("g", SqlDbType.Udt, typeof(SqlGeometry), "geometry") }; |
| 140 | + var record = new SqlDataRecord(metadata); |
| 141 | + record.SetInt32(0, 1); |
| 142 | + record.SetSqlBytes(1, new SqlBytes(Convert.FromHexString(GeometryPointWkbHex))); |
| 143 | + |
| 144 | + await InsertViaTvp(connection, "dbo.GeomRows", [record], TestContext.CancellationToken); |
| 145 | + |
| 146 | + AreEqual("POINT (3 4)", await new SqlCommand("select g.ToString() from dbo.sink", connection) |
| 147 | + .ExecuteScalarAsync(TestContext.CancellationToken)); |
| 148 | + } |
| 149 | +} |
0 commit comments