@@ -86,6 +86,11 @@ module Squeal.PostgreSQL.Expression
86 , currentTimestamp
86 , currentTimestamp
87 , localTime
87 , localTime
88 , localTimestamp
88 , localTimestamp
89 -- ** Text
94 -- ** Text
@@ -208,13 +213,13 @@ import Data.ByteString.Lazy (toStrict)
208import Data.Function ((&))
213import Data.Function ((&))
209import Data.Semigroup hiding (All)
214import Data.Semigroup hiding (All)
210import qualified Data.Aeson as JSON
215import qualified Data.Aeson as JSON
212import Data.String
216import Data.String
213import Data.Word
217import Data.Word
214import Generics.SOP hiding (All, from)
218import Generics.SOP hiding (All, from)
215import GHC.Generics ((:*:) (..))
219import GHC.Generics ((:*:) (..))
216import GHC.OverloadedLabels
220import GHC.OverloadedLabels
217import GHC.TypeLits
221import GHC.TypeLits
218import Prelude hiding (id, (.))
223import Prelude hiding (id, (.))
220import qualified Data.ByteString as ByteString
225import qualified Data.ByteString as ByteString
@@ -636,7 +641,12 @@ instance ty `In` PGNum
636instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
641instance (ty `In` PGNum, ty `In` PGFloating) => Fractional
637 (Expression outer grp commons schemas params from (nullity ty)) where
642 (Expression outer grp commons schemas params from (nullity ty)) where
638 (/) = unsafeBinaryOp "/"
643 (/) = unsafeBinaryOp "/"
639− fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
644+ fromRational
648+ . showFFloat Nothing
641instance (ty `In` PGNum, ty `In` PGFloating) => Floating
651instance (ty `In` PGNum, ty `In` PGFloating) => Floating
642 (Expression outer grp commons schemas params from (nullity ty)) where
652 (Expression outer grp commons schemas params from (nullity ty)) where
@@ -2283,3 +2293,134 @@ nthValue
2283 -> WindowFunction outer grp commons schemas params from ('Null ty)
2293 -> WindowFunction outer grp commons schemas params from ('Null ty)
2284nthValue value nth = UnsafeWindowFunction $ "nth_value"
2294nthValue value nth = UnsafeWindowFunction $ "nth_value"
2285 <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])
2295 <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])
2298+Create date from year, month and day fields
2300+>>> printSQL (makeDate 1984 7 3)
2301+make_date(1984, 7, 3)
2304+ :: Expression outer grp commons schemas params from (nullity 'PGint4)
2306+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2308+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2310+ -> Expression outer grp commons schemas params from (nullity 'PGdate)
2311+makeDate y m d = UnsafeExpression $ "make_date" <>
2312+ parenthesized (renderCommaSeparated renderSQL (y :* m :* d :* Nil))
2315+Create time from hour, minute and seconds fields
2317+>>> printSQL (makeTime 8 15 23.5)
2318+make_time(8, 15, 23.5)
2321+ :: Expression outer grp commons schemas params from (nullity 'PGint4)
2323+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2325+ -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2327+ -> Expression outer grp commons schemas params from (nullity 'PGtime)
2328+makeTime h m s = UnsafeExpression $ "make_time" <>
2329+ parenthesized (renderCommaSeparated renderSQL (h :* m :* s :* Nil))
2332+Create timestamp from year, month, day, hour, minute and seconds fields
2334+>>> printSQL (makeTimestamp 2013 7 15 8 15 23.5)
2335+make_timestamp(2013, 7, 15, 8, 15, 23.5)
2338+ :: Expression outer grp commons schemas params from (nullity 'PGint4)
2340+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2342+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2344+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2346+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2348+ -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2350+ -> Expression outer grp commons schemas params from (nullity 'PGtimestamp)
2351+makeTimestamp y mon d h m s =
2352+ UnsafeExpression $ "make_timestamp" <> parenthesized
2353+ (renderCommaSeparated renderSQL (y :* mon :* d :* h :* m :* s :* Nil))
2356+Create timestamp with time zone from
2357+year, month, day, hour, minute and seconds fields;
2358+the current time zone is used
2360+>>> printSQL (makeTimestamptz 2013 7 15 8 15 23.5)
2361+make_timestamptz(2013, 7, 15, 8, 15, 23.5)
2364+ :: Expression outer grp commons schemas params from (nullity 'PGint4)
2366+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2368+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2370+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2372+ -> Expression outer grp commons schemas params from (nullity 'PGint4)
2374+ -> Expression outer grp commons schemas params from (nullity 'PGfloat8)
2376+ -> Expression outer grp commons schemas params from (nullity 'PGtimestamptz)
2377+makeTimestamptz y mon d h m s =
2378+ UnsafeExpression $ "make_timestamptz" <> parenthesized
2379+ (renderCommaSeparated renderSQL (y :* mon :* d :* h :* m :* s :* Nil))
2382+Affine space operations on time types.
2384+class TimeOp time diff | time -> diff where
2386+ >>> printSQL (makeDate 1984 7 3 !+ 365)
2387+ (make_date(1984, 7, 3) + 365)
2390+ :: Expression outer grp commons schemas params from (nullity time)
2391+ -> Expression outer grp commons schemas params from (nullity diff)
2392+ -> Expression outer grp commons schemas params from (nullity time)
2393+ (!+) = unsafeBinaryOp "+"
2395+ >>> printSQL (365 +! makeDate 1984 7 3)
2396+ (365 + make_date(1984, 7, 3))
2399+ :: Expression outer grp commons schemas params from (nullity diff)
2400+ -> Expression outer grp commons schemas params from (nullity time)
2401+ -> Expression outer grp commons schemas params from (nullity time)
2402+ (+!) = unsafeBinaryOp "+"
2404+ >>> printSQL (makeDate 1984 7 3 !- 365)
2405+ (make_date(1984, 7, 3) - 365)
2408+ :: Expression outer grp commons schemas params from (nullity time)
2409+ -> Expression outer grp commons schemas params from (nullity diff)
2410+ -> Expression outer grp commons schemas params from (nullity time)
2411+ (!-) = unsafeBinaryOp "-"
2413+ >>> printSQL (makeDate 1984 7 3 !-! currentDate)
2414+ (make_date(1984, 7, 3) - CURRENT_DATE)
2417+ :: Expression outer grp commons schemas params from (nullity time)
2418+ -> Expression outer grp commons schemas params from (nullity time)
2419+ -> Expression outer grp commons schemas params from (nullity diff)
2420+ (!-!) = unsafeBinaryOp "-"
2421+instance TimeOp 'PGtimestamp 'PGinterval
2422+instance TimeOp 'PGtimestamptz 'PGinterval
2423+instance TimeOp 'PGtime 'PGinterval
2424+instance TimeOp 'PGtimetz 'PGinterval
2425+instance TimeOp 'PGinterval 'PGinterval
2426+instance TimeOp 'PGdate 'PGint4