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 STAsBinary returning null for a single point #1074

Merged
merged 4 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,31 @@ abstract class SQLServerSpatialDatatype {
/**
* Serializes the Geogemetry/Geography instance to WKB.
*
* @param noZM
* flag to indicate if Z and M coordinates should be included
* @param excludeZMFromWKB
* flag to indicate if Z and M coordinates should be excluded from the WKB representation
* @param type
* Type of Spatial Datatype (Geometry/Geography)
*/
protected void serializeToWkb(boolean noZM, SQLServerSpatialDatatype type) {
ByteBuffer buf = ByteBuffer.allocate(determineWkbCapacity());
protected void serializeToWkb(boolean excludeZMFromWKB, SQLServerSpatialDatatype type) {
ByteBuffer buf = ByteBuffer.allocate(determineWkbCapacity(excludeZMFromWKB));
createSerializationProperties();

buf.order(ByteOrder.LITTLE_ENDIAN);
buf.putInt(srid);
buf.put(version);
buf.put(serializationProperties);
if (excludeZMFromWKB) {
byte serializationPropertiesNoZM = serializationProperties;
if (hasZvalues) {
serializationPropertiesNoZM -= hasZvaluesMask;
}

if (hasMvalues) {
serializationPropertiesNoZM -= hasMvaluesMask;
}
buf.put(serializationPropertiesNoZM);
} else {
buf.put(serializationProperties);
}

if (!isSinglePoint && !isSingleLineSegment) {
buf.putInt(numberOfPoints);
Expand All @@ -124,7 +136,7 @@ protected void serializeToWkb(boolean noZM, SQLServerSpatialDatatype type) {
}
}

if (!noZM) {
if (!excludeZMFromWKB) {
if (hasZvalues) {
for (int i = 0; i < numberOfPoints; i++) {
buf.putDouble(zValues[i]);
Expand All @@ -139,7 +151,11 @@ protected void serializeToWkb(boolean noZM, SQLServerSpatialDatatype type) {
}

if (isSinglePoint || isSingleLineSegment) {
wkb = buf.array();
if (excludeZMFromWKB) {
wkbNoZM = buf.array();
} else {
wkb = buf.array();
}
return;
}

Expand All @@ -163,7 +179,7 @@ protected void serializeToWkb(boolean noZM, SQLServerSpatialDatatype type) {
}
}

if (noZM) {
if (excludeZMFromWKB) {
wkbNoZM = buf.array();
} else {
wkb = buf.array();
Expand Down Expand Up @@ -1282,32 +1298,36 @@ protected void createSerializationProperties() {
}
}

protected int determineWkbCapacity() {
protected int determineWkbCapacity(boolean excludeZMFromWKB) {
int totalSize = 0;

totalSize += 6; // SRID + version + SerializationPropertiesByte

if (isSinglePoint || isSingleLineSegment) {
totalSize += 16 * numberOfPoints;

if (hasZvalues) {
totalSize += 8 * numberOfPoints;
}
if (!excludeZMFromWKB) {
if (hasZvalues) {
totalSize += 8 * numberOfPoints;
}

if (hasMvalues) {
totalSize += 8 * numberOfPoints;
if (hasMvalues) {
totalSize += 8 * numberOfPoints;
}
}

return totalSize;
}

int pointSize = 16;
if (hasZvalues) {
pointSize += 8;
}
if (!excludeZMFromWKB) {
if (hasZvalues) {
pointSize += 8;
}

if (hasMvalues) {
pointSize += 8;
if (hasMvalues) {
pointSize += 8;
}
}

totalSize += 12; // 4 bytes for 3 ints, each representing the number of points, shapes and figures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.microsoft.sqlserver.jdbc.datatypes;

import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
Expand Down Expand Up @@ -957,8 +958,8 @@ public void testSTAsBinary() throws SQLException {
byte[] geomWKB2 = geomWKT2.STAsBinary();
byte[] geogWKB2 = geogWKT2.STAsBinary();

assertEquals(geomWKB, geomWKB2);
assertEquals(geogWKB, geogWKB2);
assertArrayEquals(geomWKB, geomWKB2);
assertArrayEquals(geogWKB, geogWKB2);
}

@Test
Expand Down