• 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

47.06
/src/FluentCommand/Query/UpdateBuilder.cs
1
using FluentCommand.Extensions;
2
using FluentCommand.Query.Generators;
3

4
namespace FluentCommand.Query;
5

6
/// <summary>
7
/// Update query statement builder
8
/// </summary>
9
public class UpdateBuilder : UpdateBuilder<UpdateBuilder>
10
{
11
    /// <summary>
12
    /// Initializes a new instance of the <see cref="UpdateBuilder"/> class.
13
    /// </summary>
14
    /// <param name="queryGenerator">The query generator.</param>
15
    /// <param name="parameters">The parameters.</param>
16
    /// <param name="logicalOperator">The logical operator.</param>
17
    public UpdateBuilder(
18
        IQueryGenerator queryGenerator,
19
        List<QueryParameter> parameters,
20
        LogicalOperators logicalOperator = LogicalOperators.And)
21
        : base(queryGenerator, parameters, logicalOperator)
×
22
    {
23
    }
×
24
}
25

26
/// <summary>
27
/// Insert query statement builder
28
/// </summary>
29
public abstract class UpdateBuilder<TBuilder> : WhereBuilder<TBuilder>
30
    where TBuilder : UpdateBuilder<TBuilder>
31
{
32
    /// <summary>
33
    /// Initializes a new instance of the <see cref="UpdateBuilder{TBuilder}"/> class.
34
    /// </summary>
35
    /// <param name="queryGenerator">The query generator.</param>
36
    /// <param name="parameters">The parameters.</param>
37
    /// <param name="logicalOperator">The logical operator.</param>
38
    protected UpdateBuilder(
39
        IQueryGenerator queryGenerator,
40
        List<QueryParameter> parameters,
41
        LogicalOperators logicalOperator = LogicalOperators.And)
42
        : base(queryGenerator, parameters, logicalOperator)
6✔
43
    {
44
    }
6✔
45

46

47
    /// <summary>
48
    /// Gets the update expressions.
49
    /// </summary>
50
    /// <value>
51
    /// The update expressions.
52
    /// </value>
53
    protected HashSet<UpdateExpression> UpdateExpressions { get; } = new();
6✔
54

55
    /// <summary>
56
    /// Gets the output expressions.
57
    /// </summary>
58
    /// <value>
59
    /// The output expressions.
60
    /// </value>
61
    protected HashSet<ColumnExpression> OutputExpressions { get; } = new();
6✔
62

63
    /// <summary>
64
    /// Gets from expressions.
65
    /// </summary>
66
    /// <value>
67
    /// From expressions.
68
    /// </value>
69
    protected HashSet<TableExpression> FromExpressions { get; } = new();
6✔
70

71
    /// <summary>
72
    /// Gets the join expressions.
73
    /// </summary>
74
    /// <value>
75
    /// The join expressions.
76
    /// </value>
77
    protected HashSet<JoinExpression> JoinExpressions { get; } = new();
6✔
78

79
    /// <summary>
80
    /// Gets the table expression.
81
    /// </summary>
82
    /// <value>
83
    /// The table expression.
84
    /// </value>
85
    protected TableExpression TableExpression { get; private set; }
86

87

88
    /// <summary>
89
    /// Set the target table to update.
90
    /// </summary>
91
    /// <param name="tableName">Name of the table.</param>
92
    /// <param name="tableSchema">The table schema.</param>
93
    /// <param name="tableAlias">The table alias.</param>
94
    /// <returns>
95
    /// The same builder so that multiple calls can be chained.
96
    /// </returns>
97
    public TBuilder Table(
98
        string tableName,
99
        string tableSchema = null,
100
        string tableAlias = null)
101
    {
102
        TableExpression = new TableExpression(tableName, tableSchema, tableAlias);
6✔
103

104
        return (TBuilder)this;
6✔
105
    }
106

107

108
    /// <summary>
109
    /// Adds a value with specified column name and value.
110
    /// </summary>
111
    /// <typeparam name="TValue">The type of the value.</typeparam>
112
    /// <param name="columnName">Name of the column.</param>
113
    /// <param name="parameterValue">The parameter value.</param>
114
    /// <returns>
115
    /// The same builder so that multiple calls can be chained.
116
    /// </returns>
117
    public TBuilder Value<TValue>(
118
        string columnName,
119
        TValue parameterValue)
120
    {
121
        return Value(columnName, parameterValue, typeof(TValue));
11✔
122
    }
123

124
    /// <summary>
125
    /// Adds a value with specified column name and value.
126
    /// </summary>
127
    /// <param name="columnName">Name of the column.</param>
128
    /// <param name="parameterValue">The parameter value.</param>
129
    /// <param name="parameterType">Type of the parameter.</param>
130
    /// <returns>
131
    /// The same builder so that multiple calls can be chained.
132
    /// </returns>
133
    public TBuilder Value(
134
        string columnName,
135
        object parameterValue,
136
        Type parameterType)
137
    {
138
        if (string.IsNullOrWhiteSpace(columnName))
11!
139
            throw new ArgumentException($"'{nameof(columnName)}' cannot be null or empty.", nameof(columnName));
×
140

141
        var paramterName = NextParameter();
11✔
142
        var updateClause = new UpdateExpression(columnName, paramterName);
11✔
143

144
        UpdateExpressions.Add(updateClause);
11✔
145
        Parameters.Add(new QueryParameter(paramterName, parameterValue, parameterType));
11✔
146

147
        return (TBuilder)this;
11✔
148
    }
149

150
    /// <summary>
151
    /// Conditionally adds a value with specified column name and value.
152
    /// </summary>
153
    /// <typeparam name="TValue">The type of the value.</typeparam>
154
    /// <param name="columnName">Name of the column.</param>
155
    /// <param name="parameterValue">The parameter value.</param>
156
    /// <param name="condition">The condition.</param>
157
    /// <returns>
158
    /// The same builder so that multiple calls can be chained.
159
    /// </returns>
160
    public TBuilder ValueIf<TValue>(
161
        string columnName,
162
        TValue parameterValue,
163
        Func<string, TValue, bool> condition)
164
    {
165
        if (condition != null && !condition(columnName, parameterValue))
×
166
            return (TBuilder)this;
×
167

168
        return Value(columnName, parameterValue);
×
169
    }
170

171

172
    /// <summary>
173
    /// Add an output clause for the specified column names.
174
    /// </summary>
175
    /// <param name="columnNames">The column names.</param>
176
    /// <param name="tableAlias">The table alias.</param>
177
    /// <returns>
178
    /// The same builder so that multiple calls can be chained.
179
    /// </returns>
180
    public TBuilder Output(
181
        IEnumerable<string> columnNames,
182
        string tableAlias = "INSERTED")
183
    {
184
        if (columnNames is null)
×
185
            throw new ArgumentNullException(nameof(columnNames));
×
186

187
        foreach (var column in columnNames)
×
188
            Output(column, tableAlias);
×
189

190
        return (TBuilder)this;
×
191
    }
192

193
    /// <summary>
194
    /// Add an output clause for the specified column name.
195
    /// </summary>
196
    /// <param name="columnName">Name of the column.</param>
197
    /// <param name="tableAlias">The table alias.</param>
198
    /// <param name="columnAlias">The column alias.</param>
199
    /// <returns>
200
    /// The same builder so that multiple calls can be chained.
201
    /// </returns>
202
    public TBuilder Output(
203
        string columnName,
204
        string tableAlias = "INSERTED",
205
        string columnAlias = null)
206
    {
207
        var outputClause = new ColumnExpression(columnName, tableAlias, columnAlias);
6✔
208

209
        OutputExpressions.Add(outputClause);
6✔
210

211
        return (TBuilder)this;
6✔
212
    }
213

214
    /// <summary>
215
    /// Conditionally add an output clause for the specified column name.
216
    /// </summary>
217
    /// <param name="columnName">Name of the column.</param>
218
    /// <param name="tableAlias">The table alias.</param>
219
    /// <param name="columnAlias">The column alias.</param>
220
    /// <param name="condition">The condition.</param>
221
    /// <returns>
222
    /// The same builder so that multiple calls can be chained.
223
    /// </returns>
224
    public TBuilder OutputIf(
225
        string columnName,
226
        string tableAlias = "INSERTED",
227
        string columnAlias = null,
228
        Func<string, bool> condition = null)
229
    {
230
        if (condition != null && !condition(columnName))
×
231
            return (TBuilder)this;
×
232

233
        return Output(columnName, tableAlias, columnAlias);
×
234
    }
235

236

237
    /// <summary>
238
    /// Add a from clause to the query.
239
    /// </summary>
240
    /// <param name="tableName">Name of the table.</param>
241
    /// <param name="tableSchema">The table schema.</param>
242
    /// <param name="tableAlias">The table alias.</param>
243
    /// <returns>
244
    /// The same builder so that multiple calls can be chained.
245
    /// </returns>
246
    public virtual TBuilder From(
247
        string tableName,
248
        string tableSchema = null,
249
        string tableAlias = null)
250
    {
251
        var fromClause = new TableExpression(tableName, tableSchema, tableAlias);
1✔
252

253
        FromExpressions.Add(fromClause);
1✔
254

255
        return (TBuilder)this;
1✔
256
    }
257

258
    /// <summary>
259
    /// Add a raw from clause to the query.
260
    /// </summary>
261
    /// <param name="fromClause">From clause.</param>
262
    /// <returns>
263
    /// The same builder so that multiple calls can be chained.
264
    /// </returns>
265
    public TBuilder FromRaw(string fromClause)
266
    {
267
        if (fromClause.HasValue())
×
268
            FromExpressions.Add(new TableExpression(fromClause, IsRaw: true));
×
269

270
        return (TBuilder)this;
×
271
    }
272

273
    /// <summary>
274
    /// Add a join clause using the specified builder action
275
    /// </summary>
276
    /// <param name="builder">The builder.</param>
277
    /// <returns>
278
    /// The same builder so that multiple calls can be chained.
279
    /// </returns>
280
    public TBuilder Join(Action<JoinBuilder> builder)
281
    {
282
        var innerBuilder = new JoinBuilder(QueryGenerator, Parameters);
×
283
        builder(innerBuilder);
×
284

285
        JoinExpressions.Add(innerBuilder.BuildExpression());
×
286

287
        return (TBuilder)this;
×
288
    }
289

290
    /// <inheritdoc />
291
    public override QueryStatement BuildStatement()
292
    {
293
        var updateStatement = new UpdateStatement(
6✔
294
            TableExpression,
6✔
295
            UpdateExpressions,
6✔
296
            OutputExpressions,
6✔
297
            FromExpressions,
6✔
298
            JoinExpressions,
6✔
299
            WhereExpressions,
6✔
300
            CommentExpressions);
6✔
301

302
        var statement = QueryGenerator.BuildUpdate(updateStatement);
6✔
303

304
        return new QueryStatement(statement, Parameters);
6✔
305
    }
306

307
}
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