Skip to content

Commit

Permalink
feat: Add SelectOnlyByNames method to filter columns by alias names i…
Browse files Browse the repository at this point in the history
…n SELECT clause

- Added SelectOnlyByNames extension method for SelectQuery
- Filters the SELECT clause to only include columns with specified alias names
- Throws NullReferenceException if the SELECT clause is null
  • Loading branch information
mk3008 committed Oct 12, 2024
1 parent e9a462a commit 4f1d0ef
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Carbunql/Fluent/SelectQuerySelectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

public static class SelectQuerySelectExtensions
{
/// <summary>
/// Adds all columns of the specified table to the SELECT clause.
/// If column aliases are present, they are used; otherwise, the table's column names are used.
/// If no column names are available, a wildcard (*) is used.
/// </summary>
/// <param name="query">The <see cref="SelectQuery"/> object to which the columns will be added.</param>
/// <param name="table">The <see cref="FluentTable"/> object that contains the column information.</param>
/// <returns>The <see cref="SelectQuery"/> object with the columns added.</returns>
public static SelectQuery SelectAll(this SelectQuery query, FluentTable table)
{
if (table.ColumnAliases.Any())
Expand All @@ -25,6 +33,20 @@ public static SelectQuery SelectAll(this SelectQuery query, string querySourceNa
return query;
}

/// <summary>
/// Modifies the SELECT clause to include only the columns with the specified alias names, removing all other columns.
/// </summary>
/// <param name="query">The <see cref="SelectQuery"/> object to be modified.</param>
/// <param name="columnAliasNames">A list of column alias names to retain in the SELECT clause.</param>
/// <returns>The modified <see cref="SelectQuery"/> object.</returns>
/// <exception cref="NullReferenceException">Thrown if the SELECT clause (<see cref="query.SelectClause"/>) is null.</exception>
public static SelectQuery SelectOnlyByNames(this SelectQuery query, IEnumerable<string> columnAliasNames)
{
if (query.SelectClause == null) throw new NullReferenceException(nameof(query.SelectClause));
query.SelectClause.FilterInColumns(columnAliasNames);
return query;
}

public static SelectQuery Select(this SelectQuery query, string tableAlias, string column, string columnAlias = "")
{
query.AddSelect($"{tableAlias}.{column}", columnAlias);
Expand Down

0 comments on commit 4f1d0ef

Please sign in to comment.