From 52dca204ee223fb8495dcd1e3fae03a4b1129fbd Mon Sep 17 00:00:00 2001 From: Mohammad Dameer Date: Fri, 5 Jul 2024 19:22:48 +0300 Subject: [PATCH] Fix graphql where clause when database schema is configured (#16364) Co-authored-by: mdameer Co-authored-by: Hisham Bin Ateya Co-authored-by: Tony Han --- .../Queries/Predicates/PredicateQuery.cs | 4 +- .../GraphQL/Queries/PredicateQueryTests.cs | 81 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 test/OrchardCore.Tests/Apis/GraphQL/Queries/PredicateQueryTests.cs diff --git a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Predicates/PredicateQuery.cs b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Predicates/PredicateQuery.cs index e4acee87f10..0909c25f1e8 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Predicates/PredicateQuery.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.GraphQL/Queries/Predicates/PredicateQuery.cs @@ -127,7 +127,7 @@ public string GetColumnName(string propertyPath) { // Switch the given alias in the path with the mapped alias. // aliasPart.alias -> AliasPartIndex.Alias - return Dialect.QuoteForTableName($"{tableAlias}", _configuration.Schema) + "." + Dialect.QuoteForColumnName(columnName); + return $"{Dialect.QuoteForAliasName(tableAlias)}.{Dialect.QuoteForColumnName(columnName)}"; } } else @@ -135,7 +135,7 @@ public string GetColumnName(string propertyPath) // no property provider exists; hope sql is case-insensitive (will break postgres; property providers must be supplied for postgres) // Switch the given alias in the path with the mapped alias. // aliasPart.Alias -> AliasPartIndex.alias - return Dialect.QuoteForTableName($"{tableAlias}", _configuration.Schema) + "." + Dialect.QuoteForColumnName(values[0]); + return $"{Dialect.QuoteForAliasName(tableAlias)}.{Dialect.QuoteForColumnName(values.Last())}"; } } diff --git a/test/OrchardCore.Tests/Apis/GraphQL/Queries/PredicateQueryTests.cs b/test/OrchardCore.Tests/Apis/GraphQL/Queries/PredicateQueryTests.cs new file mode 100644 index 00000000000..99b567547dc --- /dev/null +++ b/test/OrchardCore.Tests/Apis/GraphQL/Queries/PredicateQueryTests.cs @@ -0,0 +1,81 @@ +using OrchardCore.ContentManagement.GraphQL.Queries; +using OrchardCore.ContentManagement.GraphQL.Queries.Predicates; +using YesSql.Indexes; + +namespace OrchardCore.Tests.Apis.GraphQL; + +public class PredicateQueryTests +{ + private YesSql.IConfiguration _configuration; + + public PredicateQueryTests() + { + _configuration = new Configuration() + .UseSqlServer("Fake database connection string for testing;", "TenantSchema") + .SetTablePrefix("Tenant1"); + } + + [Fact] + public void ShouldReturnQuotedColumnNameWhenAliasNotExists() + { + // Arrange + var predicateQuery = new PredicateQuery(_configuration, []); + + // Act + var columnName = predicateQuery.GetColumnName("ListItemIndex.Value"); + + // Assert + Assert.Equal("[ListItemIndex.Value]", columnName); + } + + [Fact] + public void ShouldReturnQuotedAliasColumnNameWhenAliasExists() + { + // Arrange + var predicateQuery = new PredicateQuery(_configuration, []); + predicateQuery.CreateAlias("ListItemIndexPath.ValuePath", "ListItemIndexAlias.ValueAlias"); + + // Act + var columnName = predicateQuery.GetColumnName("ListItemIndexPath.ValuePath"); + + // Assert + Assert.Equal("[ListItemIndexAlias.ValueAlias]", columnName); + } + + [Fact] + public void ShouldReturnQuotedTableAliasAndColumnNameWhenProviderExists() + { + // Arrange + var predicateQuery = new PredicateQuery(_configuration, [new IndexPropertyProvider()]); + + predicateQuery.CreateAlias("ListItemIndexPath", nameof(ListItemIndex)); + predicateQuery.CreateTableAlias(nameof(ListItemIndex), "ListItemIndexAlias"); + + // Act + var columnName = predicateQuery.GetColumnName("ListItemIndexPath.Value"); + + // Assert + Assert.Equal("[ListItemIndexAlias].[Value]", columnName); + } + + [Fact] + public void ShouldReturnQuotedTableAliasAndPathWhenProviderNotExists() + { + // Arrange + var predicateQuery = new PredicateQuery(_configuration, []); + + predicateQuery.CreateAlias("ListItemIndexPath", nameof(ListItemIndex)); + predicateQuery.CreateTableAlias(nameof(ListItemIndex), "ListItemIndexAlias"); + + // Act + var columnName = predicateQuery.GetColumnName("ListItemIndexPath.Value"); + + // Assert + Assert.Equal("[ListItemIndexAlias].[Value]", columnName); + } + + public class ListItemIndex : MapIndex + { + public string Value { get; set; } + } +}