ClickHouseDecimalToNullableDecimalPropertyHandler
A property handler that maps the ClickHouse Decimal types into a nullable decimal property, rejecting values that do not fit.
A property handler that maps the ClickHouse Decimal types (Decimal32, Decimal64, Decimal128 and Decimal256) into a Nullable<decimal> property. Values that are outside of the range of a decimal are rejected instead of being silently altered.
The
Decimal128andDecimal256types can hold values that adecimalcannot. Such values are rejected with anOverflowExceptioninstead of being silently altered. Use ClickHouseDecimalToStringPropertyHandler to keep every digit of a wideDecimal.
Overview
| Name | Description |
|---|---|
| Class | ClickHouseDecimalToNullableDecimalPropertyHandler |
| Namespace | RepoDb.PropertyHandlers.ClickHouse |
| Package | RepoDb.ClickHouse |
| Implements | IPropertyHandler<object, decimal?> |
| Column type | The ClickHouse Decimal32, Decimal64, Decimal128 and Decimal256 types |
| Property type | decimal? |
Conversions
| Method | Direction | Description |
|---|---|---|
| Get | Database to property | Converts the value returned by the driver (a decimal, a ClickHouseDecimal, or any value convertible to decimal) into a decimal?. null and DBNull are returned as null. |
| Set | Property to database | Converts the decimal into a ClickHouseDecimal, to be written into the Decimal column. A null value is written as null. |
Exceptions
| Exception | Thrown When |
|---|---|
OverflowException |
The value is outside of the range of a decimal. |
Usability
Bind the handler to the property that maps to a nullable Decimal column.
Attribute
public class Invoice
{
public int Id { get; set; }
[Map("discount"), PropertyHandler(typeof(ClickHouseDecimalToNullableDecimalPropertyHandler))]
public decimal? Discount { 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, Discount = null });
var invoice = connection.Query<Invoice>(e => e.Id == 1).First();
// invoice.Discount is null
}
Fluent Mapping
To configure via FluentMapper:
FluentMapper
.Entity<Invoice>()
.PropertyHandler<ClickHouseDecimalToNullableDecimalPropertyHandler>(e => e.Discount);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Invoice, ClickHouseDecimalToNullableDecimalPropertyHandler>(e => e.Discount, new ClickHouseDecimalToNullableDecimalPropertyHandler(), true);
Type Level Mapping
To apply the handler to every property of the type decimal?:
PropertyHandlerMapper.Add<decimal?, ClickHouseDecimalToNullableDecimalPropertyHandler>(new ClickHouseDecimalToNullableDecimalPropertyHandler(), true);
A type level mapping is process-wide: it is applied to every
decimal?property of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless everydecimal?property maps to aDecimalcolumn.