Skip to content

Commit

Permalink
Add support for date_trunc
Browse files Browse the repository at this point in the history
  • Loading branch information
njaremko committed Oct 10, 2023
1 parent 80e46d0 commit eac3bed
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Opaleye/Operators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ module Opaleye.Operators
, IntervalNum
, addInterval
, minusInterval
, TimestampPrecision(..)
, date_trunc
-- * Deprecated
)

Expand Down Expand Up @@ -137,6 +139,9 @@ import qualified Opaleye.Internal.HaskellDB.PrimQuery as HPQ

import qualified Data.Profunctor.Product.Default as D

import qualified Data.Text as Text
import qualified Data.Text.Encoding as TE

{-| Keep only the rows of a query satisfying a given condition, using an
SQL @WHERE@ clause. It is equivalent to the Haskell function
Expand Down Expand Up @@ -492,3 +497,25 @@ minusInterval = C.binOp (HPQ.:-)
-- | Current date and time (start of current transaction)
now :: F.Field T.SqlTimestamptz
now = Column $ HPQ.FunExpr "now" []

data TimestampPrecision =
MicrosecondsPrecision
| MillisecondsPrecision
| SecondPrecision
| MinutePrecision
| HourPrecision
| DayPrecision
| WeekPrecision
| MonthPrecision
| QuarterPrecision
| YearPrecision
| DecadePrecision
| CenturyPrecision
| MillenniumPrecision
deriving Show

precisionToExpr :: TimestampPrecision -> HPQ.PrimExpr
precisionToExpr p = HPQ.ConstExpr . HPQ.ByteStringLit . TE.encodeUtf8 . Text.dropEnd 9 . Text.pack $ show p

date_trunc :: TimestampPrecision -> F.Field T.SqlTimestamptz -> F.Field T.SqlTimestamptz
date_trunc p (Column e) = Column $ HPQ.FunExpr "date_trunc" [(precisionToExpr p), e]

0 comments on commit eac3bed

Please sign in to comment.