MscmpSystDb.DbTypes protocol (mscmp_syst_db v0.1.0)

Defines the common functions which should be implemented for all custom DbTypes implemented in MscmpSystDb.

Summary

Types

t()

All the types that implement this protocol.

Functions

Compares two values and returns the effective operating describing the relationship.

Compares two values and tests the resulting effective operator against that supplied by the user returning true or false.

Types

@type t() :: term()

All the types that implement this protocol.

Functions

Link to this function

compare(left, right)

Compares two values and returns the effective operating describing the relationship.

Parameters

  • left - a range type or base type for use in the comparison which would be the left side when used with an infix operator such as >.

  • right - a range type or base type for use in the comparison which would be the right side when used with an infix operator such as >.

Examples

Comparing two ranges where the left and right overlap, but the left side is less than the right (returns "Less Than Overlapping").

iex> left_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 10,
...>     upper: 100
...>   }
iex> right_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 20,
...>     upper: 200
...>   }
iex> MscmpSystDb.DbTypes.compare(left_range, right_range)
:lto

Comparing a range and a base type value where the range contains the base type value (returns "Left Contains Right").

iex> left_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 10,
...>     upper: 100
...>   }
iex> MscmpSystDb.DbTypes.compare(left_range, 50)
:lcr

Comparing interval base types where the left is less than the right (returns "Less Than")

iex> left_interval =
...>   %MscmpSystDb.DbTypes.Interval{
...>     days: 1
...>   }
iex> right_interval =
...>   %MscmpSystDb.DbTypes.Interval{
...>     months: 1
...>   }
iex> MscmpSystDb.DbTypes.compare(left_interval, right_interval)
:lt
Link to this function

test_compare(left, right, operator)

@spec test_compare(t(), t(), MscmpSystDb.Types.db_type_comparison_operators()) ::
  boolean()

Compares two values and tests the resulting effective operator against that supplied by the user returning true or false.

Parameters

  • left - a range type or base type for use in the comparison which would be the left side when used with an infix operator such as >.

  • right - a range type or base type for use in the comparison which would be the right side when used with an infix operator such as >.

  • operator - the desired operator to test.

Examples

Comparing two ranges where the left and right overlap, but the left side is less than the right. In this case we match the desired "Less Than Overlapping" operator resulting in a return value of true.

iex> left_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 10,
...>     upper: 100
...>   }
iex> right_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 20,
...>     upper: 200
...>   }
iex> MscmpSystDb.DbTypes.test_compare(left_range, right_range, :lto)
true

Comparing a range and a base type value where the range contains the base type value, but the desired operator is "Greater Than" which does not match resulting in a return value of false.

iex> left_range =
...>   %MscmpSystDb.DbTypes.IntegerRange{
...>     lower: 10,
...>     upper: 100
...>   }
iex> MscmpSystDb.DbTypes.test_compare(left_range, 50, :gt)
false

Comparing interval base types where the intervals are equal and testing for equality resulting a true result.

iex> left_interval =
...>   %MscmpSystDb.DbTypes.Interval{
...>     secs: 100
...>   }
iex> right_interval =
...>   %MscmpSystDb.DbTypes.Interval{
...>     secs: 100
...>   }
iex> MscmpSystDb.DbTypes.test_compare(left_interval, right_interval, :eq)
true