Interface Codec<A>
- Type Parameters:
A- the type of the value
- All Known Implementing Classes:
CompositeCodec,EnumCodec
Each codec supports two wire formats:
- Textual — the PostgreSQL text representation. Low-level:
encodeInText(java.lang.StringBuilder, A)anddecodeInText(java.lang.CharSequence, int). Convenience:encodeInTextToString(A)anddecodeInTextFromString(java.lang.String). The low-level methods are also used when encoding composite and array fields inside a composite/array literal. - Binary — the PostgreSQL binary wire format. Low-level:
encodeInBinary(A, java.io.ByteArrayOutputStream)anddecodeInBinary(java.nio.ByteBuffer, int). Convenience:encodeInBinaryToBytes(A),encodeInBinaryToByteBuffer(A), anddecodeInBinaryFromBytes(byte[]). Binary encoding is compact, unambiguous and required when assembling composite or array values in binary protocol mode.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classThrown when a text or binary value cannot be parsed into the expected type.static final classHolds the parsed value together with the offset of the first unconsumed character. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Codec<Multirange<LocalDate>> static final Codec<Multirange<Integer>> static final Codec<Multirange<Long>> static final Codec<com.fasterxml.jackson.databind.JsonNode> static final Codec<com.fasterxml.jackson.databind.JsonNode> static final Codec<BigDecimal> static final Codec<Multirange<BigDecimal>> static final Codec<Range<BigDecimal>> static final Codec<LocalDateTime> static final Codec<Multirange<LocalDateTime>> static final Codec<Range<LocalDateTime>> static final Codec<Multirange<Instant>> -
Method Summary
Modifier and TypeMethodDescriptiondefault intarrayOid()Returns the PostgreSQL array-type OID for this element type, or0if not known.bit(int n) Returns a codec for PostgreSQLbit(n)— a fixed-length bit string of exactlynbits.bpchar(int n) Returns a codec for PostgreSQLbpchar(n)— a fixed-length blank-padded character string of exactlyncharacters.decodeInBinary(ByteBuffer buf, int length) Decodes a value from the PostgreSQL binary wire format.default AdecodeInBinaryFromBytes(byte[] bytes) Decodes a value from a byte array in the PostgreSQL binary wire format.decodeInText(CharSequence input, int offset) Parses a PostgreSQL text-format literal of type A frominputstarting atoffset.default AdecodeInTextFromString(String input) Decodes a PostgreSQL text-format literal of type A from the given string.default intReturns the number of array dimensions for this codec.voidencodeInBinary(A value, ByteArrayOutputStream out) Encodes the given non-null value into the PostgreSQL binary wire format, appending the bytes directly toout.default ByteBufferencodeInBinaryToByteBuffer(A value) Convenience overload that encodes the value into aByteBuffer.default byte[]encodeInBinaryToBytes(A value) Convenience overload that encodes the value into a freshly-allocated byte array and returns it.voidencodeInText(StringBuilder sb, A value) Writes the given value to the string builder in PostgreSQL textual literal form.default StringencodeInTextToString(A value) Encodes the given non-null value as a PostgreSQL text literal and returns it as aString.inDim()Returns an array codec whose element type is this codec.default intjdbcType()Returns the JDBCTypesconstant that best represents this PostgreSQL type.default <B> Codec<B> Returns a new codec that maps values of type A to type B using the provided pair of mapping functions.static Codec<BigDecimal> money(int decimals) Returns a codec for PostgreSQLmoney— a monetary amount stored as a scaled integer.name()Returns the PostgreSQL type name (e.g.default intoid()Returns the OID for this type: array OID whendimensions()> 0, else scalar OID.Generates a random value of type A, for testing purposes.default intReturns the PostgreSQL base-type OID, or0if not statically known (e.g. user-defined composite types).default Stringschema()Returns the PostgreSQL schema name for this type, or empty string ornullif the type is in the default search path.default StringtypeSig()Returns the full PostgreSQL type signature, including schema if applicable.varbit(int n) Returns a codec for PostgreSQLvarbit(n)— a variable-length bit string of at mostnbits.varchar(int n) Returns a codec for PostgreSQLvarchar(n)— a variable-length character string of at mostncharacters.Returns a new codec for a PostgreSQL domain type based on this codec.Returns a new codec for a PostgreSQL domain type based on this codec, with explicit OIDs.
-
Field Details
-
INT4
-
TEXT
-
INET
-
MACADDR
-
BOOL
-
INT2
-
INT8
-
FLOAT4
-
FLOAT8
-
NUMERIC
-
BYTEA
-
UUID
-
JSON
-
JSONB
-
VARCHAR
-
BPCHAR
-
CHAR
-
OID
-
DATE
-
TIME
-
TIMETZ
-
TIMESTAMP
-
TIMESTAMPTZ
-
INTERVAL
-
POINT
-
LINE
-
LSEG
-
BOX
-
PATH
-
POLYGON
-
CIRCLE
-
CIDR
-
MACADDR8
-
BIT
-
VARBIT
-
CITEXT
-
TSVECTOR
-
HSTORE
-
LTREE
-
INT4RANGE
-
INT8RANGE
-
NUMRANGE
-
TSRANGE
-
TSTZRANGE
-
DATERANGE
-
INT4MULTIRANGE
-
INT8MULTIRANGE
-
NUMMULTIRANGE
-
TSMULTIRANGE
-
TSTZMULTIRANGE
-
DATEMULTIRANGE
-
-
Method Details
-
bit
Returns a codec for PostgreSQLbit(n)— a fixed-length bit string of exactlynbits.If
n <= 0, this returns the unparameterizedBITcodec. -
varbit
Returns a codec for PostgreSQLvarbit(n)— a variable-length bit string of at mostnbits.If
n <= 0, this returns the unparameterizedVARBITcodec. -
varchar
Returns a codec for PostgreSQLvarchar(n)— a variable-length character string of at mostncharacters.If
n <= 0, this returns the unparameterizedVARCHARcodec. -
money
Returns a codec for PostgreSQLmoney— a monetary amount stored as a scaled integer.The
decimalsparameter specifies the number of fractional decimal digits, which is determined by the database'slc_monetarylocale setting. The most common value is2(cents), but locales without a fractional unit (e.g. Japanese yen) use0.- Parameters:
decimals- the number of fractional digits (must be ≥ 0)
-
bpchar
Returns a codec for PostgreSQLbpchar(n)— a fixed-length blank-padded character string of exactlyncharacters. -
schema
Returns the PostgreSQL schema name for this type, or empty string ornullif the type is in the default search path. -
name
String name()Returns the PostgreSQL type name (e.g."int4","text"). -
dimensions
default int dimensions()Returns the number of array dimensions for this codec.0for scalar codecs;nfor n-dimensional array codecs. -
typeSig
Returns the full PostgreSQL type signature, including schema if applicable. -
inDim
Returns an array codec whose element type is this codec. The returned codec uses PostgreSQL's array literal syntax ({elem1,elem2,...}or{elem1;elem2;...}) for text format and the standard binary array header for binary format. -
scalarOid
default int scalarOid()Returns the PostgreSQL base-type OID, or0if not statically known (e.g. user-defined composite types).The OID is used inside binary-format array and composite headers to tag each element with its type.
-
arrayOid
default int arrayOid()Returns the PostgreSQL array-type OID for this element type, or0if not known. -
oid
default int oid()Returns the OID for this type: array OID whendimensions()> 0, else scalar OID. -
jdbcType
default int jdbcType()Returns the JDBCTypesconstant that best represents this PostgreSQL type. -
encodeInText
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.
-
encodeInTextToString
Encodes the given non-null value as a PostgreSQL text literal and returns it as aString. Convenience wrapper aroundencodeInText(StringBuilder, Object). -
decodeInText
Parses 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.- Throws:
Codec.DecodingException
-
decodeInTextFromString
Decodes a PostgreSQL text-format literal of type A from the given string. Convenience wrapper arounddecodeInText(CharSequence, int)that passes the full string starting at offset 0.- Throws:
Codec.DecodingException- if the input cannot be interpreted as a valid literal of type A
-
encodeInBinary
Encodes the given non-null value into the PostgreSQL binary wire format, appending the bytes directly toout.Appends exactly the binary payload for the value — no length prefix. The caller (e.g.
CompositeCodec) is responsible for prepending the 4-byteint32length header required by the PostgreSQL composite and array binary protocols.The byte order is always big-endian, as required by the PostgreSQL wire protocol.
- Throws:
UnsupportedOperationException- if binary encoding is not implemented for this type
-
encodeInBinaryToBytes
Convenience overload that encodes the value into a freshly-allocated byte array and returns it. Delegates toencodeInBinary(Object, ByteArrayOutputStream). -
encodeInBinaryToByteBuffer
Convenience overload that encodes the value into aByteBuffer. Delegates toencodeInBinaryToBytes(Object). -
decodeInBinary
Decodes a value from the PostgreSQL binary wire format.bufmust be a big-endianByteBufferpositioned at the first byte of the value's payload.lengthis the number of bytes that make up the payload (as read from the precedingint32length header). The method advances the buffer position by exactlylengthbytes.NULL handling (
length == -1) must be performed by the caller before invoking this method.- Throws:
Codec.DecodingException- if the binary data is malformedUnsupportedOperationException- if binary decoding is not implemented for this type
-
decodeInBinaryFromBytes
Decodes a value from a byte array in the PostgreSQL binary wire format. Convenience wrapper arounddecodeInBinary(ByteBuffer, int).- Throws:
Codec.DecodingException- if the binary data is malformedUnsupportedOperationException- if binary decoding is not implemented for this type
-
random
Generates 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. -
map
Returns a new codec that maps values of type A to type B using the provided pair of mapping functions. The returned codec encodes and decodes values of type B by delegating to this codec for the underlying A values.- Type Parameters:
B- the target value type- Parameters:
to- function mapping from A to Bfrom- function mapping from B back to A- Returns:
- a new codec for values of type B
-
withType
Returns a new codec for a PostgreSQL domain type based on this codec. The returned codec uses the providedschemaandnameas its type identity but delegates all encoding and decoding to this codec. The OIDs default to0(unknown), which is appropriate when the domain's OID is not statically known.Example — defining a domain
CREATE DOMAIN positive_amount AS numeric CHECK (VALUE > 0):Codec<BigDecimal> positiveAmount = Codec.NUMERIC.withType("", "positive_amount");- Parameters:
schema- the PostgreSQL schema, or empty/null for the default search pathname- the PostgreSQL domain type name- Returns:
- a new codec for the domain type
-
withType
Returns a new codec for a PostgreSQL domain type based on this codec, with explicit OIDs. Use this overload when the domain's OIDs are known ahead of time (e.g. looked up frompg_type).- Parameters:
schema- the PostgreSQL schema, or empty/null for the default search pathname- the PostgreSQL domain type namescalarOid- the PostgreSQL scalar OID for this domain typearrayOid- the PostgreSQL array OID for this domain type- Returns:
- a new codec for the domain type
-