Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EXTRACT function to Rel8.Expr.Time #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Rel8/Expr/Time.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Rel8.Expr.Time
, addTime
, diffTime
, subtractTime
, unsafeExtractFromTime

-- * Working with @CalendarDiffTime@
, scaleInterval
Expand All @@ -24,6 +25,7 @@ module Rel8.Expr.Time
, week, weeks
, month, months
, year, years
, unsafeExtractFromInterval
) where

-- base
Expand All @@ -32,8 +34,10 @@ import Prelude

-- rel8
import Rel8.Expr ( Expr )
import Rel8.Expr.Function (binaryOperator, function)
import Rel8.Expr.Opaleye ( castExpr, unsafeCastExpr, unsafeLiteral )
import Rel8.Expr.Function ( binaryOperator, function )
import Rel8.Expr.Opaleye ( castExpr, unsafeCastExpr, unsafeLiteral, fromPrimExpr, toPrimExpr )
import Rel8.Type ( DBType )
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye

-- time
import Data.Time.Calendar ( Day )
Expand Down Expand Up @@ -90,6 +94,14 @@ diffTime = binaryOperator "-"
subtractTime :: Expr CalendarDiffTime -> Expr UTCTime -> Expr UTCTime
subtractTime = flip (binaryOperator "-")

-- | Extract a part of a point in time. See possibilities
-- [here](https://www.postgresqltutorial.com/postgresql-date-functions/postgresql-extract/).
-- This function is unsafe because you must decide yourself the output type.
unsafeExtractFromTime :: DBType a => String -> Expr UTCTime -> Expr a
unsafeExtractFromTime name expr =
castExpr $
fromPrimExpr $
Opaleye.FunExpr "EXTRACT" [Opaleye.FunExpr (name <> " FROM") [toPrimExpr expr]]

scaleInterval :: Expr Double -> Expr CalendarDiffTime -> Expr CalendarDiffTime
scaleInterval = binaryOperator "*"
Expand Down Expand Up @@ -167,3 +179,13 @@ years = (`scaleInterval` year)

singleton :: String -> Expr CalendarDiffTime
singleton unit = castExpr $ unsafeLiteral $ "'1 " ++ unit ++ "'"

-- | Extract a part of an interval. See possibilities
-- [here](https://www.postgresqltutorial.com/postgresql-date-functions/postgresql-extract/).
-- This function is unsafe because you must decide yourself the output type.
unsafeExtractFromInterval :: DBType a => String -> Expr CalendarDiffTime -> Expr a
unsafeExtractFromInterval name expr =
castExpr $
fromPrimExpr $
Opaleye.FunExpr "EXTRACT" [Opaleye.FunExpr (name <> " FROM") [toPrimExpr expr]]