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

JaCraig / ObjectCartographer / 16751360440

05 Aug 2025 01:24PM UTC coverage: 83.921% (-1.2%) from 85.158%
16751360440

push

github

web-flow
Merge pull request #300 from JaCraig/dependabot/nuget/ObjectCartographer/dependencies-e24bd1f4e2

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%)

6693.99 hits per line

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

96.58
/ObjectCartographer.SQL/Converters/DbTypeConverter.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
    /// DbType converter
13
    /// </summary>
14
    /// <seealso cref="IConverter"/>
15
    public class DbTypeConverter : IConverter
16
    {
17
        /// <summary>
18
        /// Initializes a new instance of the <see cref="DbTypeConverter"/> class.
19
        /// </summary>
20
        public DbTypeConverter()
87✔
21
        {
22
            ConvertToTypes = new Dictionary<Type, Func<DbType, object>>
87✔
23
            {
87✔
24
                { typeof(Type), DbTypeToType },
87✔
25
                { typeof(SqlDbType), DbTypeToSqlDbType }
87✔
26
            };
87✔
27
        }
87✔
28

29
        /// <summary>
30
        /// Gets the convert to types.
31
        /// </summary>
32
        /// <value>The convert to types.</value>
33
        public Dictionary<Type, Func<DbType, object>> ConvertToTypes { get; }
54✔
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>
16✔
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(DbType) && (sourceType == typeof(SqlDbType) || sourceType == typeof(Type) || sourceType == typeof(Type).GetType()))
132✔
124
                || (sourceType == typeof(DbType) && (destinationType == typeof(SqlDbType) || destinationType == typeof(Type) || destinationType == typeof(Type).GetType()));
132✔
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 DbType ConvertFrom<TSource>(TSource value)
133
        {
134
            if (value is null)
29✔
135
                return DbType.Int32;
1✔
136

137
            if (value is Type TypeObject)
28✔
138
                return TypeToDbType(TypeObject);
15✔
139
            if (value is SqlDbType SqlDBType)
13!
140
                return SqlDbTypeToDbType(SqlDBType);
13✔
141

142
            return DbType.Int32;
×
143
        }
144

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

159
            return default!;
1✔
160
        }
161

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

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

201
        /// <summary>
202
        /// Databases the type of the type to.
203
        /// </summary>
204
        /// <param name="value">The value.</param>
205
        /// <returns></returns>
206
        private static object DbTypeToType(DbType value) => DbTypeToTypeConversions.TryGetValue(value, out Type? returnValue) ? returnValue : typeof(int);
13!
207

208
        /// <summary>
209
        /// SQLs the type of the database type to database.
210
        /// </summary>
211
        /// <param name="Temp">The temporary.</param>
212
        /// <returns></returns>
213
        private static DbType SqlDbTypeToDbType(SqlDbType Temp)
214
        {
215
            if (Temp == SqlDbType.Time)
13✔
216
                return DbType.Time;
1✔
217

218
            var Parameter = new SqlParameter
12✔
219
            {
12✔
220
                SqlDbType = Temp
12✔
221
            };
12✔
222
            return Parameter.DbType;
12✔
223
        }
224

225
        /// <summary>
226
        /// Types the type of to database.
227
        /// </summary>
228
        /// <param name="TempValue">The temporary value.</param>
229
        /// <returns></returns>
230
        private static DbType TypeToDbType(Type TempValue)
231
        {
232
            if (TempValue.IsEnum)
15✔
233
            {
234
                TempValue = Enum.GetUnderlyingType(TempValue);
1✔
235
            }
236
            return Conversions.TryGetValue(TempValue, out DbType returnValue) ? returnValue : DbType.Int32;
15!
237
        }
238
    }
239
}
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