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

loresoft / FluentCommand / 26594173245

28 May 2026 06:28PM UTC coverage: 55.553% (+0.7%) from 54.902%
26594173245

push

github

pwelter34
Move JSON support, add docs and examples

1358 of 3215 branches covered (42.24%)

Branch coverage included in aggregate %.

103 of 234 new or added lines in 9 files covered. (44.02%)

371 existing lines in 26 files now uncovered.

4389 of 7130 relevant lines covered (61.56%)

312.89 hits per line

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

44.68
/src/FluentCommand/Query/DeleteBuilder.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 DELETE statements with fluent, chainable methods.
8
/// </summary>
9
public class DeleteBuilder : DeleteBuilder<DeleteBuilder>
10
{
11
    /// <summary>
12
    /// Initializes a new instance of the <see cref="DeleteBuilder"/> 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
    /// <param name="logicalOperator">The logical operator (<see cref="LogicalOperators"/>) to combine WHERE expressions. Defaults to <see cref="LogicalOperators.And"/>.</param>
17
    public DeleteBuilder(
18
        IQueryGenerator queryGenerator,
19
        List<QueryParameter> parameters,
20
        LogicalOperators logicalOperator = LogicalOperators.And)
21
        : base(queryGenerator, parameters, logicalOperator)
×
22
    {
23
    }
×
24
}
25

26
/// <summary>
27
/// Provides a generic base class for building SQL DELETE statements with fluent, chainable methods.
28
/// </summary>
29
/// <typeparam name="TBuilder">The type of the builder for fluent chaining.</typeparam>
30
public abstract class DeleteBuilder<TBuilder> : WhereBuilder<TBuilder>
31
    where TBuilder : DeleteBuilder<TBuilder>
32
{
33
    /// <summary>
34
    /// Initializes a new instance of the <see cref="DeleteBuilder{TBuilder}"/> class.
35
    /// </summary>
36
    /// <param name="queryGenerator">The <see cref="IQueryGenerator"/> used to generate SQL expressions.</param>
37
    /// <param name="parameters">The list of <see cref="QueryParameter"/> objects for the query.</param>
38
    /// <param name="logicalOperator">The logical operator (<see cref="LogicalOperators"/>) to combine WHERE expressions. Defaults to <see cref="LogicalOperators.And"/>.</param>
39
    protected DeleteBuilder(
40
        IQueryGenerator queryGenerator,
41
        List<QueryParameter> parameters,
42
        LogicalOperators logicalOperator = LogicalOperators.And)
43
        : base(queryGenerator, parameters, logicalOperator)
6✔
44
    {
45
    }
6✔
46

47
    /// <summary>
48
    /// Gets the collection of output column expressions for the DELETE statement.
49
    /// </summary>
50
    /// <value>
51
    /// A <see cref="HashSet{ColumnExpression}"/> containing the output column expressions.
52
    /// </value>
53
    protected HashSet<ColumnExpression> OutputExpressions { get; } = new();
54

55
    /// <summary>
56
    /// Gets the collection of FROM table expressions for the DELETE statement.
57
    /// </summary>
58
    /// <value>
59
    /// A <see cref="HashSet{TableExpression}"/> containing the FROM table expressions.
60
    /// </value>
61
    protected HashSet<TableExpression> FromExpressions { get; } = new();
62

63
    /// <summary>
64
    /// Gets the collection of JOIN expressions for the DELETE statement.
65
    /// </summary>
66
    /// <value>
67
    /// A <see cref="HashSet{JoinExpression}"/> containing the JOIN expressions.
68
    /// </value>
69
    protected HashSet<JoinExpression> JoinExpressions { get; } = new();
70

71
    /// <summary>
72
    /// Gets the target table expression for the DELETE statement.
73
    /// </summary>
74
    /// <value>
75
    /// The <see cref="TableExpression"/> representing the target table.
76
    /// </value>
77
    protected TableExpression? TableExpression { get; private set; }
78

79
    /// <summary>
80
    /// Sets the target table to delete from.
81
    /// </summary>
82
    /// <param name="tableName">The name of the table.</param>
83
    /// <param name="tableSchema">The schema of the table (optional).</param>
84
    /// <param name="tableAlias">The alias for the table (optional).</param>
85
    /// <returns>
86
    /// The same builder instance for method chaining.
87
    /// </returns>
88
    public TBuilder Table(
89
        string tableName,
90
        string? tableSchema = null,
91
        string? tableAlias = null)
92
    {
93
        TableExpression = new TableExpression(tableName, tableSchema, tableAlias);
6✔
94

95
        return (TBuilder)this;
6✔
96
    }
97

98
    /// <summary>
99
    /// Adds an OUTPUT clause for the specified column names.
100
    /// </summary>
101
    /// <param name="columnNames">The collection of column names to include in the OUTPUT clause.</param>
102
    /// <param name="tableAlias">The alias for the table (optional).</param>
103
    /// <returns>
104
    /// The same builder instance for method chaining.
105
    /// </returns>
106
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="columnNames"/> is <c>null</c>.</exception>
107
    public TBuilder Output(
108
        IEnumerable<string> columnNames,
109
        string? tableAlias = null)
110
    {
111
        ArgumentNullException.ThrowIfNull(columnNames);
×
112

UNCOV
113
        foreach (var column in columnNames)
×
114
            Output(column, tableAlias);
×
115

UNCOV
116
        return (TBuilder)this;
×
117
    }
118

119
    /// <summary>
120
    /// Adds an OUTPUT clause for the specified column name.
121
    /// </summary>
122
    /// <param name="columnName">The name of the column to include in the OUTPUT clause.</param>
123
    /// <param name="tableAlias">The alias for the table (optional).</param>
124
    /// <param name="columnAlias">The alias for the column (optional).</param>
125
    /// <returns>
126
    /// The same builder instance for method chaining.
127
    /// </returns>
128
    public TBuilder Output(
129
        string columnName,
130
        string? tableAlias = null,
131
        string? columnAlias = null)
132
    {
133
        var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias);
6✔
134

135
        OutputExpressions.Add(outputClause);
6✔
136

137
        return (TBuilder)this;
6✔
138
    }
139

140
    /// <summary>
141
    /// Conditionally adds an OUTPUT clause for the specified column name if the condition is met.
142
    /// </summary>
143
    /// <param name="columnName">The name of the column to include in the OUTPUT clause.</param>
144
    /// <param name="tableAlias">The alias for the table (optional).</param>
145
    /// <param name="columnAlias">The alias for the column (optional).</param>
146
    /// <param name="condition">A function that determines whether to add the OUTPUT clause. If <c>null</c>, the clause is always added.</param>
147
    /// <returns>
148
    /// The same builder instance for method chaining.
149
    /// </returns>
150
    public TBuilder OutputIf(
151
        string columnName,
152
        string? tableAlias = null,
153
        string? columnAlias = null,
154
        Func<string, bool>? condition = null)
155
    {
UNCOV
156
        if (condition != null && !condition(columnName))
×
157
            return (TBuilder)this;
×
158

UNCOV
159
        return Output(columnName, tableAlias, columnAlias);
×
160
    }
161

162
    /// <summary>
163
    /// Adds a FROM clause to the DELETE statement.
164
    /// </summary>
165
    /// <param name="tableName">The name of the table to include in the FROM clause.</param>
166
    /// <param name="tableSchema">The schema of the table (optional).</param>
167
    /// <param name="tableAlias">The alias for the table (optional).</param>
168
    /// <returns>
169
    /// The same builder instance for method chaining.
170
    /// </returns>
171
    public virtual TBuilder From(
172
        string tableName,
173
        string? tableSchema = null,
174
        string? tableAlias = null)
175
    {
176
        var fromClause = new TableExpression(tableName, tableSchema, tableAlias);
1✔
177

178
        FromExpressions.Add(fromClause);
1✔
179

180
        return (TBuilder)this;
1✔
181
    }
182

183
    /// <summary>
184
    /// Adds a raw FROM clause to the DELETE statement.
185
    /// </summary>
186
    /// <param name="fromClause">The raw SQL FROM clause.</param>
187
    /// <returns>
188
    /// The same builder instance for method chaining.
189
    /// </returns>
190
    public TBuilder FromRaw(string fromClause)
191
    {
UNCOV
192
        if (fromClause.HasValue())
×
193
            FromExpressions.Add(new TableExpression(fromClause, IsRaw: true));
×
194

UNCOV
195
        return (TBuilder)this;
×
196
    }
197

198
    /// <summary>
199
    /// Adds a JOIN clause to the DELETE statement using the specified builder action.
200
    /// </summary>
201
    /// <param name="builder">An action that configures the join using a <see cref="JoinBuilder"/>.</param>
202
    /// <returns>
203
    /// The same builder instance for method chaining.
204
    /// </returns>
205
    public TBuilder Join(Action<JoinBuilder> builder)
206
    {
UNCOV
207
        var innerBuilder = new JoinBuilder(QueryGenerator, Parameters);
×
208
        builder(innerBuilder);
×
209

UNCOV
210
        JoinExpressions.Add(innerBuilder.BuildExpression());
×
211

UNCOV
212
        return (TBuilder)this;
×
213
    }
214

215
    /// <summary>
216
    /// Builds the SQL DELETE statement using the current configuration.
217
    /// </summary>
218
    /// <returns>
219
    /// A <see cref="QueryStatement"/> containing the SQL DELETE statement and its parameters.
220
    /// </returns>
221
    public override QueryStatement? BuildStatement()
222
    {
223
        if (TableExpression is null)
6!
UNCOV
224
            throw new InvalidOperationException("Table must be specified before building a delete statement.");
×
225

226
        var deleteStatement = new DeleteStatement(
6✔
227
            TableExpression,
6✔
228
            OutputExpressions,
6✔
229
            FromExpressions,
6✔
230
            JoinExpressions,
6✔
231
            WhereExpressions,
6✔
232
            CommentExpressions);
6✔
233

234
        var statement = QueryGenerator.BuildDelete(deleteStatement);
6✔
235

236
        return new QueryStatement(statement, Parameters);
6✔
237
    }
238
}
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