33import com .clickhouse .client .api .data_formats .ClickHouseBinaryFormatReader ;
44import com .clickhouse .client .api .metadata .TableSchema ;
55import com .clickhouse .client .api .query .QueryResponse ;
6- import com .clickhouse .data .ClickHouseDataType ;
6+ import com .clickhouse .data .ClickHouseColumn ;
77import com .clickhouse .jdbc .internal .ExceptionUtils ;
88import com .clickhouse .jdbc .internal .FeatureManager ;
99import com .clickhouse .jdbc .internal .JdbcUtils ;
3535import java .sql .Timestamp ;
3636import java .time .ZonedDateTime ;
3737import java .util .Calendar ;
38+ import java .util .Collections ;
3839import java .util .Map ;
3940import java .util .function .Consumer ;
4041
@@ -481,16 +482,6 @@ protected void setMetaData(ResultSetMetaDataImpl metaData) {
481482 this .metaData = metaData ;
482483 }
483484
484- @ Override
485- public Object getObject (int columnIndex ) throws SQLException {
486- return getObject (columnIndex , JdbcUtils .convertToJavaClass (getSchema ().getColumnByIndex (columnIndex ).getDataType ()));
487- }
488-
489- @ Override
490- public Object getObject (String columnLabel ) throws SQLException {
491- return getObject (columnLabel , JdbcUtils .convertToJavaClass (getSchema ().getColumnByName (columnLabel ).getDataType ()));
492- }
493-
494485 @ Override
495486 public int findColumn (String columnLabel ) throws SQLException {
496487 checkClosed ();
@@ -962,12 +953,6 @@ public Statement getStatement() throws SQLException {
962953 return this .parentStatement ;
963954 }
964955
965- @ Override
966- public Object getObject (int columnIndex , Map <String , Class <?>> map ) throws SQLException {
967- ClickHouseDataType type = getSchema ().getColumnByIndex (columnIndex ).getDataType ();
968- return getObject (columnIndex , map .get (JdbcUtils .convertToSqlType (type ).getName ()));
969- }
970-
971956 @ Override
972957 public Ref getRef (int columnIndex ) throws SQLException {
973958 return getRef (columnIndexToName (columnIndex ));
@@ -988,12 +973,6 @@ public java.sql.Array getArray(int columnIndex) throws SQLException {
988973 return getObject (columnIndex , java .sql .Array .class );
989974 }
990975
991- @ Override
992- public Object getObject (String columnLabel , Map <String , Class <?>> map ) throws SQLException {
993- checkClosed ();
994- return getObject (columnLabel , map .get (JdbcUtils .convertToSqlType (getSchema ().getColumnByName (columnLabel ).getDataType ()).getName ()));
995- }
996-
997976 @ Override
998977 public Ref getRef (String columnLabel ) throws SQLException {
999978 checkClosed ();
@@ -1437,38 +1416,72 @@ public void updateNClob(String columnLabel, Reader reader) throws SQLException {
14371416 }
14381417
14391418 @ Override
1440- public <T > T getObject (int columnIndex , Class <T > type ) throws SQLException {
1419+ public Object getObject (int columnIndex ) throws SQLException {
1420+ return getObject (columnIndexToName (columnIndex ));
1421+ }
1422+
1423+ @ Override
1424+ public Object getObject (String columnLabel ) throws SQLException {
1425+ return getObjectImpl (columnLabel , null , Collections .emptyMap ());
1426+ }
1427+
1428+ @ Override
1429+ public Object getObject (int columnIndex , Map <String , Class <?>> map ) throws SQLException {
1430+ return getObject (columnIndexToName (columnIndex ), map );
1431+ }
1432+
1433+ @ Override
1434+ public Object getObject (String columnLabel , Map <String , Class <?>> map ) throws SQLException {
14411435 checkClosed ();
1442- try {
1443- if (reader .hasValue (columnIndex )) {
1444- wasNull = false ;
1445- if (type == null ) {//As a fallback, try to get the value as is
1446- return reader .readValue (columnIndex );
1447- }
1436+ return getObjectImpl (columnLabel , null , map );
1437+ }
14481438
1449- return (T ) JdbcUtils .convert (reader .readValue (columnIndex ), type , type == java .sql .Array .class ? getSchema ().getColumnByIndex (columnIndex ) : null );
1450- } else {
1451- wasNull = true ;
1452- return null ;
1453- }
1454- } catch (Exception e ) {
1455- throw ExceptionUtils .toSqlState (String .format ("Method: getObject(\" %s\" , %s) encountered an exception." ,
1456- reader .getSchema ().columnIndexToName (columnIndex ), type ),
1457- String .format ("SQL: [%s]" , parentStatement .getLastStatementSql ()), e );
1458- }
1439+ @ Override
1440+ public <T > T getObject (int columnIndex , Class <T > type ) throws SQLException {
1441+ checkClosed ();
1442+ return getObject (columnIndexToName (columnIndex ), type );
14591443 }
14601444
14611445 @ Override
14621446 public <T > T getObject (String columnLabel , Class <T > type ) throws SQLException {
14631447 checkClosed ();
1448+ return getObjectImpl (columnLabel , type , Collections .emptyMap ());
1449+ }
1450+
1451+ @ SuppressWarnings ("unchecked" )
1452+ public <T > T getObjectImpl (String columnLabel , Class <?> type , Map <String , Class <?>> typeMap ) throws SQLException {
14641453 try {
1454+ ClickHouseColumn column = getSchema ().getColumnByName (columnLabel );
1455+ if (column == null ) {
1456+ throw new SQLException ("Column \" " + columnLabel + "\" does not exist." );
1457+ }
1458+
14651459 if (reader .hasValue (columnLabel )) {
14661460 wasNull = false ;
1461+
1462+ if (type == null ) {
1463+ switch (column .getDataType ()) {
1464+ case Point :
1465+ case Ring :
1466+ case LineString :
1467+ case Polygon :
1468+ case MultiPolygon :
1469+ case MultiLineString :
1470+ break ; // read as is
1471+ default :
1472+ if (typeMap == null || typeMap .isEmpty ()) {
1473+ type = JdbcUtils .convertToJavaClass (column .getDataType ());
1474+ } else {
1475+ type = typeMap .get (JdbcUtils .convertToSqlType (column .getDataType ()).getName ());
1476+ }
1477+ }
1478+ }
1479+
14671480 if (type == null ) {//As a fallback, try to get the value as is
14681481 return reader .readValue (columnLabel );
14691482 }
14701483
1471- return (T ) JdbcUtils .convert (reader .readValue (columnLabel ), type , type == java . sql . Array . class ? getSchema (). getColumnByName ( columnLabel ) : null );
1484+ return (T ) JdbcUtils .convert (reader .readValue (columnLabel ), type , column );
14721485 } else {
14731486 wasNull = true ;
14741487 return null ;
0 commit comments