Skip to content

Commit c0de704

Browse files
Improve handling of numeric SQL types and BigDecimal conversion
Added support for NUMERIC and DECIMAL SQL types in `BasicSQLUtils`, ensuring proper conversion from various data types. In `GenericDBConversion`, introduced a helper method to safely convert objects to BigDecimal and updated relevant code to use it, improving robustness and error handling. Also added logic to set nulls for specific field patterns during DB conversion. Co-Authored-By: Gitesh Sagvekar <giteshsagvekar07@gmail.com>
1 parent 3edca4f commit c0de704

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

src/edu/ku/brc/specify/conversion/BasicSQLUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,12 +1792,20 @@ public static void setData(final PreparedStatement pStmt, final int type, final
17921792

17931793
case java.sql.Types.REAL:
17941794
case java.sql.Types.DOUBLE:
1795+
case java.sql.Types.NUMERIC:
1796+
case java.sql.Types.DECIMAL:
17951797
if (isStr)
17961798
{
17971799
pStmt.setString(colInx, (String)data);
17981800
} else
17991801
{
1800-
pStmt.setDouble(colInx, (Double)data);
1802+
if (data instanceof Number)
1803+
{
1804+
pStmt.setDouble(colInx, ((Number)data).doubleValue());
1805+
} else
1806+
{
1807+
pStmt.setDouble(colInx, Double.parseDouble(data.toString()));
1808+
}
18011809
}
18021810
break;
18031811

src/edu/ku/brc/specify/conversion/GenericDBConversion.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6369,6 +6369,20 @@ public boolean convertDeterminationRecords()
63696369
{
63706370
pStmt.setInt(fldInx, getCollectionMemberId());
63716371

6372+
} else if (newFieldName.matches("Integer[1-5]") ||
6373+
newFieldName.matches("Number[1-5]") ||
6374+
newFieldName.matches("Text[3-8]") ||
6375+
newFieldName.matches("YesNo[3-5]"))
6376+
{
6377+
FieldMetaData fldMetaData = newFieldMetaData.get(i);
6378+
if (fldMetaData != null)
6379+
{
6380+
pStmt.setNull(fldInx, fldMetaData.getSqlType());
6381+
} else
6382+
{
6383+
pStmt.setObject(fldInx, null);
6384+
}
6385+
63726386
} else
63736387
{
63746388
Integer index = null;
@@ -10248,6 +10262,38 @@ protected GeologicTimePeriodTreeDefItem addGtpDefItem(Integer rankCode,
1024810262
return item;
1024910263
}
1025010264

10265+
private BigDecimal toBigDecimal(final Object value)
10266+
{
10267+
if (value == null)
10268+
{
10269+
return null;
10270+
}
10271+
if (value instanceof BigDecimal)
10272+
{
10273+
return (BigDecimal)value;
10274+
}
10275+
if (value instanceof Number)
10276+
{
10277+
try
10278+
{
10279+
return new BigDecimal(value.toString());
10280+
} catch (NumberFormatException ex)
10281+
{
10282+
log.error("Unable to convert numeric value [" + value + "] to BigDecimal", ex);
10283+
return null;
10284+
}
10285+
}
10286+
10287+
try
10288+
{
10289+
return new BigDecimal(value.toString());
10290+
} catch (NumberFormatException ex)
10291+
{
10292+
log.error("Unable to convert value [" + value + "] to BigDecimal", ex);
10293+
return null;
10294+
}
10295+
}
10296+
1025110297
/**
1025210298
* @param tblWriter
1025310299
* @param treeDef
@@ -10315,10 +10361,10 @@ public void convertGTP(final TableWriter tblWriter, final GeologicTimePeriodTree
1031510361
Date creTDate = rs.getDate(7);
1031610362
Timestamp modT = (modTDate != null) ? new Timestamp(modTDate.getTime()) : null;
1031710363
Timestamp creT = (creTDate != null) ? new Timestamp(creTDate.getTime()) : null;
10318-
BigDecimal upper = new BigDecimal((Double)rs.getObject(8));
10319-
BigDecimal uError = new BigDecimal((Double)rs.getObject(9));
10320-
BigDecimal lower = new BigDecimal((Double)rs.getObject(10));
10321-
BigDecimal lError = new BigDecimal((Double)rs.getObject(11));
10364+
BigDecimal upper = toBigDecimal(rs.getObject(8));
10365+
BigDecimal uError = toBigDecimal(rs.getObject(9));
10366+
BigDecimal lower = toBigDecimal(rs.getObject(10));
10367+
BigDecimal lError = toBigDecimal(rs.getObject(11));
1032210368

1032310369
if (isEmpty(name))
1032410370
{

0 commit comments

Comments
 (0)