4
4
using System . IO ;
5
5
using System . Threading ;
6
6
using System . Threading . Tasks ;
7
+ using MySql . Data . MySqlClient . Types ;
7
8
using MySql . Data . Protocol . Serialization ;
8
9
using MySql . Data . Serialization ;
9
10
@@ -21,6 +22,7 @@ public async Task<ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
21
22
// ResultSet can be re-used, so initialize everything
22
23
BufferState = ResultSetState . None ;
23
24
ColumnDefinitions = null ;
25
+ ColumnTypes = null ;
24
26
LastInsertId = 0 ;
25
27
RecordsAffected = 0 ;
26
28
State = ResultSetState . None ;
@@ -45,6 +47,7 @@ public async Task<ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
45
47
RecordsAffected += ok . AffectedRowCount ;
46
48
LastInsertId = ok . LastInsertId ;
47
49
ColumnDefinitions = null ;
50
+ ColumnTypes = null ;
48
51
State = ( ok . ServerStatus & ServerStatus . MoreResultsExist ) == 0
49
52
? ResultSetState . NoMoreData
50
53
: ResultSetState . HasMoreData ;
@@ -92,6 +95,7 @@ public async Task<ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
92
95
Array . Resize ( ref m_columnDefinitionPayloads , columnCount * 96 ) ;
93
96
94
97
ColumnDefinitions = new ColumnDefinitionPayload [ columnCount ] ;
98
+ ColumnTypes = new MySqlDbType [ columnCount ] ;
95
99
m_dataOffsets = new int [ columnCount ] ;
96
100
m_dataLengths = new int [ columnCount ] ;
97
101
@@ -105,7 +109,9 @@ public async Task<ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
105
109
Array . Resize ( ref m_columnDefinitionPayloads , Math . Max ( m_columnDefinitionPayloadUsedBytes + arraySegment . Count , m_columnDefinitionPayloadUsedBytes * 2 ) ) ;
106
110
Buffer . BlockCopy ( arraySegment . Array , arraySegment . Offset , m_columnDefinitionPayloads , m_columnDefinitionPayloadUsedBytes , arraySegment . Count ) ;
107
111
108
- ColumnDefinitions [ column ] = ColumnDefinitionPayload . Create ( new ArraySegment < byte > ( m_columnDefinitionPayloads , m_columnDefinitionPayloadUsedBytes , arraySegment . Count ) ) ;
112
+ var columnDefinition = ColumnDefinitionPayload . Create ( new ArraySegment < byte > ( m_columnDefinitionPayloads , m_columnDefinitionPayloadUsedBytes , arraySegment . Count ) ) ;
113
+ ColumnDefinitions [ column ] = columnDefinition ;
114
+ ColumnTypes [ column ] = TypeMapper . ConvertToMySqlDbType ( columnDefinition , treatTinyAsBoolean : Connection . TreatTinyAsBoolean , oldGuids : Connection . OldGuids ) ;
109
115
m_columnDefinitionPayloadUsedBytes += arraySegment . Count ;
110
116
}
111
117
@@ -269,74 +275,96 @@ public string GetDataTypeName(int ordinal)
269
275
if ( ordinal < 0 || ordinal > ColumnDefinitions . Length )
270
276
throw new ArgumentOutOfRangeException ( nameof ( ordinal ) , "value must be between 0 and {0}." . FormatInvariant ( ColumnDefinitions . Length ) ) ;
271
277
272
- var columnDefinition = ColumnDefinitions [ ordinal ] ;
273
- switch ( columnDefinition . ColumnType )
278
+ switch ( ColumnTypes [ ordinal ] )
274
279
{
275
- case ColumnType . Tiny :
276
- return Connection . TreatTinyAsBoolean && columnDefinition . ColumnLength == 1 ? "BOOL" : "TINYINT ";
280
+ case MySqlDbType . Bool :
281
+ return "BOOL" ;
277
282
278
- case ColumnType . Short :
279
- return "SMALLINT" ;
283
+ case MySqlDbType . UByte :
284
+ case MySqlDbType . Byte :
285
+ return "TINYINT" ;
280
286
281
- case ColumnType . Int24 :
282
- return "MEDIUMINT" ;
287
+ case MySqlDbType . UInt16 :
288
+ case MySqlDbType . Int16 :
289
+ return "SMALLINT" ;
283
290
284
- case ColumnType . Long :
285
- return "INT" ;
291
+ case MySqlDbType . UInt24 :
292
+ case MySqlDbType . Int24 :
293
+ return "MEDIUMINT" ;
286
294
287
- case ColumnType . Longlong :
288
- return "BIGINT" ;
295
+ case MySqlDbType . UInt32 :
296
+ case MySqlDbType . Int32 :
297
+ return "INT" ;
289
298
290
- case ColumnType . Bit :
291
- return "BIT" ;
299
+ case MySqlDbType . UInt64 :
300
+ case MySqlDbType . Int64 :
301
+ return "BIGINT" ;
292
302
293
- case ColumnType . String :
294
- return columnDefinition . CharacterSet == CharacterSet . Binary ? "BLOB" :
295
- ( columnDefinition . ColumnFlags & ColumnFlags . Enum ) != 0 ? "ENUM" :
296
- ( columnDefinition . ColumnFlags & ColumnFlags . Set ) != 0 ? "SET" :
297
- string . Format ( CultureInfo . InvariantCulture , "CHAR({0})" , columnDefinition . ColumnLength / SerializationUtility . GetBytesPerCharacter ( columnDefinition . CharacterSet ) ) ;
303
+ case MySqlDbType . Bit :
304
+ return "BIT" ;
298
305
299
- case ColumnType . VarString :
300
- case ColumnType . TinyBlob :
301
- case ColumnType . Blob :
302
- case ColumnType . MediumBlob :
303
- case ColumnType . LongBlob :
304
- return columnDefinition . CharacterSet == CharacterSet . Binary ? "BLOB" : "VARCHAR" ;
306
+ case MySqlDbType . Enum :
307
+ return "ENUM" ;
305
308
306
- case ColumnType . Date :
307
- return "DATE " ;
309
+ case MySqlDbType . Set :
310
+ return "SET " ;
308
311
309
- case ColumnType . DateTime :
310
- return "DATETIME " ;
312
+ case MySqlDbType . Guid :
313
+ return "CHAR(36) " ;
311
314
312
- case ColumnType . Timestamp :
313
- return "TIMESTAMP" ;
315
+ case MySqlDbType . String :
316
+ var columnDefinition = ColumnDefinitions [ ordinal ] ;
317
+ return string . Format ( CultureInfo . InvariantCulture , "CHAR({0})" , columnDefinition . ColumnLength / SerializationUtility . GetBytesPerCharacter ( columnDefinition . CharacterSet ) ) ;
314
318
315
- case ColumnType . Time :
316
- return "TIME" ;
319
+ case MySqlDbType . VarString :
320
+ case MySqlDbType . TinyText :
321
+ case MySqlDbType . Text :
322
+ case MySqlDbType . MediumText :
323
+ case MySqlDbType . LongText :
324
+ return "VARCHAR" ;
317
325
318
- case ColumnType . Year :
319
- return "YEAR" ;
326
+ case MySqlDbType . Binary :
327
+ case MySqlDbType . VarBinary :
328
+ case MySqlDbType . TinyBlob :
329
+ case MySqlDbType . Blob :
330
+ case MySqlDbType . MediumBlob :
331
+ case MySqlDbType . LongBlob :
332
+ return "BLOB" ;
320
333
321
- case ColumnType . Float :
322
- return "FLOAT " ;
334
+ case MySqlDbType . Date :
335
+ return "DATE " ;
323
336
324
- case ColumnType . Double :
325
- return "DOUBLE " ;
337
+ case MySqlDbType . DateTime :
338
+ return "DATETIME " ;
326
339
327
- case ColumnType . Decimal :
328
- case ColumnType . NewDecimal :
329
- return "DECIMAL" ;
340
+ case MySqlDbType . Timestamp :
341
+ return "TIMESTAMP" ;
330
342
331
- case ColumnType . Json :
332
- return "JSON " ;
343
+ case MySqlDbType . Time :
344
+ return "TIME " ;
333
345
334
- case ColumnType . Null :
335
- // not a valid data type name, but only happens when there is no way to infer the type of the column, e.g., "SELECT NULL;"
336
- return "NULL" ;
346
+ case MySqlDbType . Year :
347
+ return "YEAR" ;
337
348
338
- default :
339
- throw new NotImplementedException ( "GetDataTypeName for {0} is not implemented" . FormatInvariant ( columnDefinition . ColumnType ) ) ;
349
+ case MySqlDbType . Float :
350
+ return "FLOAT" ;
351
+
352
+ case MySqlDbType . Double :
353
+ return "DOUBLE" ;
354
+
355
+ case MySqlDbType . Decimal :
356
+ case MySqlDbType . NewDecimal :
357
+ return "DECIMAL" ;
358
+
359
+ case MySqlDbType . JSON :
360
+ return "JSON" ;
361
+
362
+ case MySqlDbType . Null :
363
+ // not a valid data type name, but only happens when there is no way to infer the type of the column, e.g., "SELECT NULL;"
364
+ return "NULL" ;
365
+
366
+ default :
367
+ throw new NotImplementedException ( "GetDataTypeName for {0} is not implemented" . FormatInvariant ( ColumnTypes [ ordinal ] ) ) ;
340
368
}
341
369
}
342
370
@@ -439,6 +467,7 @@ public Row GetCurrentRow()
439
467
440
468
public ResultSetState BufferState { get ; private set ; }
441
469
public ColumnDefinitionPayload [ ] ColumnDefinitions { get ; private set ; }
470
+ public MySqlDbType [ ] ColumnTypes { get ; private set ; }
442
471
public long LastInsertId { get ; private set ; }
443
472
public int RecordsAffected { get ; private set ; }
444
473
public ResultSetState State { get ; private set ; }
0 commit comments