Class CompositeCodec<Z>

java.lang.Object
io.codemine.java.postgresql.codecs.CompositeCodec<Z>
Type Parameters:
Z - the composite type
All Implemented Interfaces:
Codec<Z>

public final class CompositeCodec<Z> extends Object implements Codec<Z>
Codec for PostgreSQL composite (row) types.

Supports both the PostgreSQL text literal format (val1,val2,...) and the binary composite wire format.

Binary format:

 int32  field_count
 [for each field]:
   int32  field_oid    (OID of the field type; 0 if not statically known)
   int32  field_length (-1 for NULL)
   byte[] field_data   (only present when field_length != -1)
 
  • Constructor Details

    • CompositeCodec

      public CompositeCodec(String schema, String name, Function<A,Z> construct, CompositeCodec.Field<Z,A> fieldA)
      Creates a 1-field composite codec.
      Type Parameters:
      A - type of the single field
      Parameters:
      schema - PostgreSQL schema name, or empty/null for default search path
      name - PostgreSQL composite type name
      construct - constructor function
      fieldA - single field descriptor
    • CompositeCodec

      public CompositeCodec(String schema, String name, Function<A,Function<B,Z>> construct, CompositeCodec.Field<Z,A> fieldA, CompositeCodec.Field<Z,B> fieldB)
      Creates a 2-field composite codec.
      Type Parameters:
      A - type of the first field
      B - type of the second field
      Parameters:
      schema - PostgreSQL schema name, or empty/null for default search path
      name - PostgreSQL composite type name
      construct - curried constructor function
      fieldA - first field descriptor
      fieldB - second field descriptor
    • CompositeCodec

      public CompositeCodec(String schema, String name, Function<A,Function<B,Function<C,Z>>> construct, CompositeCodec.Field<Z,A> fieldA, CompositeCodec.Field<Z,B> fieldB, CompositeCodec.Field<Z,C> fieldC)
      Creates a 3-field composite codec.
      Type Parameters:
      A - type of the first field
      B - type of the second field
      C - type of the third field
      Parameters:
      schema - PostgreSQL schema name, or empty/null for default search path
      name - PostgreSQL composite type name
      construct - curried constructor function
      fieldA - first field descriptor
      fieldB - second field descriptor
      fieldC - third field descriptor
    • CompositeCodec

      public CompositeCodec(String schema, String name, Function<A,Function<B,Function<C,Function<D,Z>>>> construct, CompositeCodec.Field<Z,A> fieldA, CompositeCodec.Field<Z,B> fieldB, CompositeCodec.Field<Z,C> fieldC, CompositeCodec.Field<Z,D> fieldD)
      Creates a 4-field composite codec.
      Type Parameters:
      A - type of the first field
      B - type of the second field
      C - type of the third field
      D - type of the fourth field
      Parameters:
      schema - PostgreSQL schema name, or empty/null for default search path
      name - PostgreSQL composite type name
      construct - curried constructor function
      fieldA - first field descriptor
      fieldB - second field descriptor
      fieldC - third field descriptor
      fieldD - fourth field descriptor
    • CompositeCodec

      public CompositeCodec(String schema, String name, Function<A,Function<B,Function<C,Function<D,Function<E,Z>>>>> construct, CompositeCodec.Field<Z,A> fieldA, CompositeCodec.Field<Z,B> fieldB, CompositeCodec.Field<Z,C> fieldC, CompositeCodec.Field<Z,D> fieldD, CompositeCodec.Field<Z,E> fieldE)
      Creates a 5-field composite codec.
      Type Parameters:
      A - type of the first field
      B - type of the second field
      C - type of the third field
      D - type of the fourth field
      E - type of the fifth field
      Parameters:
      schema - PostgreSQL schema name, or empty/null for default search path
      name - PostgreSQL composite type name
      construct - curried constructor function
      fieldA - first field descriptor
      fieldB - second field descriptor
      fieldC - third field descriptor
      fieldD - fourth field descriptor
      fieldE - fifth field descriptor
    • CompositeCodec

      public CompositeCodec(String schema, String name, Function<A,Function<B,Function<C,Function<D,Function<E,Function<F,Z>>>>>> construct, CompositeCodec.Field<Z,A> fieldA, CompositeCodec.Field<Z,B> fieldB, CompositeCodec.Field<Z,C> fieldC, CompositeCodec.Field<Z,D> fieldD, CompositeCodec.Field<Z,E> fieldE, CompositeCodec.Field<Z,F> fieldF)
      Creates a 6-field composite codec.
      Type Parameters:
      A - type of the first field
      B - type of the second field
      C - type of the third field
      D - type of the fourth field
      E - type of the fifth field
      F - type of the sixth field
      Parameters:
      schema - PostgreSQL schema name, or empty/null for default search path
      name - PostgreSQL composite type name
      construct - curried constructor function
      fieldA - first field descriptor
      fieldB - second field descriptor
      fieldC - third field descriptor
      fieldD - fourth field descriptor
      fieldE - fifth field descriptor
      fieldF - sixth field descriptor
    • CompositeCodec

      public CompositeCodec(String schema, String name, Function<A,Function<B,Function<C,Function<D,Function<E,Function<F,Function<G,Z>>>>>>> construct, CompositeCodec.Field<Z,A> fieldA, CompositeCodec.Field<Z,B> fieldB, CompositeCodec.Field<Z,C> fieldC, CompositeCodec.Field<Z,D> fieldD, CompositeCodec.Field<Z,E> fieldE, CompositeCodec.Field<Z,F> fieldF, CompositeCodec.Field<Z,G> fieldG)
      Creates a 7-field composite codec.
      Type Parameters:
      A - type of the first field
      B - type of the second field
      C - type of the third field
      D - type of the fourth field
      E - type of the fifth field
      F - type of the sixth field
      G - type of the seventh field
      Parameters:
      schema - PostgreSQL schema name, or empty/null for default search path
      name - PostgreSQL composite type name
      construct - curried constructor function
      fieldA - first field descriptor
      fieldB - second field descriptor
      fieldC - third field descriptor
      fieldD - fourth field descriptor
      fieldE - fifth field descriptor
      fieldF - sixth field descriptor
      fieldG - seventh field descriptor
    • CompositeCodec

      @SafeVarargs public CompositeCodec(String schema, String name, Function<Object[],Z> construct, CompositeCodec.Field<Z,?>... fields)
      Creates a composite codec for any number of fields using an untyped vararg array constructor.

      This constructor is intended for composite types with more than 7 fields, or whenever the fully-typed curried constructors are impractical. The construct function receives an Object[] whose elements correspond positionally to the supplied field descriptors.

      Note: this constructor is less safely typed than the arity-specific overloads. Callers are responsible for casting elements of the array to the correct types.

      Parameters:
      schema - PostgreSQL schema name, or empty/null for default search path
      name - PostgreSQL composite type name
      construct - function that maps an Object[] of decoded field values to Z
      fields - field descriptors in declaration order
  • 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<Z>
    • name

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

      public void encodeInText(StringBuilder sb, Z 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<Z>
    • decodeInText

      public Codec.ParsingResult<Z> 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<Z>
      Throws:
      Codec.DecodingException
    • encodeInBinary

      public void encodeInBinary(Z value, ByteArrayOutputStream out)
      Encodes the composite value in the PostgreSQL binary composite format, appending the bytes to out.

      Layout:

       int32  field_count
       [for each field]:
         int32  field_oid    (OID of the field type; 0 if not statically known)
         int32  field_length (-1 for NULL)
         byte[] field_data
       
      Specified by:
      encodeInBinary in interface Codec<Z>
    • decodeInBinary

      public Z decodeInBinary(ByteBuffer buf, int length) throws Codec.DecodingException
      Decodes a composite value from the PostgreSQL binary composite format.
      Specified by:
      decodeInBinary in interface Codec<Z>
      Throws:
      Codec.DecodingException - if the binary data is malformed
    • random

      public Z 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<Z>