Skip to content

Commit

Permalink
Fix graphql where clause when database schema is configured (OrchardC…
Browse files Browse the repository at this point in the history
…MS#16364)

Co-authored-by: mdameer <[email protected]>
Co-authored-by: Hisham Bin Ateya <[email protected]>
Co-authored-by: Tony Han <[email protected]>
  • Loading branch information
4 people authored Jul 5, 2024
1 parent 5207b37 commit 52dca20
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ 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
{
// 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())}";
}
}

Expand Down
81 changes: 81 additions & 0 deletions test/OrchardCore.Tests/Apis/GraphQL/Queries/PredicateQueryTests.cs
Original file line number Diff line number Diff line change
@@ -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<ListItemIndex>()]);

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; }
}
}

0 comments on commit 52dca20

Please sign in to comment.