Skip to content

Commit

Permalink
Adding logic to not allow rs.getGeometry against geography and vice v…
Browse files Browse the repository at this point in the history
…ersa.
  • Loading branch information
peterbae committed Nov 26, 2018
1 parent 093783d commit 171a590
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/DDC.java
Original file line number Diff line number Diff line change
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(), 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 171a590

Please sign in to comment.