Skip to content

Commit

Permalink
add filter to Columns query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Sep 14, 2023
1 parent 9961fa9 commit cb59220
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/FluentCommand/Query/SelectEntityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public SelectEntityBuilder(IQueryGenerator queryGenerator, List<QueryParameter>
}

public SelectEntityBuilder<TEntity> Columns(
string tableAlias = null)
string tableAlias = null,
Func<IMemberAccessor, bool> filter = null)
{
var properties = _typeAccessor.GetProperties();

Expand All @@ -96,6 +97,9 @@ public SelectEntityBuilder(IQueryGenerator queryGenerator, List<QueryParameter>
if (property.IsNotMapped)
continue;

if (filter != null && !filter(property))
continue;

// alias column as property name if don't match
if (property.Name == property.Column)
Column(property.Column, tableAlias);
Expand All @@ -107,7 +111,8 @@ public SelectEntityBuilder(IQueryGenerator queryGenerator, List<QueryParameter>
}

public SelectEntityBuilder<TEntity> Columns<TModel>(
string tableAlias = null)
string tableAlias = null,
Func<IMemberAccessor, bool> filter = null)
{
var typeAccessor = TypeAccessor.GetAccessor(typeof(TModel));
var properties = typeAccessor.GetProperties();
Expand All @@ -117,6 +122,9 @@ public SelectEntityBuilder(IQueryGenerator queryGenerator, List<QueryParameter>
if (property.IsNotMapped)
continue;

if (filter != null && !filter(property))
continue;

// alias column as property name if don't match
if (property.Name == property.Column)
Column(property.Column, tableAlias);
Expand Down
15 changes: 15 additions & 0 deletions test/FluentCommand.Tests/Query/SelectBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ public async System.Threading.Tasks.Task SelectEntityJoinBuilder()
await Verifier.Verify(sql).UseDirectory("Snapshots");
}

[Fact]
public async System.Threading.Tasks.Task SelectEntityFilterBuilder()
{
var sqlProvider = new SqlServerGenerator();
var parameters = new List<QueryParameter>();

var builder = new SelectEntityBuilder<User>(sqlProvider, parameters)
.Columns("t", p => p.Name != nameof(User.PasswordHash));

var queryStatement = builder.BuildStatement();

var sql = queryStatement.Statement;

await Verifier.Verify(sql).UseDirectory("Snapshots");
}
private class EntityAlias
{
[Column("EntityId")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT [t].[Id], [t].[EmailAddress], [t].[IsEmailAddressConfirmed], [t].[DisplayName], [t].[FirstName], [t].[LastName], [t].[ResetHash], [t].[InviteHash], [t].[AccessFailedCount], [t].[LockoutEnabled], [t].[LockoutEnd], [t].[LastLogin], [t].[IsDeleted], [t].[Created], [t].[CreatedBy], [t].[Updated], [t].[UpdatedBy], [t].[RowVersion]
FROM [User];

0 comments on commit cb59220

Please sign in to comment.