• 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

66.67
/src/FluentCommand/DataParameterHandlers.cs
1
using System.Collections.Concurrent;
2
using System.Data.Common;
3

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

7
namespace FluentCommand;
8

9
/// <summary>
10
/// 
11
/// </summary>
12
public static class DataParameterHandlers
13
{
14
    private static readonly ConcurrentDictionary<Type, IDataParameterHandler> _dataTypeHandlers;
15

16
    static DataParameterHandlers()
17
    {
18
        _dataTypeHandlers = new ConcurrentDictionary<Type, IDataParameterHandler>();
3✔
19
        _dataTypeHandlers.TryAdd(typeof(ConcurrencyToken), new ConcurrencyTokenHandler());
3✔
20

21
#if NET6_0_OR_GREATER
22
        // once ADO supports DateOnly & TimeOnly, this can be removed
23
        _dataTypeHandlers.TryAdd(typeof(DateOnly), new DateOnlyHandler());
3✔
24
        _dataTypeHandlers.TryAdd(typeof(TimeOnly), new TimeOnlyHandler());
3✔
25
#endif
26
    }
3✔
27

28
    /// <summary>
29
    /// Adds the data type handler.
30
    /// </summary>
31
    /// <typeparam name="THandler">The type of the handler.</typeparam>
32
    public static void AddTypeHandler<THandler>()
33
        where THandler : IDataParameterHandler, new()
34
    {
35
        var handler = new THandler();
×
36
        AddTypeHandler(handler);
×
37
    }
×
38

39
    /// <summary>
40
    /// Adds the data type handler.
41
    /// </summary>
42
    /// <typeparam name="THandler">The type of the handler.</typeparam>
43
    /// <param name="handler">The handler.</param>
44
    public static void AddTypeHandler<THandler>(THandler handler)
45
        where THandler : IDataParameterHandler
46
    {
47
        _dataTypeHandlers.TryAdd(handler.ValueType, handler);
×
48
    }
×
49

50
    /// <summary>
51
    /// Gets the type handler for the specified <paramref name="type"/>.
52
    /// </summary>
53
    /// <param name="type">The type to get a handler for.</param>
54
    /// <returns>The <see cref="IDataParameterHandler"/> for the specified type; otherwise null if not found</returns>
55
    public static IDataParameterHandler GetTypeHandler(Type type)
56
    {
57
        var underlyingType = type.GetUnderlyingType();
304✔
58
        return _dataTypeHandlers.TryGetValue(underlyingType, out var handler) ? handler : null;
304!
59
    }
60

61

62
    /// <summary>
63
    /// Sets the Value and DbType on the specified <paramref name="parameter"/> using the registered type handlers.
64
    /// </summary>
65
    /// <param name="parameter">The parameter to set the value on.</param>
66
    /// <param name="value">The value to set.</param>
67
    /// <param name="type">The data type to use.</param>
68
    public static void SetValue(DbParameter parameter, object value, Type type)
69
    {
70
        var valueType = type.GetUnderlyingType();
299✔
71
        var handler = GetTypeHandler(valueType);
299✔
72

73
        value ??= DBNull.Value;
299✔
74

75
        if (handler != null)
299!
76
        {
77
            handler.SetValue(parameter, value);
×
78
            return;
×
79
        }
80

81
        parameter.Value = value;
299✔
82
        parameter.DbType = valueType.ToDbType();
299✔
83
    }
299✔
84
}
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