Skip to content

Commit ed24c4e

Browse files
committed
add hidden column test
dlegate metadata construction to common ctor from crypto ctor
1 parent d373bd1 commit ed24c4e

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/AlwaysEncryptedHelperClasses.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,9 @@ sealed internal partial class _SqlMetaDataSet
234234
internal readonly SqlTceCipherInfoTable? cekTable; // table of "column encryption keys" used for this metadataset
235235

236236
internal _SqlMetaDataSet(int count, SqlTceCipherInfoTable? cipherTable)
237+
: this(count)
237238
{
238239
cekTable = cipherTable;
239-
_metaDataArray = new _SqlMetaData[count];
240-
for (int i = 0; i < _metaDataArray.Length; ++i)
241-
{
242-
_metaDataArray[i] = new _SqlMetaData(i);
243-
}
244240
}
245241
}
246242

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderTest.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.Data;
78
using System.Text;
@@ -225,5 +226,81 @@ private static bool IsColumnBitSet(SqlConnection con, string selectQuery, int in
225226
}
226227
return columnSetPresent;
227228
}
229+
230+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
231+
public static void CheckHiddenColumns()
232+
{
233+
// hidden columns can be found by using CommandBehavious.KeyInfo or at the sql level
234+
// by using the FOR BROWSE option. These features return the column information requested and
235+
// also include any key information required to be able to find the row containing the data
236+
// requested. The additional key infomration is provided as hidden columns and can be seen using
237+
// the difference between VisibleFieldCount and FieldCount on the reader
238+
239+
string tempTableName = DataTestUtility.GenerateObjectName();
240+
241+
string createQuery = $@"
242+
create table [{tempTableName}] (
243+
user_id int not null identity(1,1),
244+
first_name varchar(100) null,
245+
last_name varchar(100) null);
246+
247+
alter table [{tempTableName}] add constraint pk_{tempTableName}_user_id primary key (user_id);
248+
249+
insert into [{tempTableName}] (first_name,last_name) values ('Joe','Smith')
250+
";
251+
252+
string dataQuery = $@"select first_name, last_name from [{tempTableName}]";
253+
254+
255+
int fieldCount = 0;
256+
int visibleFieldCount = 0;
257+
Type[] types = null;
258+
string[] names = null;
259+
260+
using (SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString))
261+
{
262+
connection.Open();
263+
264+
try
265+
{
266+
using (SqlCommand createCommand = new SqlCommand(createQuery, connection))
267+
{
268+
createCommand.ExecuteNonQuery();
269+
}
270+
271+
using (SqlCommand queryCommand = new SqlCommand(dataQuery, connection))
272+
{
273+
using (SqlDataReader reader = queryCommand.ExecuteReader(CommandBehavior.KeyInfo))
274+
{
275+
fieldCount = reader.FieldCount;
276+
visibleFieldCount = reader.VisibleFieldCount;
277+
types = new Type[fieldCount];
278+
names = new string[fieldCount];
279+
for (int index = 0; index < fieldCount; index++)
280+
{
281+
types[index] = reader.GetFieldType(index);
282+
names[index] = reader.GetName(index);
283+
}
284+
}
285+
}
286+
}
287+
finally
288+
{
289+
DataTestUtility.DropTable(connection, tempTableName);
290+
}
291+
}
292+
293+
Assert.Equal(3, fieldCount);
294+
Assert.Equal(2, visibleFieldCount);
295+
Assert.NotNull(types);
296+
Assert.NotNull(names);
297+
298+
// requested fields
299+
Assert.Contains("first_name", names, StringComparer.Ordinal);
300+
Assert.Contains("last_name", names, StringComparer.Ordinal);
301+
302+
// hidden field
303+
Assert.Contains("user_id", names, StringComparer.Ordinal);
304+
}
228305
}
229306
}

0 commit comments

Comments
 (0)