Skip to content

Commit

Permalink
Merge pull request #460 from mk3008/459-abolish-idatarow-1
Browse files Browse the repository at this point in the history
The IDataSet interface has been overhauled.
  • Loading branch information
mk3008 authored Jun 24, 2024
2 parents fd3fe87 + 014dad4 commit 1d1df14
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 25 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions demo/AOPFiltering/Program.cs → demo/Filtering/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static void Main(string[] args)
Console.WriteLine(SelectStoreSaleReportByStaffId(staffId).ToText());
Console.WriteLine(";");

//アスペクト指向フィルタリング
//拡張メソッドによるフィルタリング
Console.WriteLine(SelectAreaSaleReport().FilterByStaffId(staffId).ToText());
Console.WriteLine(";");

Expand Down Expand Up @@ -93,7 +93,7 @@ private static FluentSelectQuery<Store_sale_report> SelectStoreSaleReportByStaff
}
}

public static class AspectOrientedFiltering
public static class Filtering
{
/// <summary>
/// 選択クエリをスタッフIDでフィルタリングします。
Expand Down
2 changes: 0 additions & 2 deletions src/Carbunql.TypeSafe/CTEDataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class CTEDataSet(string name, SelectQuery query) : IDataSet

public SelectQuery Query { get; init; } = query;

public List<string> Columns { get; init; } = query.GetColumnNames().ToList();

public SelectQuery BuildFromClause(SelectQuery query, string alias)
{
var cte = query.With(Query).As(Name);
Expand Down
7 changes: 4 additions & 3 deletions src/Carbunql.TypeSafe/FluentSelectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ public FluentSelectQuery Select<T>(Expression<Func<T>> expression) where T : cla
{
// DataSet.*
var table = mem.Member.Name;
var c = mem.CompileAndInvoke();

if (!GetSelectableTables().Where(x => x.Alias == table).Any())
{
throw new InvalidProgramException($"A dataset that is not defined in the FROM clause has been referenced. Name:{table}");
}

if (c is IDataRow dr)
var c = mem.CompileAndInvoke();

if (c != null)
{
foreach (var item in dr.DataSet.Columns)
foreach (var item in PropertySelector.SelectLiteralProperties(c.GetType()).Select(x => x.Name))
{
//Do not add duplicate columns
if (SelectClause != null && SelectClause.Where(x => x.Alias.IsEqualNoCase(item)).FirstOrDefault() != null)
Expand Down
6 changes: 6 additions & 0 deletions src/Carbunql.TypeSafe/IDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Carbunql.TypeSafe;

/// <summary>
/// You can get information about the dataset you belong to.
/// </summary>
public interface IDataRow
{
/// <summary>
/// The dataset it belongs to (table, subquery, CTE)
/// </summary>
[IgnoreMapping]
IDataSet DataSet { get; set; }
}
2 changes: 0 additions & 2 deletions src/Carbunql.TypeSafe/PhysicalTableDataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public class PhysicalTableDataSet(ITable tb, IEnumerable<string> columns) : IDat
{
public SelectableTable Table { get; set; } = new PhysicalTable(tb).ToSelectable();

public List<string> Columns { get; init; } = columns.ToList();

public SelectQuery BuildFromClause(SelectQuery query, string alias)
{
query.From(Table).As(alias);
Expand Down
2 changes: 0 additions & 2 deletions src/Carbunql.TypeSafe/QueryDataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ public class QueryDataSet(SelectQuery query) : IDataSet
{
public SelectQuery Query { get; init; } = query;

public List<string> Columns { get; init; } = query.GetColumnNames().ToList();

public SelectQuery BuildFromClause(SelectQuery query, string alias)
{
query.From(Query).As(alias);
Expand Down
12 changes: 6 additions & 6 deletions src/Carbunql.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Carbunql.TypeSafe", "Carbun
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Carbunql.TypeSafe.Test", "..\test\Carbunql.TypeSafe.Test\Carbunql.TypeSafe.Test.csproj", "{EA3034E3-564E-46B4-9FA8-B0A10D9DCBCE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.AOPFiltering", "..\demo\AOPFiltering\Demo.AOPFiltering.csproj", "{63CE8E1D-71A9-4ACE-AE52-613B99394060}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.TypeSafeBuild", "..\demo\TypeSafeBuild\Demo.TypeSafeBuild.csproj", "{B1016F33-6565-4A6E-844E-EF966E54F862}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo.Filtering", "..\demo\Filtering\Demo.Filtering.csproj", "{3EBB0CEC-FE66-4D04-A4DF-20B25A31178B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -103,14 +103,14 @@ Global
{EA3034E3-564E-46B4-9FA8-B0A10D9DCBCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA3034E3-564E-46B4-9FA8-B0A10D9DCBCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA3034E3-564E-46B4-9FA8-B0A10D9DCBCE}.Release|Any CPU.Build.0 = Release|Any CPU
{63CE8E1D-71A9-4ACE-AE52-613B99394060}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{63CE8E1D-71A9-4ACE-AE52-613B99394060}.Debug|Any CPU.Build.0 = Debug|Any CPU
{63CE8E1D-71A9-4ACE-AE52-613B99394060}.Release|Any CPU.ActiveCfg = Release|Any CPU
{63CE8E1D-71A9-4ACE-AE52-613B99394060}.Release|Any CPU.Build.0 = Release|Any CPU
{B1016F33-6565-4A6E-844E-EF966E54F862}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B1016F33-6565-4A6E-844E-EF966E54F862}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1016F33-6565-4A6E-844E-EF966E54F862}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1016F33-6565-4A6E-844E-EF966E54F862}.Release|Any CPU.Build.0 = Release|Any CPU
{3EBB0CEC-FE66-4D04-A4DF-20B25A31178B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3EBB0CEC-FE66-4D04-A4DF-20B25A31178B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EBB0CEC-FE66-4D04-A4DF-20B25A31178B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EBB0CEC-FE66-4D04-A4DF-20B25A31178B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 14 additions & 2 deletions src/Carbunql/IDataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@

public interface IDataSet
{
public List<string> Columns { get; }

/// <summary>
/// Build the FROM clause
/// </summary>
/// <param name="query"></param>
/// <param name="alias"></param>
/// <returns></returns>
SelectQuery BuildFromClause(SelectQuery query, string alias);

/// <summary>
/// Build the JOIN clause
/// </summary>
/// <param name="query"></param>
/// <param name="join"></param>
/// <param name="alias"></param>
/// <param name="condition"></param>
/// <returns></returns>
SelectQuery BuildJoinClause(SelectQuery query, string join, string alias, string condition = "");
}
6 changes: 3 additions & 3 deletions test/Carbunql.TypeSafe.Test/CTETest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ store AS s
s.location = :localtion
)
SELECT
r.order_date,
r.customer_name,
r.store_id,
r.order_detail_id,
r.order_id,
r.product_id,
r.quantity,
r.price,
r.order_date,
r.customer_name,
r.store_id,
s.name AS store_name
FROM
order_detail_report AS r
Expand Down
6 changes: 3 additions & 3 deletions test/Carbunql.TypeSafe.Test/SubQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ public void SubQueryJoin()
:localtion = 'japan'
*/
SELECT
r.order_date,
r.customer_name,
r.store_id,
r.order_detail_id,
r.order_id,
r.product_id,
r.quantity,
r.price,
r.order_date,
r.customer_name,
r.store_id,
s.name AS store_name
FROM
(
Expand Down

0 comments on commit 1d1df14

Please sign in to comment.