Skip to content

Commit

Permalink
Make aggregate check input columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgeelen-uipath committed Jun 25, 2021
1 parent 10ae29c commit ae2bcb2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 7 additions & 0 deletions QueryBuilder.Tests/AggregateTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using SqlKata.Compilers;
using SqlKata.Tests.Infrastructure;
using System;
using Xunit;

namespace SqlKata.Tests
{
public class AggregateTests : TestSupport
{
[Fact]
public void AggregateAsEmpty()
{
Assert.Throws<ArgumentException>(() => new Query("A").AggregateAs("aggregate", new string[] { }));
}

[Fact]
public void CountAs()
{
Expand Down
10 changes: 7 additions & 3 deletions QueryBuilder/Query.Aggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public Query AsAggregate(string type, string[] columns = null)
);
}

public Query AggregateAs(string type, IEnumerable<string> columns, string alias)
public Query AggregateAs(string type, IEnumerable<string> columns, string alias = null)
{
if (columns.Count() == 0)
{
throw new System.ArgumentException("Cannot aggregate without columns");
}

Method = "aggregate";

Expand All @@ -32,12 +36,12 @@ public Query AggregateAs(string type, IEnumerable<string> columns, string alias)

public Query CountAs(string column = null, string alias = null)
{
return CountAs(new[] { column ?? "*" }, alias);
return CountAs(column != null ? new[] { column } : new string[] { }, alias);
}

public Query CountAs(IEnumerable<string> columns, string alias = null)
{
return AggregateAs("count", columns, alias);
return AggregateAs("count", columns.Count() == 0 ? new[] { "*" } : columns, alias);
}

public Query AsAvg(string column)
Expand Down

0 comments on commit ae2bcb2

Please sign in to comment.