Show / Hide Table of Contents

Class NpgsqlTypeHandler<TDefault>

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.

Inheritance
System.Object
NpgsqlTypeHandler
NpgsqlTypeHandler<TDefault>
BitStringHandler
ByteaHandler
TsQueryHandler
TsVectorHandler
PathHandler
PolygonHandler
HstoreHandler
JsonHandler
TextHandler
NpgsqlSimpleTypeHandler<TDefault>
Implements
INpgsqlTypeHandler<TDefault>
Inherited Members
NpgsqlTypeHandler.CreateConversionButNoParamException(Type)
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
Namespace: Npgsql.TypeHandling
Assembly: Npgsql.dll
Syntax
public abstract class NpgsqlTypeHandler<TDefault> : NpgsqlTypeHandler, INpgsqlTypeHandler<TDefault>
Type Parameters
Name Description
TDefault

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

Constructors

NpgsqlTypeHandler(PostgresType)

Constructs an NpgsqlTypeHandler<TDefault>.

Declaration
protected NpgsqlTypeHandler(PostgresType postgresType)
Parameters
Type Name Description
PostgresType postgresType

Methods

CreateArrayHandler(PostgresArrayType)

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

Declaration
public override ArrayHandler CreateArrayHandler(PostgresArrayType arrayBackendType)
Parameters
Type Name Description
PostgresArrayType arrayBackendType
Returns
Type Description
ArrayHandler
Overrides
NpgsqlTypeHandler.CreateArrayHandler(PostgresArrayType)

CreateRangeHandler(PostgresRangeType)

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

Declaration
public override RangeHandler CreateRangeHandler(PostgresRangeType rangeBackendType)
Parameters
Type Name Description
PostgresRangeType rangeBackendType
Returns
Type Description
RangeHandler
Overrides
NpgsqlTypeHandler.CreateRangeHandler(PostgresRangeType)

Read(NpgsqlReadBuffer, Int32, Boolean, FieldDescription)

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

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

The buffer from which to read.

System.Int32 len

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

System.Boolean async

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
Type Description
ValueTask<TDefault>

The fully-read value.

Read<TAny>(NpgsqlReadBuffer, Int32, FieldDescription)

Reads a value of type TAny with the given length from the provided buffer, with the assumption that it is entirely present in the provided memory buffer and no I/O will be required. This can save the overhead of async functions and improves performance.

Declaration
public override TAny Read<TAny>(NpgsqlReadBuffer buf, int len, FieldDescription fieldDescription = null)
Parameters
Type Name Description
NpgsqlReadBuffer buf

The buffer from which to read.

System.Int32 len

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

FieldDescription fieldDescription

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

Returns
Type Description
TAny

The fully-read value.

Type Parameters
Name Description
TAny
Overrides
NpgsqlTypeHandler.Read<TAny>(NpgsqlReadBuffer, Int32, FieldDescription)

Read<TAny>(NpgsqlReadBuffer, Int32, Boolean, 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, Int32, Boolean, FieldDescription) - but may do so in exceptional cases where reading of arbitrary types is required.

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

The buffer from which to read.

System.Int32 len

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

System.Boolean async

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
Type Description
ValueTask<TAny>

The fully-read value.

Type Parameters
Name Description
TAny
Overrides
NpgsqlTypeHandler.Read<TAny>(NpgsqlReadBuffer, Int32, Boolean, FieldDescription)

ValidateAndGetLength(TDefault, ref NpgsqlLengthCache, NpgsqlParameter)

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

Declaration
public abstract int ValidateAndGetLength(TDefault value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
Parameters
Type Name Description
TDefault value
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
Returns
Type Description
System.Int32

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.

Declaration
protected override int ValidateAndGetLength<TAny>(TAny value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
Parameters
Type Name Description
TAny value
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
Returns
Type Description
System.Int32
Type Parameters
Name Description
TAny
Overrides
Npgsql.TypeHandling.NpgsqlTypeHandler.ValidateAndGetLength<TAny>(TAny, Npgsql.NpgsqlLengthCache, Npgsql.NpgsqlParameter)

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.

Declaration
protected override int ValidateObjectAndGetLength(object value, ref NpgsqlLengthCache lengthCache, NpgsqlParameter parameter)
Parameters
Type Name Description
System.Object value
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
Returns
Type Description
System.Int32
Overrides
NpgsqlTypeHandler.ValidateObjectAndGetLength(Object, ref NpgsqlLengthCache, NpgsqlParameter)

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

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

Declaration
public abstract Task Write(TDefault value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
Parameters
Type Name Description
TDefault value
NpgsqlWriteBuffer buf
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
System.Boolean async
Returns
Type Description
System.Threading.Tasks.Task

WriteObjectWithLength(Object, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean)

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

Declaration
protected override Task WriteObjectWithLength(object value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
Parameters
Type Name Description
System.Object value
NpgsqlWriteBuffer buf
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
System.Boolean async
Returns
Type Description
System.Threading.Tasks.Task
Overrides
NpgsqlTypeHandler.WriteObjectWithLength(Object, NpgsqlWriteBuffer, NpgsqlLengthCache, NpgsqlParameter, Boolean)

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

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 Npgsql.TypeHandling.NpgsqlTypeHandler`1.WriteWithLengthInternal``1(``0,Npgsql.NpgsqlWriteBuffer,Npgsql.NpgsqlLengthCache,Npgsql.NpgsqlParameter,System.Boolean)).

Declaration
protected virtual Task WriteWithLength<TAny>(TAny value, NpgsqlWriteBuffer buf, NpgsqlLengthCache lengthCache, NpgsqlParameter parameter, bool async)
Parameters
Type Name Description
TAny value
NpgsqlWriteBuffer buf
NpgsqlLengthCache lengthCache
NpgsqlParameter parameter
System.Boolean async
Returns
Type Description
System.Threading.Tasks.Task
Type Parameters
Name Description
TAny

Implements

INpgsqlTypeHandler<T>
In This Article
Back to top Generated by DocFX