|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.r2dbc.postgresql.codec; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.r2dbc.postgresql.message.Format; |
| 21 | +import io.r2dbc.postgresql.type.PostgresqlObjectId; |
| 22 | +import io.r2dbc.postgresql.util.Assert; |
| 23 | +import io.r2dbc.postgresql.util.ByteBufUtils; |
| 24 | +import reactor.util.annotation.Nullable; |
| 25 | + |
| 26 | +import java.math.BigDecimal; |
| 27 | + |
| 28 | +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
| 29 | + |
| 30 | +/** |
| 31 | + * Utility methods to decode a numeric value. |
| 32 | + */ |
| 33 | +final class NumericDecodeUtils { |
| 34 | + |
| 35 | + private NumericDecodeUtils() { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Decode {@code buffer} to {@link Number} according to {@link PostgresqlObjectId}. |
| 40 | + * |
| 41 | + * @param buffer the data buffer |
| 42 | + * @param dataType the well-known {@link PostgresqlObjectId type OID} |
| 43 | + * @param format the data type {@link Format}, text or binary |
| 44 | + * @return the decoded number |
| 45 | + */ |
| 46 | + public static Number decodeNumber(ByteBuf buffer, PostgresqlObjectId dataType, @Nullable Format format) { |
| 47 | + |
| 48 | + Assert.requireNonNull(buffer, "byteBuf must not be null"); |
| 49 | + |
| 50 | + switch (dataType) { |
| 51 | + |
| 52 | + case NUMERIC: |
| 53 | + if (format == FORMAT_BINARY) { |
| 54 | + return decodeBinary(buffer); |
| 55 | + } |
| 56 | + return new BigDecimal(ByteBufUtils.decode(buffer)); |
| 57 | + |
| 58 | + case INT2: |
| 59 | + if (FORMAT_BINARY == format) { |
| 60 | + return buffer.readShort(); |
| 61 | + } |
| 62 | + return Short.parseShort(ByteBufUtils.decode(buffer)); |
| 63 | + case INT4: |
| 64 | + case OID: |
| 65 | + if (FORMAT_BINARY == format) { |
| 66 | + return buffer.readInt(); |
| 67 | + } |
| 68 | + return Integer.parseInt(ByteBufUtils.decode(buffer)); |
| 69 | + case INT8: |
| 70 | + if (FORMAT_BINARY == format) { |
| 71 | + return buffer.readLong(); |
| 72 | + } |
| 73 | + return Long.parseLong(ByteBufUtils.decode(buffer)); |
| 74 | + case FLOAT4: |
| 75 | + if (FORMAT_BINARY == format) { |
| 76 | + return buffer.readFloat(); |
| 77 | + } |
| 78 | + return Float.parseFloat(ByteBufUtils.decode(buffer)); |
| 79 | + case FLOAT8: |
| 80 | + if (FORMAT_BINARY == format) { |
| 81 | + return buffer.readDouble(); |
| 82 | + } |
| 83 | + return Double.parseDouble(ByteBufUtils.decode(buffer)); |
| 84 | + default: |
| 85 | + throw new UnsupportedOperationException(String.format("Cannot decode value for type %s, format %s", dataType, format)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static BigDecimal decodeBinary(ByteBuf byteBuf) { |
| 90 | + |
| 91 | + // extract values |
| 92 | + short numOfDigits = byteBuf.readShort(); |
| 93 | + if (numOfDigits == 0) { |
| 94 | + return BigDecimal.ZERO; |
| 95 | + } |
| 96 | + short weight = byteBuf.readShort(); |
| 97 | + short sign = byteBuf.readShort(); |
| 98 | + short scale = byteBuf.readShort(); |
| 99 | + short[] digits = new short[numOfDigits]; |
| 100 | + for (short i = 0; i < numOfDigits; i++) { |
| 101 | + digits[i] = byteBuf.readShort(); |
| 102 | + } |
| 103 | + |
| 104 | + StringBuilder builder = new StringBuilder(); |
| 105 | + // whole part |
| 106 | + builder.append(digits[0]); |
| 107 | + for (short i = 0; i < weight * 4; i++) { |
| 108 | + builder.append(0); |
| 109 | + } |
| 110 | + // decimal part |
| 111 | + if (scale > 0) { |
| 112 | + builder.append('.'); |
| 113 | + for (short i = 0; i < scale; i++) { |
| 114 | + builder.append(0); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + int expectedLength = builder.length(); |
| 119 | + int baseOffset = Short.toString(digits[0]).length(); |
| 120 | + |
| 121 | + for (short i = 1; i < numOfDigits; i++) { |
| 122 | + weight--; |
| 123 | + String temp = Short.toString(digits[i]); |
| 124 | + int offset = baseOffset + 4 * i - temp.length(); |
| 125 | + if (weight < 0) { |
| 126 | + offset++; // dot between whole and decimal parts |
| 127 | + } |
| 128 | + builder.replace(offset, offset + temp.length(), temp); |
| 129 | + } |
| 130 | + |
| 131 | + builder.setLength(expectedLength); // remove zeros from the end |
| 132 | + |
| 133 | + if (sign == 0) { |
| 134 | + return new BigDecimal(builder.toString()); |
| 135 | + } else { |
| 136 | + return new BigDecimal("-" + builder.toString()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments