Skip to content

Commit

Permalink
Fix | Changes error message to be thrown when data out of range for D…
Browse files Browse the repository at this point in the history
…ECIMAL/NUMERIC types (#796)
  • Loading branch information
cheenamalhotra authored Sep 25, 2018
1 parent ca7141f commit e19c524
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum SQLState {
STATEMENT_CANCELED("HY008"),
DATA_EXCEPTION_NOT_SPECIFIC("22000"),
DATA_EXCEPTION_DATETIME_FIELD_OVERFLOW("22008"),
NUMERIC_DATA_OUT_OF_RANGE("22003"),
DATA_EXCEPTION_LENGTH_MISMATCH("22026"),
COL_NOT_FOUND("42S22");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ protected Object[][] getContents() {
{"R_invalidProperty", "This property is not supported: {0}."},
{"R_referencingFailedTSP", "The DataSource trustStore password needs to be set."},
{"R_valueOutOfRange", "One or more values is out of range of values for the {0} SQL Server data type."},
{"R_valueOutOfRangeSQLType", "One or more values is out of range of values for the {0} SQL type."},
{"R_integratedAuthenticationFailed", "Integrated authentication failed."},
{"R_permissionDenied", "Security violation. Permission to target \"{0}\" denied."},
{"R_getSchemaError", "Error getting default schema name."},
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -1083,8 +1083,16 @@ void execute(DTV dtv, Double doubleValue) throws SQLServerException {

void execute(DTV dtv, BigDecimal bigDecimalValue) throws SQLServerException {
if (DDC.exceedsMaxRPCDecimalPrecisionOrScale(bigDecimalValue)) {
String strValue = bigDecimalValue.toString();
tdsWriter.writeRPCStringUnicode(name, strValue, isOutParam, collation);
if (JDBCType.DECIMAL == dtv.getJdbcType() || JDBCType.NUMERIC == dtv.getJdbcType()) {
// Throw exception for DECIMAL and NUMERIC Datatypes
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_valueOutOfRangeSQLType"));
Object[] msgArgs = {dtv.getJdbcType()};
throw new SQLServerException(form.format(msgArgs), SQLState.NUMERIC_DATA_OUT_OF_RANGE,
DriverError.NOT_SET, null);
} else {
String strValue = bigDecimalValue.toString();
tdsWriter.writeRPCStringUnicode(name, strValue, isOutParam, collation);
}
} else {
tdsWriter.writeRPCBigDecimal(name, bigDecimalValue, outScale, isOutParam);
}
Expand Down Expand Up @@ -1758,7 +1766,7 @@ else if ((JDBCType.VARCHAR == jdbcTypeSetByUser) || (JDBCType.CHAR == jdbcTypeSe
SQLServerException.getErrString("R_valueOutOfRange"));
Object[] msgArgs = {cryptoMeta.getBaseTypeInfo().getSSTypeName()};
throw new SQLServerException(form.format(msgArgs),
SQLState.DATA_EXCEPTION_DATETIME_FIELD_OVERFLOW, DriverError.NOT_SET, null);
SQLState.NUMERIC_DATA_OUT_OF_RANGE, DriverError.NOT_SET, null);
}
} else {
// if the precision that user provides is smaller than the precision of the actual
Expand All @@ -1772,7 +1780,7 @@ else if ((JDBCType.VARCHAR == jdbcTypeSetByUser) || (JDBCType.CHAR == jdbcTypeSe
SQLServerException.getErrString("R_valueOutOfRange"));
Object[] msgArgs = {SSType.DECIMAL};
throw new SQLServerException(form.format(msgArgs),
SQLState.DATA_EXCEPTION_DATETIME_FIELD_OVERFLOW, DriverError.NOT_SET, null);
SQLState.NUMERIC_DATA_OUT_OF_RANGE, DriverError.NOT_SET, null);
}
}

Expand Down

0 comments on commit e19c524

Please sign in to comment.