ResultConverter

interface ResultConverter<S : Any, T : Any>

Turns a decoded database value into the Kotlin type the caller asked for.

This is the read half of the conversion SPI. By the time a converter runs, the codec has already decoded the column's bytes into S — an Int, a String, a PgComposite, a PgArray — and the converter's job is to reshape that into T. Register one on a session through TypeManager.registerResultConverter, or on a single query through registerResultConverter.

Converters are consulted in reverse registration order, so a later one wins over an earlier one, and lookup is keyed on supportedSourceClass. Registering under Any::class opts into being asked about every value, which is how the built-in reflective converters work; it also means canConvert has to be precise, since a converter that claims a value it cannot produce raises MappingException (CONVERSION_ERROR) naming the converter rather than mapping the value.

Whether a converter may touch the session it is reading from depends on what invoked it, because that decides whether the exchange with the server is still open. Under fetchObject*, fetchField* and the forEach* family the mapping happens as rows arrive, so a converter runs inside an unfinished exchange and issuing a query on that same session collides with it - the driver refuses the call with InvalidOperationException (CONNECTION_BUSY), surfacing as the cause of a MappingException (CONVERSION_ERROR) carrying the path to the column. fetchRows and its siblings hand back undecoded rows instead, so a converter reached later through Row.get runs after the exchange has finished and is free to query. Write a converter that needs the database against a second session and the distinction stops mattering.

Type Parameters

S

The decoded source type this converter accepts.

T

The Kotlin type it produces.

Inheritors

Properties

Link copied to clipboard

The decoded class this converter is indexed under. Use Any::class to be offered every value.

Functions

Link copied to clipboard
abstract fun canConvert(sourceClass: KClass<*>, expectedType: KType, sourceType: PgType, context: DeserializationContext): Boolean

Decides whether this converter handles the value at hand. Must return false unless convert can produce an instance of expectedType.

Link copied to clipboard
abstract fun convert(source: S, expectedType: KType, sourceType: PgType, context: DeserializationContext): T

Converts a value this converter has claimed.