ClickHouseDecimalToDecimalPropertyHandler

A property handler that maps the ClickHouse Decimal types into a decimal property, rejecting values that do not fit.

A property handler that maps the ClickHouse Decimal types (Decimal32, Decimal64, Decimal128 and Decimal256) into a decimal property. Values that are outside of the range of a decimal are rejected instead of being silently altered.

The Decimal128 and Decimal256 types can hold values that a decimal cannot. Such values are rejected with an OverflowException instead of being silently altered. Use ClickHouseDecimalToStringPropertyHandler to keep every digit of a wide Decimal.

Overview

Name Description
Class ClickHouseDecimalToDecimalPropertyHandler
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 0.
Set Property to database Converts the decimal into a ClickHouseDecimal, to be written into the Decimal column.

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 Decimal column.

Attribute

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

    [Map("amount"), PropertyHandler(typeof(ClickHouseDecimalToDecimalPropertyHandler))]
    public decimal Amount { 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, Amount = 1234.5678m });
    var invoice = connection.Query<Invoice>(e => e.Id == 1).First();
    // invoice.Amount is 1234.5678
}

Fluent Mapping

To configure via FluentMapper:

FluentMapper
    .Entity<Invoice>()
    .PropertyHandler<ClickHouseDecimalToDecimalPropertyHandler>(e => e.Amount);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Invoice, ClickHouseDecimalToDecimalPropertyHandler>(e => e.Amount, new ClickHouseDecimalToDecimalPropertyHandler(), true);

Type Level Mapping

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

PropertyHandlerMapper.Add<decimal, ClickHouseDecimalToDecimalPropertyHandler>(new ClickHouseDecimalToDecimalPropertyHandler(), 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 every decimal property maps to a Decimal column.

See Also