Class BigDecimalCodec

  • All Implemented Interfaces:
    Codec<java.math.BigDecimal>

    public class BigDecimalCodec
    extends java.lang.Object
    implements Codec<java.math.BigDecimal>
    Big decimal codec
    • Field Detail

      • MAX_BIG_DECIMAL_STRING_LENGTH

        public static final int MAX_BIG_DECIMAL_STRING_LENGTH
        Maximum length of a numeric string the driver will accept before parsing it into a BigDecimal. Comfortably above any legitimate numeric encoding (MariaDB DECIMAL maxes out at 65 digits, ASCII-formatted doubles fit in ~25 chars), while small enough that the O(n²) cost of new BigDecimal(String) stays sub-millisecond even for an at-cap input. Inputs longer than this are rejected with SQLDataException so attacker-controlled text columns can't be turned into a CPU-exhaustion vector.
        See Also:
        Constant Field Values
      • COMPATIBLE_TYPES

        private static final java.util.EnumSet<DataType> COMPATIBLE_TYPES
    • Constructor Detail

      • BigDecimalCodec

        public BigDecimalCodec()
    • Method Detail

      • parseBigDecimal

        public static java.math.BigDecimal parseBigDecimal​(java.lang.String str)
                                                    throws java.sql.SQLDataException
        Parse a numeric string into a BigDecimal, rejecting inputs longer than MAX_BIG_DECIMAL_STRING_LENGTH characters. Use this helper everywhere the driver would otherwise call new BigDecimal(<userControlledString>).
        Parameters:
        str - numeric text
        Returns:
        parsed BigDecimal
        Throws:
        java.sql.SQLDataException - if str is longer than the cap
        java.lang.NumberFormatException - if str isn't a valid number (caller may catch and rewrap with column-specific context)
      • className

        public java.lang.String className()
        Description copied from interface: Codec
        Codec native type
        Specified by:
        className in interface Codec<java.math.BigDecimal>
        Returns:
        code native return type
      • canDecode

        public boolean canDecode​(ColumnDecoder column,
                                 java.lang.Class<?> type)
        Description copied from interface: Codec
        If codec can decode this a server datatype to a java class type
        Specified by:
        canDecode in interface Codec<java.math.BigDecimal>
        Parameters:
        column - server datatype
        type - java return class
        Returns:
        true if codec can decode it
      • canEncode

        public boolean canEncode​(java.lang.Object value)
        Description copied from interface: Codec
        Can Codec encode the java object type
        Specified by:
        canEncode in interface Codec<java.math.BigDecimal>
        Parameters:
        value - java object type
        Returns:
        true if codec can encode java type
      • decodeText

        public java.math.BigDecimal decodeText​(ReadableByteBuf buf,
                                               MutableInt length,
                                               ColumnDecoder column,
                                               java.util.Calendar cal,
                                               Context context)
                                        throws java.sql.SQLDataException
        Description copied from interface: Codec
        Decode from a mysql packet text encoded a value to codec java type
        Specified by:
        decodeText in interface Codec<java.math.BigDecimal>
        Parameters:
        buf - mysql packet buffer
        length - encoded value length
        column - server column metadata
        cal - calendar
        context - connection context
        Returns:
        decoded value
        Throws:
        java.sql.SQLDataException - if unexpected error occurs during decoding
      • decodeBinary

        public java.math.BigDecimal decodeBinary​(ReadableByteBuf buf,
                                                 MutableInt length,
                                                 ColumnDecoder column,
                                                 java.util.Calendar cal,
                                                 Context context)
                                          throws java.sql.SQLDataException
        Description copied from interface: Codec
        Decode from a mysql packet binary encoded a value to codec java type
        Specified by:
        decodeBinary in interface Codec<java.math.BigDecimal>
        Parameters:
        buf - mysql packet buffer
        length - encoded value length
        column - server column metadata
        cal - calendar
        context - connection context
        Returns:
        decoded value
        Throws:
        java.sql.SQLDataException - if unexpected error occurs during decoding
      • encodeText

        public void encodeText​(Writer encoder,
                               Context context,
                               java.math.BigDecimal value,
                               java.util.Calendar cal,
                               java.lang.Long length)
                        throws java.io.IOException
        Description copied from interface: Codec
        Text encode value to writer
        Specified by:
        encodeText in interface Codec<java.math.BigDecimal>
        Parameters:
        encoder - writer
        context - connection context
        value - value to encode
        cal - calendar
        length - maximum value length
        Throws:
        java.io.IOException - if any socket error occurs
      • getApproximateTextProtocolLength

        public int getApproximateTextProtocolLength​(java.math.BigDecimal value,
                                                    java.lang.Long length)
        Description copied from interface: Codec
        Get approximate text length, or -1 if unknown
        Specified by:
        getApproximateTextProtocolLength in interface Codec<java.math.BigDecimal>
        Parameters:
        value - parameter object
        length - max length
        Returns:
        max length if known, -1 if unknown
      • encodeBinary

        public void encodeBinary​(Writer encoder,
                                 Context context,
                                 java.math.BigDecimal value,
                                 java.util.Calendar cal,
                                 java.lang.Long maxLength)
                          throws java.io.IOException
        Description copied from interface: Codec
        Binary encode value to writer
        Specified by:
        encodeBinary in interface Codec<java.math.BigDecimal>
        Parameters:
        encoder - writer
        context - connection context
        value - value to encode
        cal - calendar
        maxLength - maximum value length
        Throws:
        java.io.IOException - if any socket error occurs
      • getBinaryEncodeType

        public int getBinaryEncodeType()
        Description copied from interface: Codec
        Return server encoding data type
        Specified by:
        getBinaryEncodeType in interface Codec<java.math.BigDecimal>
        Returns:
        server encoding data type