circuithub / rel8

Hey! Hey! Can u rel8?
https://rel8.readthedocs.io
Other
150 stars 38 forks source link

Add EXTRACT function to Rel8.Expr.Time #237

Open guaraqe opened 1 year ago

guaraqe commented 1 year ago

This adds unsafe EXTRACT functions to Rel8.Expr.Time, using the low-level Opaleye functions. Should solve #236.

In principle a safe function can also be done with a GADT, but that would need more work.

ocharles commented 12 months ago

I think I'd like to do this with some kind of GADT or something to track what is being extracted, rather than allowing any DBType.

idontgetoutmuch commented 7 months ago

I need this so I think I will use @guaraqe's PR until something better comes along.

idontgetoutmuch commented 7 months ago

And now I have

data Pair f = Pair
  { pairYear  :: Column f Double
  , pairMonth :: Column f Double
  , pairDayofMonth :: Column f Double
  }
  deriving stock (Generic)
  deriving anyclass (Rel8able)

deriving stock instance f ~ Result => Show (Pair f)

getYearMonth :: Query (Pair Expr)
getYearMonth = (\x -> let z = Rel8.fromDay x.dDate
                        in Pair { pairYear       = Rel8.unsafeExtractFromTime "YEAR" z
                                  , pairMonth      = Rel8.unsafeExtractFromTime "MONTH" z
                                  , pairDayofMonth = Rel8.unsafeExtractFromTime "DAY" z}) <$>
                 each daysInMonth

@ocharles if you are worried about type safety (which obviously we all are) what about some specific functions like toGregorian :: Day -> (Year, MonthOfYear, DayOfMonth)?