Skip to content

Commit

Permalink
Fix STAsBinary to return WKB format instead of CLR (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbae authored Jul 3, 2020
1 parent 6f5ecb7 commit c82222a
Show file tree
Hide file tree
Showing 6 changed files with 2,135 additions and 345 deletions.
47 changes: 26 additions & 21 deletions src/main/java/com/microsoft/sqlserver/jdbc/Geography.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,28 @@ protected Geography(String wkt, int srid) throws SQLServerException {

parseWKTForSerialization(this, currentWktPos, -1, false);

serializeToWkb(false, this);
serializeToClr(false, this);
isNull = false;
}

/**
* Private constructor used for creating a Geography object from WKB.
* Private constructor used for creating a Geography object from internal SQL Server format.
*
* @param wkb
* Well-Known Binary (WKB) provided by the user.
* @param clr
* Internal SQL Server format provided by the user.
* @throws SQLServerException
* if an exception occurs
*/
protected Geography(byte[] wkb) throws SQLServerException {
if (null == wkb || wkb.length <= 0) {
throwIllegalWKB();
protected Geography(byte[] clr) throws SQLServerException {
if (null == clr || clr.length <= 0) {
throwIllegalByteArray();
}

this.wkb = wkb;
buffer = ByteBuffer.wrap(wkb);
this.clr = clr;
buffer = ByteBuffer.wrap(clr);
buffer.order(ByteOrder.LITTLE_ENDIAN);

parseWkb(this);
parseClr(this);

WKTsb = new StringBuffer();
WKTsbNoZM = new StringBuffer();
Expand Down Expand Up @@ -91,6 +91,11 @@ public static Geography STGeomFromText(String wkt, int srid) throws SQLServerExc
* Constructor for a Geography instance from an Open Geospatial Consortium (OGC) Well-Known Binary (WKB)
* representation.
*
* Note: This method currently uses internal SQL Server format (CLR) to create a Geography instance,
* but in the future this will be changed to accept WKB data instead, as the SQL Server counterpart of this
* method (STGeomFromWKB) uses WKB.
* For existing users who are already using this method, consider switching to deserialize(byte) instead.
*
* @param wkb
* Well-Known Binary (WKB) provided by the user.
* @return Geography Geography instance created from WKB
Expand All @@ -104,14 +109,14 @@ public static Geography STGeomFromWKB(byte[] wkb) throws SQLServerException {
/**
* Constructor for a Geography instance from an internal SQL Server format for spatial data.
*
* @param wkb
* Well-Known Binary (WKB) provided by the user.
* @return Geography Geography instance created from WKB
* @param clr
* Internal SQL Server format provided by the user.
* @return Geography Geography instance created from clr
* @throws SQLServerException
* if an exception occurs
*/
public static Geography deserialize(byte[] wkb) throws SQLServerException {
return new Geography(wkb);
public static Geography deserialize(byte[] clr) throws SQLServerException {
return new Geography(clr);
}

/**
Expand Down Expand Up @@ -156,10 +161,10 @@ public static Geography point(double lat, double lon, int srid) throws SQLServer
*/
public String STAsText() throws SQLServerException {
if (null == wktNoZM) {
buffer = ByteBuffer.wrap(wkb);
buffer = ByteBuffer.wrap(clr);
buffer.order(ByteOrder.LITTLE_ENDIAN);

parseWkb(this);
parseClr(this);

WKTsb = new StringBuffer();
WKTsbNoZM = new StringBuffer();
Expand All @@ -176,10 +181,10 @@ public String STAsText() throws SQLServerException {
* @return byte array representation of the Geography object.
*/
public byte[] STAsBinary() {
if (null == wkbNoZM) {
serializeToWkb(true, this);
if (null == wkb) {
serializeToWkb(this);
}
return wkbNoZM;
return wkb;
}

/**
Expand All @@ -188,7 +193,7 @@ public byte[] STAsBinary() {
* @return byte array representation of the Geography object.
*/
public byte[] serialize() {
return wkb;
return clr;
}

/**
Expand Down
47 changes: 26 additions & 21 deletions src/main/java/com/microsoft/sqlserver/jdbc/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,28 @@ protected Geometry(String wkt, int srid) throws SQLServerException {

parseWKTForSerialization(this, currentWktPos, -1, false);

serializeToWkb(false, this);
serializeToClr(false, this);
isNull = false;
}

/**
* Private constructor used for creating a Geometry object from WKB.
* Private constructor used for creating a Geometry object from internal SQL Server format.
*
* @param wkb
* Well-Known Binary (WKB) provided by the user.
* @param clr
* Internal SQL Server format provided by the user.
* @throws SQLServerException
* if an exception occurs
*/
protected Geometry(byte[] wkb) throws SQLServerException {
if (null == wkb || wkb.length <= 0) {
throwIllegalWKB();
protected Geometry(byte[] clr) throws SQLServerException {
if (null == clr || clr.length <= 0) {
throwIllegalByteArray();
}

this.wkb = wkb;
buffer = ByteBuffer.wrap(wkb);
this.clr = clr;
buffer = ByteBuffer.wrap(clr);
buffer.order(ByteOrder.LITTLE_ENDIAN);

parseWkb(this);
parseClr(this);

WKTsb = new StringBuffer();
WKTsbNoZM = new StringBuffer();
Expand Down Expand Up @@ -91,6 +91,11 @@ public static Geometry STGeomFromText(String wkt, int srid) throws SQLServerExce
* Constructor for a Geometry instance from an Open Geospatial Consortium (OGC) Well-Known Binary (WKB)
* representation.
*
* Note: This method currently uses internal SQL Server format (CLR) to create a Geometry instance,
* but in the future this will be changed to accept WKB data instead, as the SQL Server counterpart of this
* method (STGeomFromWKB) uses WKB.
* For existing users who are already using this method, consider switching to deserialize(byte) instead.
*
* @param wkb
* Well-Known Binary (WKB) provided by the user.
* @return Geometry Geometry instance created from WKB
Expand All @@ -104,14 +109,14 @@ public static Geometry STGeomFromWKB(byte[] wkb) throws SQLServerException {
/**
* Constructor for a Geometry instance from an internal SQL Server format for spatial data.
*
* @param wkb
* Well-Known Binary (WKB) provided by the user.
* @return Geometry Geometry instance created from WKB
* @param clr
* Internal SQL Server format provided by the user.
* @return Geometry Geometry instance created from clr
* @throws SQLServerException
* if an exception occurs
*/
public static Geometry deserialize(byte[] wkb) throws SQLServerException {
return new Geometry(wkb);
public static Geometry deserialize(byte[] clr) throws SQLServerException {
return new Geometry(clr);
}

/**
Expand Down Expand Up @@ -156,10 +161,10 @@ public static Geometry point(double x, double y, int srid) throws SQLServerExcep
*/
public String STAsText() throws SQLServerException {
if (null == wktNoZM) {
buffer = ByteBuffer.wrap(wkb);
buffer = ByteBuffer.wrap(clr);
buffer.order(ByteOrder.LITTLE_ENDIAN);

parseWkb(this);
parseClr(this);

WKTsb = new StringBuffer();
WKTsbNoZM = new StringBuffer();
Expand All @@ -176,10 +181,10 @@ public String STAsText() throws SQLServerException {
* @return byte array representation of the Geometry object.
*/
public byte[] STAsBinary() {
if (null == wkbNoZM) {
serializeToWkb(true, this);
if (null == wkb) {
serializeToWkb(this);
}
return wkbNoZM;
return wkb;
}

/**
Expand All @@ -188,7 +193,7 @@ public byte[] STAsBinary() {
* @return byte array representation of the Geometry object.
*/
public byte[] serialize() {
return wkb;
return clr;
}

/**
Expand Down
Loading

0 comments on commit c82222a

Please sign in to comment.