Skip to content

Commit a9eeabb

Browse files
authored
Merge pull request #663 from aguibert/decimal-precision
Decimal precision fix
2 parents db11f4d + c9a48e2 commit a9eeabb

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

vertx-db2-client/src/main/java/io/vertx/db2client/impl/drda/DRDAQueryRequest.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package io.vertx.db2client.impl.drda;
1717

1818
import java.math.BigDecimal;
19+
import java.math.MathContext;
20+
import java.math.RoundingMode;
1921
import java.sql.ResultSet;
2022
import java.sql.RowId;
2123
import java.sql.Types;
@@ -27,6 +29,7 @@
2729
import java.util.Hashtable;
2830

2931
import io.netty.buffer.ByteBuf;
32+
import io.netty.buffer.ByteBufUtil;
3033

3134
public class DRDAQueryRequest extends DRDAConnectRequest {
3235

@@ -919,7 +922,7 @@ private void buildSQLDTA(int numColumns,
919922
}
920923

921924
buildSQLDTAGRP(numColumns, lidAndLengthOverrides, overrideExists, overrideMap);
922-
925+
923926
if (overrideExists) {
924927
buffer.writeBytes(FdocaConstants.MDD_SQLDTA_TOSEND);
925928
}
@@ -942,25 +945,28 @@ private void buildSQLDTAGRP(int numVars,
942945
Hashtable overrideMap) {
943946
int n = 0;
944947
int offset = 0;
948+
945949

946950
n = calculateColumnsInSQLDTAGRPtriplet(numVars);
947951
buildTripletHeader(((3 * n) + 3),
948952
FdocaConstants.NGDA_TRIPLET_TYPE,
949953
FdocaConstants.SQLDTAGRP_LID);
954+
950955

951956
do {
952957
writeLidAndLengths(lidAndLengthOverrides, n, offset, mddRequired, overrideMap);
953958
numVars -= n;
954959
if (numVars == 0) {
955960
break;
956961
}
957-
962+
958963
offset += n;
959964
n = calculateColumnsInSQLDTAGRPtriplet(numVars);
960965
buildTripletHeader(((3 * n) + 3),
961966
FdocaConstants.CPT_TRIPLET_TYPE,
962967
0x00);
963968
} while (true);
969+
964970
}
965971

966972
private int calculateColumnsInSQLDTAGRPtriplet(int numVars) {
@@ -1123,17 +1129,21 @@ private Hashtable computeProtocolTypesAndLengths(
11231129
}
11241130
else
11251131
{
1126-
// adjust scale if it is negative. Packed Decimal cannot handle
1127-
// negative scale. We don't want to change the original
1128-
// object so make a new one.
1129-
if (bigDecimal.scale() < 0)
1130-
{
1132+
if (bigDecimal.scale() < 0) {
11311133
bigDecimal = bigDecimal.setScale(0);
11321134
inputRow[i] = bigDecimal;
1133-
}
1135+
}
1136+
if (bigDecimal.precision() > parameterMetaData.sqlPrecision_[i]) {
1137+
bigDecimal = bigDecimal.round(new MathContext(parameterMetaData.sqlPrecision_[i]));
1138+
inputRow[i] = bigDecimal;
1139+
}
1140+
if (bigDecimal.scale() > parameterMetaData.sqlScale_[i]) {
1141+
bigDecimal = bigDecimal.setScale(parameterMetaData.sqlScale_[i], RoundingMode.HALF_UP);
1142+
inputRow[i] = bigDecimal;
1143+
}
11341144
scale = bigDecimal.scale();
1135-
precision = Decimal.computeBigDecimalPrecision(bigDecimal);
1136-
}
1145+
precision = bigDecimal.precision();
1146+
}
11371147
lidAndLengths[i][0] = DRDAConstants.DRDA_TYPE_NDECIMAL;
11381148
lidAndLengths[i][1] = (precision << 8) + // use precision above
11391149
(scale << 0);
@@ -1399,7 +1409,7 @@ private Hashtable computeProtocolTypesAndLengths(
13991409
lidAndLengths[i][0]--;
14001410
}
14011411
}
1402-
return overrideMap;
1412+
return overrideMap; // @AGG this is never used
14031413
// }
14041414
// catch ( SQLException se )
14051415
// {

vertx-db2-client/src/main/java/io/vertx/db2client/impl/drda/NetSqlca.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,31 @@ public static void throwSqlError(NetSqlca sqlca) {
123123
switch(sqlca.sqlCode_) {
124124
// The SQL syntax is invalid
125125
case SqlCode.INVALID_SQL_STATEMENT:
126-
throw new DB2Exception("The SQL syntax provided was invalid", SqlCode.INVALID_SQL_STATEMENT, sqlca.sqlState_);
126+
throw new DB2Exception("The SQL syntax provided was invalid", sqlca.sqlCode_, sqlca.sqlState_);
127127
// The object (table?) is not defined/available
128128
case SqlCode.OBJECT_NOT_DEFINED:
129129
if (sqlErrmc.length() > 0)
130-
throw new DB2Exception("The object '" + sqlErrmc + "' provided is not defined", SqlCode.OBJECT_NOT_DEFINED, sqlca.sqlState_);
130+
throw new DB2Exception("The object '" + sqlErrmc + "' provided is not defined", sqlca.sqlCode_, sqlca.sqlState_);
131131
else
132-
throw new DB2Exception("An object provided is not defined", SqlCode.OBJECT_NOT_DEFINED, sqlca.sqlState_);
132+
throw new DB2Exception("An object provided is not defined", sqlca.sqlCode_, sqlca.sqlState_);
133133
// The object (table?) is not defined/available
134134
case SqlCode.COLUMN_DOES_NOT_EXIST:
135135
if (sqlErrmc.length() > 0)
136-
throw new DB2Exception("The column '" + sqlErrmc + "' provided does not exist", SqlCode.COLUMN_DOES_NOT_EXIST, sqlca.sqlState_);
136+
throw new DB2Exception("The column '" + sqlErrmc + "' provided does not exist", sqlca.sqlCode_, sqlca.sqlState_);
137137
else
138-
throw new DB2Exception("A column provided does not exist", SqlCode.COLUMN_DOES_NOT_EXIST, sqlca.sqlState_);
138+
throw new DB2Exception("A column provided does not exist", sqlca.sqlCode_, sqlca.sqlState_);
139139
// Invalid database specified
140140
case SqlCode.DATABASE_NOT_FOUND:
141141
if (sqlErrmc.length() > 0)
142-
throw new DB2Exception("The database '" + sqlErrmc + "' provided was not found", SqlCode.DATABASE_NOT_FOUND, sqlca.sqlState_);
142+
throw new DB2Exception("The database '" + sqlErrmc + "' provided was not found", sqlca.sqlCode_, sqlca.sqlState_);
143143
else
144-
throw new DB2Exception("The database provided was not found", SqlCode.DATABASE_NOT_FOUND, sqlca.sqlState_);
144+
throw new DB2Exception("The database provided was not found", sqlca.sqlCode_, sqlca.sqlState_);
145+
case SqlCode.DATA_TYPE_INVALID_ATTR:
146+
if (sqlErrmc.length() > 0)
147+
throw new DB2Exception("The statement cannot be processed. The data type definition '" + sqlErrmc +
148+
"' specifies an invalid attribute such as precision, length, or scale.", sqlca.sqlCode_, sqlca.sqlState_);
149+
else
150+
throw new DB2Exception("The statement cannot be processed. A data type definition specifies an invalid attribute such as precision, length, or scale.", sqlca.sqlCode_, sqlca.sqlState_);
145151
default:
146152
throw new IllegalStateException("ERROR: " + sqlca.toString());
147153
}

vertx-db2-client/src/main/java/io/vertx/db2client/impl/drda/SqlCode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class SqlCode {
4040
public static final int OBJECT_NOT_DEFINED = -204;
4141
public static final int COLUMN_DOES_NOT_EXIST = -206;
4242

43+
public static final int DATA_TYPE_INVALID_ATTR = -604;
44+
4345
private final int code_;
4446

4547
SqlCode(int code) {

0 commit comments

Comments
 (0)