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

loresoft / FluentCommand / 23256000174

18 Mar 2026 04:13PM UTC coverage: 56.658% (+0.2%) from 56.486%
23256000174

push

github

pwelter34
Add SQL Server structured parameter support

1303 of 2875 branches covered (45.32%)

Branch coverage included in aggregate %.

106 of 149 new or added lines in 8 files covered. (71.14%)

6 existing lines in 1 file now uncovered.

3969 of 6430 relevant lines covered (61.73%)

361.39 hits per line

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

91.78
/src/FluentCommand.SqlServer/SqlTypeMapping.cs
1
using System.Data;
2

3
namespace FluentCommand;
4

5
/// <summary>
6
/// Provides mapping between .NET types and their corresponding SQL Server native types.
7
/// </summary>
8
public static class SqlTypeMapping
9
{
10
    private static readonly Dictionary<Type, string> _nativeType = new()
1✔
11
    {
1✔
12
        {typeof(bool), "bit"},
1✔
13
        {typeof(sbyte), "smallint"},
1✔
14
        {typeof(byte), "tinyint"},
1✔
15
        {typeof(short), "smallint"},
1✔
16
        {typeof(ushort), "int"},
1✔
17
        {typeof(int), "int"},
1✔
18
        {typeof(uint), "bigint"},
1✔
19
        {typeof(long), "bigint"},
1✔
20
        {typeof(ulong), "decimal(20,0)"},
1✔
21
        {typeof(float), "real"},
1✔
22
        {typeof(double), "float"},
1✔
23
        {typeof(decimal), "decimal"},
1✔
24
        {typeof(byte[]), "varbinary(MAX)"},
1✔
25
        {typeof(string), "nvarchar(MAX)"},
1✔
26
        {typeof(char), "nchar(1)"},
1✔
27
        {typeof(TimeSpan), "time"},
1✔
28
        {typeof(DateTime), "datetime2"},
1✔
29
        {typeof(DateTimeOffset), "datetimeoffset"},
1✔
30
        {typeof(Guid), "uniqueidentifier"},
1✔
31
        #if NET6_0_OR_GREATER
1✔
32
        {typeof(DateOnly), "date"},
1✔
33
        {typeof(TimeOnly), "time"},
1✔
34
        #endif
1✔
35
    };
1✔
36

37
    private static readonly Dictionary<Type, SqlDbType> _dbType = new()
1✔
38
    {
1✔
39
        {typeof(bool), SqlDbType.Bit},
1✔
40
        {typeof(sbyte), SqlDbType.SmallInt},
1✔
41
        {typeof(byte), SqlDbType.TinyInt},
1✔
42
        {typeof(short), SqlDbType.SmallInt},
1✔
43
        {typeof(ushort), SqlDbType.Int},
1✔
44
        {typeof(int), SqlDbType.Int},
1✔
45
        {typeof(uint), SqlDbType.BigInt},
1✔
46
        {typeof(long), SqlDbType.BigInt},
1✔
47
        {typeof(ulong), SqlDbType.Decimal},
1✔
48
        {typeof(float), SqlDbType.Real},
1✔
49
        {typeof(double), SqlDbType.Float},
1✔
50
        {typeof(decimal), SqlDbType.Decimal},
1✔
51
        {typeof(byte[]), SqlDbType.VarBinary},
1✔
52
        {typeof(string), SqlDbType.NVarChar},
1✔
53
        {typeof(char), SqlDbType.NChar},
1✔
54
        {typeof(TimeSpan), SqlDbType.Time},
1✔
55
        {typeof(DateTime), SqlDbType.DateTime2},
1✔
56
        {typeof(DateTimeOffset), SqlDbType.DateTimeOffset},
1✔
57
        {typeof(Guid), SqlDbType.UniqueIdentifier},
1✔
58
        #if NET6_0_OR_GREATER
1✔
59
        {typeof(DateOnly), SqlDbType.Date},
1✔
60
        {typeof(TimeOnly), SqlDbType.Time},
1✔
61
        #endif
1✔
62
    };
1✔
63

64
    /// <summary>
65
    /// Gets the SQL Server native type name for the specified generic .NET type <typeparamref name="T"/>.
66
    /// </summary>
67
    /// <typeparam name="T">The .NET type to map to a SQL Server native type.</typeparam>
68
    /// <returns>
69
    /// The SQL Server native type name as a <see cref="string"/>.
70
    /// Returns <c>sql_variant</c> if the type is not explicitly mapped.
71
    /// </returns>
72
    public static string NativeType<T>()
73
    {
74
        return NativeType(typeof(T));
×
75
    }
76

77
    /// <summary>
78
    /// Gets the SQL Server native type name for the specified <see cref="Type"/>.
79
    /// </summary>
80
    /// <param name="type">The .NET <see cref="Type"/> to map to a SQL Server native type.</param>
81
    /// <returns>
82
    /// The SQL Server native type name as a <see cref="string"/>.
83
    /// Returns <c>sql_variant</c> if the type is not explicitly mapped.
84
    /// </returns>
85
    public static string NativeType(Type type)
86
    {
87
        var dataType = Nullable.GetUnderlyingType(type) ?? type;
131✔
88
        dataType = dataType.IsEnum ? Enum.GetUnderlyingType(dataType) : dataType;
131!
89

90
        _nativeType.TryGetValue(dataType, out var value);
131✔
91

92
        return value ?? "sql_variant";
131!
93
    }
94

95
    /// <summary>
96
    /// Gets the SQL Server <see cref="SqlDbType"/> for the specified generic .NET type <typeparamref name="T"/>.
97
    /// </summary>
98
    /// <typeparam name="T">The .NET type to map to a SQL Server <see cref="SqlDbType"/>.</typeparam>
99
    /// <returns>
100
    /// The SQL Server <see cref="SqlDbType"/> value.
101
    /// Returns <see cref="SqlDbType.NVarChar"/> if the type is not explicitly mapped.
102
    /// </returns>
103
    public static SqlDbType DbType<T>()
104
    {
NEW
105
        return DbType(typeof(T));
×
106
    }
107

108
    /// <summary>
109
    /// Gets the SQL Server <see cref="SqlDbType"/> for the specified <see cref="Type"/>.
110
    /// </summary>
111
    /// <param name="type">The .NET <see cref="Type"/> to map to a SQL Server <see cref="SqlDbType"/>.</param>
112
    /// <returns>
113
    /// The SQL Server <see cref="SqlDbType"/> value.
114
    /// Returns <see cref="SqlDbType.NVarChar"/> if the type is not explicitly mapped.
115
    /// </returns>
116
    public static SqlDbType DbType(Type type)
117
    {
118
        var dataType = Nullable.GetUnderlyingType(type) ?? type;
7✔
119
        dataType = dataType.IsEnum ? Enum.GetUnderlyingType(dataType) : dataType;
7!
120

121
        return _dbType.TryGetValue(dataType, out var value) ? value : SqlDbType.NVarChar;
7!
122
    }
123
}
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