Skip to content

Commit

Permalink
remove component type from clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
sidneyxvr committed Feb 23, 2023
1 parent 2a4e674 commit 94933b1
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 158 deletions.
62 changes: 62 additions & 0 deletions Argon.QueryBuilder.MySql.Tests/AggregateTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Argon.QueryBuilder.Tests;

namespace Argon.QueryBuilder.MySql.Tests;

public class AggregateTest : AggregateTestBase
{
public override void Average()
{
base.Average();

AssertSql("SELECT AVG(`TTL`) AS `avg` FROM `A`");
}

public override void Count()
{
base.Count();

AssertSql("SELECT COUNT(*) AS `count` FROM `A`");
}

public override void CountMultipleColumns()
{
base.CountMultipleColumns();

AssertSql("SELECT COUNT(*) AS `count` FROM (SELECT 1 FROM `A` WHERE `ColumnA` IS NOT NULL AND `ColumnB` IS NOT NULL) AS `countQuery`");
}

public override void DistinctCount()
{
base.DistinctCount();

AssertSql("SELECT COUNT(*) AS `count` FROM (SELECT DISTINCT * FROM `A`) AS `countQuery`");
}

public override void DistinctCountMultipleColumns()
{
base.DistinctCountMultipleColumns();

AssertSql("SELECT COUNT(*) AS `count` FROM (SELECT DISTINCT `ColumnA`, `ColumnB` FROM `A`) AS `countQuery`");
}

public override void Max()
{
base.Max();

AssertSql("SELECT MAX(`LatencyMs`) AS `max` FROM `A`");
}

public override void Min()
{
base.Min();

AssertSql("SELECT MIN(`LatencyMs`) AS `min` FROM `A`");
}

public override void Sum()
{
base.Sum();

AssertSql("SELECT SUM(`PacketsDropped`) AS `sum` FROM `A`");
}
}
38 changes: 38 additions & 0 deletions Argon.QueryBuilder.Tests/AggregateTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Xunit;

namespace Argon.QueryBuilder.Tests;

public class AggregateTestBase : TestBase
{
[Fact]
public virtual void Count()
=> AssertQuery(new Query("A").AsCount());

[Fact]
public virtual void CountMultipleColumns()
=> AssertQuery(new Query("A").AsCount(new[] { "ColumnA", "ColumnB" }));

[Fact]
public virtual void DistinctCount()
=> AssertQuery(new Query("A").Distinct().AsCount());

[Fact]
public virtual void DistinctCountMultipleColumns()
=> AssertQuery(new Query("A").Distinct().AsCount(new[] { "ColumnA", "ColumnB" }));

[Fact]
public virtual void Average()
=> AssertQuery(new Query("A").AsAverage("TTL"));

[Fact]
public virtual void Sum()
=> AssertQuery(new Query("A").AsSum("PacketsDropped"));

[Fact]
public virtual void Max()
=> AssertQuery(new Query("A").AsMax("LatencyMs"));

[Fact]
public virtual void Min()
=> AssertQuery(new Query("A").AsMin("LatencyMs"));
}
89 changes: 0 additions & 89 deletions Argon.QueryBuilder.Tests/AggregateTests.cs

This file was deleted.

4 changes: 1 addition & 3 deletions Argon.QueryBuilder/Base.Where.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Argon.QueryBuilder.Clauses;
using System.Data.Common;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Argon.QueryBuilder;

Expand Down Expand Up @@ -556,7 +554,7 @@ public Q OrWhereNotTime(string column, object value)
}
}

private static (string Name, string? Alias) ExpandColumn(string expression)
protected static (string Name, string? Alias) ExpandColumn(string expression)
{
if (expression.Contains(" as ", StringComparison.OrdinalIgnoreCase))
{
Expand Down
1 change: 0 additions & 1 deletion Argon.QueryBuilder/BaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public Q NewChild()
/// <returns></returns>
public Q AddComponent(ComponentType component, AbstractClause clause)
{
clause.Component = component;
if (component == ComponentType.Select)
{
Columns.Add((AbstractColumn)clause);
Expand Down
1 change: 0 additions & 1 deletion Argon.QueryBuilder/Clauses/AbstractClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public abstract class AbstractClause
/// <value>
/// The component name.
/// </value>
public ComponentType? Component { get; set; }
public abstract AbstractClause Clone();
}
1 change: 0 additions & 1 deletion Argon.QueryBuilder/Clauses/AggregateClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ public override AbstractClause Clone()
{
Type = Type,
Columns = new List<string>(Columns),
Component = Component,
};
}
4 changes: 0 additions & 4 deletions Argon.QueryBuilder/Clauses/ColumnClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public override AbstractClause Clone()
{
Name = Name,
Alias = Alias,
Component = Component
};
}

Expand All @@ -46,7 +45,6 @@ public override AbstractClause Clone()
=> new QueryColumn
{
Query = Query.Clone(),
Component = Component,
};
}

Expand All @@ -67,7 +65,6 @@ public override AbstractClause Clone()
{
Expression = Expression,
Bindings = Bindings,
Component = Component,
};
}

Expand All @@ -93,6 +90,5 @@ public override AbstractClause Clone()
Filter = Filter?.Clone(),
Column = (Column)Column.Clone(),
Aggregate = Aggregate,
Component = Component,
};
}
2 changes: 0 additions & 2 deletions Argon.QueryBuilder/Clauses/Combine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public override AbstractClause Clone()
=> new Combine
{
Operation = Operation,
Component = Component,
Query = Query,
All = All
};
Expand All @@ -50,7 +49,6 @@ public class RawCombine : AbstractCombine
public override AbstractClause Clone()
=> new RawCombine
{
Component = Component,
Expression = Expression,
Bindings = Bindings
};
Expand Down
13 changes: 0 additions & 13 deletions Argon.QueryBuilder/Clauses/ConditionClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public override AbstractClause Clone()
Value = Value,
IsOr = IsOr,
IsNot = IsNot,
Component = Component
};
}

Expand Down Expand Up @@ -53,7 +52,6 @@ public override AbstractClause Clone()
IsOr = IsOr,
IsNot = IsNot,
EscapeCharacter = EscapeCharacter,
Component = Component,
};
}

Expand All @@ -71,7 +69,6 @@ public override AbstractClause Clone()
IsOr = IsOr,
IsNot = IsNot,
Part = Part,
Component = Component
};
}

Expand All @@ -93,7 +90,6 @@ public override AbstractClause Clone()
Second = Second,
IsOr = IsOr,
IsNot = IsNot,
Component = Component,
};
}

Expand All @@ -115,7 +111,6 @@ public override AbstractClause Clone()
Query = Query.Clone(),
IsOr = IsOr,
IsNot = IsNot,
Component = Component,
};
}

Expand All @@ -137,7 +132,6 @@ public override AbstractClause Clone()
Query = Query.Clone(),
IsOr = IsOr,
IsNot = IsNot,
Component = Component
};
}

Expand All @@ -155,7 +149,6 @@ public override AbstractClause Clone()
Values = new List<T>(Values),
IsOr = IsOr,
IsNot = IsNot,
Component = Component,
};
}

Expand All @@ -173,7 +166,6 @@ public override AbstractClause Clone()
Query = Query.Clone(),
IsOr = IsOr,
IsNot = IsNot,
Component = Component,
};
}

Expand All @@ -194,7 +186,6 @@ public override AbstractClause Clone()
Lower = Lower,
IsOr = IsOr,
IsNot = IsNot,
Component = Component
};
}

Expand All @@ -212,7 +203,6 @@ public override AbstractClause Clone()
Column = Column,
IsOr = IsOr,
IsNot = IsNot,
Component = Component,
};
}

Expand All @@ -231,7 +221,6 @@ public override AbstractClause Clone()
Column = Column,
IsOr = IsOr,
IsNot = IsNot,
Component = Component,
Value = Value
};
}
Expand All @@ -249,7 +238,6 @@ public override AbstractClause Clone()
Query = Query.Clone(),
IsOr = IsOr,
IsNot = IsNot,
Component = Component,
};
}

Expand All @@ -267,6 +255,5 @@ public override AbstractClause Clone()
Query = Query.Clone(),
IsOr = IsOr,
IsNot = IsNot,
Component = Component
};
}
Loading

0 comments on commit 94933b1

Please sign in to comment.