@@ -35,16 +35,16 @@ private TypeMapper()
35
35
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "TINYINT" , typeByte , MySqlDbType . UByte , unsigned : true ) ) ;
36
36
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "SMALLINT" , typeShort , MySqlDbType . Int16 , unsigned : false ) ) ;
37
37
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "SMALLINT" , typeUshort , MySqlDbType . UInt16 , unsigned : true ) ) ;
38
- AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "MEDIUMINT" , typeInt , MySqlDbType . Int24 , unsigned : false ) ) ;
39
- AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "MEDIUMINT" , typeUint , MySqlDbType . UInt24 , unsigned : true ) ) ;
40
38
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "INT" , typeInt , MySqlDbType . Int32 , unsigned : false ) ) ;
41
39
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "INT" , typeUint , MySqlDbType . UInt32 , unsigned : true ) ) ;
40
+ AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "MEDIUMINT" , typeInt , MySqlDbType . Int24 , unsigned : false ) ) ;
41
+ AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "MEDIUMINT" , typeUint , MySqlDbType . UInt24 , unsigned : true ) ) ;
42
42
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "BIGINT" , typeLong , MySqlDbType . Int64 , unsigned : false ) ) ;
43
43
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "BIGINT" , typeUlong , MySqlDbType . UInt64 , unsigned : true ) ) ;
44
44
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "BIT" , typeUlong , MySqlDbType . Bit ) ) ;
45
45
46
46
// decimals
47
- var typeDecimal = AddDbTypeMapping ( new DbTypeMapping ( typeof ( decimal ) , new [ ] { DbType . Decimal } , convert : o => Convert . ToDecimal ( o ) ) ) ;
47
+ var typeDecimal = AddDbTypeMapping ( new DbTypeMapping ( typeof ( decimal ) , new [ ] { DbType . Decimal , DbType . Currency , DbType . VarNumeric } , convert : o => Convert . ToDecimal ( o ) ) ) ;
48
48
var typeDouble = AddDbTypeMapping ( new DbTypeMapping ( typeof ( double ) , new [ ] { DbType . Double } , convert : o => Convert . ToDouble ( o ) ) ) ;
49
49
var typeFloat = AddDbTypeMapping ( new DbTypeMapping ( typeof ( float ) , new [ ] { DbType . Single } , convert : o => Convert . ToSingle ( o ) ) ) ;
50
50
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "DECIMAL" , typeDecimal , MySqlDbType . NewDecimal ) ) ;
@@ -53,8 +53,9 @@ private TypeMapper()
53
53
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "FLOAT" , typeFloat , MySqlDbType . Float ) ) ;
54
54
55
55
// string
56
- var typeString = AddDbTypeMapping ( new DbTypeMapping ( typeof ( string ) , new [ ] { DbType . String , DbType . StringFixedLength , DbType . AnsiString , DbType . AnsiStringFixedLength } , convert : o => Convert . ToString ( o ) ) ) ;
57
- AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "CHAR" , typeString , MySqlDbType . String ) ) ;
56
+ var typeFixedString = AddDbTypeMapping ( new DbTypeMapping ( typeof ( string ) , new [ ] { DbType . StringFixedLength , DbType . AnsiStringFixedLength } , convert : Convert . ToString ) ) ;
57
+ var typeString = AddDbTypeMapping ( new DbTypeMapping ( typeof ( string ) , new [ ] { DbType . String , DbType . AnsiString , DbType . Xml } , convert : Convert . ToString ) ) ;
58
+ AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "CHAR" , typeFixedString , MySqlDbType . String ) ) ;
58
59
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "VARCHAR" , typeString , MySqlDbType . VarChar ) ) ;
59
60
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "VARCHAR" , typeString , MySqlDbType . VarString ) ) ;
60
61
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "TINYTEXT" , typeString , MySqlDbType . TinyText , simpleDataTypeName : "VARCHAR" ) ) ;
@@ -67,10 +68,10 @@ private TypeMapper()
67
68
68
69
// binary
69
70
var typeBinary = AddDbTypeMapping ( new DbTypeMapping ( typeof ( byte [ ] ) , new [ ] { DbType . Binary } ) ) ;
71
+ AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "BLOB" , typeBinary , MySqlDbType . Blob , binary : true , simpleDataTypeName : "BLOB" ) ) ;
70
72
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "BINARY" , typeBinary , MySqlDbType . Binary , binary : true , simpleDataTypeName : "BLOB" ) ) ;
71
73
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "VARBINARY" , typeBinary , MySqlDbType . VarBinary , binary : true , simpleDataTypeName : "BLOB" ) ) ;
72
74
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "TINYBLOB" , typeBinary , MySqlDbType . TinyBlob , binary : true , simpleDataTypeName : "BLOB" ) ) ;
73
- AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "BLOB" , typeBinary , MySqlDbType . Blob , binary : true , simpleDataTypeName : "BLOB" ) ) ;
74
75
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "MEDIUMBLOB" , typeBinary , MySqlDbType . MediumBlob , binary : true , simpleDataTypeName : "BLOB" ) ) ;
75
76
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "LONGBLOB" , typeBinary , MySqlDbType . LongBlob , binary : true , simpleDataTypeName : "BLOB" ) ) ;
76
77
@@ -85,10 +86,12 @@ private TypeMapper()
85
86
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "GEOMETRYCOLLECTION" , typeBinary , MySqlDbType . Geometry , binary : true ) ) ;
86
87
87
88
// date/time
88
- var typeDateTime = AddDbTypeMapping ( new DbTypeMapping ( typeof ( DateTime ) , new [ ] { DbType . DateTime , DbType . Date , DbType . DateTime2 , DbType . DateTimeOffset } ) ) ;
89
+ var typeDate = AddDbTypeMapping ( new DbTypeMapping ( typeof ( DateTime ) , new [ ] { DbType . Date } ) ) ;
90
+ var typeDateTime = AddDbTypeMapping ( new DbTypeMapping ( typeof ( DateTime ) , new [ ] { DbType . DateTime , DbType . DateTime2 , DbType . DateTimeOffset } ) ) ;
89
91
var typeTime = AddDbTypeMapping ( new DbTypeMapping ( typeof ( TimeSpan ) , new [ ] { DbType . Time } ) ) ;
90
92
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "DATETIME" , typeDateTime , MySqlDbType . DateTime ) ) ;
91
- AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "DATE" , typeDateTime , MySqlDbType . Date ) ) ;
93
+ AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "DATE" , typeDate , MySqlDbType . Date ) ) ;
94
+ AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "DATE" , typeDate , MySqlDbType . Newdate ) ) ;
92
95
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "TIME" , typeTime , MySqlDbType . Time ) ) ;
93
96
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "TIMESTAMP" , typeDateTime , MySqlDbType . Timestamp ) ) ;
94
97
AddMySqlDbTypeMapping ( new ColumnTypeMapping ( "YEAR" , typeInt , MySqlDbType . Year ) ) ;
@@ -104,6 +107,18 @@ private TypeMapper()
104
107
105
108
public ColumnTypeMapping GetColumnMapping ( MySqlDbType mySqlDbType ) => m_mySqlDbTypeToColumnTypeMapping [ mySqlDbType ] ;
106
109
110
+ public DbType GetDbTypeForMySqlDbType ( MySqlDbType mySqlDbType ) => m_mySqlDbTypeToColumnTypeMapping [ mySqlDbType ] . DbTypeMapping . DbTypes [ 0 ] ;
111
+
112
+ public MySqlDbType GetMySqlDbTypeForDbType ( DbType dbType )
113
+ {
114
+ foreach ( var pair in m_mySqlDbTypeToColumnTypeMapping )
115
+ {
116
+ if ( pair . Value . DbTypeMapping . DbTypes . Contains ( dbType ) )
117
+ return pair . Key ;
118
+ }
119
+ return MySqlDbType . VarChar ;
120
+ }
121
+
107
122
private DbTypeMapping AddDbTypeMapping ( DbTypeMapping dbTypeMapping )
108
123
{
109
124
m_dbTypeMappingsByClrType [ dbTypeMapping . ClrType ] = dbTypeMapping ;
@@ -244,89 +259,6 @@ public static MySqlDbType ConvertToMySqlDbType(ColumnDefinitionPayload columnDef
244
259
}
245
260
}
246
261
247
- internal static MySqlDbType ConverToMySqlDbType ( DbType dbtype )
248
- {
249
- switch ( dbtype )
250
- {
251
- case DbType . AnsiString : return MySqlDbType . String ;
252
- case DbType . Binary : return MySqlDbType . Binary ;
253
- case DbType . Byte : return MySqlDbType . Byte ;
254
- case DbType . Boolean : return MySqlDbType . Bit ;
255
- case DbType . Currency : return MySqlDbType . Decimal ;
256
- case DbType . Date : return MySqlDbType . Date ;
257
- case DbType . DateTime : return MySqlDbType . DateTime ;
258
- case DbType . Decimal : return MySqlDbType . Decimal ;
259
- case DbType . Double : return MySqlDbType . Double ;
260
- case DbType . Guid : return MySqlDbType . Guid ;
261
- case DbType . Int16 : return MySqlDbType . Int16 ;
262
- case DbType . Int32 : return MySqlDbType . Int32 ;
263
- case DbType . Int64 : return MySqlDbType . Int64 ;
264
- case DbType . Object : return MySqlDbType . Text ;
265
- case DbType . SByte : return MySqlDbType . UByte ;
266
- case DbType . Single : return MySqlDbType . Float ;
267
- case DbType . String : return MySqlDbType . String ;
268
- case DbType . Time : return MySqlDbType . Time ;
269
- case DbType . UInt16 : return MySqlDbType . UInt16 ;
270
- case DbType . UInt32 : return MySqlDbType . UInt32 ;
271
- case DbType . UInt64 : return MySqlDbType . UInt64 ;
272
- case DbType . VarNumeric : return MySqlDbType . Decimal ;
273
- case DbType . AnsiStringFixedLength : return MySqlDbType . String ;
274
- case DbType . StringFixedLength : return MySqlDbType . VarChar ;
275
- case DbType . Xml : return MySqlDbType . Text ;
276
- case DbType . DateTime2 : return MySqlDbType . Newdate ;
277
- case DbType . DateTimeOffset : return MySqlDbType . Timestamp ;
278
- }
279
- throw new InvalidCastException ( "Never reached. " + dbtype . ToString ( ) ) ;
280
- }
281
- internal static DbType ConvertFromMySqlDbType ( MySqlDbType dbtype )
282
- {
283
- switch ( dbtype )
284
- {
285
- case MySqlDbType . Decimal : return DbType . Decimal ;
286
- case MySqlDbType . Byte : return DbType . Byte ;
287
- case MySqlDbType . Int16 : return DbType . Int16 ;
288
- case MySqlDbType . Int24 : return DbType . Int32 ;
289
- case MySqlDbType . Int32 : return DbType . Int32 ;
290
- case MySqlDbType . Int64 : return DbType . Int64 ;
291
- case MySqlDbType . Float : return DbType . Single ;
292
- case MySqlDbType . Double : return DbType . Double ;
293
- case MySqlDbType . Timestamp : return DbType . DateTimeOffset ;
294
- case MySqlDbType . Date : return DbType . Date ;
295
- case MySqlDbType . Time : return DbType . Time ;
296
- case MySqlDbType . DateTime : return DbType . DateTime ;
297
- case MySqlDbType . Year : return DbType . Int16 ;
298
- case MySqlDbType . Newdate : return DbType . DateTime2 ;
299
- case MySqlDbType . VarString : return DbType . String ;
300
- case MySqlDbType . Bit : return DbType . Boolean ;
301
- case MySqlDbType . JSON : return DbType . String ;
302
- case MySqlDbType . NewDecimal : return DbType . Decimal ;
303
- case MySqlDbType . Enum : return DbType . Int16 ;
304
- case MySqlDbType . Set : return DbType . Object ;
305
- case MySqlDbType . TinyBlob : return DbType . Binary ;
306
- case MySqlDbType . MediumBlob : return DbType . Binary ;
307
- case MySqlDbType . LongBlob : return DbType . Binary ;
308
- case MySqlDbType . Blob : return DbType . Binary ;
309
- case MySqlDbType . VarChar : return DbType . StringFixedLength ;
310
- case MySqlDbType . String : return DbType . String ;
311
- case MySqlDbType . Geometry : return DbType . Binary ;
312
- case MySqlDbType . UByte : return DbType . SByte ;
313
- case MySqlDbType . UInt16 : return DbType . UInt16 ;
314
- case MySqlDbType . UInt24 : return DbType . UInt32 ;
315
- case MySqlDbType . UInt32 : return DbType . UInt32 ;
316
- case MySqlDbType . UInt64 : return DbType . UInt64 ;
317
- case MySqlDbType . Binary : return DbType . Binary ;
318
- case MySqlDbType . VarBinary : return DbType . Binary ;
319
- case MySqlDbType . TinyText : return DbType . String ;
320
- case MySqlDbType . MediumText : return DbType . String ;
321
- case MySqlDbType . LongText : return DbType . String ;
322
- case MySqlDbType . Text : return DbType . String ;
323
- case MySqlDbType . Guid : return DbType . Guid ;
324
- case MySqlDbType . Bool : return DbType . Boolean ;
325
- case MySqlDbType . Null : return DbType . Object ;
326
- }
327
- throw new InvalidCastException ( "Never reached. " + dbtype . ToString ( ) ) ;
328
- }
329
-
330
262
internal IEnumerable < ColumnTypeMapping > GetColumnMappings ( )
331
263
{
332
264
return m_columnTypeMappingLookup . Values . AsEnumerable ( ) ;
0 commit comments