4
4
5
5
package net .snowflake .client .jdbc ;
6
6
7
+ import static net .snowflake .client .jdbc .SnowflakeUtil .mapSFExceptionToSQLException ;
8
+
7
9
import com .fasterxml .jackson .core .type .TypeReference ;
8
10
import com .fasterxml .jackson .databind .JsonNode ;
9
11
import com .fasterxml .jackson .databind .ObjectMapper ;
35
37
import java .util .List ;
36
38
import java .util .Map ;
37
39
import java .util .TimeZone ;
40
+ import net .snowflake .client .core .ColumnTypeHelper ;
38
41
import net .snowflake .client .core .JsonSqlInput ;
39
42
import net .snowflake .client .core .ObjectMapperFactory ;
40
43
import net .snowflake .client .core .SFBaseResultSet ;
@@ -1372,8 +1375,6 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
1372
1375
return (T ) (Double ) getDouble (columnIndex );
1373
1376
} else if (Date .class .isAssignableFrom (type )) {
1374
1377
return (T ) getDate (columnIndex );
1375
- } else if (Date .class .isAssignableFrom (type )) {
1376
- return (T ) getDate (columnIndex );
1377
1378
} else if (Time .class .isAssignableFrom (type )) {
1378
1379
return (T ) getTime (columnIndex );
1379
1380
} else if (Timestamp .class .isAssignableFrom (type )) {
@@ -1395,43 +1396,299 @@ public <T> List<T> getList(int columnIndex, Class<T> type) throws SQLException {
1395
1396
}
1396
1397
1397
1398
public <T > T [] getArray (int columnIndex , Class <T > type ) throws SQLException {
1398
- Map [] jsonMaps = (Map []) getArray (columnIndex ).getArray ();
1399
- T [] arr = (T []) java .lang .reflect .Array .newInstance (type , jsonMaps .length );
1399
+ int columnSubType = resultSetMetaData .getInternalColumnType (columnIndex );
1400
+ int columnType = ColumnTypeHelper .getColumnType (columnSubType , session );
1401
+ ;
1402
+ int scale = resultSetMetaData .getScale (columnIndex );
1403
+ TimeZone tz = sfBaseResultSet .getSessionTimeZone ();
1404
+ Object [] objects = (Object []) getArray (columnIndex ).getArray ();
1405
+ T [] arr = (T []) java .lang .reflect .Array .newInstance (type , objects .length );
1400
1406
int counter = 0 ;
1401
- for (Map map : jsonMaps ) {
1402
- SQLData instance = (SQLData ) SQLDataCreationHelper .create (type );
1403
- SQLInput sqlInput =
1404
- new JsonSqlInput (
1405
- OBJECT_MAPPER .convertValue (map , JsonNode .class ),
1406
- session ,
1407
- sfBaseResultSet .getConverters (),
1408
- sfBaseResultSet .getMetaData ().getColumnMetadata ().get (columnIndex - 1 ).getFields (),
1409
- sfBaseResultSet .getSessionTimezone ());
1410
- instance .readSQL (sqlInput , null );
1411
- arr [counter ++] = (T ) instance ;
1407
+ for (Object value : objects ) {
1408
+ if (SQLData .class .isAssignableFrom (type )) {
1409
+ Map data = (Map ) value ;
1410
+ SQLData instance = (SQLData ) SQLDataCreationHelper .create (type );
1411
+ SQLInput sqlInput =
1412
+ new JsonSqlInput (
1413
+ OBJECT_MAPPER .convertValue (data , JsonNode .class ),
1414
+ session ,
1415
+ sfBaseResultSet .getConverters (),
1416
+ sfBaseResultSet .getMetaData ().getColumnMetadata ().get (columnIndex - 1 ).getFields (),
1417
+ sfBaseResultSet .getSessionTimezone ());
1418
+ instance .readSQL (sqlInput , null );
1419
+ arr [counter ++] = (T ) instance ;
1420
+ } else if (String .class .isAssignableFrom (type )) {
1421
+ arr [counter ++] =
1422
+ mapSFExceptionToSQLException (
1423
+ () ->
1424
+ (T )
1425
+ sfBaseResultSet
1426
+ .getConverters ()
1427
+ .getStringConverter ()
1428
+ .getString (value , columnType , columnSubType , scale ));
1429
+ } else if (Boolean .class .isAssignableFrom (type )) {
1430
+ arr [counter ++] =
1431
+ mapSFExceptionToSQLException (
1432
+ () ->
1433
+ (T )
1434
+ sfBaseResultSet
1435
+ .getConverters ()
1436
+ .getBooleanConverter ()
1437
+ .getBoolean (value , columnType ));
1438
+ } else if (Byte .class .isAssignableFrom (type )) {
1439
+ arr [counter ++] =
1440
+ mapSFExceptionToSQLException (
1441
+ () ->
1442
+ (T )
1443
+ sfBaseResultSet
1444
+ .getConverters ()
1445
+ .getBytesConverter ()
1446
+ .getBytes (value , columnType , columnSubType , scale ));
1447
+ } else if (Short .class .isAssignableFrom (type )) {
1448
+ arr [counter ++] =
1449
+ mapSFExceptionToSQLException (
1450
+ () ->
1451
+ (T )
1452
+ Short .valueOf (
1453
+ sfBaseResultSet
1454
+ .getConverters ()
1455
+ .getNumberConverter ()
1456
+ .getShort (value , columnType )));
1457
+ } else if (Integer .class .isAssignableFrom (type )) {
1458
+ arr [counter ++] =
1459
+ mapSFExceptionToSQLException (
1460
+ () ->
1461
+ (T )
1462
+ Integer .valueOf (
1463
+ sfBaseResultSet
1464
+ .getConverters ()
1465
+ .getNumberConverter ()
1466
+ .getInt (value , columnType )));
1467
+ } else if (Long .class .isAssignableFrom (type )) {
1468
+ arr [counter ++] =
1469
+ mapSFExceptionToSQLException (
1470
+ () ->
1471
+ (T )
1472
+ Long .valueOf (
1473
+ sfBaseResultSet
1474
+ .getConverters ()
1475
+ .getNumberConverter ()
1476
+ .getLong (value , columnType )));
1477
+ } else if (Float .class .isAssignableFrom (type )) {
1478
+ arr [counter ++] =
1479
+ mapSFExceptionToSQLException (
1480
+ () ->
1481
+ (T )
1482
+ Float .valueOf (
1483
+ sfBaseResultSet
1484
+ .getConverters ()
1485
+ .getNumberConverter ()
1486
+ .getFloat (value , columnType )));
1487
+ } else if (Double .class .isAssignableFrom (type )) {
1488
+ arr [counter ++] =
1489
+ mapSFExceptionToSQLException (
1490
+ () ->
1491
+ (T )
1492
+ Double .valueOf (
1493
+ sfBaseResultSet
1494
+ .getConverters ()
1495
+ .getNumberConverter ()
1496
+ .getDouble (value , columnType )));
1497
+ } else if (Date .class .isAssignableFrom (type )) {
1498
+ arr [counter ++] =
1499
+ mapSFExceptionToSQLException (
1500
+ () ->
1501
+ (T )
1502
+ sfBaseResultSet
1503
+ .getConverters ()
1504
+ .getDateTimeConverter ()
1505
+ .getDate (value , columnType , columnSubType , tz , scale ));
1506
+ } else if (Time .class .isAssignableFrom (type )) {
1507
+ arr [counter ++] =
1508
+ mapSFExceptionToSQLException (
1509
+ () ->
1510
+ (T )
1511
+ sfBaseResultSet
1512
+ .getConverters ()
1513
+ .getDateTimeConverter ()
1514
+ .getTime (value , columnType , columnSubType , tz , scale ));
1515
+ } else if (Timestamp .class .isAssignableFrom (type )) {
1516
+ mapSFExceptionToSQLException (
1517
+ () ->
1518
+ (T )
1519
+ sfBaseResultSet
1520
+ .getConverters ()
1521
+ .getDateTimeConverter ()
1522
+ .getTimestamp (value , columnType , columnSubType , tz , scale ));
1523
+ } else if (BigDecimal .class .isAssignableFrom (type )) {
1524
+ arr [counter ++] = (T ) getBigDecimal (columnIndex );
1525
+ } else {
1526
+ logger .warn (
1527
+ "Unsupported type passed to getArray(int columnIndex, Class<T> type): "
1528
+ + type .getName ());
1529
+ throw new SQLException (
1530
+ "Type passed to 'getObject(int columnIndex,Class<T> type)' is unsupported. Type: "
1531
+ + type .getName ());
1532
+ }
1412
1533
}
1413
-
1414
1534
return arr ;
1415
1535
}
1416
1536
1417
1537
public <T > Map <String , T > getMap (int columnIndex , Class <T > type ) throws SQLException {
1538
+ int columnType = resultSetMetaData .getInternalColumnType (columnIndex );
1539
+ int columnSubType = resultSetMetaData .getInternalColumnType (columnIndex );
1540
+ int scale = resultSetMetaData .getScale (columnIndex );
1541
+ TimeZone tz = sfBaseResultSet .getSessionTimeZone ();
1418
1542
Object object = getObject (columnIndex );
1419
1543
JsonNode jsonNode = ((JsonSqlInput ) object ).getInput ();
1420
1544
Map <String , Object > map =
1421
1545
OBJECT_MAPPER .convertValue (jsonNode , new TypeReference <Map <String , Object >>() {});
1422
1546
Map <String , T > resultMap = new HashMap <>();
1423
-
1424
1547
for (Map .Entry <String , Object > entry : map .entrySet ()) {
1425
- SQLData instance = (SQLData ) SQLDataCreationHelper .create (type );
1426
- SQLInput sqlInput =
1427
- new JsonSqlInput (
1428
- jsonNode .get (entry .getKey ()),
1429
- session ,
1430
- sfBaseResultSet .getConverters (),
1431
- sfBaseResultSet .getMetaData ().getColumnMetadata ().get (columnIndex - 1 ).getFields (),
1432
- sfBaseResultSet .getSessionTimezone ());
1433
- instance .readSQL (sqlInput , null );
1434
- resultMap .put (entry .getKey (), (T ) instance );
1548
+ if (SQLData .class .isAssignableFrom (type )) {
1549
+ SQLData instance = (SQLData ) SQLDataCreationHelper .create (type );
1550
+ SQLInput sqlInput =
1551
+ new JsonSqlInput (
1552
+ jsonNode .get (entry .getKey ()),
1553
+ session ,
1554
+ sfBaseResultSet .getConverters (),
1555
+ sfBaseResultSet .getMetaData ().getColumnMetadata ().get (columnIndex - 1 ).getFields (),
1556
+ sfBaseResultSet .getSessionTimezone ());
1557
+ instance .readSQL (sqlInput , null );
1558
+ resultMap .put (entry .getKey (), (T ) instance );
1559
+ } else if (String .class .isAssignableFrom (type )) {
1560
+ resultMap .put (
1561
+ entry .getKey (),
1562
+ mapSFExceptionToSQLException (
1563
+ () ->
1564
+ (T )
1565
+ sfBaseResultSet
1566
+ .getConverters ()
1567
+ .getStringConverter ()
1568
+ .getString (entry .getValue (), columnType , columnSubType , scale )));
1569
+ } else if (Boolean .class .isAssignableFrom (type )) {
1570
+ resultMap .put (
1571
+ entry .getKey (),
1572
+ mapSFExceptionToSQLException (
1573
+ () ->
1574
+ (T )
1575
+ sfBaseResultSet
1576
+ .getConverters ()
1577
+ .getBooleanConverter ()
1578
+ .getBoolean (entry .getValue (), columnType )));
1579
+ } else if (Byte .class .isAssignableFrom (type )) {
1580
+ resultMap .put (
1581
+ entry .getKey (),
1582
+ mapSFExceptionToSQLException (
1583
+ () ->
1584
+ (T )
1585
+ sfBaseResultSet
1586
+ .getConverters ()
1587
+ .getBytesConverter ()
1588
+ .getBytes (entry .getValue (), columnType , columnSubType , scale )));
1589
+ } else if (Short .class .isAssignableFrom (type )) {
1590
+ resultMap .put (
1591
+ entry .getKey (),
1592
+ mapSFExceptionToSQLException (
1593
+ () ->
1594
+ (T )
1595
+ (Short )
1596
+ sfBaseResultSet
1597
+ .getConverters ()
1598
+ .getNumberConverter ()
1599
+ .getShort (entry .getValue (), columnType )));
1600
+ } else if (Integer .class .isAssignableFrom (type )) {
1601
+ resultMap .put (
1602
+ entry .getKey (),
1603
+ mapSFExceptionToSQLException (
1604
+ () ->
1605
+ (T )
1606
+ (Integer )
1607
+ sfBaseResultSet
1608
+ .getConverters ()
1609
+ .getNumberConverter ()
1610
+ .getInt (entry .getValue (), columnType )));
1611
+ } else if (Long .class .isAssignableFrom (type )) {
1612
+ resultMap .put (
1613
+ entry .getKey (),
1614
+ mapSFExceptionToSQLException (
1615
+ () ->
1616
+ (T )
1617
+ (Long )
1618
+ sfBaseResultSet
1619
+ .getConverters ()
1620
+ .getNumberConverter ()
1621
+ .getLong (entry .getValue (), columnType )));
1622
+ } else if (Float .class .isAssignableFrom (type )) {
1623
+ resultMap .put (
1624
+ entry .getKey (),
1625
+ mapSFExceptionToSQLException (
1626
+ () ->
1627
+ (T )
1628
+ (Float )
1629
+ sfBaseResultSet
1630
+ .getConverters ()
1631
+ .getNumberConverter ()
1632
+ .getFloat (entry .getValue (), columnType )));
1633
+ } else if (Double .class .isAssignableFrom (type )) {
1634
+ resultMap .put (
1635
+ entry .getKey (),
1636
+ mapSFExceptionToSQLException (
1637
+ () ->
1638
+ (T )
1639
+ (Double )
1640
+ sfBaseResultSet
1641
+ .getConverters ()
1642
+ .getNumberConverter ()
1643
+ .getDouble (entry .getValue (), columnType )));
1644
+ } else if (BigDecimal .class .isAssignableFrom (type )) {
1645
+ resultMap .put (
1646
+ entry .getKey (),
1647
+ mapSFExceptionToSQLException (
1648
+ () ->
1649
+ (T )
1650
+ sfBaseResultSet
1651
+ .getConverters ()
1652
+ .getNumberConverter ()
1653
+ .getBigDecimal (entry .getValue (), columnType )));
1654
+ } else if (Date .class .isAssignableFrom (type )) {
1655
+ resultMap .put (
1656
+ entry .getKey (),
1657
+ mapSFExceptionToSQLException (
1658
+ () ->
1659
+ (T )
1660
+ sfBaseResultSet
1661
+ .getConverters ()
1662
+ .getDateTimeConverter ()
1663
+ .getDate (entry .getValue (), columnType , columnSubType , tz , scale )));
1664
+ } else if (Time .class .isAssignableFrom (type )) {
1665
+ resultMap .put (
1666
+ entry .getKey (),
1667
+ mapSFExceptionToSQLException (
1668
+ () ->
1669
+ (T )
1670
+ sfBaseResultSet
1671
+ .getConverters ()
1672
+ .getDateTimeConverter ()
1673
+ .getTime (entry .getValue (), columnType , columnSubType , tz , scale )));
1674
+ } else if (Timestamp .class .isAssignableFrom (type )) {
1675
+ resultMap .put (
1676
+ entry .getKey (),
1677
+ mapSFExceptionToSQLException (
1678
+ () ->
1679
+ (T )
1680
+ sfBaseResultSet
1681
+ .getConverters ()
1682
+ .getDateTimeConverter ()
1683
+ .getTimestamp (entry .getValue (), columnType , columnSubType , tz , scale )));
1684
+ } else {
1685
+ logger .debug (
1686
+ "Unsupported type passed to getObject(int columnIndex,Class<T> type): "
1687
+ + type .getName ());
1688
+ throw new SQLException (
1689
+ "Type passed to 'getObject(int columnIndex,Class<T> type)' is unsupported. Type: "
1690
+ + type .getName ());
1691
+ }
1435
1692
}
1436
1693
1437
1694
return resultMap ;
0 commit comments