Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigDecimal Precision/Scale Fix #1912

Merged
merged 10 commits into from
Sep 21, 2022
37 changes: 23 additions & 14 deletions src/main/java/com/microsoft/sqlserver/jdbc/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,14 @@ final class GetTypeDefinitionOp extends DTVExecuteOp {

private final Parameter param;
private final SQLServerConnection con;
private BigDecimal providedDecimal;

GetTypeDefinitionOp(Parameter param, SQLServerConnection con) {
this.param = param;
this.con = con;
}

private void setTypeDefinition(DTV dtv) {
private void setTypeDefinition(DTV dtv) throws SQLServerException {
switch (dtv.getJdbcType()) {
case TINYINT:
param.typeDefinition = SSType.TINYINT.toString();
Expand Down Expand Up @@ -505,34 +506,42 @@ private void setTypeDefinition(DTV dtv) {
// so, here, if the decimal parameter is encrypted and it is null and it is not outparameter
// then we set precision as the default precision instead of max precision
if (!isOutput()) {
param.typeDefinition = "decimal(" + SQLServerConnection.defaultDecimalPrecision + ", "
+ scale + ")";
param.typeDefinition = SSType.DECIMAL.toString() + "("
+ SQLServerConnection.defaultDecimalPrecision + ", " + scale + ")";
}
} else {
if (SQLServerConnection.defaultDecimalPrecision >= valueLength) {
param.typeDefinition = "decimal(" + SQLServerConnection.defaultDecimalPrecision + ","
+ scale + ")";
param.typeDefinition = SSType.DECIMAL.toString() + "("
+ SQLServerConnection.defaultDecimalPrecision + "," + scale + ")";

if (SQLServerConnection.defaultDecimalPrecision < (valueLength + scale)) {
param.typeDefinition = "decimal("
+ (SQLServerConnection.defaultDecimalPrecision + scale) + "," + scale + ")";
param.typeDefinition = SSType.DECIMAL.toString() + "("
+ (SQLServerConnection.defaultDecimalPrecision + scale) + "," + scale + ")";
}
} else {
param.typeDefinition = "decimal(" + SQLServerConnection.maxDecimalPrecision + ","
+ scale + ")";
param.typeDefinition = SSType.DECIMAL.toString() + "("
+ SQLServerConnection.maxDecimalPrecision + "," + scale + ")";
}
}

if (isOutput()) {
param.typeDefinition = "decimal(" + SQLServerConnection.maxDecimalPrecision + ", " + scale
+ ")";
param.typeDefinition = SSType.DECIMAL.toString() + "("
+ SQLServerConnection.maxDecimalPrecision + ", " + scale + ")";
}

if (userProvidesPrecision) {
param.typeDefinition = "decimal(" + valueLength + "," + scale + ")";
param.typeDefinition = SSType.DECIMAL.toString() + "(" + valueLength + "," + scale + ")";
}
} else
param.typeDefinition = "decimal(" + SQLServerConnection.maxDecimalPrecision + "," + scale + ")";
} else if (dtv.getJavaType() == JavaType.BIGDECIMAL
&& (providedDecimal = (BigDecimal) dtv.getValue(dtv.getJdbcType(), scale, null, null,
typeInfo, cryptoMeta, null, null)) != null
&& providedDecimal.precision() >= scale) {
param.typeDefinition = SSType.DECIMAL.toString() + "(" + providedDecimal.precision()
+ "," + scale + ")";
} else {
param.typeDefinition = SSType.DECIMAL.toString() + "("
+ SQLServerConnection.maxDecimalPrecision + "," + scale + ")";
}

break;

Expand Down
Loading