Skip to content

Commit

Permalink
Improve GetFieldType, GetDataTypeName. Fixes #261
Browse files Browse the repository at this point in the history
Handle ColumnType.Null, which can be returned from a query that doesn't
specify data types, e.g., "SELECT NULL".
  • Loading branch information
bgrainger committed May 13, 2017
1 parent ec3d086 commit 939e6a2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/MySqlConnector/MySqlClient/Results/ResultSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ public string GetDataTypeName(int ordinal)
case ColumnType.Json:
return "JSON";

case ColumnType.Null:
// 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;"
return "NULL";

default:
throw new NotImplementedException("GetDataTypeName for {0} is not implemented".FormatInvariant(columnDefinition.ColumnType));
}
Expand Down Expand Up @@ -356,6 +360,9 @@ public Type GetFieldType(int ordinal)
case ColumnType.NewDecimal:
return typeof(decimal);

case ColumnType.Null:
return typeof(object);

default:
throw new NotImplementedException("GetFieldType for {0} is not implemented".FormatInvariant(columnDefinition.ColumnType));
}
Expand Down
59 changes: 59 additions & 0 deletions tests/SideBySide/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,65 @@ public void UseReaderWithoutDisposing()
throw ex;
}

[Theory]
#if BASELINE
[InlineData("null", typeof(string))]
#else
[InlineData("null", typeof(object))]
#endif
[InlineData("cast(null as char)", typeof(string))]
[InlineData("1", typeof(long))]
[InlineData("cast(1 as unsigned)", typeof(ulong))]
[InlineData("1.0", typeof(decimal))]
[InlineData("'text'", typeof(string))]
[InlineData("cast('text' as char(4))", typeof(string))]
[InlineData("cast('2000-01-02' as date)", typeof(DateTime))]
[InlineData("cast('2000-01-02 13:45:56' as datetime)", typeof(DateTime))]
[InlineData("cast('13:45:56' as time)", typeof(TimeSpan))]
[InlineData("_binary'00112233'", typeof(byte[]))]
[InlineData("sqrt(2)", typeof(double))]
public void GetFieldType(string value, Type expectedType)
{
using (var cmd = m_database.Connection.CreateCommand())
{
cmd.CommandText = "select " + value + ";";
using (var reader = cmd.ExecuteReader())
{
Assert.True(reader.Read());
Assert.Equal(expectedType, reader.GetFieldType(0));
}
}
}

[Theory]
#if BASELINE
[InlineData("null", "VARCHAR")]
#else
[InlineData("null", "NULL")]
#endif
[InlineData("cast(null as char)", "VARCHAR")]
[InlineData("1", "BIGINT")]
[InlineData("cast(1 as unsigned)", "BIGINT")]
[InlineData("1.0", "DECIMAL")]
[InlineData("'text'", "VARCHAR")]
[InlineData("cast('2000-01-02' as date)", "DATE")]
[InlineData("cast('2000-01-02 13:45:56' as datetime)", "DATETIME")]
[InlineData("cast('13:45:56' as time)", "TIME")]
[InlineData("_binary'00112233'", "BLOB")]
[InlineData("sqrt(2)", "DOUBLE")]
public void GetDataTypeName(string value, string expectedDataType)
{
using (var cmd = m_database.Connection.CreateCommand())
{
cmd.CommandText = "select " + value + ";";
using (var reader = cmd.ExecuteReader())
{
Assert.True(reader.Read());
Assert.Equal(expectedDataType, reader.GetDataTypeName(0));
}
}
}

private void UseReaderWithoutDisposingThread(object obj)
{
var data = (UseReaderWithoutDisposingThreadData) obj;
Expand Down

0 comments on commit 939e6a2

Please sign in to comment.