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

loresoft / FluentCommand / 29168809467

11 Jul 2026 09:26PM UTC coverage: 65.067% (+0.02%) from 65.051%
29168809467

push

github

pwelter34
update packages

1745 of 3480 branches covered (50.14%)

Branch coverage included in aggregate %.

5549 of 7730 relevant lines covered (71.79%)

302.92 hits per line

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

90.67
/src/FluentCommand.SqlServer/SqlTypeMapping.cs
1
using System.Data;
2
using System.Text.Json;
3

4
namespace FluentCommand;
5

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

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

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

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

93
        _nativeType.TryGetValue(dataType, out var value);
133✔
94

95
        return value ?? "sql_variant";
133!
96
    }
97

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

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

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