rel8 icon indicating copy to clipboard operation
rel8 copied to clipboard

Add EXTRACT function to Rel8.Expr.Time

Open guaraqe opened this issue 1 year ago • 3 comments

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.

guaraqe avatar May 12 '23 11:05 guaraqe

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.

ocharles avatar Jul 07 '23 18:07 ocharles

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

idontgetoutmuch avatar Nov 20 '23 11:11 idontgetoutmuch

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)?

idontgetoutmuch avatar Nov 20 '23 12:11 idontgetoutmuch