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

loresoft / FluentCommand / 6648415992

26 Oct 2023 01:49AM UTC coverage: 51.645% (+0.1%) from 51.515%
6648415992

push

github

pwelter34
Update InsertBuilder.cs

981 of 2442 branches covered (0.0%)

Branch coverage included in aggregate %.

2896 of 5065 relevant lines covered (57.18%)

156.37 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
/// Query order by builder
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 query generator.</param>
15
    /// <param name="parameters">The query parameters.</param>
16
    public OrderBuilder(IQueryGenerator queryGenerator, List<QueryParameter> parameters)
17
        : base(queryGenerator, parameters)
×
18
    {
19
    }
×
20
}
21

22
/// <summary>
23
/// Query order by builder
24
/// </summary>
25
/// <typeparam name="TBuilder">The type of the builder.</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 query generator.</param>
33
    /// <param name="parameters">The query parameters.</param>
34
    protected OrderBuilder(IQueryGenerator queryGenerator, List<QueryParameter> parameters)
35
        : base(queryGenerator, parameters)
×
36
    {
37
    }
×
38

39
    /// <summary>
40
    /// Gets the sort expressions.
41
    /// </summary>
42
    /// <value>
43
    /// The sort expressions.
44
    /// </value>
45
    protected HashSet<SortExpression> SortExpressions { get; } = new();
×
46

47

48
    /// <summary>
49
    /// Add an order by clause with the specified column name and sort direction.
50
    /// </summary>
51
    /// <param name="columnName">Name of the column.</param>
52
    /// <param name="sortDirection">The sort direction.</param>
53
    /// <returns>
54
    /// The same builder so that multiple calls can be chained.
55
    /// </returns>
56
    public TBuilder OrderBy(
57
        string columnName,
58
        SortDirections sortDirection = SortDirections.Ascending)
59
    {
60
        return OrderBy(columnName, null, sortDirection);
×
61
    }
62

63
    /// <summary>
64
    /// Add an order by clause with the specified column name, sort direction and table alias.
65
    /// </summary>
66
    /// <param name="columnName">Name of the column.</param>
67
    /// <param name="tableAlias">The table alias.</param>
68
    /// <param name="sortDirection">The sort direction.</param>
69
    /// <returns>
70
    /// The same builder so that multiple calls can be chained.
71
    /// </returns>
72
    public TBuilder OrderBy(
73
        string columnName,
74
        string tableAlias,
75
        SortDirections sortDirection = SortDirections.Ascending)
76
    {
77
        var orderClause = new SortExpression(columnName, tableAlias, sortDirection);
×
78

79
        SortExpressions.Add(orderClause);
×
80

81
        return (TBuilder)this;
×
82
    }
83

84
    /// <summary>
85
    /// Conditionally add an order by clause with the specified column name, sort direction and table alias.
86
    /// </summary>
87
    /// <param name="columnName">Name of the column.</param>
88
    /// <param name="tableAlias">The table alias.</param>
89
    /// <param name="sortDirection">The sort direction.</param>
90
    /// <param name="condition">The condition.</param>
91
    /// <returns>
92
    /// The same builder so that multiple calls can be chained.
93
    /// </returns>
94
    public TBuilder OrderByIf(
95
        string columnName,
96
        string tableAlias = null,
97
        SortDirections sortDirection = SortDirections.Ascending,
98
        Func<string, bool> condition = null)
99
    {
100
        if (condition != null && !condition(columnName))
×
101
            return (TBuilder)this;
×
102

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

106
    /// <summary>
107
    /// Add a raw order by clause to the query.
108
    /// </summary>
109
    /// <param name="sortExpression">The order by clause.</param>
110
    /// <returns>
111
    /// The same builder so that multiple calls can be chained.
112
    /// </returns>
113
    public TBuilder OrderByRaw(string sortExpression)
114
    {
115
        if (sortExpression.HasValue())
×
116
            SortExpressions.Add(new SortExpression(sortExpression, IsRaw: true));
×
117

118
        return (TBuilder)this;
×
119
    }
120

121
    /// <summary>
122
    /// Conditionally add a raw order by clause to the query.
123
    /// </summary>
124
    /// <param name="sortExpression">The order by clause.</param>
125
    /// <param name="condition">The condition.</param>
126
    /// <returns>
127
    /// The same builder so that multiple calls can be chained.
128
    /// </returns>
129
    public TBuilder OrderByRawIf(string sortExpression, Func<string, bool> condition = null)
130
    {
131
        if (condition != null && !condition(sortExpression))
×
132
            return (TBuilder)this;
×
133

134
        return OrderByRaw(sortExpression);
×
135
    }
136

137
    /// <summary>
138
    /// Add multiple raw order by clauses to the query.
139
    /// </summary>
140
    /// <param name="sortExpressions">The order by clauses.</param>
141
    /// <returns>
142
    /// The same builder so that multiple calls can be chained.
143
    /// </returns>
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
    /// <inheritdoc />
156
    public override QueryStatement BuildStatement()
157
    {
158
        if (SortExpressions == null || SortExpressions.Count == 0)
×
159
            return null;
×
160

161
        var statement = QueryGenerator.BuildOrder(SortExpressions);
×
162

163
        return new QueryStatement(statement, Parameters);
×
164
    }
165
}
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