Table of Contents

Class NpgsqlTypeHandler<TDefault>

Namespace
Npgsql.TypeHandling
Assembly
Npgsql.dll

Base class for all type handlers, which read and write CLR types into their PostgreSQL binary representation. Unless your type is arbitrary-length, consider inheriting from NpgsqlSimpleTypeHandler<TDefault> instead.

public abstract class NpgsqlTypeHandler<TDefault> : NpgsqlTypeHandler, INpgsqlTypeHandler<TDefault>

Type Parameters

TDefault

The default CLR type that this handler will read and write. For example, calling GetValue(int) on a column with this handler will return a value with type TDefault. Type handlers can support additional types by implementing INpgsqlTypeHandler<T>.

Inheritance
NpgsqlTypeHandler<TDefault>
Implements
Derived
Inherited Members

Constructors

NpgsqlTypeHandler()

protected NpgsqlTypeHandler()

Methods

CreateArrayHandler(PostgresType)

Creates a type handler for arrays of this handler's type.

public override ArrayHandler CreateArrayHandler(PostgresType arrayBackendType)

Parameters

arrayBackendType PostgresType

Returns

ArrayHandler

CreateRangeHandler(PostgresType)

Creates a type handler for ranges of this handler's type.

public override RangeHandler CreateRangeHandler(PostgresType rangeBackendType)

Parameters

rangeBackendType PostgresType

Returns

RangeHandler

Read(NpgsqlReadBuffer, int, bool, FieldDescription)

Reads a value of type TDefault with the given length from the provided buffer, using either sync or async I/O.

public abstract ValueTask<TDefault> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)

Parameters

buf NpgsqlReadBuffer

The buffer from which to read.

len int

The byte length of the value. The buffer might not contain the full length, requiring I/O to be performed.

async bool

If I/O is required to read the full length of the value, whether it should be performed synchronously or asynchronously.

fieldDescription FieldDescription

Additional PostgreSQL information about the type, such as the length in varchar(30).

Returns

ValueTask<TDefault>

The fully-read value.

Read<TAny>(NpgsqlReadBuffer, int, bool, FieldDescription)

Reads a value of type TDefault with the given length from the provided buffer, using either sync or async I/O. Type handlers typically don't need to override this - override Read(NpgsqlReadBuffer, int, bool, FieldDescription) - but may do so in exceptional cases where reading of arbitrary types is required.

protected override ValueTask<TAny> Read<TAny>(NpgsqlReadBuffer buf, int len, bool async, FieldDescription fieldDescription = null)

Parameters

buf NpgsqlReadBuffer

The buffer from which to read.

len int

The byte length of the value. The buffer might not contain the full length, requiring I/O to be performed.

async bool

If I/O is required to read the full length of the value, whether it should be performed synchronously or asynchronously.

fieldDescription FieldDescription

Additional PostgreSQL information about the type, such as the length in varchar(30).

Returns

ValueTask<TAny>

The fully-read value.

Type Parameters

TAny

ValidateAndGetLength(TDefault, ref NpgsqlLengthCache, NpgsqlParameter)

Called to validate and get the length of a value of a generic NpgsqlParameter<T>.

public abstract int ValidateAndGetLength(TDefault value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)

Parameters

value TDefault
lengthCache NpgsqlLengthCache
parameter NpgsqlParameter

Returns

int

ValidateAndGetLength<TAny>(TAny, ref NpgsqlLengthCache, NpgsqlParameter)

Called to validate and get the length of a value of an arbitrary type. Checks that the current handler supports that type and throws an exception otherwise.

protected override int ValidateAndGetLength<TAny>(TAny value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)

Parameters

value TAny
lengthCache NpgsqlLengthCache
parameter NpgsqlParameter

Returns

int

Type Parameters

TAny

ValidateObjectAndGetLength(object, ref NpgsqlLengthCache, NpgsqlParameter)

Called to validate and get the length of a value of a non-generic NpgsqlParameter. Type handlers generally don't need to override this.

protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)

Parameters

value object
lengthCache NpgsqlLengthCache
parameter NpgsqlParameter

Returns

int

Write(TDefault, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, bool)

Called to write the value of a generic NpgsqlParameter<T>.

public abstract Task Write(TDefault value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)

Parameters

value TDefault
buf NpgsqlWriteBuffer
lengthCache NpgsqlLengthCache
parameter NpgsqlParameter
async bool

Returns

Task

WriteObjectWithLength(object, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, bool)

Called to write the value of a non-generic NpgsqlParameter. Type handlers generally don't need to override this.

protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)

Parameters

value object
buf NpgsqlWriteBuffer
lengthCache NpgsqlLengthCache
parameter NpgsqlParameter
async bool

Returns

Task

WriteWithLength<TAny>(TAny, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, bool)

Typically does not need to be overridden by type handlers, but may be needed in some cases (e.g. ArrayHandler. Note that this method assumes it can write 4 bytes of length (already verified by WriteWithLengthInternal<TAny>(TAny, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, bool)).

protected virtual Task WriteWithLength<TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)

Parameters

value TAny
buf NpgsqlWriteBuffer
lengthCache NpgsqlLengthCache
parameter NpgsqlParameter
async bool

Returns

Task

Type Parameters

TAny