MscmpSystDb.DbTypes.Range protocol (mscmp_syst_db v0.1.0)

Defines the common functions which should be implemented for all database range types.

Summary

Types

t()

All the types that implement this protocol.

Functions

Compares a range with either a range or related base type and returns a map indicating the applicable comparison operator for the lower and upper bound of the range.

Extracts and returns the lower bound of a range.

Extracts and returns the upper bound of a range.

Types

@type t() :: term()

All the types that implement this protocol.

Functions

Link to this function

bounds_compare(left, right)

@spec bounds_compare(any(), any()) :: MscmpSystDb.Types.BoundsCompareResult.t()

Compares a range with either a range or related base type and returns a map indicating the applicable comparison operator for the lower and upper bound of the range.

Examples

Comparing two ranges where both the lower and upper values of the left range are less than the corresponding values of the right side.

iex> left_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 10,
...>     upper: 100
...>   }
iex> right_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 20,
...>     upper: 200
...>   }
iex> MscmpSystDb.DbTypes.Range.bounds_compare(left_range, right_range)
%MscmpSystDb.Types.BoundsCompareResult{lower_comparison: :lt, upper_comparison: :lt}

Comparing a range value on the left side with a base value on the right. In this case the left side value is less than the right side value, but equal to the upper left side value.

iex> left_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 10,
...>     upper: 100,
...>     upper_inclusive: false
...>   }
iex> MscmpSystDb.DbTypes.Range.bounds_compare(left_range, 99)
%MscmpSystDb.Types.BoundsCompareResult{lower_comparison: :lt, upper_comparison: :eq}

Comparing two ranges where the right side range is contained by the left side range. The left side lower bound is less than the right side lower bound and the left side upper bound is greater than the right side upper bound.

iex> left_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 10,
...>     upper: 100,
...>     upper_inclusive: false
...>   }
iex> right_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 20,
...>     upper: 50
...>   }
iex> MscmpSystDb.DbTypes.Range.bounds_compare(left_range, right_range)
%MscmpSystDb.Types.BoundsCompareResult{lower_comparison: :lt, upper_comparison: :gt}
@spec lower(any()) :: any()

Extracts and returns the lower bound of a range.

Examples

iex> range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 10,
...>     upper: 100,
...>     upper_inclusive: false
...>   }
iex> MscmpSystDb.DbTypes.Range.lower(range)
10
@spec upper(any()) :: any()

Extracts and returns the upper bound of a range.

Examples

iex> range =
...>   %MscmpSystDb.DbTypes.DecimalRange{
...>     lower: Decimal.new("11.55"),
...>     upper: Decimal.new("75.50"),
...>     upper_inclusive: false
...>   }
iex> MscmpSystDb.DbTypes.Range.upper(range)
%Decimal{coef: 7549, exp: -2, sign: 1}