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

loresoft / SchemaSaurus / 27082301842

07 Jun 2026 04:09AM UTC coverage: 86.906% (-0.1%) from 87.024%
27082301842

push

github

pwelter34
Merge branch 'main' of https://github.com/loresoft/SchemaSaurus

1065 of 1430 branches covered (74.48%)

Branch coverage included in aggregate %.

5738 of 6398 relevant lines covered (89.68%)

227.89 hits per line

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

98.45
/src/SchemaSaurus.SqlServer/SqlServerTypeMapper.cs
1
using System.Collections.Frozen;
2
using System.Data;
3

4
namespace SchemaSaurus.SqlServer;
5

6
/// <summary>
7
/// Provides mappings from SQL Server native data type names to common .NET data type metadata.
8
/// </summary>
9
public static class SqlServerTypeMapper
10
{
11
    // Mapping of SQL Server system type names to DbType, SqlDbType, CLR type, and Unicode/fixed-length attributes.
12
    private static readonly FrozenDictionary<string, (DbType DbType, SqlDbType SqlDbType, Type SystemType, bool? IsUnicode, bool? IsFixedLength)> SqlServerTypeMappings
1✔
13
        = new Dictionary<string, (DbType DbType, SqlDbType SqlDbType, Type SystemType, bool? IsUnicode, bool? IsFixedLength)>(StringComparer.OrdinalIgnoreCase)
1✔
14
        {
1✔
15
            ["bigint"] = (DbType.Int64, SqlDbType.BigInt, typeof(long), null, null),
1✔
16
            ["int"] = (DbType.Int32, SqlDbType.Int, typeof(int), null, null),
1✔
17
            ["smallint"] = (DbType.Int16, SqlDbType.SmallInt, typeof(short), null, null),
1✔
18
            ["tinyint"] = (DbType.Byte, SqlDbType.TinyInt, typeof(byte), null, null),
1✔
19
            ["bit"] = (DbType.Boolean, SqlDbType.Bit, typeof(bool), null, null),
1✔
20
            ["decimal"] = (DbType.Decimal, SqlDbType.Decimal, typeof(decimal), null, null),
1✔
21
            ["numeric"] = (DbType.Decimal, SqlDbType.Decimal, typeof(decimal), null, null),
1✔
22
            ["money"] = (DbType.Currency, SqlDbType.Money, typeof(decimal), null, null),
1✔
23
            ["smallmoney"] = (DbType.Currency, SqlDbType.SmallMoney, typeof(decimal), null, null),
1✔
24
            ["float"] = (DbType.Double, SqlDbType.Float, typeof(double), null, null),
1✔
25
            ["real"] = (DbType.Single, SqlDbType.Real, typeof(float), null, null),
1✔
26
            ["datetime"] = (DbType.DateTime, SqlDbType.DateTime, typeof(DateTime), null, null),
1✔
27
            ["smalldatetime"] = (DbType.DateTime, SqlDbType.SmallDateTime, typeof(DateTime), null, null),
1✔
28
            ["datetime2"] = (DbType.DateTime2, SqlDbType.DateTime2, typeof(DateTime), null, null),
1✔
29
            ["datetimeoffset"] = (DbType.DateTimeOffset, SqlDbType.DateTimeOffset, typeof(DateTimeOffset), null, null),
1✔
30
            ["char"] = (DbType.AnsiStringFixedLength, SqlDbType.Char, typeof(string), false, true),
1✔
31
            ["varchar"] = (DbType.AnsiString, SqlDbType.VarChar, typeof(string), false, false),
1✔
32
            ["text"] = (DbType.AnsiString, SqlDbType.Text, typeof(string), false, false),
1✔
33
            ["nchar"] = (DbType.StringFixedLength, SqlDbType.NChar, typeof(string), true, true),
1✔
34
            ["nvarchar"] = (DbType.String, SqlDbType.NVarChar, typeof(string), true, false),
1✔
35
            ["ntext"] = (DbType.String, SqlDbType.NText, typeof(string), true, false),
1✔
36
            ["json"] = (DbType.String, GetJsonSqlDbType(), typeof(string), true, false),
1✔
37
            ["binary"] = (DbType.Binary, SqlDbType.Binary, typeof(byte[]), null, true),
1✔
38
            ["varbinary"] = (DbType.Binary, SqlDbType.VarBinary, typeof(byte[]), null, false),
1✔
39
            ["image"] = (DbType.Binary, SqlDbType.Image, typeof(byte[]), null, false),
1✔
40
            ["timestamp"] = (DbType.Binary, SqlDbType.Timestamp, typeof(byte[]), null, null),
1✔
41
            ["rowversion"] = (DbType.Binary, SqlDbType.Timestamp, typeof(byte[]), null, null),
1✔
42
            ["uniqueidentifier"] = (DbType.Guid, SqlDbType.UniqueIdentifier, typeof(Guid), null, null),
1✔
43
            ["xml"] = (DbType.Xml, SqlDbType.Xml, typeof(string), null, null),
1✔
44
            ["geometry"] = (DbType.Object, SqlDbType.Udt, typeof(object), null, null),
1✔
45
            ["geography"] = (DbType.Object, SqlDbType.Udt, typeof(object), null, null),
1✔
46
            ["hierarchyid"] = (DbType.Object, SqlDbType.Udt, typeof(object), null, null),
1✔
47
            ["vector"] = (DbType.Object, GetVectorSqlDbType(), typeof(float[]), null, null),
1✔
48
            ["sql_variant"] = (DbType.Object, SqlDbType.Variant, typeof(object), null, null),
1✔
49
#if NET6_0_OR_GREATER
1✔
50
            ["time"] = (DbType.Time, SqlDbType.Time, typeof(TimeOnly), null, null),
1✔
51
            ["date"] = (DbType.Date, SqlDbType.Date, typeof(DateOnly), null, null),
1✔
52
#else
1✔
53
            ["time"] = (DbType.Time, SqlDbType.Time, typeof(TimeSpan), null, null),
1✔
54
            ["date"] = (DbType.Date, SqlDbType.Date, typeof(DateTime), null, null),
1✔
55
#endif
1✔
56
        }.ToFrozenDictionary();
1✔
57

58
    private static readonly FrozenDictionary<DbType, SqlDbType> DbTypeToSqlDbTypeMappings
1✔
59
        = new Dictionary<DbType, SqlDbType>
1✔
60
        {
1✔
61
            [DbType.AnsiString] = SqlDbType.VarChar,
1✔
62
            [DbType.AnsiStringFixedLength] = SqlDbType.Char,
1✔
63
            [DbType.Binary] = SqlDbType.VarBinary,
1✔
64
            [DbType.Boolean] = SqlDbType.Bit,
1✔
65
            [DbType.Byte] = SqlDbType.TinyInt,
1✔
66
            [DbType.Currency] = SqlDbType.Money,
1✔
67
            [DbType.Date] = SqlDbType.Date,
1✔
68
            [DbType.DateTime] = SqlDbType.DateTime,
1✔
69
            [DbType.DateTime2] = SqlDbType.DateTime2,
1✔
70
            [DbType.DateTimeOffset] = SqlDbType.DateTimeOffset,
1✔
71
            [DbType.Decimal] = SqlDbType.Decimal,
1✔
72
            [DbType.Double] = SqlDbType.Float,
1✔
73
            [DbType.Guid] = SqlDbType.UniqueIdentifier,
1✔
74
            [DbType.Int16] = SqlDbType.SmallInt,
1✔
75
            [DbType.Int32] = SqlDbType.Int,
1✔
76
            [DbType.Int64] = SqlDbType.BigInt,
1✔
77
            [DbType.Object] = SqlDbType.Variant,
1✔
78
            [DbType.Single] = SqlDbType.Real,
1✔
79
            [DbType.String] = SqlDbType.NVarChar,
1✔
80
            [DbType.StringFixedLength] = SqlDbType.NChar,
1✔
81
            [DbType.Time] = SqlDbType.Time,
1✔
82
            [DbType.Xml] = SqlDbType.Xml,
1✔
83
        }.ToFrozenDictionary();
1✔
84

85
    private static readonly FrozenDictionary<SqlDbType, DbType> SqlDbTypeToDbTypeMappings
1✔
86
        = new Dictionary<SqlDbType, DbType>
1✔
87
        {
1✔
88
            [SqlDbType.BigInt] = DbType.Int64,
1✔
89
            [SqlDbType.Binary] = DbType.Binary,
1✔
90
            [SqlDbType.Bit] = DbType.Boolean,
1✔
91
            [SqlDbType.Char] = DbType.AnsiStringFixedLength,
1✔
92
            [SqlDbType.Date] = DbType.Date,
1✔
93
            [SqlDbType.DateTime] = DbType.DateTime,
1✔
94
            [SqlDbType.DateTime2] = DbType.DateTime2,
1✔
95
            [SqlDbType.DateTimeOffset] = DbType.DateTimeOffset,
1✔
96
            [SqlDbType.Decimal] = DbType.Decimal,
1✔
97
            [SqlDbType.Float] = DbType.Double,
1✔
98
            [SqlDbType.Image] = DbType.Binary,
1✔
99
            [SqlDbType.Int] = DbType.Int32,
1✔
100
            [SqlDbType.Money] = DbType.Currency,
1✔
101
            [SqlDbType.NChar] = DbType.StringFixedLength,
1✔
102
            [SqlDbType.NText] = DbType.String,
1✔
103
            [SqlDbType.NVarChar] = DbType.String,
1✔
104
            [SqlDbType.Real] = DbType.Single,
1✔
105
            [SqlDbType.SmallDateTime] = DbType.DateTime,
1✔
106
            [SqlDbType.SmallInt] = DbType.Int16,
1✔
107
            [SqlDbType.SmallMoney] = DbType.Currency,
1✔
108
            [SqlDbType.Structured] = DbType.Object,
1✔
109
            [SqlDbType.Text] = DbType.AnsiString,
1✔
110
            [SqlDbType.Time] = DbType.Time,
1✔
111
            [SqlDbType.Timestamp] = DbType.Binary,
1✔
112
            [SqlDbType.TinyInt] = DbType.Byte,
1✔
113
            [SqlDbType.Udt] = DbType.Object,
1✔
114
            [SqlDbType.UniqueIdentifier] = DbType.Guid,
1✔
115
            [SqlDbType.VarBinary] = DbType.Binary,
1✔
116
            [SqlDbType.VarChar] = DbType.AnsiString,
1✔
117
            [SqlDbType.Variant] = DbType.Object,
1✔
118
            [SqlDbType.Xml] = DbType.Xml,
1✔
119
#if NET9_0_OR_GREATER
1✔
120
            [SqlDbType.Json] = DbType.String,
1✔
121
#endif
1✔
122
#if NET10_0_OR_GREATER
1✔
123
            [SqlDbType.Vector] = DbType.Object,
1✔
124
#endif
1✔
125
        }.ToFrozenDictionary();
1✔
126

127

128
    /// <summary>
129
    /// Maps a SQL Server native data type name to its corresponding <see cref="DbType"/>, provider type, CLR type, and text attributes.
130
    /// </summary>
131
    /// <param name="typeName">The SQL Server native type name (for example, <c>nvarchar</c> or <c>datetime2</c>).</param>
132
    /// <returns>
133
    /// A tuple containing mapped <see cref="DbType"/>, <see cref="SqlDbType"/>, CLR <see cref="Type"/>,
134
    /// and optional Unicode/fixed-length flags. Unknown types map to object/variant defaults.
135
    /// </returns>
136
    public static (DbType DbType, SqlDbType SqlDbType, Type SystemType, bool? IsUnicode, bool? IsFixedLength) MapNativeType(string typeName)
137
    {
138
        if (SqlServerTypeMappings.TryGetValue(typeName, out var mapping))
1,773✔
139
            return mapping;
1,767✔
140

141
        return (DbType.Object, SqlDbType.Variant, typeof(object), null, null);
6✔
142
    }
143

144
    /// <summary>
145
    /// Maps a <see cref="DbType"/> value to its closest SQL Server-specific <see cref="SqlDbType"/> value.
146
    /// </summary>
147
    /// <param name="dbType">The provider-independent database type to map.</param>
148
    /// <returns>The closest matching <see cref="SqlDbType"/> value. Unknown values map to <see cref="SqlDbType.Variant"/>.</returns>
149
    public static SqlDbType ToSqlDbType(DbType dbType)
150
    {
151
        if (DbTypeToSqlDbTypeMappings.TryGetValue(dbType, out var sqlDbType))
7✔
152
            return sqlDbType;
6✔
153

154
        return SqlDbType.Variant;
1✔
155
    }
156

157
    /// <summary>
158
    /// Maps a SQL Server-specific <see cref="SqlDbType"/> value to its closest provider-independent <see cref="DbType"/> value.
159
    /// </summary>
160
    /// <param name="sqlDbType">The SQL Server-specific database type to map.</param>
161
    /// <returns>The closest matching <see cref="DbType"/> value. Unknown values map to <see cref="DbType.Object"/>.</returns>
162
    public static DbType ToDbType(SqlDbType sqlDbType)
163
    {
164
        if (SqlDbTypeToDbTypeMappings.TryGetValue(sqlDbType, out var dbType))
8!
165
            return dbType;
8✔
166

167
        return DbType.Object;
×
168
    }
169

170

171
    private static SqlDbType GetJsonSqlDbType()
172
    {
173
#if NET9_0_OR_GREATER
174
        return SqlDbType.Json;
1✔
175
#else
176
        return SqlDbType.NVarChar;
177
#endif
178
    }
179

180
    private static SqlDbType GetVectorSqlDbType()
181
    {
182
#if NET10_0_OR_GREATER
183
        return SqlDbType.Vector;
1✔
184
#else
185
        return SqlDbType.Variant;
186
#endif
187
    }
188
}
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