rel8
rel8 copied to clipboard
Add EXTRACT function to Rel8.Expr.Time
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.
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
.
I need this so I think I will use @guaraqe's PR until something better comes along.
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)
?