Skip to content

Commit 939e6a2

Browse files
committed
Improve GetFieldType, GetDataTypeName. Fixes #261
Handle ColumnType.Null, which can be returned from a query that doesn't specify data types, e.g., "SELECT NULL".
1 parent ec3d086 commit 939e6a2

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/MySqlConnector/MySqlClient/Results/ResultSet.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ public string GetDataTypeName(int ordinal)
287287
case ColumnType.Json:
288288
return "JSON";
289289

290+
case ColumnType.Null:
291+
// 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;"
292+
return "NULL";
293+
290294
default:
291295
throw new NotImplementedException("GetDataTypeName for {0} is not implemented".FormatInvariant(columnDefinition.ColumnType));
292296
}
@@ -356,6 +360,9 @@ public Type GetFieldType(int ordinal)
356360
case ColumnType.NewDecimal:
357361
return typeof(decimal);
358362

363+
case ColumnType.Null:
364+
return typeof(object);
365+
359366
default:
360367
throw new NotImplementedException("GetFieldType for {0} is not implemented".FormatInvariant(columnDefinition.ColumnType));
361368
}

tests/SideBySide/QueryTests.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,65 @@ public void UseReaderWithoutDisposing()
582582
throw ex;
583583
}
584584

585+
[Theory]
586+
#if BASELINE
587+
[InlineData("null", typeof(string))]
588+
#else
589+
[InlineData("null", typeof(object))]
590+
#endif
591+
[InlineData("cast(null as char)", typeof(string))]
592+
[InlineData("1", typeof(long))]
593+
[InlineData("cast(1 as unsigned)", typeof(ulong))]
594+
[InlineData("1.0", typeof(decimal))]
595+
[InlineData("'text'", typeof(string))]
596+
[InlineData("cast('text' as char(4))", typeof(string))]
597+
[InlineData("cast('2000-01-02' as date)", typeof(DateTime))]
598+
[InlineData("cast('2000-01-02 13:45:56' as datetime)", typeof(DateTime))]
599+
[InlineData("cast('13:45:56' as time)", typeof(TimeSpan))]
600+
[InlineData("_binary'00112233'", typeof(byte[]))]
601+
[InlineData("sqrt(2)", typeof(double))]
602+
public void GetFieldType(string value, Type expectedType)
603+
{
604+
using (var cmd = m_database.Connection.CreateCommand())
605+
{
606+
cmd.CommandText = "select " + value + ";";
607+
using (var reader = cmd.ExecuteReader())
608+
{
609+
Assert.True(reader.Read());
610+
Assert.Equal(expectedType, reader.GetFieldType(0));
611+
}
612+
}
613+
}
614+
615+
[Theory]
616+
#if BASELINE
617+
[InlineData("null", "VARCHAR")]
618+
#else
619+
[InlineData("null", "NULL")]
620+
#endif
621+
[InlineData("cast(null as char)", "VARCHAR")]
622+
[InlineData("1", "BIGINT")]
623+
[InlineData("cast(1 as unsigned)", "BIGINT")]
624+
[InlineData("1.0", "DECIMAL")]
625+
[InlineData("'text'", "VARCHAR")]
626+
[InlineData("cast('2000-01-02' as date)", "DATE")]
627+
[InlineData("cast('2000-01-02 13:45:56' as datetime)", "DATETIME")]
628+
[InlineData("cast('13:45:56' as time)", "TIME")]
629+
[InlineData("_binary'00112233'", "BLOB")]
630+
[InlineData("sqrt(2)", "DOUBLE")]
631+
public void GetDataTypeName(string value, string expectedDataType)
632+
{
633+
using (var cmd = m_database.Connection.CreateCommand())
634+
{
635+
cmd.CommandText = "select " + value + ";";
636+
using (var reader = cmd.ExecuteReader())
637+
{
638+
Assert.True(reader.Read());
639+
Assert.Equal(expectedDataType, reader.GetDataTypeName(0));
640+
}
641+
}
642+
}
643+
585644
private void UseReaderWithoutDisposingThread(object obj)
586645
{
587646
var data = (UseReaderWithoutDisposingThreadData) obj;

0 commit comments

Comments
 (0)