Skip to content

Commit

Permalink
Fix Geography.point method (#853)
Browse files Browse the repository at this point in the history
Fix | Fixes Geography.point method to expected behavior (#853)
  • Loading branch information
peterbae authored and ulvii committed Nov 30, 2018
1 parent 5dfeaab commit 123abb0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/DDC.java
Expand Up @@ -596,8 +596,14 @@ static final Object convertStreamToObject(BaseInputStream stream, TypeInfo typeI
if (JDBCType.GUID == jdbcType) {
return Util.readGUID(byteValue);
} else if (JDBCType.GEOMETRY == jdbcType) {
if (!typeInfo.getSSTypeName().equalsIgnoreCase(jdbcType.toString())) {
DataTypes.throwConversionError(typeInfo.getSSTypeName().toUpperCase(), jdbcType.toString());
}
return Geometry.STGeomFromWKB(byteValue);
} else if (JDBCType.GEOGRAPHY == jdbcType) {
if (!typeInfo.getSSTypeName().equalsIgnoreCase(jdbcType.toString())) {
DataTypes.throwConversionError(typeInfo.getSSTypeName().toUpperCase(), jdbcType.toString());
}
return Geography.STGeomFromWKB(byteValue);
} else {
String hexString = Util.bytesToHexString(byteValue, byteValue.length);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/microsoft/sqlserver/jdbc/Geography.java
Expand Up @@ -140,7 +140,7 @@ public static Geography parse(String wkt) throws SQLServerException {
* if an exception occurs
*/
public static Geography point(double lat, double lon, int srid) throws SQLServerException {
return new Geography("POINT (" + lat + " " + lon + ")", srid);
return new Geography("POINT (" + lon + " " + lat + ")", srid);
}

/**
Expand Down Expand Up @@ -212,8 +212,8 @@ public boolean hasZ() {
* @return double value that represents the latitude.
*/
public Double getLatitude() {
if (null != internalType && internalType == InternalSpatialDatatype.POINT && xValues.length == 1) {
return xValues[0];
if (null != internalType && internalType == InternalSpatialDatatype.POINT && yValues.length == 1) {
return yValues[0];
}
return null;
}
Expand All @@ -224,8 +224,8 @@ public Double getLatitude() {
* @return double value that represents the longitude.
*/
public Double getLongitude() {
if (null != internalType && internalType == InternalSpatialDatatype.POINT && yValues.length == 1) {
return yValues[0];
if (null != internalType && internalType == InternalSpatialDatatype.POINT && xValues.length == 1) {
return xValues[0];
}
return null;
}
Expand Down
Expand Up @@ -4,6 +4,7 @@
*/
package com.microsoft.sqlserver.jdbc.datatypes;

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

import java.io.IOException;
Expand Down Expand Up @@ -812,7 +813,7 @@ public void testPoint() throws SQLException {
String geoWKT = "POINT(1 2)";

Geometry geomWKT = Geometry.point(1, 2, 0);
Geography geogWKT = Geography.point(1, 2, 4326);
Geography geogWKT = Geography.point(2, 1, 4326);

try (Connection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
Statement stmt = con.createStatement()) {
Expand Down Expand Up @@ -968,8 +969,8 @@ public void testGetXGetY() throws SQLException {

x = geog.getLatitude();
y = geog.getLongitude();
assertEquals(x, 1);
assertEquals(y, 2);
assertEquals(x, 2);
assertEquals(y, 1);
}

@Test
Expand Down Expand Up @@ -1041,6 +1042,48 @@ public void testNull() throws SQLException {
}
}
}

@Test
public void testWrongtype() throws SQLException {
beforeEachSetup();

Geometry geomWKT = Geometry.point(1, 2, 0);
Geography geogWKT = Geography.point(2, 1, 4326);

try (Connection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
Statement stmt = con.createStatement()) {

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement(
"insert into " + AbstractSQLGenerator.escapeIdentifier(geomTableName) + " values (?)");) {
pstmt.setGeometry(1, geomWKT);
pstmt.execute();

try {
SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(geomTableName));
rs.next();
rs.getGeography(1); // should fail
fail();
} catch (SQLServerException e) {
assertEquals(e.getMessage(), "The conversion from GEOMETRY to GEOGRAPHY is unsupported.");
}
}

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement(
"insert into " + AbstractSQLGenerator.escapeIdentifier(geogTableName) + " values (?)");) {
pstmt.setGeography(1, geogWKT);
pstmt.execute();

try {
SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(geogTableName));
rs.next();
rs.getGeometry(1); // should fail
fail();
} catch (SQLServerException e) {
assertEquals(e.getMessage(), "The conversion from GEOGRAPHY to GEOMETRY is unsupported.");
}
}
}
}

private void beforeEachSetup() throws SQLException {
try (Connection con = (SQLServerConnection) DriverManager.getConnection(connectionString);
Expand Down

0 comments on commit 123abb0

Please sign in to comment.