Skip to content

Commit

Permalink
Merge pull request #519 from mk3008/516-support-filtering-by-both-col…
Browse files Browse the repository at this point in the history
…umn-names-and-column-aliases

Improved filter search target
  • Loading branch information
mk3008 authored Aug 26, 2024
2 parents 284f9f1 + e9dcc96 commit 5a3e31b
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Carbunql/Fluent/SelectQueryOrderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ public static class SelectQueryOrderExtensions
{
public static SelectQuery OrderBy(this SelectQuery query, string column)
{
query.AddOrder(column, (source, col) => $"{source.Alias}.{col}");
query.AddOrder(column, (source, col) => $"{col}", true);
return query;
}

public static SelectQuery OrderByDescending(this SelectQuery query, string column)
{
query.AddOrder(column, (source, col) => $"{source.Alias}.{col} desc");
query.AddOrder(column, (source, col) => $"{col} desc", true);
return query;
}
}
16 changes: 7 additions & 9 deletions src/Carbunql/Fluent/SelectQueryWhereExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Carbunql.Clauses;

namespace Carbunql.Fluent;
namespace Carbunql.Fluent;

public static class SelectQueryWhereExtensions
{
Expand Down Expand Up @@ -40,15 +38,15 @@ private static void AddComparison(SelectQuery query, string columnName, string o
var (op, val) = GenerateComparison(operatorSymbol, value);
if (op == ">")
{
query.AddWhere(columnName, (source, column) => $"{val} < {source.Alias}.{column}");
query.AddWhere(columnName, (source, column) => $"{val} < {column}", true);
}
if (op == ">=")
{
query.AddWhere(columnName, (source, column) => $"{val} <= {source.Alias}.{column}");
query.AddWhere(columnName, (source, column) => $"{val} <= {column}", true);
}
else
{
query.AddWhere(columnName, (source, column) => $"{source.Alias}.{column} {op} {val}");
query.AddWhere(columnName, (source, column) => $"{column} {op} {val}", true);
}
}

Expand All @@ -62,15 +60,15 @@ private static void AddComparison(SelectQuery query, string tableName, string co
var (op, val) = GenerateComparison(operatorSymbol, value);
if (op == ">")
{
query.AddWhere(tableName, columnName, (source, column) => $"{val} < {source.Alias}.{column}");
query.AddWhere(tableName, columnName, (source, column) => $"{val} < {column}", true);
}
if (op == ">=")
{
query.AddWhere(tableName, columnName, (source, column) => $"{val} <= {source.Alias}.{column}");
query.AddWhere(tableName, columnName, (source, column) => $"{val} <= {column}", true);
}
else
{
query.AddWhere(tableName, columnName, (source, column) => $"{source.Alias}.{column} {op} {val}");
query.AddWhere(tableName, columnName, (source, column) => $"{column} {op} {val}", true);
}
}

Expand Down
76 changes: 60 additions & 16 deletions src/Carbunql/SelectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,10 @@ public SelectQuery OverrideSelect(string tableName, string columnAliasName, Func
/// <param name="columnName">The name of the column to search in.</param>
/// <param name="adder">The function to create the search condition.</param>
/// <returns>The modified select query.</returns>
public SelectQuery AddWhere(string columnName, Func<IQuerySource, string> adder)
public SelectQuery AddWhere(string columnName, Func<IQuerySource, string> adder, bool isAliasIncluded = false)
{
GetQuerySources()
.Where(x => x.ColumnNames.Where(x => x.IsEqualNoCase(columnName)).Any())
.Where(x => HasColumn(x, columnName, isAliasIncluded))
.EnsureAny($"column:{columnName}")
.GetRootsBySource()
.ForEach(x =>
Expand All @@ -1035,20 +1035,64 @@ public SelectQuery AddWhere(string columnName, Func<IQuerySource, string> adder)
/// <param name="columnName">The name of the column to search in.</param>
/// <param name="adder">The function to create the search condition.</param>
/// <returns>The modified select query.</returns>
public SelectQuery AddWhere(string columnName, Func<IQuerySource, string, string> adder)
public SelectQuery AddWhere(string columnName, Func<IQuerySource, string, string> adder, bool isAliasIncluded = false)
{
GetQuerySources()
.Where(x => x.ColumnNames.Where(x => x.IsEqualNoCase(columnName)).Any())
.Where(x => HasColumn(x, columnName, isAliasIncluded))
.EnsureAny($"column:{columnName}")
.GetRootsBySource()
.ForEach(x =>
{
x.Query.Where(adder(x, columnName));
x.Query.Where(adder(x, GetColumn(x, columnName, isAliasIncluded)));
});

return this;
}

private bool HasTable(IQuerySource source, string tableName, bool isAliasIncluded)
{
if (isAliasIncluded && source.Alias.IsEqualNoCase(tableName))
{
return true;
}
else
{
return source.GetTableFullName().IsEqualNoCase(tableName);
}
}

private bool HasColumn(IQuerySource source, string columnName, bool isAliasIncluded)
{
if (isAliasIncluded && source.Query.GetColumnNames().Where(x => x.IsEqualNoCase(columnName)).Any())
{
return true;
}
else
{
return source.ColumnNames.Where(x => x.IsEqualNoCase(columnName)).Any();
}
}

private string GetColumn(IQuerySource x, string columnName, bool isAliasIncluded)
{
if (x.Query.SelectClause != null && isAliasIncluded)
{
var selectableItem = x.Query.SelectClause!.Where(x => x.Alias.IsEqualNoCase(columnName)).FirstOrDefault();
if (selectableItem != null)
{
return selectableItem.Value.ToOneLineText();
}
}

var column = x.ColumnNames.Where(x => x.IsEqualNoCase(columnName)).FirstOrDefault();
if (column != null)
{
return $"{x.Alias}.{column}";
}

throw new InvalidProgramException();
}

/// <summary>
/// Adds a search condition.
/// </summary>
Expand Down Expand Up @@ -1078,12 +1122,12 @@ public SelectQuery AddWhere(Func<string> adder)
/// <param name="columnName">The name of the column to search in.</param>
/// <param name="adder">The function to create the search condition.</param>
/// <returns>The modified select query.</returns>
public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySource, string> adder)
public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySource, string> adder, bool isAliasIncluded = false)
{
GetQuerySources()
.Where(x => x.GetTableFullName().IsEqualNoCase(tableName))
.Where(x => HasTable(x, tableName, true))
.EnsureAny($"table:{tableName}")
.Where(x => x.ColumnNames.Where(x => x.IsEqualNoCase(columnName)).Any())
.Where(x => HasColumn(x, columnName, isAliasIncluded))
.EnsureAny($"The table exists, but there is no corresponding column in the table. table:{tableName}, column:{columnName}")
.GetRootsBySource()
.ForEach(x =>
Expand All @@ -1101,17 +1145,17 @@ public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySour
/// <param name="columnName">The name of the column to search in.</param>
/// <param name="adder">The function to create the search condition.</param>
/// <returns>The modified select query.</returns>
public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySource, string, string> adder)
public SelectQuery AddWhere(string tableName, string columnName, Func<IQuerySource, string, string> adder, bool isAliasIncluded = false)
{
GetQuerySources()
.Where(x => x.GetTableFullName().IsEqualNoCase(tableName) || x.Alias.IsEqualNoCase(tableName))
.Where(x => HasTable(x, tableName, true))
.EnsureAny($"table:{tableName}")
.Where(x => x.ColumnNames.Where(x => x.IsEqualNoCase(columnName)).Any())
.Where(x => HasColumn(x, columnName, isAliasIncluded))
.EnsureAny($"The table exists, but there is no corresponding column in the table. table:{tableName}, column:{columnName}")
.GetRootsBySource()
.ForEach(x =>
{
x.Query.Where(adder(x, columnName));
x.Query.Where(adder(x, GetColumn(x, columnName, isAliasIncluded)));
});

return this;
Expand Down Expand Up @@ -1155,7 +1199,7 @@ public SelectQuery AddExists(IEnumerable<string> keyColumnNames, string validati
public SelectQuery AddExists(string sourceTableName, IEnumerable<string> keyColumnNames, string validationTableName, Action<IQuerySource>? action = null)
{
GetQuerySources()
.Where(x => x.GetTableFullName().IsEqualNoCase(sourceTableName) || x.Alias.IsEqualNoCase(sourceTableName))
.Where(x => HasTable(x, sourceTableName, true))
.EnsureAny($"table:{sourceTableName}")
.Where(x => keyColumnNames.All(keyColumn => x.ColumnNames.Contains(keyColumn)))
.EnsureAny($"The table exists, but there is no corresponding column in the table. table:{sourceTableName}, columns:{string.Join(",", keyColumnNames)}")
Expand Down Expand Up @@ -1213,7 +1257,7 @@ public SelectQuery AddNotExists(IEnumerable<string> keyColumnNames, string valid
public SelectQuery AddNotExists(string sourceTableName, IEnumerable<string> keyColumnNames, string validationTableName, Action<IQuerySource>? action = null)
{
GetQuerySources()
.Where(x => x.GetTableFullName().IsEqualNoCase(sourceTableName))
.Where(x => HasTable(x, sourceTableName, true))
.EnsureAny($"table:{sourceTableName}")
.Where(x => keyColumnNames.All(keyColumn => x.ColumnNames.Contains(keyColumn)))
.EnsureAny($"The table exists, but there is no corresponding column in the table. table:{sourceTableName}, columns:{string.Join(",", keyColumnNames)}")
Expand Down Expand Up @@ -1268,14 +1312,14 @@ public SelectQuery AddParameter(QueryParameter prm)
return this;
}

public SelectQuery AddOrder(string columnName, Func<IQuerySource, string, string> adder)
public SelectQuery AddOrder(string columnName, Func<IQuerySource, string, string> adder, bool isAliasIncluded = false)
{
GetQuerySources()
.Where(x => x.Query.Equals(this))
.ForEach(x =>
{
x.Query.OrderClause ??= new();
x.Query.OrderClause.Add(SortableItemParser.Parse(adder(x, columnName)));
x.Query.OrderClause.Add(SortableItemParser.Parse(adder(x, GetColumn(x, columnName, isAliasIncluded))));
});

return this;
Expand Down
Loading

0 comments on commit 5a3e31b

Please sign in to comment.