PostgreSqlTsVectorToStringPropertyHandler
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
NpgsqlTsVectoris 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 | PostgreSqlTsVectorToStringPropertyHandler |
| Namespace | RepoDb.PropertyHandlers.PostgreSql |
| Package | RepoDb.PostgreSql |
| 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(PostgreSqlTsVectorToStringPropertyHandler))]
public string SearchVector { get; set; }
}
Once bound, the library invokes the handler automatically when the property is read or written.
using (var connection = new NpgsqlConnection(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<PostgreSqlTsVectorToStringPropertyHandler>(e => e.SearchVector);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Article, PostgreSqlTsVectorToStringPropertyHandler>(e => e.SearchVector, new PostgreSqlTsVectorToStringPropertyHandler(), true);
Type Level Mapping
To apply the handler to every property of the type string:
PropertyHandlerMapper.Add<string, PostgreSqlTsVectorToStringPropertyHandler>(new PostgreSqlTsVectorToStringPropertyHandler(), true);
A type level mapping is process-wide: it is applied to every
stringproperty of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless everystringproperty maps to atsvectorcolumn.