Skip to content

Commit

Permalink
Add HasColumn method to SelectQuery
Browse files Browse the repository at this point in the history
- Implemented the HasColumn method to check for the existence of a specified column name in the SelectQuery.
- The method allows for an optional parameter to include or exclude aliases in the check.
- Added XML documentation for clarity on parameters and return values.
  • Loading branch information
mk3008 committed Oct 12, 2024
1 parent 4f1d0ef commit c6fc74b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
32 changes: 30 additions & 2 deletions src/Carbunql/Fluent/SelectQueryWhereExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public static class SelectQueryWhereExtensions
internal static char[] ParameterSymbols = { '@', ':', '$' };

/// <summary>
/// Conditionally applies the specified function to the query if the condition is true.
/// Conditionally applies the specified function to the query if the given condition is true.
/// </summary>
/// <param name="query">The query to apply the function to.</param>
/// <param name="query">The query to which the function will be applied.</param>
/// <param name="condition">The condition to evaluate.</param>
/// <param name="func">The function to apply if the condition is true.</param>
/// <returns>The modified query if the condition is true; otherwise, the original query.</returns>
Expand All @@ -22,6 +22,20 @@ public static SelectQuery If(this SelectQuery query, bool condition, Func<Select
: query;
}

/// <summary>
/// Conditionally applies the specified function to the query if the validation function returns true.
/// </summary>
/// <param name="query">The query to which the function will be applied.</param>
/// <param name="validation">The validation function to evaluate against the query.</param>
/// <param name="func">The function to apply if the validation function returns true.</param>
/// <returns>The modified query if the validation function returns true; otherwise, the original query.</returns>
public static SelectQuery If(this SelectQuery query, Func<SelectQuery, bool> validation, Func<SelectQuery, SelectQuery> func)
{
return validation(query)
? func(query)
: query;
}

/// <summary>
/// Applies the specified function to the query if the value is not null.
/// </summary>
Expand Down Expand Up @@ -64,6 +78,20 @@ public static SelectQuery IfNotEmpty<T>(this SelectQuery query, IEnumerable<T> v
: query;
}

/// <summary>
/// Checks whether the specified column name exists in the query.
/// You can specify whether to include aliases in the check.
/// </summary>
/// <param name="query">The <see cref="SelectQuery"/> object to search.</param>
/// <param name="columnName">The column name to check for.</param>
/// <param name="isAliasIncluded">If true, aliases are included in the check. The default value is true.</param>
/// <returns>Returns true if the specified column name exists in the query; otherwise, false.</returns>
public static bool HasColumn(this SelectQuery query, string columnName, bool isAliasIncluded = true)
{
return query.GetQuerySources()
.Where(x => query.HasColumn(x, columnName, isAliasIncluded)).Any();
}

private static (string, string) GenerateComparison(string operatorSymbol, object? value)
{
if (value == null)
Expand Down
2 changes: 1 addition & 1 deletion src/Carbunql/SelectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ private bool HasTable(IQuerySource source, string tableName, bool isAliasInclude
}
}

private bool HasColumn(IQuerySource source, string columnName, bool isAliasIncluded)
internal bool HasColumn(IQuerySource source, string columnName, bool isAliasIncluded)
{
if (isAliasIncluded && source.Query.GetColumnNames().Where(x => x.IsEqualNoCase(columnName)).Any())
{
Expand Down

0 comments on commit c6fc74b

Please sign in to comment.