Skip to content

Commit acb0fc0

Browse files
committed
Return blob for GEOMETRY columns. Fixes #70
1 parent 8e1e56a commit acb0fc0

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffs
9494
var columnType = column.ColumnType;
9595
if ((column.ColumnFlags & ColumnFlags.Binary) == 0 ||
9696
(columnType != ColumnType.String && columnType != ColumnType.VarString && columnType != ColumnType.TinyBlob &&
97-
columnType != ColumnType.Blob && columnType != ColumnType.MediumBlob && columnType != ColumnType.LongBlob))
97+
columnType != ColumnType.Blob && columnType != ColumnType.MediumBlob && columnType != ColumnType.LongBlob &&
98+
columnType != ColumnType.Geometry))
9899
{
99100
throw new InvalidCastException("Can't convert {0} to bytes.".FormatInvariant(columnType));
100101
}
@@ -421,6 +422,9 @@ public object GetValue(int ordinal)
421422
case ColumnType.NewDecimal:
422423
return Utf8Parser.TryParse(data, out decimal decimalValue, out int bytesConsumed) && bytesConsumed == data.Length ? decimalValue : throw new FormatException();
423424

425+
case ColumnType.Geometry:
426+
return data.ToArray();
427+
424428
default:
425429
throw new NotImplementedException("Reading {0} not implemented".FormatInvariant(columnDefinition.ColumnType));
426430
}

src/MySqlConnector/Core/TypeMapper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ public static MySqlDbType ConvertToMySqlDbType(ColumnDefinitionPayload columnDef
263263
case ColumnType.NewDecimal:
264264
return MySqlDbType.NewDecimal;
265265

266+
case ColumnType.Geometry:
267+
return MySqlDbType.Geometry;
268+
266269
case ColumnType.Null:
267270
return MySqlDbType.Null;
268271

tests/SideBySide/DataTypes.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,24 @@ public void GetMySqlDateTime(string columnName)
945945
}
946946
}
947947

948+
[Fact]
949+
public void Geometry()
950+
{
951+
DoQuery("geometry", "Geometry", "GEOMETRY", new object[]
952+
{
953+
null,
954+
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 },
955+
new byte[] { 0, 0, 0, 0, 1, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 64 }
956+
},
957+
#if !BASELINE
958+
GetBytes
959+
#else
960+
// NOTE: Connector/NET returns 'null' for NULL so simulate an exception for the tests
961+
x => x.IsDBNull(0) ? throw new GetValueWhenNullException() : x.GetValue(0)
962+
#endif
963+
);
964+
}
965+
948966
#if !NETCOREAPP1_1_2
949967
[Theory]
950968
[InlineData("Bit1", "datatypes_bits", MySqlDbType.Bit, 1, typeof(ulong), "N", 0, 0)]
@@ -1000,6 +1018,7 @@ public void GetMySqlDateTime(string columnName)
10001018
[InlineData("Timestamp", "datatypes_times", MySqlDbType.Timestamp, 26, typeof(DateTime), "N", 0, 6)]
10011019
[InlineData("Time", "datatypes_times", MySqlDbType.Time, 17, typeof(TimeSpan), "N", 0, 6)]
10021020
[InlineData("Year", "datatypes_times", MySqlDbType.Year, 4, typeof(int), "N", 0, 0)]
1021+
[InlineData("Geometry", "datatypes_geometry", MySqlDbType.Geometry, int.MaxValue, typeof(byte[]), "LN", 0, 0)]
10031022
public void GetSchemaTable(string column, string table, MySqlDbType mySqlDbType, int columnSize, Type dataType, string flags, int precision, int scale) =>
10041023
DoGetSchemaTable(column, table, mySqlDbType, columnSize, dataType, flags, precision, scale);
10051024

@@ -1123,6 +1142,7 @@ private void DoGetSchemaTable(string column, string table, MySqlDbType mySqlDbTy
11231142
[InlineData("Timestamp", "datatypes_times", MySqlDbType.Timestamp, "TIMESTAMP", 26, typeof(DateTime), "N", -1, 6)]
11241143
[InlineData("Time", "datatypes_times", MySqlDbType.Time, "TIME", 17, typeof(TimeSpan), "N", -1, 6)]
11251144
[InlineData("Year", "datatypes_times", MySqlDbType.Year, "YEAR", 4, typeof(int), "N", -1, 0)]
1145+
[InlineData("Geometry", "datatypes_geometry", MySqlDbType.Geometry, "GEOMETRY", int.MaxValue, typeof(byte[]), "LN", -1, 0)]
11261146
public void GetColumnSchema(string column, string table, MySqlDbType mySqlDbType, string dataTypeName, int columnSize, Type dataType, string flags, int precision, int scale)
11271147
{
11281148
if (table == "datatypes_json_core" && !AppConfig.SupportsJson)

tests/SideBySide/DataTypesFixture.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ insert into datatypes_guids (char38, char38bin, `text`, `blob`)
195195
'33221100-5544-7766-8899-aabbccddeeff', X'00112233445566778899AABBCCDDEEFF'),
196196
('{33221100-5544-7766-8899-aabbccddeeff}', '{33221100-5544-7766-8899-aabbccddeeff}',
197197
'{33221100-5544-7766-8899-aabbccddeeff}', X'00112233445566778899AABBCCDDEEFF');
198+
199+
drop table if exists datatypes_geometry;
200+
create table datatypes_geometry (
201+
rowid integer not null primary key auto_increment,
202+
`Geometry` geometry null
203+
);
204+
205+
insert into datatypes_geometry (`Geometry`)
206+
values
207+
(null),
208+
(ST_GeomFromText('POINT(1 1)')),
209+
(ST_GeomFromText('LINESTRING(0 0,1 1,2 2)'));
198210
");
199211

200212
if (AppConfig.SupportsJson)

0 commit comments

Comments
 (0)