Skip to content

Commit

Permalink
Created a sense of unity in function names.
Browse files Browse the repository at this point in the history
AddPartiton function has been changed to Partition function.
The AddOrder function has been changed to the Order function.
This is because the description of the Order clause is an Order function.
  • Loading branch information
mk3008 committed Jan 31, 2024
1 parent ba02051 commit 808a7b0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
64 changes: 32 additions & 32 deletions src/Carbunql/Building/WindowFunctionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,117 +5,117 @@ namespace Carbunql.Building;

public static class WindowFunctionExtension
{
public static void AddPartition(this OverClause source, ValueBase partition)
public static void Partition(this OverClause source, ValueBase partition)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddPartition(partition);
source.WindowDefinition.Partition(partition);
}

public static void AddPartition(this OverClause source, Func<ValueBase> partitionbuilder)
public static void Partition(this OverClause source, Func<ValueBase> partitionbuilder)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddPartition(partitionbuilder());
source.WindowDefinition.Partition(partitionbuilder());
}

public static void AddPartition(this OverClause source, SelectableItem order)
public static void Partition(this OverClause source, SelectableItem order)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddPartition(order.Value);
source.WindowDefinition.Partition(order.Value);
}

public static void AddOrder(this OverClause source, ColumnValue order)
public static void Order(this OverClause source, ColumnValue order)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddOrder(order.ToSortable());
source.WindowDefinition.Order(order.ToSortable());
}

public static void AddOrder(this OverClause source, SortableItem order)
public static void Order(this OverClause source, SortableItem order)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddOrder(order);
source.WindowDefinition.Order(order);
}

public static void AddOrder(this OverClause source, Func<SortableItem> orderbuilder)
public static void Order(this OverClause source, Func<SortableItem> orderbuilder)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddOrder(orderbuilder());
source.WindowDefinition.Order(orderbuilder());
}

public static void AddPartition(this WindowDefinition source, ValueBase partition)
public static void Partition(this WindowDefinition source, ValueBase partition)
{
source.PartitionBy ??= new();
source.PartitionBy.Add(partition);
}

public static void AddPartition(this WindowDefinition source, Func<ValueBase> partitionbuilder)
public static void Partition(this WindowDefinition source, Func<ValueBase> partitionbuilder)
{
source.PartitionBy ??= new();
source.PartitionBy.Add(partitionbuilder());
}

public static void AddPartition(this WindowDefinition source, SelectableItem order)
public static void Partition(this WindowDefinition source, SelectableItem order)
{
source.PartitionBy ??= new();
source.PartitionBy.Add(order.Value);
}

public static void AddOrder(this WindowDefinition source, ColumnValue order)
public static void Order(this WindowDefinition source, ColumnValue order)
{
source.OrderBy ??= new();
source.OrderBy.Add(order.ToSortable());
}

public static void AddOrder(this WindowDefinition source, SortableItem order)
public static void Order(this WindowDefinition source, SortableItem order)
{
source.OrderBy ??= new();
source.OrderBy.Add(order);
}

public static void AddOrder(this WindowDefinition source, Func<SortableItem> orderbuilder)
public static void Order(this WindowDefinition source, Func<SortableItem> orderbuilder)
{
source.OrderBy ??= new();
source.OrderBy.Add(orderbuilder());
}

public static void AddPartition(this NamedWindowDefinition source, ValueBase partition)
public static void Partition(this NamedWindowDefinition source, ValueBase partition)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddPartition(partition);
source.WindowDefinition.Partition(partition);
}

public static void AddPartition(this NamedWindowDefinition source, Func<ValueBase> partitionbuilder)
public static void Partition(this NamedWindowDefinition source, Func<ValueBase> partitionbuilder)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddPartition(partitionbuilder());
source.WindowDefinition.Partition(partitionbuilder());
}

public static void AddPartition(this NamedWindowDefinition source, SelectableItem order)
public static void Partition(this NamedWindowDefinition source, SelectableItem order)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddPartition(order.Value);
source.WindowDefinition.Partition(order.Value);
}

public static void AddOrder(this NamedWindowDefinition source, ColumnValue order)
public static void Order(this NamedWindowDefinition source, ColumnValue order)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddOrder(order.ToSortable());
source.WindowDefinition.Order(order.ToSortable());
}

public static void AddOrder(this NamedWindowDefinition source, SelectableItem order)
public static void Order(this NamedWindowDefinition source, SelectableItem order)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddOrder(order.Value.ToSortable());
source.WindowDefinition.Order(order.Value.ToSortable());
}

public static void AddOrder(this NamedWindowDefinition source, SortableItem order)
public static void Order(this NamedWindowDefinition source, SortableItem order)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddOrder(order);
source.WindowDefinition.Order(order);
}

public static void AddOrder(this NamedWindowDefinition source, Func<SortableItem> orderbuilder)
public static void Order(this NamedWindowDefinition source, Func<SortableItem> orderbuilder)
{
source.WindowDefinition ??= new();
source.WindowDefinition.AddOrder(orderbuilder());
source.WindowDefinition.Order(orderbuilder());
}
}
4 changes: 2 additions & 2 deletions test/Carbunql.Building.Test/FunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public void RowNumberTest()
sq.Select(() =>
{
var wf = new OverClause();
wf.AddPartition(new ColumnValue(a, "parent_id"));
wf.AddOrder(new ColumnValue(a, "id").ToSortable());
wf.Partition(new ColumnValue(a, "parent_id"));
wf.Order(new ColumnValue(a, "id").ToSortable());
return new FunctionValue("row_number", wf);
}).As("val");

Expand Down
4 changes: 2 additions & 2 deletions test/Carbunql.Building.Test/SerializeTest.ValueBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ public void QueryContainer()
public void WindowFunction()
{
var win = new OverClause();
win.AddPartition(new ColumnValue("shop_id"));
win.AddOrder(new Clauses.SortableItem(new ColumnValue("order_id")));
win.Partition(new ColumnValue("shop_id"));
win.Order(new SortableItem(new ColumnValue("order_id")));

var sq = new FunctionValue("row_number", win);

Expand Down
31 changes: 29 additions & 2 deletions test/Carbunql.Building.Test/WindowTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void NamedWindow()
var (_, a) = sq.From("table_a").As("a");

var w = new NamedWindowDefinition("w1");
w.AddPartition(new ColumnValue(a, "name"));
w.AddOrder(new ColumnValue(a, "a_id"));
w.Partition(new ColumnValue(a, "name"));
w.Order(new ColumnValue(a, "a_id"));

sq.Window(w);

Expand All @@ -81,4 +81,31 @@ ORDER BY

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

0 comments on commit 808a7b0

Please sign in to comment.