• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 23278216331

19 Mar 2026 03:19AM UTC coverage: 57.398% (+0.7%) from 56.658%
23278216331

push

github

pwelter34
Enable nullable and improve source generators

1403 of 3069 branches covered (45.72%)

Branch coverage included in aggregate %.

527 of 907 new or added lines in 58 files covered. (58.1%)

22 existing lines in 10 files now uncovered.

4288 of 6846 relevant lines covered (62.64%)

330.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/FluentCommand/Query/OrderBuilder.cs
1
using FluentCommand.Extensions;
2
using FluentCommand.Query.Generators;
3

4
namespace FluentCommand.Query;
5

6
/// <summary>
7
/// Provides a builder for constructing SQL ORDER BY clauses with fluent, chainable methods.
8
/// </summary>
9
public class OrderBuilder : OrderBuilder<OrderBuilder>
10
{
11
    /// <summary>
12
    /// Initializes a new instance of the <see cref="OrderBuilder"/> class.
13
    /// </summary>
14
    /// <param name="queryGenerator">The <see cref="IQueryGenerator"/> used to generate SQL expressions.</param>
15
    /// <param name="parameters">The list of <see cref="QueryParameter"/> objects for the query.</param>
16
    public OrderBuilder(IQueryGenerator queryGenerator, List<QueryParameter> parameters)
17
        : base(queryGenerator, parameters)
×
18
    {
19
    }
×
20
}
21

22
/// <summary>
23
/// Provides a generic base class for building SQL ORDER BY clauses with fluent, chainable methods.
24
/// </summary>
25
/// <typeparam name="TBuilder">The type of the builder for fluent chaining.</typeparam>
26
public abstract class OrderBuilder<TBuilder> : StatementBuilder<TBuilder>
27
    where TBuilder : OrderBuilder<TBuilder>
28
{
29
    /// <summary>
30
    /// Initializes a new instance of the <see cref="OrderBuilder{TBuilder}"/> class.
31
    /// </summary>
32
    /// <param name="queryGenerator">The <see cref="IQueryGenerator"/> used to generate SQL expressions.</param>
33
    /// <param name="parameters">The list of <see cref="QueryParameter"/> objects for the query.</param>
34
    protected OrderBuilder(IQueryGenerator queryGenerator, List<QueryParameter> parameters)
35
        : base(queryGenerator, parameters)
×
36
    {
37
    }
×
38

39
    /// <summary>
40
    /// Gets the collection of sort expressions for the ORDER BY clause.
41
    /// </summary>
42
    /// <value>
43
    /// A <see cref="HashSet{SortExpression}"/> containing the sort expressions.
44
    /// </value>
45
    protected HashSet<SortExpression> SortExpressions { get; } = new();
×
46

47
    /// <summary>
48
    /// Adds an ORDER BY clause with the specified column name and sort direction.
49
    /// </summary>
50
    /// <param name="columnName">The name of the column to sort by.</param>
51
    /// <param name="sortDirection">The sort direction (default is <see cref="SortDirections.Ascending"/>).</param>
52
    /// <returns>
53
    /// The same builder instance for method chaining.
54
    /// </returns>
55
    public TBuilder OrderBy(
56
        string columnName,
57
        SortDirections sortDirection = SortDirections.Ascending)
58
    {
59
        return OrderBy(columnName, null, sortDirection);
×
60
    }
61

62
    /// <summary>
63
    /// Adds an ORDER BY clause with the specified column name, sort direction, and table alias.
64
    /// </summary>
65
    /// <param name="columnName">The name of the column to sort by.</param>
66
    /// <param name="tableAlias">The alias of the table (optional).</param>
67
    /// <param name="sortDirection">The sort direction (default is <see cref="SortDirections.Ascending"/>).</param>
68
    /// <returns>
69
    /// The same builder instance for method chaining.
70
    /// </returns>
71
    public TBuilder OrderBy(
72
        string columnName,
73
        string? tableAlias,
74
        SortDirections sortDirection = SortDirections.Ascending)
75
    {
76
        var orderClause = new SortExpression(columnName, tableAlias, sortDirection);
×
77

78
        SortExpressions.Add(orderClause);
×
79

80
        return (TBuilder)this;
×
81
    }
82

83
    /// <summary>
84
    /// Conditionally adds an ORDER BY clause with the specified column name, sort direction, and table alias.
85
    /// </summary>
86
    /// <param name="columnName">The name of the column to sort by.</param>
87
    /// <param name="tableAlias">The alias of the table (optional).</param>
88
    /// <param name="sortDirection">The sort direction (default is <see cref="SortDirections.Ascending"/>).</param>
89
    /// <param name="condition">A function that determines whether to add the ORDER BY clause, based on the column name. If <c>null</c>, the clause is always added.</param>
90
    /// <returns>
91
    /// The same builder instance for method chaining.
92
    /// </returns>
93
    public TBuilder OrderByIf(
94
        string columnName,
95
        string? tableAlias = null,
96
        SortDirections sortDirection = SortDirections.Ascending,
97
        Func<string, bool>? condition = null)
98
    {
99
        if (condition != null && !condition(columnName))
×
100
            return (TBuilder)this;
×
101

102
        return OrderBy(columnName, tableAlias, sortDirection);
×
103
    }
104

105
    /// <summary>
106
    /// Adds a raw ORDER BY clause to the query.
107
    /// </summary>
108
    /// <param name="sortExpression">The raw SQL ORDER BY clause.</param>
109
    /// <returns>
110
    /// The same builder instance for method chaining.
111
    /// </returns>
112
    public TBuilder OrderByRaw(string sortExpression)
113
    {
114
        if (sortExpression.HasValue())
×
115
            SortExpressions.Add(new SortExpression(sortExpression, IsRaw: true));
×
116

117
        return (TBuilder)this;
×
118
    }
119

120
    /// <summary>
121
    /// Conditionally adds a raw ORDER BY clause to the query.
122
    /// </summary>
123
    /// <param name="sortExpression">The raw SQL ORDER BY clause.</param>
124
    /// <param name="condition">A function that determines whether to add the ORDER BY clause, based on the expression. If <c>null</c>, the clause is always added.</param>
125
    /// <returns>
126
    /// The same builder instance for method chaining.
127
    /// </returns>
128
    public TBuilder OrderByRawIf(string sortExpression, Func<string, bool>? condition = null)
129
    {
130
        if (condition != null && !condition(sortExpression))
×
131
            return (TBuilder)this;
×
132

133
        return OrderByRaw(sortExpression);
×
134
    }
135

136
    /// <summary>
137
    /// Adds multiple raw ORDER BY clauses to the query.
138
    /// </summary>
139
    /// <param name="sortExpressions">A collection of raw SQL ORDER BY clauses.</param>
140
    /// <returns>
141
    /// The same builder instance for method chaining.
142
    /// </returns>
143
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="sortExpressions"/> is <c>null</c>.</exception>
144
    public TBuilder OrderByRaw(IEnumerable<string> sortExpressions)
145
    {
146
        if (sortExpressions is null)
×
147
            throw new ArgumentNullException(nameof(sortExpressions));
×
148

149
        foreach (var sortExpression in sortExpressions)
×
150
            OrderByRaw(sortExpression);
×
151

152
        return (TBuilder)this;
×
153
    }
154

155
    /// <summary>
156
    /// Builds the SQL ORDER BY statement using the current sort expressions.
157
    /// </summary>
158
    /// <returns>
159
    /// A <see cref="QueryStatement"/> containing the SQL ORDER BY clause and its parameters,
160
    /// or <c>null</c> if no sort expressions are present.
161
    /// </returns>
162
    public override QueryStatement? BuildStatement()
163
    {
164
        if (SortExpressions == null || SortExpressions.Count == 0)
×
165
            return null;
×
166

167
        var statement = QueryGenerator.BuildOrder(SortExpressions);
×
168

NEW
169
        if (statement.IsNullOrWhiteSpace())
×
NEW
170
            return null;
×
171

UNCOV
172
        return new QueryStatement(statement, Parameters);
×
173
    }
174
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc