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

loresoft / FluentCommand / 6187016377

14 Sep 2023 02:36PM UTC coverage: 51.068% (-0.001%) from 51.069%
6187016377

push

github

pwelter34
add filter to Columns query builder

952 of 2400 branches covered (0.0%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 1 file covered. (100.0%)

2778 of 4904 relevant lines covered (56.65%)

161.3 hits per line

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

61.54
/src/FluentCommand/Query/SelectEntityBuilder.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
public class SelectEntityBuilder<TEntity>
10
    : SelectBuilder<SelectEntityBuilder<TEntity>>, IWhereEntityBuilder<TEntity, SelectEntityBuilder<TEntity>>
11
    where TEntity : class
12
{
13
    private static readonly TypeAccessor _typeAccessor = TypeAccessor.GetAccessor<TEntity>();
10✔
14

15
    public SelectEntityBuilder(IQueryGenerator queryGenerator, List<QueryParameter> parameters, LogicalOperators logicalOperator = LogicalOperators.And)
16
        : base(queryGenerator, parameters, logicalOperator)
47✔
17
    {
18
    }
47✔
19

20
    public SelectEntityBuilder<TEntity> Column(
21
        Expression<Func<TEntity, object>> property,
22
        string tableAlias = null,
23
        string columnAlias = null)
24
    {
25
        var propertyAccessor = _typeAccessor.FindProperty(property);
26✔
26

27
        // alais column as property name if don't match
28
        if (propertyAccessor.Name == propertyAccessor.Column)
26✔
29
            return Column(propertyAccessor.Column, tableAlias, columnAlias);
25✔
30
        else
31
            return Column(propertyAccessor.Column, tableAlias, columnAlias ?? propertyAccessor.Name);
1✔
32

33
    }
34

35
    public SelectEntityBuilder<TEntity> Column<TModel>(
36
        Expression<Func<TModel, object>> property,
37
        string tableAlias = null,
38
        string columnAlias = null) where TModel : class
39
    {
40
        var typeAccessor = TypeAccessor.GetAccessor<TModel>();
6✔
41
        var propertyAccessor = typeAccessor.FindProperty(property);
6✔
42

43
        // alais column as property name if don't match
44
        if (propertyAccessor.Name == propertyAccessor.Column)
6!
45
            return Column(propertyAccessor.Column, tableAlias, columnAlias);
6✔
46
        else
47
            return Column(propertyAccessor.Column, tableAlias, columnAlias ?? propertyAccessor.Name);
×
48

49
    }
50

51
    public SelectEntityBuilder<TEntity> ColumnIf<TValue>(
52
        Expression<Func<TEntity, TValue>> property,
53
        string tableAlias = null,
54
        string columnAlias = null,
55
        Func<string, bool> condition = null)
56
    {
57
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
58

59
        // alais column as property name if don't match
60
        if (propertyAccessor.Name == propertyAccessor.Column)
×
61
            return ColumnIf(propertyAccessor.Column, tableAlias, columnAlias, condition);
×
62
        else
63
            return ColumnIf(propertyAccessor.Column, tableAlias, columnAlias ?? propertyAccessor.Name, condition);
×
64
    }
65

66
    public override SelectEntityBuilder<TEntity> Columns(
67
        IEnumerable<string> columnNames,
68
        string tableAlias = null)
69
    {
70
        if (columnNames is null)
1!
71
            throw new ArgumentNullException(nameof(columnNames));
×
72

73
        foreach (var column in columnNames)
6✔
74
        {
75
            var propertyAccessor = _typeAccessor.FindColumn(column);
2✔
76
            if (propertyAccessor is null)
2✔
77
                continue;
78

79
            // alias column as property name if don't match
80
            if (propertyAccessor.Name == propertyAccessor.Column)
2✔
81
                Column(propertyAccessor.Column, tableAlias);
1✔
82
            else
83
                Column(propertyAccessor.Column, tableAlias, propertyAccessor.Name);
1✔
84
        }
85

86
        return this;
1✔
87
    }
88

89
    public SelectEntityBuilder<TEntity> Columns(
90
        string tableAlias = null,
91
        Func<IMemberAccessor, bool> filter = null)
92
    {
93
        var properties = _typeAccessor.GetProperties();
3✔
94

95
        foreach (var property in properties)
144✔
96
        {
97
            if (property.IsNotMapped)
69✔
98
                continue;
99

100
            if (filter != null && !filter(property))
57✔
101
                continue;
102

103
            // alias column as property name if don't match
104
            if (property.Name == property.Column)
56!
105
                Column(property.Column, tableAlias);
56✔
106
            else
107
                Column(property.Column, tableAlias, property.Name);
×
108
        }
109

110
        return this;
3✔
111
    }
112

113
    public SelectEntityBuilder<TEntity> Columns<TModel>(
114
        string tableAlias = null,
115
        Func<IMemberAccessor, bool> filter = null)
116
    {
117
        var typeAccessor = TypeAccessor.GetAccessor(typeof(TModel));
×
118
        var properties = typeAccessor.GetProperties();
×
119

120
        foreach (var property in properties)
×
121
        {
122
            if (property.IsNotMapped)
×
123
                continue;
124

125
            if (filter != null && !filter(property))
×
126
                continue;
127

128
            // alias column as property name if don't match
129
            if (property.Name == property.Column)
×
130
                Column(property.Column, tableAlias);
×
131
            else
132
                Column(property.Column, tableAlias, property.Name);
×
133
        }
134

135
        return this;
×
136
    }
137

138
    public SelectEntityBuilder<TEntity> Count<TValue>(
139
        Expression<Func<TEntity, TValue>> property,
140
        string tableAlias = null,
141
        string columnAlias = null)
142
    {
143
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
144

145
        return Count(propertyAccessor.Column, tableAlias, columnAlias);
×
146
    }
147

148
    public SelectEntityBuilder<TEntity> Aggregate<TValue>(
149
        Expression<Func<TEntity, TValue>> property,
150
        AggregateFunctions function,
151
        string tableAlias = null,
152
        string columnAlias = null)
153
    {
154
        var propertyAccessor = _typeAccessor.FindProperty(property);
3✔
155

156
        return Aggregate(function, propertyAccessor.Column, tableAlias, columnAlias);
3✔
157
    }
158

159
    public override SelectEntityBuilder<TEntity> From(
160
        string tableName = null,
161
        string tableSchema = null,
162
        string tableAlias = null)
163
    {
164
        return base.From(
44✔
165
            tableName ?? _typeAccessor.TableName,
44✔
166
            tableSchema ?? _typeAccessor.TableSchema,
44✔
167
            tableAlias);
44✔
168
    }
169

170
    public SelectEntityBuilder<TEntity> Join<TRight>(Action<JoinEntityBuilder<TEntity, TRight>> builder)
171
        where TRight : class
172
    {
173
        var innerBuilder = new JoinEntityBuilder<TEntity, TRight>(QueryGenerator, Parameters);
7✔
174
        builder(innerBuilder);
7✔
175

176
        JoinExpressions.Add(innerBuilder.BuildExpression());
7✔
177

178
        return this;
7✔
179
    }
180

181
    public SelectEntityBuilder<TEntity> Join<TLeft, TRight>(Action<JoinEntityBuilder<TLeft, TRight>> builder)
182
        where TLeft : class
183
        where TRight : class
184
    {
185
        var innerBuilder = new JoinEntityBuilder<TLeft, TRight>(QueryGenerator, Parameters);
3✔
186
        builder(innerBuilder);
3✔
187

188
        JoinExpressions.Add(innerBuilder.BuildExpression());
3✔
189

190
        return this;
3✔
191
    }
192

193
    public SelectEntityBuilder<TEntity> Where<TValue>(
194
        Expression<Func<TEntity, TValue>> property,
195
        TValue parameterValue,
196
        FilterOperators filterOperator = FilterOperators.Equal)
197
    {
198
        return Where<TValue>(property, parameterValue, null, filterOperator);
20✔
199
    }
200

201
    public SelectEntityBuilder<TEntity> Where<TValue>(
202
        Expression<Func<TEntity, TValue>> property,
203
        TValue parameterValue,
204
        string tableAlias,
205
        FilterOperators filterOperator = FilterOperators.Equal)
206
    {
207
        var propertyAccessor = _typeAccessor.FindProperty(property);
24✔
208

209
        return Where(propertyAccessor.Column, parameterValue, tableAlias, filterOperator);
24✔
210
    }
211

212
    public SelectEntityBuilder<TEntity> Where<TModel, TValue>(
213
        Expression<Func<TModel, TValue>> property,
214
        TValue parameterValue,
215
        string tableAlias,
216
        FilterOperators filterOperator = FilterOperators.Equal)
217
    {
218
        var typeAccessor = TypeAccessor.GetAccessor<TModel>();
1✔
219
        var propertyAccessor = typeAccessor.FindProperty(property);
1✔
220

221
        return Where(propertyAccessor.Column, parameterValue, tableAlias, filterOperator);
1✔
222
    }
223

224
    public SelectEntityBuilder<TEntity> WhereIf<TValue>(
225
        Expression<Func<TEntity, TValue>> property,
226
        TValue parameterValue,
227
        FilterOperators filterOperator = FilterOperators.Equal,
228
        Func<string, TValue, bool> condition = null)
229
    {
230
        return WhereIf(property, parameterValue, null, filterOperator, condition);
×
231
    }
232

233
    public SelectEntityBuilder<TEntity> WhereIf<TValue>(
234
        Expression<Func<TEntity, TValue>> property,
235
        TValue parameterValue,
236
        string tableAlias,
237
        FilterOperators filterOperator = FilterOperators.Equal,
238
        Func<string, TValue, bool> condition = null)
239
    {
240
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
241

242
        return WhereIf(propertyAccessor.Column, parameterValue, tableAlias, filterOperator, condition);
×
243
    }
244

245
    public SelectEntityBuilder<TEntity> WhereIn<TValue>(
246
        Expression<Func<TEntity, TValue>> property,
247
        IEnumerable<TValue> parameterValues,
248
        string tableAlias = null)
249
    {
250
        var propertyAccessor = _typeAccessor.FindProperty(property);
8✔
251

252
        return WhereIn(propertyAccessor?.Column, parameterValues, tableAlias);
8!
253
    }
254

255
    public SelectEntityBuilder<TEntity> WhereInIf<TValue>(
256
        Expression<Func<TEntity, TValue>> property,
257
        IEnumerable<TValue> parameterValues,
258
        Func<string, IEnumerable<TValue>, bool> condition = null)
259
    {
260
        var propertyAccessor = _typeAccessor.FindProperty(property);
1✔
261

262
        return WhereInIf(propertyAccessor?.Column, parameterValues, condition);
1!
263
    }
264

265
    public SelectEntityBuilder<TEntity> WhereInIf<TValue>(
266
        Expression<Func<TEntity, TValue>> property,
267
        IEnumerable<TValue> parameterValues,
268
        string tableAlias,
269
        Func<string, IEnumerable<TValue>, bool> condition = null)
270
    {
271
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
272

273
        return WhereInIf(propertyAccessor?.Column, parameterValues, tableAlias, condition);
×
274
    }
275

276
    public SelectEntityBuilder<TEntity> WhereOr(Action<LogicalEntityBuilder<TEntity>> builder)
277
    {
278
        var innerBuilder = new LogicalEntityBuilder<TEntity>(QueryGenerator, Parameters, LogicalOperators.Or);
5✔
279

280
        builder(innerBuilder);
5✔
281

282
        var statement = innerBuilder.BuildStatement();
5✔
283

284
        if (statement != null)
5✔
285
            WhereExpressions.Add(new WhereExpression(statement.Statement, IsRaw: true));
5✔
286

287
        return this;
5✔
288
    }
289

290
    public SelectEntityBuilder<TEntity> WhereAnd(Action<LogicalEntityBuilder<TEntity>> builder)
291
    {
292
        var innerBuilder = new LogicalEntityBuilder<TEntity>(QueryGenerator, Parameters, LogicalOperators.And);
×
293

294
        builder(innerBuilder);
×
295

296
        var statement = innerBuilder.BuildStatement();
×
297

298
        if (statement != null && statement.Statement.HasValue())
×
299
            WhereExpressions.Add(new WhereExpression(statement.Statement, IsRaw: true));
×
300

301
        return this;
×
302
    }
303

304

305
    public SelectEntityBuilder<TEntity> OrderBy<TValue>(
306
        Expression<Func<TEntity, TValue>> property,
307
        SortDirections sortDirection = SortDirections.Ascending)
308
    {
309
        var propertyAccessor = _typeAccessor.FindProperty(property);
21✔
310

311
        return OrderBy(propertyAccessor.Column, null, sortDirection);
21✔
312
    }
313

314
    public SelectEntityBuilder<TEntity> OrderBy<TValue>(
315
        Expression<Func<TEntity, TValue>> property,
316
        string tableAlias,
317
        SortDirections sortDirection = SortDirections.Ascending)
318
    {
319
        var propertyAccessor = _typeAccessor.FindProperty(property);
4✔
320

321
        return OrderBy(propertyAccessor.Column, tableAlias, sortDirection);
4✔
322
    }
323

324
    public SelectEntityBuilder<TEntity> OrderByIf<TValue>(
325
        Expression<Func<TEntity, TValue>> property,
326
        SortDirections sortDirection = SortDirections.Ascending,
327
        Func<string, bool> condition = null)
328
    {
329
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
330

331
        return OrderByIf(propertyAccessor.Column, null, sortDirection, condition);
×
332
    }
333

334
    public SelectEntityBuilder<TEntity> OrderByIf<TValue>(
335
        Expression<Func<TEntity, TValue>> property,
336
        string tableAlias,
337
        SortDirections sortDirection = SortDirections.Ascending,
338
        Func<string, bool> condition = null)
339
    {
340
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
341

342
        return OrderByIf(propertyAccessor.Column, tableAlias, sortDirection, condition);
×
343
    }
344

345
    public SelectEntityBuilder<TEntity> GroupBy<TValue>(
346
        Expression<Func<TEntity, TValue>> property,
347
        string tableAlias = null)
348
    {
349
        var propertyAccessor = _typeAccessor.FindProperty(property);
3✔
350

351
        return GroupBy(propertyAccessor.Column, tableAlias);
3✔
352
    }
353

354
    public override QueryStatement BuildStatement()
355
    {
356
        // add table and schema from attribute if not set
357
        if (FromExpressions.Count == 0)
47✔
358
            From(_typeAccessor.TableName, _typeAccessor.TableSchema);
39✔
359

360
        return base.BuildStatement();
47✔
361
    }
362

363

364

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