• 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

82.05
/src/FluentCommand/Query/InsertEntityBuilder.cs
1
using System.Linq.Expressions;
2

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

6
namespace FluentCommand.Query;
7

8
/// <summary>
9
/// Insert query statement builder
10
/// </summary>
11
/// <typeparam name="TEntity">The type of the entity.</typeparam>
12
public class InsertEntityBuilder<TEntity> : InsertBuilder<InsertEntityBuilder<TEntity>>
13
    where TEntity : class
14
{
15
    private static readonly TypeAccessor _typeAccessor = TypeAccessor.GetAccessor<TEntity>();
4✔
16

17
    /// <summary>
18
    /// Initializes a new instance of the <see cref="InsertEntityBuilder{TEntity}"/> class.
19
    /// </summary>
20
    /// <param name="queryGenerator">The query generator.</param>
21
    /// <param name="parameters">The query parameters.</param>
22
    public InsertEntityBuilder(
23
        IQueryGenerator queryGenerator,
24
        List<QueryParameter> parameters)
25
        : base(queryGenerator, parameters)
11✔
26
    {
27
    }
11✔
28

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

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

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

81
        var properties = _typeAccessor.GetProperties();
7✔
82
        var columnSet = new HashSet<string>(columnNames ?? Enumerable.Empty<string>());
7✔
83

84
        foreach (var property in properties)
336✔
85
        {
86
            if (columnSet.Count > 0 && !columnSet.Contains(property.Column))
161!
87
                continue;
88

89
            if (property.IsNotMapped || property.IsDatabaseGenerated)
161✔
90
                continue;
91

92
            // include the type to prevent issues with null
93
            Value(property.Column, property.GetValue(entity), property.MemberType);
126✔
94
        }
95

96
        return this;
7✔
97
    }
98

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

118
    /// <summary>
119
    /// Conditionally add an output clause for the specified property.
120
    /// </summary>
121
    /// <typeparam name="TValue">The type of the value.</typeparam>
122
    /// <param name="property">The property.</param>
123
    /// <param name="tableAlias">The table alias.</param>
124
    /// <param name="columnAlias">The column alias.</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 InsertEntityBuilder<TEntity> OutputIf<TValue>(
130
        Expression<Func<TEntity, TValue>> property,
131
        string tableAlias = "INSERTED",
132
        string columnAlias = null,
133
        Func<string, bool> condition = null)
134
    {
135
        var propertyAccessor = _typeAccessor.FindProperty(property);
×
136
        return OutputIf(propertyAccessor.Column, tableAlias, columnAlias, condition);
×
137
    }
138

139
    /// <inheritdoc />
140
    public override QueryStatement BuildStatement()
141
    {
142
        // add table and schema from attribute if not set
143
        if (TableExpression == null)
11✔
144
            Into(_typeAccessor.TableName, _typeAccessor.TableSchema);
11✔
145

146
        return base.BuildStatement();
11✔
147
    }
148
}
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