diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/DDC.java b/src/main/java/com/microsoft/sqlserver/jdbc/DDC.java index 845fe5b90..ee3fff262 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(), jdbcType.toString()); + } return Geometry.STGeomFromWKB(byteValue); } else if (JDBCType.GEOGRAPHY == jdbcType) { + if (!typeInfo.getSSTypeName().equalsIgnoreCase(jdbcType.toString())) { + DataTypes.throwConversionError(typeInfo.getSSTypeName(), jdbcType.toString()); + } return Geography.STGeomFromWKB(byteValue); } else { String hexString = Util.bytesToHexString(byteValue, byteValue.length); 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 a31fc75b5..fcf537819 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/SQLServerSpatialDatatypeTest.java @@ -1041,6 +1041,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 + throw new SQLException ("getGeography against Geometry column should 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 + throw new SQLException ("getGeometry against Geography column should 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);