• 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

30.77
/src/FluentCommand/Query/UpdateEntityBuilder.cs
1
using System.Linq.Expressions;
2

3
using FluentCommand.Extensions;
4
using FluentCommand.Query.Generators;
5
using FluentCommand.Reflection;
6

7
namespace FluentCommand.Query;
8

9
/// <summary>
10
/// Update query statement builder
11
/// </summary>
12
/// <typeparam name="TEntity">The type of the entity.</typeparam>
13
public class UpdateEntityBuilder<TEntity>
14
    : UpdateBuilder<UpdateEntityBuilder<TEntity>>, IWhereEntityBuilder<TEntity, UpdateEntityBuilder<TEntity>>
15
    where TEntity : class
16
{
17
    private static readonly TypeAccessor _typeAccessor = TypeAccessor.GetAccessor<TEntity>();
5✔
18

19
    /// <summary>
20
    /// Initializes a new instance of the <see cref="UpdateEntityBuilder{TEntity}"/> class.
21
    /// </summary>
22
    /// <param name="queryGenerator">The query generator.</param>
23
    /// <param name="parameters">The parameters.</param>
24
    /// <param name="logicalOperator">The logical operator.</param>
25
    public UpdateEntityBuilder(
26
        IQueryGenerator queryGenerator,
27
        List<QueryParameter> parameters,
28
        LogicalOperators logicalOperator = LogicalOperators.And)
29
        : base(queryGenerator, parameters, logicalOperator)
6✔
30
    {
31
    }
6✔
32

33
    /// <summary>
34
    /// Adds a value with specified property and value.
35
    /// </summary>
36
    /// <typeparam name="TValue">The type of the value.</typeparam>
37
    /// <param name="property">The property.</param>
38
    /// <param name="parameterValue">The parameter value.</param>
39
    /// <returns>
40
    /// The same builder so that multiple calls can be chained.
41
    /// </returns>
42
    public UpdateEntityBuilder<TEntity> Value<TValue>(
43
        Expression<Func<TEntity, TValue>> property,
44
        TValue parameterValue)
45
    {
46
        var propertyAccessor = _typeAccessor.FindProperty(property);
11✔
47
        return Value(propertyAccessor?.Column, parameterValue);
11!
48
    }
49

50
    /// <summary>
51
    /// Conditionally adds a value with specified property and value.
52
    /// </summary>
53
    /// <typeparam name="TValue">The type of the value.</typeparam>
54
    /// <param name="property">The property.</param>
55
    /// <param name="parameterValue">The parameter value.</param>
56
    /// <param name="condition">The condition.</param>
57
    /// <returns>
58
    /// The same builder so that multiple calls can be chained.
59
    /// </returns>
60
    public UpdateEntityBuilder<TEntity> ValueIf<TValue>(
61
        Expression<Func<TEntity, TValue>> property,
62
        TValue parameterValue,
63
        Func<string, TValue, bool> condition)
64
    {
65
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
66
        return ValueIf(propertyAccessor?.Column, parameterValue, condition);
×
67
    }
68

69
    /// <summary>
70
    /// Adds a values from the specified entity. If column names are passed in,
71
    /// only those that match an entity property name will be included.
72
    /// </summary>
73
    /// <param name="entity">The entity to update.</param>
74
    /// <param name="columnNames">The column names to include.</param>
75
    /// <returns>
76
    /// The same builder so that multiple calls can be chained.
77
    /// </returns>
78
    public UpdateEntityBuilder<TEntity> Values(
79
        TEntity entity,
80
        IEnumerable<string> columnNames = null)
81
    {
82
        if (entity is null)
×
83
            throw new ArgumentNullException(nameof(entity));
×
84

85
        var properties = _typeAccessor.GetProperties();
×
86
        var columnSet = new HashSet<string>(columnNames ?? Enumerable.Empty<string>());
×
87

88
        foreach (var property in properties)
×
89
        {
90
            if (columnSet.Count > 0 && !columnSet.Contains(property.Column))
×
91
                continue;
92

93
            if (property.IsNotMapped || property.IsDatabaseGenerated)
×
94
                continue;
95

96
            // include the type to prevent issues with null
97
            Value(property.Column, property.GetValue(entity), property.MemberType);
×
98
        }
99

100
        return this;
×
101
    }
102

103

104
    /// <summary>
105
    /// Add an output clause for the specified property.
106
    /// </summary>
107
    /// <typeparam name="TValue">The type of the value.</typeparam>
108
    /// <param name="property">The property.</param>
109
    /// <param name="tableAlias">The table alias.</param>
110
    /// <param name="columnAlias">The column alias.</param>
111
    /// <returns>
112
    /// The same builder so that multiple calls can be chained.
113
    /// </returns>
114
    public UpdateEntityBuilder<TEntity> Output<TValue>(
115
        Expression<Func<TEntity, TValue>> property,
116
        string tableAlias = "INSERTED",
117
        string columnAlias = null)
118
    {
119
        var propertyAccessor = _typeAccessor.FindProperty(property);
6✔
120
        return Output(propertyAccessor?.Column, tableAlias, columnAlias);
6!
121
    }
122

123
    /// <summary>
124
    /// Conditionally add an output clause for the specified property.
125
    /// </summary>
126
    /// <typeparam name="TValue">The type of the value.</typeparam>
127
    /// <param name="property">The property.</param>
128
    /// <param name="tableAlias">The table alias.</param>
129
    /// <param name="columnAlias">The column alias.</param>
130
    /// <param name="condition">The condition.</param>
131
    /// <returns>
132
    /// The same builder so that multiple calls can be chained.
133
    /// </returns>
134
    public UpdateEntityBuilder<TEntity> OutputIf<TValue>(
135
        Expression<Func<TEntity, TValue>> property,
136
        string tableAlias = "INSERTED",
137
        string columnAlias = null,
138
        Func<string, bool> condition = null)
139
    {
140
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
141
        return OutputIf(propertyAccessor?.Column, tableAlias, columnAlias, condition);
×
142
    }
143

144
    /// <summary>
145
    /// Add a from clause to the query.
146
    /// </summary>
147
    /// <param name="tableName">Name of the table.</param>
148
    /// <param name="tableSchema">The table schema.</param>
149
    /// <param name="tableAlias">The table alias.</param>
150
    /// <returns>
151
    /// The same builder so that multiple calls can be chained.
152
    /// </returns>
153
    public override UpdateEntityBuilder<TEntity> From(
154
        string tableName = null,
155
        string tableSchema = null,
156
        string tableAlias = null)
157
    {
158
        return base.From(
1✔
159
            tableName ?? _typeAccessor.TableName,
1✔
160
            tableSchema ?? _typeAccessor.TableSchema,
1✔
161
            tableAlias);
1✔
162
    }
163

164
    /// <summary>
165
    /// Add a join clause using the specified builder action
166
    /// </summary>
167
    /// <typeparam name="TRight">The right join entity</typeparam>
168
    /// <param name="builder">The join builder.</param>
169
    /// <returns>
170
    /// The same builder so that multiple calls can be chained.
171
    /// </returns>
172
    public UpdateEntityBuilder<TEntity> Join<TRight>(Action<JoinEntityBuilder<TEntity, TRight>> builder)
173
        where TRight : class
174
    {
175
        var innerBuilder = new JoinEntityBuilder<TEntity, TRight>(QueryGenerator, Parameters);
1✔
176
        builder(innerBuilder);
1✔
177

178
        JoinExpressions.Add(innerBuilder.BuildExpression());
1✔
179

180
        return this;
1✔
181
    }
182

183
    /// <summary>
184
    /// Add a join clause using the specified builder action
185
    /// </summary>
186
    /// <typeparam name="TLeft">The left join entity</typeparam>
187
    /// <typeparam name="TRight">The right join entity</typeparam>
188
    /// <param name="builder">The join builder.</param>
189
    /// <returns>
190
    /// The same builder so that multiple calls can be chained.
191
    /// </returns>
192
    public UpdateEntityBuilder<TEntity> Join<TLeft, TRight>(Action<JoinEntityBuilder<TLeft, TRight>> builder)
193
        where TLeft : class
194
        where TRight : class
195
    {
196
        var innerBuilder = new JoinEntityBuilder<TLeft, TRight>(QueryGenerator, Parameters);
×
197
        builder(innerBuilder);
×
198

199
        JoinExpressions.Add(innerBuilder.BuildExpression());
×
200

201
        return this;
×
202
    }
203

204

205
    /// <inheritdoc />
206
    public UpdateEntityBuilder<TEntity> Where<TValue>(
207
        Expression<Func<TEntity, TValue>> property,
208
        TValue parameterValue,
209
        FilterOperators filterOperator = FilterOperators.Equal)
210
    {
211
        return Where<TValue>(property, parameterValue, null, filterOperator);
5✔
212
    }
213

214
    /// <inheritdoc />
215
    public UpdateEntityBuilder<TEntity> Where<TValue>(
216
        Expression<Func<TEntity, TValue>> property,
217
        TValue parameterValue,
218
        string tableAlias,
219
        FilterOperators filterOperator = FilterOperators.Equal)
220
    {
221
        var propertyAccessor = _typeAccessor.FindProperty(property);
5✔
222

223
        return Where(propertyAccessor.Column, parameterValue, tableAlias, filterOperator);
5✔
224
    }
225

226
    /// <summary>
227
    /// Create a where clause with the specified property, value, operator and table alias
228
    /// </summary>
229
    /// <typeparam name="TModel">The type of the model</typeparam>
230
    /// <typeparam name="TValue">The type of the value.</typeparam>
231
    /// <param name="property">The property.</param>
232
    /// <param name="parameterValue">The parameter value.</param>
233
    /// <param name="tableAlias">The table alias.</param>
234
    /// <param name="filterOperator">The filter operator.</param>
235
    /// <returns>
236
    /// The same builder so that multiple calls can be chained.
237
    /// </returns>
238
    public UpdateEntityBuilder<TEntity> Where<TModel, TValue>(
239
        Expression<Func<TModel, TValue>> property,
240
        TValue parameterValue,
241
        string tableAlias,
242
        FilterOperators filterOperator = FilterOperators.Equal)
243
    {
244
        var typeAccessor = TypeAccessor.GetAccessor<TModel>();
1✔
245
        var propertyAccessor = typeAccessor.FindProperty(property);
1✔
246

247
        return Where(propertyAccessor.Column, parameterValue, tableAlias, filterOperator);
1✔
248
    }
249

250
    /// <inheritdoc />
251
    public UpdateEntityBuilder<TEntity> WhereIf<TValue>(
252
        Expression<Func<TEntity, TValue>> property,
253
        TValue parameterValue,
254
        FilterOperators filterOperator = FilterOperators.Equal,
255
        Func<string, TValue, bool> condition = null)
256
    {
257
        return WhereIf(property, parameterValue, null, filterOperator, condition);
×
258
    }
259

260
    /// <inheritdoc />
261
    public UpdateEntityBuilder<TEntity> WhereIf<TValue>(
262
        Expression<Func<TEntity, TValue>> property,
263
        TValue parameterValue,
264
        string tableAlias,
265
        FilterOperators filterOperator = FilterOperators.Equal,
266
        Func<string, TValue, bool> condition = null)
267
    {
268
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
269

270
        return WhereIf(propertyAccessor.Column, parameterValue, tableAlias, filterOperator, condition);
×
271
    }
272

273
    /// <inheritdoc />
274
    public UpdateEntityBuilder<TEntity> WhereIn<TValue>(
275
        Expression<Func<TEntity, TValue>> property,
276
        IEnumerable<TValue> parameterValues,
277
        string tableAlias)
278
    {
279
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
280

281
        return WhereIn(propertyAccessor?.Column, parameterValues, tableAlias);
×
282
    }
283

284
    /// <inheritdoc />
285
    public UpdateEntityBuilder<TEntity> WhereInIf<TValue>(
286
        Expression<Func<TEntity, TValue>> property,
287
        IEnumerable<TValue> parameterValues,
288
        Func<string, IEnumerable<TValue>, bool> condition = null)
289
    {
290
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
291

292
        return WhereInIf(propertyAccessor?.Column, parameterValues, condition);
×
293
    }
294

295
    /// <inheritdoc />
296
    public UpdateEntityBuilder<TEntity> WhereInIf<TValue>(
297
        Expression<Func<TEntity, TValue>> property,
298
        IEnumerable<TValue> parameterValues,
299
        string tableAlias,
300
        Func<string, IEnumerable<TValue>, bool> condition = null)
301
    {
302
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
303

304
        return WhereInIf(propertyAccessor?.Column, parameterValues, tableAlias, condition);
×
305
    }
306

307
    /// <inheritdoc />
308
    public UpdateEntityBuilder<TEntity> WhereOr(Action<LogicalEntityBuilder<TEntity>> builder)
309
    {
310
        var innerBuilder = new LogicalEntityBuilder<TEntity>(QueryGenerator, Parameters, LogicalOperators.Or);
×
311

312
        builder(innerBuilder);
×
313

314
        var statement = innerBuilder.BuildStatement();
×
315

316
        if (statement != null && statement.Statement.HasValue())
×
317
            WhereExpressions.Add(new WhereExpression(statement.Statement, IsRaw: true));
×
318

319
        return this;
×
320
    }
321

322
    /// <inheritdoc />
323
    public UpdateEntityBuilder<TEntity> WhereAnd(Action<LogicalEntityBuilder<TEntity>> builder)
324
    {
325
        var innerBuilder = new LogicalEntityBuilder<TEntity>(QueryGenerator, Parameters, LogicalOperators.And);
×
326

327
        builder(innerBuilder);
×
328

329
        var statement = innerBuilder.BuildStatement();
×
330

331
        if (statement != null && statement.Statement.HasValue())
×
332
            WhereExpressions.Add(new WhereExpression(statement.Statement, IsRaw: true));
×
333

334
        return this;
×
335
    }
336

337

338
    /// <inheritdoc />
339
    public override QueryStatement BuildStatement()
340
    {
341
        // add table and schema from attribute if not set
342
        if (TableExpression == null)
6✔
343
            Table(_typeAccessor.TableName, _typeAccessor.TableSchema);
6✔
344

345
        return base.BuildStatement();
6✔
346
    }
347
}
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