Skip to content

Commit

Permalink
Merge pull request dotnet/corefx#42122 from yukiwongky/readonlycol
Browse files Browse the repository at this point in the history
Fix getting readonly column metadata

Commit migrated from dotnet/corefx@c78b718
  • Loading branch information
yukiwongky authored Oct 25, 2019
2 parents f2d9957 + cf88b82 commit bd08c78
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private enum _SqlMetadataFlags : int
IsIdentity = 1 << 6,
IsColumnSet = 1 << 7,

IsReadOnlyMask = (Updatable | UpdateableUnknown) // two bit field (0 is read only, 1 is updatable, 2 is updatability unknown)
IsUpdatableMask = (Updatable | UpdateableUnknown) // two bit field (0 is read only, 1 is updatable, 2 is updatability unknown)
}

internal string column;
Expand Down Expand Up @@ -391,13 +391,13 @@ internal string tableName

public byte Updatability
{
get => (byte)(flags & _SqlMetadataFlags.IsReadOnlyMask);
get => (byte)(flags & _SqlMetadataFlags.IsUpdatableMask);
set => flags = (_SqlMetadataFlags)((value & 0x3) | ((int)flags & ~0x03));
}

public bool IsReadOnly
{
get => flags.HasFlag(_SqlMetadataFlags.IsReadOnlyMask);
get => (flags & _SqlMetadataFlags.IsUpdatableMask) == 0;
}

public bool IsDifferentName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Xunit;

Expand Down Expand Up @@ -1243,6 +1244,52 @@ public void TestDeriveParameters()
}
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public void TestReadOnlyColumnMetadata()
{
using (SqlConnection connection = new SqlConnection(DataTestUtility.TcpConnStr))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "Create table #t (ID int identity(1,1), sText varchar(12), sMemo as (convert(varchar,ID) + ' ' + sText))";
command.ExecuteNonQuery();
}
using (SqlDataAdapter dataAdapter = new SqlDataAdapter("Select * from #t", connection))
{
using (SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter))
{
using (SqlCommand cmd = commandBuilder.GetInsertCommand())
{
DataTestUtility.AssertEqualsWithDescription("INSERT INTO [#t] ([sText]) VALUES (@p1)", cmd.CommandText, "Unexpected insert command.");
}
}

using (DataTable dataTable = new DataTable())
{
dataAdapter.FillSchema(dataTable, SchemaType.Mapped);
Dictionary<string, bool> expAutoIncrement = new Dictionary<string, bool>()
{
{"ID", true},
{"sText", false},
{"sMemo", false}
};
Dictionary<string, bool> expReadOnly = new Dictionary<string, bool>()
{
{"ID", true},
{"sText", false},
{"sMemo", true}
};
foreach (DataColumn dataColumn in dataTable.Columns)
{
DataTestUtility.AssertEqualsWithDescription(dataColumn.AutoIncrement, expAutoIncrement[dataColumn.ColumnName], "Unexpected AutoIncrement metadata.");
DataTestUtility.AssertEqualsWithDescription(dataColumn.ReadOnly, expReadOnly[dataColumn.ColumnName], "Unexpected ReadOnly metadata.");
}
}
}
}
}

#region Utility_Methods
private void CheckParameters(SqlCommand cmd, string expectedResults)
{
Expand Down

0 comments on commit bd08c78

Please sign in to comment.