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

loresoft / FluentCommand / 5874747104

16 Aug 2023 04:44AM UTC coverage: 43.028% (+2.5%) from 40.495%
5874747104

push

github

web-flow
Merge pull request #277 from loresoft/feature/method-injectors

Feature/method injectors

765 of 2171 branches covered (35.24%)

Branch coverage included in aggregate %.

126 of 126 new or added lines in 6 files covered. (100.0%)

2361 of 5094 relevant lines covered (46.35%)

119.7 hits per line

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

73.68
/src/FluentCommand/DataQueryExtensions.cs
1
using System.Data;
2
using System.Dynamic;
3

4
using FluentCommand.Extensions;
5
using FluentCommand.Reflection;
6

7
namespace FluentCommand;
8

9
/// <summary>
10
/// Extension methods for <see cref="IDataQuery"/>
11
/// </summary>
12
public static class DataQueryExtensions
13
{
14
    /// <summary>
15
    /// Executes the command against the connection and converts the results to <typeparamref name="TEntity" /> objects asynchronously.
16
    /// </summary>
17
    /// <typeparam name="TEntity">The type of the entity.</typeparam>
18
    /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
19
    /// <param name="factory">The <see langword="delegate" /> factory to convert the <see cref="T:System.Data.IDataReader" /> to <typeparamref name="TEntity" />.</param>
20
    /// <param name="cancellationToken">The cancellation instruction.</param>
21
    /// <returns>
22
    /// An <see cref="T:System.Collections.Generic.IEnumerable`1" /> of <typeparamref name="TEntity" /> objects.
23
    /// </returns>
24
    /// <exception cref="System.ArgumentNullException"><paramref name="factory"/> is null</exception>
25
    public static Task<IEnumerable<TEntity>> QueryAsync<TEntity>(
26
        this IDataQueryAsync dataQuery,
27
        Func<IDataReader, TEntity> factory,
28
        CancellationToken cancellationToken = default)
29
    {
30
        return dataQuery.QueryAsync(factory, CommandBehavior.SingleResult, cancellationToken);
10✔
31
    }
32

33
    /// <summary>
34
    /// Executes the query and returns the first row in the result as a <typeparamref name="TEntity" /> object asynchronously.
35
    /// </summary>
36
    /// <typeparam name="TEntity">The type of the entity.</typeparam>
37
    /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
38
    /// <param name="factory">The <see langword="delegate" /> factory to convert the <see cref="T:System.Data.IDataReader" /> to <typeparamref name="TEntity" />.</param>
39
    /// <param name="cancellationToken">The cancellation instruction.</param>
40
    /// <returns>
41
    /// A instance of <typeparamref name="TEntity" /> if row exists; otherwise null.
42
    /// </returns>
43
    /// <exception cref="System.ArgumentNullException"><paramref name="factory"/> is null</exception>
44
    public static Task<TEntity> QuerySingleAsync<TEntity>(
45
        this IDataQueryAsync dataQuery,
46
        Func<IDataReader, TEntity> factory,
47
        CancellationToken cancellationToken = default)
48
    {
49
        return dataQuery.QuerySingleAsync(factory, CommandBehavior.SingleResult | CommandBehavior.SingleRow, cancellationToken);
3✔
50

51
    }
52

53

54
    /// <summary>
55
    /// Executes the query and returns the first column of the first row in the result set returned by the query. All other columns and rows are ignored.
56
    /// </summary>
57
    /// <typeparam name="TValue">The type of the value.</typeparam>
58
    /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
59
    /// <returns>
60
    /// The value of the first column of the first row in the result set.
61
    /// </returns>
62
    public static TValue QueryValue<TValue>(this IDataQuery dataQuery)
63
    {
64
        return dataQuery.QueryValue<TValue>(null);
3✔
65
    }
66

67
    /// <summary>
68
    /// Executes the query and returns the first column of the first row in the result set returned by the query asynchronously. All other columns and rows are ignored.
69
    /// </summary>
70
    /// <typeparam name="TValue">The type of the value.</typeparam>
71
    /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
72
    /// <param name="cancellationToken">The cancellation instruction.</param>
73
    /// <returns>
74
    /// The value of the first column of the first row in the result set.
75
    /// </returns>
76
    public static Task<TValue> QueryValueAsync<TValue>(
77
        this IDataQueryAsync dataQuery,
78
        CancellationToken cancellationToken = default)
79
    {
80
        return dataQuery.QueryValueAsync<TValue>(null, cancellationToken);
10✔
81
    }
82

83
    /// <summary>
84
    /// Executes the query and returns the first column values in the result set returned by the query. All other columns and rows are ignored.
85
    /// </summary>
86
    /// <typeparam name="TValue">The type of the value.</typeparam>
87
    /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
88
    /// <returns>
89
    /// The value of the first column values in the result set.
90
    /// </returns>
91
    public static IEnumerable<TValue> QueryValues<TValue>(this IDataQuery dataQuery)
92
    {
93
        return dataQuery.Query(r => r.GetValue<TValue>(0));
×
94
    }
95

96
    /// <summary>
97
    /// Executes the query and returns the first column values in the result set returned by the query. All other columns and rows are ignored.
98
    /// </summary>
99
    /// <typeparam name="TValue">The type of the value.</typeparam>
100
    /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
101
    /// <param name="cancellationToken">The cancellation instruction.</param>
102
    /// <returns>
103
    /// The value of the first column values in the result set.
104
    /// </returns>
105
    public static async Task<IEnumerable<TValue>> QueryValuesAsync<TValue>(
106
        this IDataQueryAsync dataQuery,
107
        CancellationToken cancellationToken = default)
108
    {
109
        return await dataQuery.QueryAsync(r => r.GetValue<TValue>(0), cancellationToken);
110
    }
111

112

113
    /// <summary>
114
    /// Executes the command against the connection and converts the results to dynamic objects.
115
    /// </summary>
116
    /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
117
    /// <returns>
118
    /// An <see cref="T:System.Collections.Generic.IEnumerable`1" /> of dynamic objects.
119
    /// </returns>
120
    public static IEnumerable<dynamic> Query(this IDataQuery dataQuery)
121
    {
122
        return dataQuery.Query(DataReaderExtensions.DynamicFactory);
9!
123
    }
124

125
    /// <summary>
126
    /// Executes the query and returns the first row in the result as a dynamic object.
127
    /// </summary>
128
    /// <param name="dataQuery">The <see cref="IDataQuery"/> for this extension method.</param>
129
    /// <returns>
130
    /// A instance of a dynamic object if row exists; otherwise null.
131
    /// </returns>
132
    public static dynamic QuerySingle(this IDataQuery dataQuery)
133
    {
134
        return dataQuery.QuerySingle(DataReaderExtensions.DynamicFactory);
3!
135
    }
136

137
    /// <summary>
138
    /// Executes the command against the connection and converts the results to dynamic objects asynchronously.
139
    /// </summary>
140
    /// <param name="dataQuery">The <see cref="IDataQueryAsync"/> for this extension method.</param>
141
    /// <param name="cancellationToken">The cancellation instruction.</param>
142
    /// <returns>
143
    /// An <see cref="T:System.Collections.Generic.IEnumerable`1" /> of dynamic objects.
144
    /// </returns>
145
    public static Task<IEnumerable<dynamic>> QueryAsync(this IDataQueryAsync dataQuery, CancellationToken cancellationToken = default)
146
    {
147
        return dataQuery.QueryAsync(DataReaderExtensions.DynamicFactory, cancellationToken);
9✔
148
    }
149

150
    /// <summary>
151
    /// Executes the query and returns the first row in the result as a dynamic object asynchronously.
152
    /// </summary>
153
    /// <param name="dataQuery">The <see cref="IDataQueryAsync"/> for this extension method.</param>
154
    /// <param name="cancellationToken">The cancellation instruction.</param>
155
    /// <returns>
156
    /// A instance of a dynamic object if row exists; otherwise null.
157
    /// </returns>
158
    public static Task<dynamic> QuerySingleAsync(this IDataQueryAsync dataQuery, CancellationToken cancellationToken = default)
159
    {
160
        return dataQuery.QuerySingleAsync(DataReaderExtensions.DynamicFactory, cancellationToken);
3✔
161
    }
162
}
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