ClickHouseDecimalToStringPropertyHandler
A property handler that maps the ClickHouse Decimal types into a string property that keeps every digit and the scale.
A property handler that maps the ClickHouse Decimal types (Decimal32, Decimal64, Decimal128 and Decimal256) into a string property. The string keeps every digit and the scale of the value, which a decimal cannot do for the wide Decimal128 and Decimal256 types.
Overview
| Name | Description |
|---|---|
| Class | ClickHouseDecimalToStringPropertyHandler |
| Namespace | RepoDb.PropertyHandlers.ClickHouse |
| Package | RepoDb.ClickHouse |
| Implements | IPropertyHandler<object, string> |
| Column type | The ClickHouse Decimal32, Decimal64, Decimal128 and Decimal256 types |
| Property type | string |
Conversions
| Method | Direction | Description |
|---|---|---|
| Get | Database to property | Converts the value returned by the driver into its exact, culture-invariant string. null and DBNull are returned as null. |
| Set | Property to database | Parses the culture-invariant string into a ClickHouseDecimal, to be written into the Decimal column. A null, empty or white-space value is written as null. |
Exceptions
| Exception | Thrown When |
|---|---|
FormatException |
The string being written is not a valid number. |
Usability
Bind the handler to the property that maps to a Decimal128 or Decimal256 column.
Attribute
public class Invoice
{
public int Id { get; set; }
[Map("total"), PropertyHandler(typeof(ClickHouseDecimalToStringPropertyHandler))]
public string Total { get; set; }
}
Once bound, the library invokes the handler automatically when the property is read or written.
using (var connection = new ClickHouseConnection(connectionString))
{
connection.Insert(new Invoice { Id = 1, Total = "12345678901234567890.123456789" });
var invoice = connection.Query<Invoice>(e => e.Id == 1).First();
// invoice.Total keeps every digit and the scale
}
Fluent Mapping
To configure via FluentMapper:
FluentMapper
.Entity<Invoice>()
.PropertyHandler<ClickHouseDecimalToStringPropertyHandler>(e => e.Total);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Invoice, ClickHouseDecimalToStringPropertyHandler>(e => e.Total, new ClickHouseDecimalToStringPropertyHandler(), true);
Type Level Mapping
To apply the handler to every property of the type string:
PropertyHandlerMapper.Add<string, ClickHouseDecimalToStringPropertyHandler>(new ClickHouseDecimalToStringPropertyHandler(), 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 aDecimalcolumn.