Class EnumCodec<E>

java.lang.Object
io.codemine.java.postgresql.codecs.EnumCodec<E>
Type Parameters:
E - the Java enum type
All Implemented Interfaces:
Codec<E>

public final class EnumCodec<E> extends Object implements Codec<E>
Codec for PostgreSQL ENUM types, mapping between Java enum constants and their corresponding PostgreSQL label strings.
  • Constructor Details

    • EnumCodec

      public EnumCodec(String schema, String name, Map<E,String> pgLabels)
      Creates a new codec for the PostgreSQL enum type name in schema.
      Parameters:
      schema - the PostgreSQL schema containing the enum type, or empty/null for the default search path
      name - the PostgreSQL enum type name
      pgLabels - mapping from each Java enum constant to its PostgreSQL label string
  • Method Details

    • schema

      public String schema()
      Description copied from interface: Codec
      Returns the PostgreSQL schema name for this type, or empty string or null if the type is in the default search path.
      Specified by:
      schema in interface Codec<E>
    • name

      public String name()
      Description copied from interface: Codec
      Returns the PostgreSQL type name (e.g. "int4", "text").
      Specified by:
      name in interface Codec<E>
    • encodeInText

      public void encodeInText(StringBuilder sb, E value)
      Description copied from interface: Codec
      Writes the given value to the string builder in PostgreSQL textual literal form.

      This is primarily used for encoding fields inside composite and array literals. The written form must be the canonical text representation accepted by PostgreSQL for the type.

      Specified by:
      encodeInText in interface Codec<E>
    • decodeInText

      public Codec.ParsingResult<E> decodeInText(CharSequence input, int offset) throws Codec.DecodingException
      Description copied from interface: Codec
      Parses a PostgreSQL text-format literal of type A from input starting at offset.

      The input must be a non-null CharSequence holding the raw text as returned by the PostgreSQL server (e.g. the string value of a column obtained via ResultSet.getString(int)). Passing the String directly avoids an extra copy compared to converting to a char[] first. NULL column values must be handled by the caller before invoking this method.

      Returns the parsed value together with the offset of the first character that was not consumed, allowing callers to continue parsing subsequent fields without copying the input. Throws Codec.DecodingException if the input cannot be interpreted as a valid literal of type A.

      Specified by:
      decodeInText in interface Codec<E>
      Throws:
      Codec.DecodingException
    • encodeInBinary

      public void encodeInBinary(E value, ByteArrayOutputStream out)
      Description copied from interface: Codec
      Encodes the given non-null value into the PostgreSQL binary wire format, appending the bytes directly to out.

      Appends exactly the binary payload for the value — no length prefix. The caller (e.g. CompositeCodec) is responsible for prepending the 4-byte int32 length header required by the PostgreSQL composite and array binary protocols.

      The byte order is always big-endian, as required by the PostgreSQL wire protocol.

      Specified by:
      encodeInBinary in interface Codec<E>
    • decodeInBinary

      public E decodeInBinary(ByteBuffer buf, int length) throws Codec.DecodingException
      Description copied from interface: Codec
      Decodes a value from the PostgreSQL binary wire format.

      buf must be a big-endian ByteBuffer positioned at the first byte of the value's payload. length is the number of bytes that make up the payload (as read from the preceding int32 length header). The method advances the buffer position by exactly length bytes.

      NULL handling (length == -1) must be performed by the caller before invoking this method.

      Specified by:
      decodeInBinary in interface Codec<E>
      Throws:
      Codec.DecodingException - if the binary data is malformed
    • random

      public E random(Random r, int size)
      Description copied from interface: Codec
      Generates a random value of type A, for testing purposes. The provided Random instance should be used as the source of randomness, and the generated values should cover a wide range of possible inputs, including edge cases.

      The size parameter follows the QuickCheck convention: it is a non-negative integer that controls the "size" of the generated value — for scalars it bounds the magnitude, for collections it bounds the number of elements, and for multidimensional arrays it is propagated uniformly to all sub-arrays so that the resulting array has a rectangular shape.

      Specified by:
      random in interface Codec<E>