-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from mk3008/302-request-windowclause-build-im…
…provement 302 request windowclause build improvement
- Loading branch information
Showing
7 changed files
with
240 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,121 @@ | ||
using Carbunql.Clauses; | ||
using Carbunql.Values; | ||
|
||
namespace Carbunql.Building; | ||
|
||
public static class WindowFunctionExtension | ||
{ | ||
public static void AddPartition(this OverClause source, ValueBase partition) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.PartitionBy ??= new(); | ||
source.WindowDefinition.PartitionBy.Add(partition); | ||
} | ||
public static void Partition(this OverClause source, ValueBase partition) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Partition(partition); | ||
} | ||
|
||
public static void Partition(this OverClause source, Func<ValueBase> partitionbuilder) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Partition(partitionbuilder()); | ||
} | ||
|
||
public static void Partition(this OverClause source, SelectableItem order) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Partition(order.Value); | ||
} | ||
|
||
public static void Order(this OverClause source, ColumnValue order) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Order(order.ToSortable()); | ||
} | ||
|
||
public static void Order(this OverClause source, SortableItem order) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Order(order); | ||
} | ||
|
||
public static void Order(this OverClause source, Func<SortableItem> orderbuilder) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Order(orderbuilder()); | ||
} | ||
|
||
public static void Partition(this WindowDefinition source, ValueBase partition) | ||
{ | ||
source.PartitionBy ??= new(); | ||
source.PartitionBy.Add(partition); | ||
} | ||
|
||
public static void Partition(this WindowDefinition source, Func<ValueBase> partitionbuilder) | ||
{ | ||
source.PartitionBy ??= new(); | ||
source.PartitionBy.Add(partitionbuilder()); | ||
} | ||
|
||
public static void Partition(this WindowDefinition source, SelectableItem order) | ||
{ | ||
source.PartitionBy ??= new(); | ||
source.PartitionBy.Add(order.Value); | ||
} | ||
|
||
public static void Order(this WindowDefinition source, ColumnValue order) | ||
{ | ||
source.OrderBy ??= new(); | ||
source.OrderBy.Add(order.ToSortable()); | ||
} | ||
|
||
public static void Order(this WindowDefinition source, SortableItem order) | ||
{ | ||
source.OrderBy ??= new(); | ||
source.OrderBy.Add(order); | ||
} | ||
|
||
public static void Order(this WindowDefinition source, Func<SortableItem> orderbuilder) | ||
{ | ||
source.OrderBy ??= new(); | ||
source.OrderBy.Add(orderbuilder()); | ||
} | ||
|
||
public static void Partition(this NamedWindowDefinition source, ValueBase partition) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Partition(partition); | ||
} | ||
|
||
public static void Partition(this NamedWindowDefinition source, Func<ValueBase> partitionbuilder) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Partition(partitionbuilder()); | ||
} | ||
|
||
public static void Partition(this NamedWindowDefinition source, SelectableItem order) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Partition(order.Value); | ||
} | ||
|
||
public static void Order(this NamedWindowDefinition source, ColumnValue order) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.Order(order.ToSortable()); | ||
} | ||
|
||
public static void AddPartition(this OverClause source, Func<ValueBase> partitionbuilder) | ||
{ | ||
public static void Order(this NamedWindowDefinition source, SelectableItem order) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.PartitionBy ??= new(); | ||
source.WindowDefinition.PartitionBy.Add(partitionbuilder()); | ||
} | ||
source.WindowDefinition.Order(order.Value.ToSortable()); | ||
} | ||
|
||
public static void AddOrder(this OverClause source, SortableItem order) | ||
{ | ||
public static void Order(this NamedWindowDefinition source, SortableItem order) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.OrderBy ??= new(); | ||
source.WindowDefinition.OrderBy.Add(order); | ||
} | ||
source.WindowDefinition.Order(order); | ||
} | ||
|
||
public static void AddOrder(this OverClause source, Func<SortableItem> orderbuilder) | ||
{ | ||
public static void Order(this NamedWindowDefinition source, Func<SortableItem> orderbuilder) | ||
{ | ||
source.WindowDefinition ??= new(); | ||
source.WindowDefinition.OrderBy ??= new(); | ||
source.WindowDefinition.OrderBy.Add(orderbuilder()); | ||
} | ||
source.WindowDefinition.Order(orderbuilder()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using Carbunql.Clauses; | ||
using Carbunql.Values; | ||
using Xunit.Abstractions; | ||
|
||
namespace Carbunql.Building.Test; | ||
|
||
public class WindowTest | ||
{ | ||
private readonly QueryCommandMonitor Monitor; | ||
|
||
public WindowTest(ITestOutputHelper output) | ||
{ | ||
Monitor = new QueryCommandMonitor(output); | ||
} | ||
|
||
[Fact] | ||
public void Default() | ||
{ | ||
var sq = new SelectQuery(); | ||
var (_, a) = sq.From("table_a").As("a"); | ||
|
||
var pc = new PartitionClause | ||
{ | ||
new ColumnValue(a, "name") | ||
}; | ||
var oc = new OrderClause { | ||
new ColumnValue(a, "a_id").ToSortable() | ||
}; | ||
var wd = new WindowDefinition(pc, oc); | ||
var w1 = new NamedWindowDefinition("w1", wd); | ||
|
||
sq.WindowClause ??= new(); | ||
sq.WindowClause.Add(w1); | ||
|
||
sq.Select(new FunctionValue("row_number", new OverClause(w1))).As("row_num"); | ||
|
||
Monitor.Log(sq); | ||
|
||
var expect = @"SELECT | ||
ROW_NUMBER() OVER w1 AS row_num | ||
FROM | ||
table_a AS a | ||
WINDOW | ||
w1 AS ( | ||
PARTITION BY | ||
a.name | ||
ORDER BY | ||
a.a_id | ||
)"; | ||
|
||
Assert.Equal(expect, sq.ToText().ToString(), true, true, true); | ||
} | ||
|
||
[Fact] | ||
public void NamedWindow() | ||
{ | ||
var sq = new SelectQuery(); | ||
var (_, a) = sq.From("table_a").As("a"); | ||
|
||
var w = new NamedWindowDefinition("w1"); | ||
w.Partition(new ColumnValue(a, "name")); | ||
w.Order(new ColumnValue(a, "a_id")); | ||
|
||
sq.Window(w); | ||
|
||
sq.Select(new FunctionValue("row_number", new OverClause(w))).As("row_num"); | ||
|
||
Monitor.Log(sq); | ||
|
||
var expect = @"SELECT | ||
ROW_NUMBER() OVER w1 AS row_num | ||
FROM | ||
table_a AS a | ||
WINDOW | ||
w1 AS ( | ||
PARTITION BY | ||
a.name | ||
ORDER BY | ||
a.a_id | ||
)"; | ||
|
||
Assert.Equal(expect, sq.ToText().ToString(), true, true, true); | ||
} | ||
|
||
[Fact] | ||
public void Window() | ||
{ | ||
var sq = new SelectQuery(); | ||
var (_, a) = sq.From("table_a").As("a"); | ||
|
||
var w = new WindowDefinition(); | ||
w.Partition(new ColumnValue(a, "name")); | ||
w.Order(new ColumnValue(a, "a_id")); | ||
|
||
sq.Select(new FunctionValue("row_number", new OverClause(w))).As("row_num"); | ||
|
||
Monitor.Log(sq); | ||
|
||
var expect = @"SELECT | ||
ROW_NUMBER() OVER( | ||
PARTITION BY | ||
a.name | ||
ORDER BY | ||
a.a_id | ||
) AS row_num | ||
FROM | ||
table_a AS a"; | ||
|
||
Assert.Equal(expect, sq.ToText().ToString(), true, true, true); | ||
} | ||
} |