diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/DDC.java b/src/main/java/com/microsoft/sqlserver/jdbc/DDC.java index 845fe5b900..7e18aec104 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/DDC.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/DDC.java @@ -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); diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/Geography.java b/src/main/java/com/microsoft/sqlserver/jdbc/Geography.java index d8dd00929f..c572ad70bd 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/Geography.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/Geography.java @@ -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); } /** @@ -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; } @@ -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; } diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java index 6a95f29c9e..cce188515e 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java @@ -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; @@ -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()) { @@ -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 @@ -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);