From 0dda7c81a07b7caf328a0e3067e79c0d1d05e4de Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 5 Dec 2018 12:18:07 +0100 Subject: [PATCH] Rename NEquals --- src/Umbraco.Core/ObjectExtensions.cs | 2 +- .../Querying/ExpressionVisitorBase.cs | 6 +++--- .../Implement/ContentTypeRepositoryBase.cs | 20 +++++++++---------- .../NPocoTests/NPocoSqlExtensionsTests.cs | 18 ++++++++--------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Umbraco.Core/ObjectExtensions.cs b/src/Umbraco.Core/ObjectExtensions.cs index 1cbb4a6ea1..cfd1eedb5a 100644 --- a/src/Umbraco.Core/ObjectExtensions.cs +++ b/src/Umbraco.Core/ObjectExtensions.cs @@ -797,7 +797,7 @@ namespace Umbraco.Core /// The value to use when any value is null. /// Do not use outside of Sql expressions. // see usage in ExpressionVisitorBase - public static bool NEquals(this T? value, T? other, T fallbackValue) + public static bool SqlNullableEquals(this T? value, T? other, T fallbackValue) where T : struct { return (value ?? fallbackValue).Equals(other ?? fallbackValue); diff --git a/src/Umbraco.Core/Persistence/Querying/ExpressionVisitorBase.cs b/src/Umbraco.Core/Persistence/Querying/ExpressionVisitorBase.cs index 493bbcacc7..16bfc9b164 100644 --- a/src/Umbraco.Core/Persistence/Querying/ExpressionVisitorBase.cs +++ b/src/Umbraco.Core/Persistence/Querying/ExpressionVisitorBase.cs @@ -656,13 +656,13 @@ namespace Umbraco.Core.Persistence.Querying // c# 'x == null' becomes sql 'x IS NULL' which is fine // c# 'x == y' becomes sql 'x = @0' which is fine - unless they are nullable types, // because sql 'x = NULL' is always false and the 'IS NULL' syntax is required, - // so for comparing nullable types, we use x.NEquals(y, fb) where fb is a fallback + // so for comparing nullable types, we use x.SqlNullableEquals(y, fb) where fb is a fallback // value which will be used when values are null - turning the comparison into // sql 'COALESCE(x,fb) = COALESCE(y,fb)' - of course, fb must be a value outside // of x and y range - and if that is not possible, then a manual comparison need // to be written - //TODO support NEquals with 0 parameters, using the full syntax below - case "NEquals": + //TODO support SqlNullableEquals with 0 parameters, using the full syntax below + case "SqlNullableEquals": var compareTo = Visit(m.Arguments[1]); var fallback = Visit(m.Arguments[2]); // that would work without a fallback value but is more cumbersome diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs index 7e720ba08a..cd9681fbda 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs @@ -762,7 +762,7 @@ AND umbracoNode.id <> @id", /// private void CopyTagData(int? sourceLanguageId, int? targetLanguageId, IReadOnlyCollection propertyTypeIds, IReadOnlyCollection contentTypeIds = null) { - // note: important to use NEquals for nullable types, cannot directly compare language identifiers + // note: important to use SqlNullableEquals for nullable types, cannot directly compare language identifiers // fixme - should we batch then? var whereInArgsCount = propertyTypeIds.Count + (contentTypeIds?.Count ?? 0); @@ -784,7 +784,7 @@ AND umbracoNode.id <> @id", sqlTagToDelete .WhereIn(x => x.PropertyTypeId, propertyTypeIds) - .Where(x => x.LanguageId.NEquals(targetLanguageId, -1)); + .Where(x => x.LanguageId.SqlNullableEquals(targetLanguageId, -1)); var sqlDeleteRel = Sql() .Delete() @@ -811,7 +811,7 @@ AND umbracoNode.id <> @id", sqlSelect .InnerJoin().On((tag, rel) => tag.Id == rel.TagId) - .LeftJoin("xtags").On((tag, xtag) => tag.Text == xtag.Text && tag.Group == xtag.Group && tag.LanguageId.NEquals(targetLanguageId, -1), aliasRight: "xtags"); + .LeftJoin("xtags").On((tag, xtag) => tag.Text == xtag.Text && tag.Group == xtag.Group && tag.LanguageId.SqlNullableEquals(targetLanguageId, -1), aliasRight: "xtags"); if (contentTypeIds != null) sqlSelect @@ -825,7 +825,7 @@ AND umbracoNode.id <> @id", sqlSelect .WhereIn(x => x.ContentTypeId, contentTypeIds); - sqlSelect.Where(x => x.LanguageId.NEquals(sourceLanguageId, -1)); + sqlSelect.Where(x => x.LanguageId.SqlNullableEquals(sourceLanguageId, -1)); var cols = Sql().Columns(x => x.Text, x => x.Group, x => x.LanguageId); var sqlInsertTag = Sql($"INSERT INTO {TagDto.TableName} ({cols})").Append(sqlSelect); @@ -840,8 +840,8 @@ AND umbracoNode.id <> @id", .AndSelect("otag", x => x.Id) .From() .InnerJoin().On((rel, tag) => rel.TagId == tag.Id) - .InnerJoin("otag").On((tag, otag) => tag.Text == otag.Text && tag.Group == otag.Group && otag.LanguageId.NEquals(targetLanguageId, -1), aliasRight: "otag") - .Where(x => x.LanguageId.NEquals(sourceLanguageId, -1)); + .InnerJoin("otag").On((tag, otag) => tag.Text == otag.Text && tag.Group == otag.Group && otag.LanguageId.SqlNullableEquals(targetLanguageId, -1), aliasRight: "otag") + .Where(x => x.LanguageId.SqlNullableEquals(sourceLanguageId, -1)); var cols2 = Sql().Columns(x => x.NodeId, x => x.PropertyTypeId, x => x.TagId); var sqlInsertRel = Sql($"INSERT INTO {TagRelationshipDto.TableName} ({cols2})").Append(sqlFoo); @@ -864,7 +864,7 @@ AND umbracoNode.id <> @id", sqlTagToDelete .WhereIn(x => x.PropertyTypeId, propertyTypeIds) - .Where(x => !x.LanguageId.NEquals(targetLanguageId, -1)); + .Where(x => !x.LanguageId.SqlNullableEquals(targetLanguageId, -1)); sqlDeleteRel = Sql() .Delete() @@ -891,7 +891,7 @@ AND umbracoNode.id <> @id", /// The content type identifiers. private void CopyPropertyData(int? sourceLanguageId, int? targetLanguageId, IReadOnlyCollection propertyTypeIds, IReadOnlyCollection contentTypeIds = null) { - // note: important to use NEquals for nullable types, cannot directly compare language identifiers + // note: important to use SqlNullableEquals for nullable types, cannot directly compare language identifiers // // fixme - should we batch then? var whereInArgsCount = propertyTypeIds.Count + (contentTypeIds?.Count ?? 0); @@ -920,7 +920,7 @@ AND umbracoNode.id <> @id", sqlDelete.WhereIn(x => x.VersionId, inSql); } - sqlDelete.Where(x => x.LanguageId.NEquals(targetLanguageId, -1)); + sqlDelete.Where(x => x.LanguageId.SqlNullableEquals(targetLanguageId, -1)); sqlDelete .WhereIn(x => x.PropertyTypeId, propertyTypeIds); @@ -944,7 +944,7 @@ AND umbracoNode.id <> @id", .InnerJoin().On((pdata, cversion) => pdata.VersionId == cversion.Id) .InnerJoin().On((cversion, c) => cversion.NodeId == c.NodeId); - sqlSelectData.Where(x => x.LanguageId.NEquals(sourceLanguageId, -1)); + sqlSelectData.Where(x => x.LanguageId.SqlNullableEquals(sourceLanguageId, -1)); sqlSelectData .WhereIn(x => x.PropertyTypeId, propertyTypeIds); diff --git a/src/Umbraco.Tests/Persistence/NPocoTests/NPocoSqlExtensionsTests.cs b/src/Umbraco.Tests/Persistence/NPocoTests/NPocoSqlExtensionsTests.cs index 9fbba01e94..de1e3ad5ad 100644 --- a/src/Umbraco.Tests/Persistence/NPocoTests/NPocoSqlExtensionsTests.cs +++ b/src/Umbraco.Tests/Persistence/NPocoTests/NPocoSqlExtensionsTests.cs @@ -52,14 +52,14 @@ namespace Umbraco.Tests.Persistence.NPocoTests .Where(x => (nid == null && x.LanguageId == null) || (nid != null && x.LanguageId == nid)); Assert.AreEqual("SELECT *\nFROM [umbracoPropertyData]\nWHERE ((((@0 is null) AND ([umbracoPropertyData].[languageId] is null)) OR ((@1 is not null) AND ([umbracoPropertyData].[languageId] = @2))))", sql.SQL, sql.SQL); - // new NEquals method does it automatically + // new SqlNullableEquals method does it automatically // 'course it would be nicer if '==' could do it - // see note in ExpressionVisitorBase for NEquals + // see note in ExpressionVisitorBase for SqlNullableEquals //sql = new Sql(SqlContext) // .Select("*") // .From() - // .Where(x => x.LanguageId.NEquals(nid)); + // .Where(x => x.LanguageId.SqlNullableEquals(nid)); //Assert.AreEqual("SELECT *\nFROM [umbracoPropertyData]\nWHERE ((((@0 is null) AND ([umbracoPropertyData].[languageId] is null)) OR ((@0 is not null) AND ([umbracoPropertyData].[languageId] = @0))))", sql.SQL, sql.SQL); // but, the expression above fails with SQL CE, 'specified argument for the function is not valid' in 'isnull' function @@ -68,22 +68,22 @@ namespace Umbraco.Tests.Persistence.NPocoTests sql = new Sql(SqlContext) .Select("*") .From() - .Where(x => x.LanguageId.NEquals(nid, -1)); + .Where(x => x.LanguageId.SqlNullableEquals(nid, -1)); Assert.AreEqual("SELECT *\nFROM [umbracoPropertyData]\nWHERE ((COALESCE([umbracoPropertyData].[languageId],@0) = COALESCE(@1,@0)))", sql.SQL, sql.SQL); } [Test] - public void NEqualsTest() + public void SqlNullableEqualsTest() { int? a, b; a = b = null; - Assert.IsTrue(a.NEquals(b, -1)); + Assert.IsTrue(a.SqlNullableEquals(b, -1)); b = 2; - Assert.IsFalse(a.NEquals(b, -1)); + Assert.IsFalse(a.SqlNullableEquals(b, -1)); a = 2; - Assert.IsTrue(a.NEquals(b, -1)); + Assert.IsTrue(a.SqlNullableEquals(b, -1)); b = null; - Assert.IsFalse(a.NEquals(b, -1)); + Assert.IsFalse(a.SqlNullableEquals(b, -1)); } [Test]