TsVectorToStringPropertyHandler

A property handler that maps the full-text search tsvector type into a string property.

A property handler that maps the full-text search tsvector type into a string property.

The parsing of the text into an NpgsqlTsVector is performed on the client by the driver, which marks it as unreliable for complex values (it cannot fully duplicate the PostgreSQL logic). Prefer simple, well-formed values, or use the server function (to_tsvector) in the SQL for complex ones.

Overview

Name Description
Class TsVectorToStringPropertyHandler
Namespace RepoDb.PropertyHandlers.EnterpriseDb
Package RepoDb.EnterpriseDb
Implements IPropertyHandler<object, string>
Column type The full-text search tsvector type
Property type string

Conversions

Method Direction Description
Get Database to property Converts the value returned by the driver (a string, or an NpgsqlTsVector) into its text form (for example 'cat':3 'fat':2). null and DBNull are returned as null.
Set Property to database Parses the text form into an NpgsqlTsVector, to be written into the tsvector column. A null value is written as null.

Exceptions

Exception Thrown When
ArgumentException The type of the value returned by the driver is not supported.

Usability

Bind the handler to the property that maps to a tsvector column.

Attribute

public class Article
{
    public int Id { get; set; }

    [Map("search_vector"), PropertyHandler(typeof(TsVectorToStringPropertyHandler))]
    public string SearchVector { get; set; }
}

Once bound, the library invokes the handler automatically when the property is read or written.

using (var connection = new EDBConnection(connectionString))
{
    connection.Insert(new Article { Id = 1, SearchVector = "'cat':3 'fat':2" });
    var article = connection.Query<Article>(e => e.Id == 1).First();
    // article.SearchVector is the text form of the tsvector value
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Article>()
    .PropertyHandler<TsVectorToStringPropertyHandler>(e => e.SearchVector);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Article, TsVectorToStringPropertyHandler>(e => e.SearchVector, new TsVectorToStringPropertyHandler(), true);

Type Level Mapping

To apply the handler to every property of the type string:

PropertyHandlerMapper.Add<string, TsVectorToStringPropertyHandler>(new TsVectorToStringPropertyHandler(), true);

A type level mapping is process-wide: it is applied to every string property of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless every string property maps to a tsvector column.

See Also