diff --git a/src/Microsoft.SqlTools.ServiceLayer/EditData/UpdateManagement/CellUpdate.cs b/src/Microsoft.SqlTools.ServiceLayer/EditData/UpdateManagement/CellUpdate.cs index 823d3902..1b5516de 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/EditData/UpdateManagement/CellUpdate.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/EditData/UpdateManagement/CellUpdate.cs @@ -38,8 +38,7 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement // Check for null if (valueAsString == NullString) { - Value = DBNull.Value; - ValueAsString = valueAsString; + ProcessNullValue(); } else if (columnType == typeof(byte[])) { @@ -197,6 +196,18 @@ namespace Microsoft.SqlTools.ServiceLayer.EditData.UpdateManagement ValueAsString = Value.ToString(); } + private void ProcessNullValue() + { + // Make sure that nulls are allowed if we set it to null + if (!Column.AllowDBNull.HasTrue()) + { + throw new InvalidOperationException(SR.EditDataNullNotAllowed); + } + + Value = DBNull.Value; + ValueAsString = NullString; + } + #endregion } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.cs b/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.cs index ac5414e5..58b86a8e 100755 --- a/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.cs @@ -525,6 +525,14 @@ namespace Microsoft.SqlTools.ServiceLayer } } + public static string EditDataNullNotAllowed + { + get + { + return Keys.GetString(Keys.EditDataNullNotAllowed); + } + } + public static string EE_BatchSqlMessageNoProcedureInfo { get @@ -1079,6 +1087,9 @@ namespace Microsoft.SqlTools.ServiceLayer public const string EditDataInitializeInProgress = "EditDataInitializeInProgress"; + public const string EditDataNullNotAllowed = "EditDataNullNotAllowed"; + + public const string EE_BatchSqlMessageNoProcedureInfo = "EE_BatchSqlMessageNoProcedureInfo"; diff --git a/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.resx b/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.resx index addb71fb..ecc3b2c3 100755 --- a/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.resx +++ b/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.resx @@ -435,6 +435,10 @@ Another edit data initialize is in progress for this owner URI. Please wait for completion. + + NULL is not allowed for this column + + Msg {0}, Level {1}, State {2}, Line {3} diff --git a/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.strings b/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.strings index 12792b5d..aeac66c1 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.strings +++ b/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.strings @@ -206,6 +206,8 @@ EditDataComputedColumnPlaceholder = EditDataInitializeInProgress = Another edit data initialize is in progress for this owner URI. Please wait for completion. +EditDataNullNotAllowed = NULL is not allowed for this column + ############################################################################ # DacFx Resources diff --git a/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.xlf b/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.xlf index c5cd03c9..2ede8d52 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.xlf +++ b/src/Microsoft.SqlTools.ServiceLayer/Localization/sr.xlf @@ -551,6 +551,11 @@ Cannot add row to result buffer, data reader does not contain rows + + NULL is not allowed for this column + NULL is not allowed for this column + + \ No newline at end of file diff --git a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/EditData/CellUpdateTests.cs b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/EditData/CellUpdateTests.cs index 647575fe..064329cf 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.UnitTests/EditData/CellUpdateTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.UnitTests/EditData/CellUpdateTests.cs @@ -31,11 +31,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData } [Fact] - public void NullStringTest() + public void NullStringAllowedTest() { - // If: I attempt to create a CellUpdate to set it to NULL (with mixed cases) + // If: I attempt to create a CellUpdate to set it to NULL const string nullString = "NULL"; - DbColumnWrapper col = GetWrapper("ntext"); + DbColumnWrapper col = GetWrapper("ntext", true); CellUpdate cu = new CellUpdate(col, nullString); // Then: The value should be a DBNull and the string value should be the same as what @@ -46,6 +46,14 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData Assert.Equal(col, cu.Column); } + [Fact] + public void NullStringNotAllowedTest() + { + // If: I attempt to create a cell update to set to null when its not allowed + // Then: I should get an exception thrown + Assert.Throws(() => new CellUpdate(GetWrapper("ntext", false), "NULL")); + } + [Fact] public void NullTextStringTest() { @@ -197,15 +205,16 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.EditData } } - private static DbColumnWrapper GetWrapper(string dataTypeName) + private static DbColumnWrapper GetWrapper(string dataTypeName, bool allowNull = true) { - return new DbColumnWrapper(new CellUpdateTestDbColumn(typeof(T), dataTypeName)); + return new DbColumnWrapper(new CellUpdateTestDbColumn(typeof(T), dataTypeName, allowNull)); } private class CellUpdateTestDbColumn : DbColumn { - public CellUpdateTestDbColumn(Type dataType, string dataTypeName) + public CellUpdateTestDbColumn(Type dataType, string dataTypeName, bool allowNull = true) { + AllowDBNull = allowNull; DataType = dataType; DataTypeName = dataTypeName; }