Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/Geography.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Locale;

public class Geography extends SQLServerSpatialDatatype {

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Locale;

public class Geometry extends SQLServerSpatialDatatype {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public int getColumnType(int column) throws SQLServerException {
if ( SSType.SQL_VARIANT == typeInfo.getSSType()){
jdbcType = JDBCType.SQL_VARIANT;
}
if (SSType.UDT == typeInfo.getSSType()) {
if (typeInfo.getSSTypeName().equalsIgnoreCase("geometry")) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have a problem with this, but why are we doing type check in 3 different ways. VARIANT is being checked separately outside the switch, and now GEOMETRY/GEOGRAPHY will be checked by name. Can we maybe stick to one form? This Microsoft overview of spatial datatypes seems to imply that it's only available in SQL Server 2012 and later, so shouldn't this be in the isKatamaiOrLater() block?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't stick to one form. If you debug this part of the code, you'll see that the getSSType will give SQL_VARIANT or UDT. If it's UDT, we need to look at the SSTypeName to find out if it's Geometry or Geography, so this is the only way to check for these datatypes.

The overview page mentions that new spatial features have been introduced in SQL Server 2012, but this does not mean that spatial datatypes are supported starting from 2012 - it's supported starting from 2008. so we need this check for 2008 and up.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create Constants for these strings and reference them here.

jdbcType = JDBCType.GEOMETRY;
}
if (typeInfo.getSSTypeName().equalsIgnoreCase("geography")) {
jdbcType = JDBCType.GEOGRAPHY;
}
}
int r = jdbcType.asJavaSqlType();
if (con.isKatmaiOrLater()) {
SSType sqlType = typeInfo.getSSType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ParameterMetaData;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
Expand Down Expand Up @@ -817,6 +819,43 @@ public void testSTAsBinary() throws SQLException {
assertEquals(geomWKB, geomWKB2);
assertEquals(geogWKB, geogWKB2);
}

public void testCheckGeomMetaData() throws SQLException {
beforeEachSetup();

pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + geomTableName +" (c1) VALUES (?)");
ParameterMetaData paramMetaData = pstmt.getParameterMetaData();
Geometry g = Geometry.STGeomFromText("POINT (1 2 3 4)", 0);
pstmt.setGeometry(1, g);
pstmt.execute();

int sqlType = paramMetaData.getParameterType(1);
String sqlTypeName = paramMetaData.getParameterTypeName(1);
assertEquals(sqlType, -157);
assertEquals(sqlTypeName, "geometry");
SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("select * from " + geomTableName);
ResultSetMetaData rsmd = rs.getMetaData();
assertEquals(rsmd.getColumnType(1), -157);
}

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

pstmt = (SQLServerPreparedStatement) connection.prepareStatement("INSERT INTO " + geogTableName +" (c1) VALUES (?)");
ParameterMetaData paramMetaData = pstmt.getParameterMetaData();
Geography g = Geography.STGeomFromText("POINT (1 2 3 4)", 4326);
pstmt.setGeography(1, g);
pstmt.execute();

int sqlType = paramMetaData.getParameterType(1);
String sqlTypeName = paramMetaData.getParameterTypeName(1);
assertEquals(sqlType, -158);
assertEquals(sqlTypeName, "geography");
SQLServerResultSet rs = (SQLServerResultSet) stmt.executeQuery("select * from " + geogTableName);
ResultSetMetaData rsmd = rs.getMetaData();
assertEquals(rsmd.getColumnType(1), -158);
}

private void beforeEachSetup() throws SQLException {
Utils.dropTableIfExists(geomTableName, stmt);
Expand Down