Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug #86974 #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/MySql.Data/Types/MySqlGeometry.cs
Expand Up @@ -141,8 +141,8 @@ public MySqlGeometry(MySqlDbType type, byte[] val)
var yIndex = val.Length == GEOMETRY_LENGTH ? 17 : 13;

_valBinary = buffValue;
_xValue = BitConverter.ToDouble(val, xIndex);
_yValue = BitConverter.ToDouble(val, yIndex);
_xValue = val.Length >= xIndex + 8 ? BitConverter.ToDouble(val, xIndex) : 0;
_yValue = val.Length >= yIndex + 8 ? BitConverter.ToDouble(val, yIndex) : 0;
this._srid = val.Length == GEOMETRY_LENGTH ? BitConverter.ToInt32(val, 0) : 0;
this._isNull = false;
this._type = type;
Expand Down
48 changes: 48 additions & 0 deletions Tests/MySql.Data.Tests/DataTypeTests.cs
Expand Up @@ -967,6 +967,54 @@ public void CanGetToStringFromMySqlGeometry()
Assert.Equal("POINT(47.37 -122.21)", valToString);
}

/// <summary>
/// Bug #86974 Cannot create instance of MySqlGeometry for empty geometry collection
/// </summary>
[Fact]
public void CanCreateMySqlGeometryFromEmptyGeometryCollection()
{
var bytes = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
MySqlGeometry v = new MySqlGeometry(MySqlDbType.Geometry, bytes);
Assert.Equal("POINT(3.45845952088873E-323 0)", v.ToString());
}

/// <summary>
/// Bug #86974 Cannot create instance of MySqlGeometry for empty geometry collection
/// </summary>
[Fact]
public void CanGetMySqlGeometryFromEmptyGeometryCollection()
{
if (st.version.Major < 5) return;

st.execSQL("DROP TABLE IF EXISTS Test");
st.execSQL("CREATE TABLE Test (v Geometry NOT NULL)");

MySqlCommand cmd = new MySqlCommand("INSERT INTO Test VALUES (ST_GeometryCollectionFromText(\"GEOMETRYCOLLECTION()\"))", st.conn);
cmd.ExecuteNonQuery();

// reading as binary
cmd.CommandText = "SELECT AsBinary(v) as v FROM Test";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
var val = reader.GetMySqlGeometry(0);
var valWithName = reader.GetMySqlGeometry("v");
Assert.Equal("POINT(0 0)", val.ToString());
Assert.Equal("POINT(0 0)", valWithName.ToString());
}

// reading as geometry
cmd.CommandText = "SELECT v as v FROM Test";
using (MySqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
var val = reader.GetMySqlGeometry(0);
var valWithName = reader.GetMySqlGeometry("v");
Assert.Equal("POINT(3.45845952088873E-323 0)", val.ToString());
Assert.Equal("POINT(3.45845952088873E-323 0)", valWithName.ToString());
}
}

#endregion

/// <summary>
Expand Down