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

JaCraig / ObjectCartographer / 22219553474

20 Feb 2026 09:56AM UTC coverage: 83.921% (-1.2%) from 85.158%
22219553474

push

github

web-flow
Merge pull request #379 from JaCraig/dependabot/nuget/ObjectCartographer.Tests/dependencies-6899efc6e8

chore: Bump the dependencies group with 1 update

449 of 594 branches covered (75.59%)

Branch coverage included in aggregate %.

908 of 1023 relevant lines covered (88.76%)

4120.68 hits per line

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

96.34
/ObjectCartographer.SQL/Converters/SqlDbTypeConverter.cs
1
using Microsoft.Data.SqlClient;
2
using ObjectCartographer.ExpressionBuilder;
3
using ObjectCartographer.ExpressionBuilder.Interfaces;
4
using System;
5
using System.Collections.Generic;
6
using System.Data;
7
using System.Linq.Expressions;
8

9
namespace ObjectCartographer.SQL.Converters
10
{
11
    /// <summary>
12
    /// SqlDbType converter
13
    /// </summary>
14
    /// <seealso cref="IConverter"/>
15
    public class SqlDbTypeConverter : IConverter
16
    {
17
        /// <summary>
18
        /// Initializes a new instance of the <see cref="SqlDbTypeConverter"/> class.
19
        /// </summary>
20
        public SqlDbTypeConverter()
91✔
21
        {
22
            ConvertToTypes = new Dictionary<Type, Func<SqlDbType, object>>
91✔
23
            {
91✔
24
                { typeof(Type), SqlDbTypeToType },
91✔
25
                { typeof(DbType), SqlDbTypeToDbType }
91✔
26
            };
91✔
27
        }
91✔
28

29
        /// <summary>
30
        /// Gets the convert to types.
31
        /// </summary>
32
        /// <value>The convert to types.</value>
33
        public Dictionary<Type, Func<SqlDbType, object>> ConvertToTypes { get; }
50✔
34

35
        /// <summary>
36
        /// Gets the order.
37
        /// </summary>
38
        /// <value>The order.</value>
39
        public int Order => int.MinValue;
4✔
40

41
        /// <summary>
42
        /// Conversions
43
        /// </summary>
44
        protected static Dictionary<Type, DbType> Conversions { get; } = new Dictionary<Type, DbType>
17✔
45
            {
1✔
46
                { typeof(byte), DbType.Byte },
1✔
47
                { typeof(byte?), DbType.Byte },
1✔
48
                { typeof(sbyte), DbType.Byte },
1✔
49
                { typeof(sbyte?), DbType.Byte },
1✔
50
                { typeof(short), DbType.Int16 },
1✔
51
                { typeof(short?), DbType.Int16 },
1✔
52
                { typeof(ushort), DbType.Int16 },
1✔
53
                { typeof(ushort?), DbType.Int16 },
1✔
54
                { typeof(int), DbType.Int32 },
1✔
55
                { typeof(int?), DbType.Int32 },
1✔
56
                { typeof(uint), DbType.Int32 },
1✔
57
                { typeof(uint?), DbType.Int32 },
1✔
58
                { typeof(long), DbType.Int64 },
1✔
59
                { typeof(long?), DbType.Int64 },
1✔
60
                { typeof(ulong), DbType.Int64 },
1✔
61
                { typeof(ulong?), DbType.Int64 },
1✔
62
                { typeof(float), DbType.Single },
1✔
63
                { typeof(float?), DbType.Single },
1✔
64
                { typeof(double), DbType.Double },
1✔
65
                { typeof(double?), DbType.Double },
1✔
66
                { typeof(decimal), DbType.Decimal },
1✔
67
                { typeof(decimal?), DbType.Decimal },
1✔
68
                { typeof(bool), DbType.Boolean },
1✔
69
                { typeof(bool?), DbType.Boolean },
1✔
70
                { typeof(string), DbType.String },
1✔
71
                { typeof(char), DbType.StringFixedLength },
1✔
72
                { typeof(char?), DbType.StringFixedLength },
1✔
73
                { typeof(Guid), DbType.Guid },
1✔
74
                { typeof(Guid?), DbType.Guid },
1✔
75
                { typeof(DateTime), DbType.DateTime2 },
1✔
76
                { typeof(DateTime?), DbType.DateTime2 },
1✔
77
                { typeof(DateTimeOffset), DbType.DateTimeOffset },
1✔
78
                { typeof(DateTimeOffset?), DbType.DateTimeOffset },
1✔
79
                { typeof(TimeSpan), DbType.Time },
1✔
80
                { typeof(Uri), DbType.String },
1✔
81
                { typeof(TimeSpan?), DbType.Time },
1✔
82
                { typeof(byte[]), DbType.Binary }
1✔
83
            };
1✔
84

85
        /// <summary>
86
        /// Gets the database type to type conversions.
87
        /// </summary>
88
        /// <value>The database type to type conversions.</value>
89
        protected static Dictionary<DbType, Type> DbTypeToTypeConversions { get; } = new Dictionary<DbType, Type>
14✔
90
            {
1✔
91
                { DbType.Byte , typeof(byte)},
1✔
92
                {DbType.SByte , typeof(sbyte)},
1✔
93
                {DbType.Int16 , typeof(short)},
1✔
94
                {DbType.UInt16 , typeof(ushort)},
1✔
95
                {DbType.Int32 , typeof(int)},
1✔
96
                {DbType.UInt32 , typeof(uint)},
1✔
97
                {DbType.Int64 , typeof(long)},
1✔
98
                {DbType.UInt64 , typeof(ulong)},
1✔
99
                {DbType.Single , typeof(float)},
1✔
100
                {DbType.Double , typeof(double)},
1✔
101
                {DbType.Decimal , typeof(decimal)},
1✔
102
                {DbType.Boolean , typeof(bool)},
1✔
103
                {DbType.String , typeof(string)},
1✔
104
                {DbType.StringFixedLength , typeof(char)},
1✔
105
                {DbType.Guid , typeof(Guid)},
1✔
106
                {DbType.DateTime2 , typeof(DateTime)},
1✔
107
                {DbType.DateTime , typeof(DateTime)},
1✔
108
                {DbType.DateTimeOffset , typeof(DateTimeOffset)},
1✔
109
                {DbType.Binary , typeof(byte[])},
1✔
110
                {DbType.Time , typeof(TimeSpan)},
1✔
111
            };
1✔
112

113
        /// <summary>
114
        /// Determines whether this instance can handle the specified types.
115
        /// </summary>
116
        /// <param name="sourceType">Type of the source.</param>
117
        /// <param name="destinationType">Type of the destination.</param>
118
        /// <returns>
119
        /// <c>true</c> if this instance can handle the specified types; otherwise, <c>false</c>.
120
        /// </returns>
121
        public bool CanHandle(Type sourceType, Type destinationType)
122
        {
123
            return (destinationType == typeof(SqlDbType) && (sourceType == typeof(DbType) || sourceType == typeof(Type) || sourceType == typeof(Type).GetType()))
124✔
124
                || (sourceType == typeof(SqlDbType) && (destinationType == typeof(DbType) || destinationType == typeof(Type) || destinationType == typeof(Type).GetType()));
124✔
125
        }
126

127
        /// <summary>
128
        /// Convert from an object to a DbType
129
        /// </summary>
130
        /// <param name="value">Value</param>
131
        /// <returns>The DbType version</returns>
132
        public SqlDbType ConvertFrom<TSource>(TSource value)
133
        {
134
            if (value is null)
27✔
135
                return SqlDbType.Int;
1✔
136
            if (value is DbType DBTypeObject)
26✔
137
                return DbTypeToSqlDbType(DBTypeObject);
10✔
138
            if (value is Type TypeObject)
16!
139
                return TypeToSqlDbType(TypeObject);
16✔
140

141
            return SqlDbType.Int;
×
142
        }
143

144
        /// <summary>
145
        /// Converts the DbType object to another type
146
        /// </summary>
147
        /// <typeparam name="TDestination">The type of the destination.</typeparam>
148
        /// <param name="value">The value.</param>
149
        /// <returns></returns>
150
        public TDestination ConvertTo<TDestination>(SqlDbType value)
151
        {
152
            Type DestinationType = typeof(TDestination);
25✔
153
            if (ConvertToTypes.ContainsKey(DestinationType))
25✔
154
            {
155
                return (TDestination)ConvertToTypes[DestinationType](value);
24✔
156
            }
157

158
            return default!;
1✔
159
        }
160

161
        /// <summary>
162
        /// Maps the specified source to the destination.
163
        /// </summary>
164
        /// <param name="source">The source.</param>
165
        /// <param name="destination">The destination.</param>
166
        /// <param name="sourceType">Type of the source.</param>
167
        /// <param name="destinationType">Type of the destination.</param>
168
        /// <param name="mapping">The mapping.</param>
169
        /// <param name="manager">The manager.</param>
170
        /// <returns>The resulting expression.</returns>
171
        public Expression Map(Expression source, Expression? destination, Type sourceType, Type destinationType, IExpressionMapping mapping, ExpressionBuilderManager manager)
172
        {
173
            if (sourceType == typeof(SqlDbType))
5✔
174
                return Expression.Call(Expression.Constant(this), typeof(SqlDbTypeConverter).GetMethod(nameof(SqlDbTypeConverter.ConvertTo)).MakeGenericMethod(destinationType), source);
2✔
175
            if (destinationType == typeof(SqlDbType))
3✔
176
                return Expression.Call(Expression.Constant(this), typeof(SqlDbTypeConverter).GetMethod(nameof(SqlDbTypeConverter.ConvertFrom)).MakeGenericMethod(sourceType), source);
2✔
177
            return Expression.Empty();
1✔
178
        }
179

180
        /// <summary>
181
        /// Databases the type of the type to SQL database.
182
        /// </summary>
183
        /// <param name="TempValue">The temporary value.</param>
184
        /// <returns></returns>
185
        private static SqlDbType DbTypeToSqlDbType(DbType TempValue)
186
        {
187
            if (TempValue == DbType.Time)
10✔
188
                return SqlDbType.Time;
1✔
189
            try
190
            {
191
                var Parameter = new SqlParameter
9✔
192
                {
9✔
193
                    DbType = TempValue
9✔
194
                };
9✔
195
                return Parameter.SqlDbType;
9✔
196
            }
197
            catch { return SqlDbType.Int; }
×
198
        }
9✔
199

200
        /// <summary>
201
        /// SQLs the type of the database type to database.
202
        /// </summary>
203
        /// <param name="args">The arguments.</param>
204
        /// <returns></returns>
205
        private static object SqlDbTypeToDbType(SqlDbType args)
206
        {
207
            if (args == SqlDbType.Time)
10✔
208
                return DbType.Time;
1✔
209

210
            var Parameter = new SqlParameter
9✔
211
            {
9✔
212
                SqlDbType = args
9✔
213
            };
9✔
214
            return Parameter.DbType;
9✔
215
        }
216

217
        /// <summary>
218
        /// SQLs the type of the database type to.
219
        /// </summary>
220
        /// <param name="arg">The argument.</param>
221
        /// <returns></returns>
222
        private static object SqlDbTypeToType(SqlDbType arg)
223
        {
224
            if (arg == SqlDbType.Time)
14✔
225
                return typeof(TimeSpan);
1✔
226

227
            var Parameter = new SqlParameter
13✔
228
            {
13✔
229
                SqlDbType = arg
13✔
230
            };
13✔
231
            return DbTypeToTypeConversions.TryGetValue(Parameter.DbType, out Type? returnValue) ? returnValue : typeof(int);
13!
232
        }
233

234
        /// <summary>
235
        /// Types the type of to SQL database.
236
        /// </summary>
237
        /// <param name="TempValue">The temporary value.</param>
238
        /// <returns></returns>
239
        private static SqlDbType TypeToSqlDbType(Type TempValue)
240
        {
241
            if (TempValue.IsEnum)
16✔
242
            {
243
                TempValue = Enum.GetUnderlyingType(TempValue);
1✔
244
            }
245

246
            if (!Conversions.TryGetValue(TempValue, out DbType Item))
16!
247
                return SqlDbType.Int;
×
248
            if (Item == DbType.Time)
16✔
249
                return SqlDbType.Time;
1✔
250
            var Parameter = new SqlParameter
15✔
251
            {
15✔
252
                DbType = Item
15✔
253
            };
15✔
254
            return Parameter.SqlDbType;
15✔
255
        }
256
    }
257
}
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