Skip to content

Commit

Permalink
Merge pull request #657 from peterbae/github-655
Browse files Browse the repository at this point in the history
655 Fix to return correct column type for spatial datatypes
  • Loading branch information
peterbae authored Mar 23, 2018
2 parents 2039522 + 8ad8252 commit c1aa43b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
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 @@ -121,14 +121,21 @@ public int getColumnType(int column) throws SQLServerException {
}

JDBCType jdbcType = typeInfo.getSSType().getJDBCType();
SSType sqlType = typeInfo.getSSType();
// in bulkcopy for instance, we need to return the real jdbc type which is sql variant and not the default Char one.
if ( SSType.SQL_VARIANT == typeInfo.getSSType()){
if ( SSType.SQL_VARIANT == sqlType){
jdbcType = JDBCType.SQL_VARIANT;
}
if (SSType.UDT == sqlType) {
if (typeInfo.getSSTypeName().equalsIgnoreCase(SSType.GEOMETRY.name())) {
jdbcType = JDBCType.GEOMETRY;
}
if (typeInfo.getSSTypeName().equalsIgnoreCase(SSType.GEOGRAPHY.name())) {
jdbcType = JDBCType.GEOGRAPHY;
}
}
int r = jdbcType.asJavaSqlType();
if (con.isKatmaiOrLater()) {
SSType sqlType = typeInfo.getSSType();

switch (sqlType) {
case VARCHARMAX:
r = SSType.VARCHAR.getJDBCType().asJavaSqlType();
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

0 comments on commit c1aa43b

Please sign in to comment.