Skip to content

Commit 92e037b

Browse files
committed
Support NEWDATE column type. Fixes #1007
1 parent 36f9618 commit 92e037b

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/MySqlConnector/Core/BinaryRow.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ protected override void GetDataOffsets(ReadOnlySpan<byte> data, int[] dataOffset
4949
ColumnType.Long or ColumnType.Int24 or ColumnType.Float => 4,
5050
ColumnType.Short or ColumnType.Year => 2,
5151
ColumnType.Tiny => 1,
52-
ColumnType.Date or ColumnType.DateTime or ColumnType.Timestamp or ColumnType.Time => reader.ReadByte(),
53-
ColumnType.DateTime2 or ColumnType.NewDate or ColumnType.Timestamp2 => throw new NotSupportedException("ColumnType {0} is not supported".FormatInvariant(columnDefinition.ColumnType)),
52+
ColumnType.Date or ColumnType.DateTime or ColumnType.NewDate or ColumnType.Timestamp or ColumnType.Time => reader.ReadByte(),
53+
ColumnType.DateTime2 or ColumnType.Timestamp2 => throw new NotSupportedException("ColumnType {0} is not supported".FormatInvariant(columnDefinition.ColumnType)),
5454
_ => checked((int) reader.ReadLengthEncodedInteger()),
5555
};
5656

@@ -130,6 +130,7 @@ protected override object GetValueCore(ReadOnlySpan<byte> data, ColumnDefinition
130130

131131
case ColumnType.Date:
132132
case ColumnType.DateTime:
133+
case ColumnType.NewDate:
133134
case ColumnType.Timestamp:
134135
return ReadDateTime(data);
135136

src/MySqlConnector/Core/TextRow.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ protected override object GetValueCore(ReadOnlySpan<byte> data, ColumnDefinition
8686

8787
case ColumnType.Date:
8888
case ColumnType.DateTime:
89+
case ColumnType.NewDate:
8990
case ColumnType.Timestamp:
9091
return ParseDateTime(data);
9192

src/MySqlConnector/Core/TypeMapper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ public static MySqlDbType ConvertToMySqlDbType(ColumnDefinitionPayload columnDef
250250
return isUnsigned ? MySqlDbType.UInt16 : MySqlDbType.Int16;
251251

252252
case ColumnType.Date:
253+
case ColumnType.NewDate:
253254
return MySqlDbType.Date;
254255

255256
case ColumnType.DateTime:

tests/SideBySide/DataTypes.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,37 @@ public void GetMySqlDateTime(string columnName)
953953
}
954954
}
955955

956+
[Theory]
957+
[InlineData(false)]
958+
[InlineData(true)]
959+
public void ReadNewDate(bool prepare)
960+
{
961+
// returns a NEWDATE in MySQL < 5.7.22; see https://github.com/mysql-net/MySqlConnector/issues/1007
962+
using var cmd = new MySqlCommand($"SELECT `Date` FROM datatypes_times UNION ALL SELECT `Date` FROM datatypes_times", Connection);
963+
if (prepare)
964+
cmd.Prepare();
965+
using var reader = cmd.ExecuteReader();
966+
967+
#if !BASELINE
968+
var columnSchema = reader.GetColumnSchema()[0];
969+
Assert.Equal("Date", columnSchema.ColumnName);
970+
Assert.Equal(typeof(DateTime), columnSchema.DataType);
971+
Assert.Equal("DATE", columnSchema.DataTypeName);
972+
#endif
973+
974+
#if !NETCOREAPP1_1_2
975+
var schemaRow = reader.GetSchemaTable().Rows[0];
976+
Assert.Equal("Date", schemaRow["ColumnName"]);
977+
Assert.Equal(typeof(DateTime), schemaRow["DataType"]);
978+
#endif
979+
980+
while (reader.Read())
981+
{
982+
if (!reader.IsDBNull(0))
983+
reader.GetDateTime(0);
984+
}
985+
}
986+
956987
[Theory]
957988
[InlineData("Geometry", "GEOMETRY", new byte[] { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 240, 63 })]
958989
[InlineData("Point", "GEOMETRY", new byte[] { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 240, 63 })]

0 commit comments

Comments
 (0)