Class CompositeCodec<Z>
- Type Parameters:
Z- the composite type
- All Implemented Interfaces:
Codec<Z>
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)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classDescribes a single field inside a PostgreSQL composite type.Nested classes/interfaces inherited from interface io.codemine.java.postgresql.codecs.Codec
Codec.DecodingException, Codec.ParsingResult<A> -
Field Summary
Fields inherited from interface io.codemine.java.postgresql.codecs.Codec
BIT, BOOL, BOX, BPCHAR, BYTEA, CHAR, CIDR, CIRCLE, CITEXT, DATE, DATEMULTIRANGE, DATERANGE, FLOAT4, FLOAT8, HSTORE, INET, INT2, INT4, INT4MULTIRANGE, INT4RANGE, INT8, INT8MULTIRANGE, INT8RANGE, INTERVAL, JSON, JSONB, LINE, LSEG, MACADDR, MACADDR8, MONEY, NUMERIC, NUMMULTIRANGE, NUMRANGE, OID, PATH, POINT, POLYGON, TEXT, TIME, TIMESTAMP, TIMESTAMPTZ, TIMETZ, TSMULTIRANGE, TSRANGE, TSTZMULTIRANGE, TSTZRANGE, TSVECTOR, UUID, VARBIT, VARCHAR -
Constructor Summary
ConstructorsConstructorDescriptionCompositeCodec(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.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.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.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.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.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.CompositeCodec(String schema, String name, Function<A, Z> construct, CompositeCodec.Field<Z, A> fieldA) Creates a 1-field composite codec.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. -
Method Summary
Modifier and TypeMethodDescriptiondecodeInBinary(ByteBuffer buf, int length) Decodes a composite value from the PostgreSQL binary composite format.decodeInText(CharSequence input, int offset) Parses a PostgreSQL text-format literal of type A frominputstarting atoffset.voidencodeInBinary(Z value, ByteArrayOutputStream out) Encodes the composite value in the PostgreSQL binary composite format, appending the bytes toout.voidencodeInText(StringBuilder sb, Z value) Writes the given value to the string builder in PostgreSQL textual literal form.name()Returns the PostgreSQL type name (e.g.Generates a random value of type A, for testing purposes.schema()Returns the PostgreSQL schema name for this type, or empty string ornullif the type is in the default search path.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.codemine.java.postgresql.codecs.Codec
arrayOid, decodeInBinaryFromBytes, decodeInTextFromString, dimensions, encodeInBinaryToByteBuffer, encodeInBinaryToBytes, encodeInTextToString, inDim, jdbcType, map, oid, scalarOid, typeSig, withType, withType
-
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 pathname- PostgreSQL composite type nameconstruct- constructor functionfieldA- 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 fieldB- type of the second field- Parameters:
schema- PostgreSQL schema name, or empty/null for default search pathname- PostgreSQL composite type nameconstruct- curried constructor functionfieldA- first field descriptorfieldB- 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 fieldB- type of the second fieldC- type of the third field- Parameters:
schema- PostgreSQL schema name, or empty/null for default search pathname- PostgreSQL composite type nameconstruct- curried constructor functionfieldA- first field descriptorfieldB- second field descriptorfieldC- 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 fieldB- type of the second fieldC- type of the third fieldD- type of the fourth field- Parameters:
schema- PostgreSQL schema name, or empty/null for default search pathname- PostgreSQL composite type nameconstruct- curried constructor functionfieldA- first field descriptorfieldB- second field descriptorfieldC- third field descriptorfieldD- 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 fieldB- type of the second fieldC- type of the third fieldD- type of the fourth fieldE- type of the fifth field- Parameters:
schema- PostgreSQL schema name, or empty/null for default search pathname- PostgreSQL composite type nameconstruct- curried constructor functionfieldA- first field descriptorfieldB- second field descriptorfieldC- third field descriptorfieldD- fourth field descriptorfieldE- 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 fieldB- type of the second fieldC- type of the third fieldD- type of the fourth fieldE- type of the fifth fieldF- type of the sixth field- Parameters:
schema- PostgreSQL schema name, or empty/null for default search pathname- PostgreSQL composite type nameconstruct- curried constructor functionfieldA- first field descriptorfieldB- second field descriptorfieldC- third field descriptorfieldD- fourth field descriptorfieldE- fifth field descriptorfieldF- 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 fieldB- type of the second fieldC- type of the third fieldD- type of the fourth fieldE- type of the fifth fieldF- type of the sixth fieldG- type of the seventh field- Parameters:
schema- PostgreSQL schema name, or empty/null for default search pathname- PostgreSQL composite type nameconstruct- curried constructor functionfieldA- first field descriptorfieldB- second field descriptorfieldC- third field descriptorfieldD- fourth field descriptorfieldE- fifth field descriptorfieldF- sixth field descriptorfieldG- 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
constructfunction receives anObject[]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 pathname- PostgreSQL composite type nameconstruct- function that maps anObject[]of decoded field values toZfields- field descriptors in declaration order
-
-
Method Details
-
schema
Description copied from interface:CodecReturns the PostgreSQL schema name for this type, or empty string ornullif the type is in the default search path. -
name
Description copied from interface:CodecReturns the PostgreSQL type name (e.g."int4","text"). -
encodeInText
Description copied from interface:CodecWrites 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:
encodeInTextin interfaceCodec<Z>
-
decodeInText
public Codec.ParsingResult<Z> decodeInText(CharSequence input, int offset) throws Codec.DecodingException Description copied from interface:CodecParses a PostgreSQL text-format literal of type A frominputstarting atoffset.The input must be a non-null
CharSequenceholding the raw text as returned by the PostgreSQL server (e.g. the string value of a column obtained viaResultSet.getString(int)). Passing theStringdirectly avoids an extra copy compared to converting to achar[]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.DecodingExceptionif the input cannot be interpreted as a valid literal of type A.- Specified by:
decodeInTextin interfaceCodec<Z>- Throws:
Codec.DecodingException
-
encodeInBinary
Encodes the composite value in the PostgreSQL binary composite format, appending the bytes toout.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:
encodeInBinaryin interfaceCodec<Z>
-
decodeInBinary
Decodes a composite value from the PostgreSQL binary composite format.- Specified by:
decodeInBinaryin interfaceCodec<Z>- Throws:
Codec.DecodingException- if the binary data is malformed
-
random
Description copied from interface:CodecGenerates a random value of type A, for testing purposes. The providedRandominstance should be used as the source of randomness, and the generated values should cover a wide range of possible inputs, including edge cases.The
sizeparameter 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.
-