on dev help sign in
Bound and range
commitc6e442e30b26d353945cb6e8bc88cff4aabcad23 authorEitan Chatav <eitan@EEM109LMBP-1.local> dateTue 9 Jul 2019, 13:11 (7 years ago) parentdb95c02
2 files changed+33−2
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Collection.hs+16 squeal-postgresql/src/Squeal/PostgreSQL/PG.hs+17−2
squeal-postgresql/src/Squeal/PostgreSQL/Expression/Collection.hs+16
@@ -33,8 +33,10 @@ module Squeal.PostgreSQL.Expression.Collection
3333 , unnest
3434 , row
3535 , field
36+ , range
3637 ) where
3738
39+import Data.Bool
3840import Data.String
3941import Data.Word (Word64)
4042
@@ -44,6 +46,7 @@ import Squeal.PostgreSQL.Alias
4446import Squeal.PostgreSQL.Expression
4547import Squeal.PostgreSQL.Expression.SetOf
4648import Squeal.PostgreSQL.List
49+import Squeal.PostgreSQL.PG
4750import Squeal.PostgreSQL.Render
4851import Squeal.PostgreSQL.Schema
4952
@@ -167,3 +170,16 @@ field
167170field td fld expr = UnsafeExpression $
168171 parenthesized (renderSQL expr <> "::" <> renderSQL td)
169172 <> "." <> renderSQL fld
173+
174+-- | >>> printSQL $ range (Closed (Just now)) (Open Nothing)
175+-- [now(), )
176+range
177+ :: Bound (Expression outer commons grp schemas params from ('NotNull ty))
178+ -> Bound (Expression outer commons grp schemas params from ('NotNull ty))
179+ -> Expression outer commons grp schemas params from (null ('PGrange ty))
180+range l u = UnsafeExpression $ commaSeparated [renderLower l, renderUpper u]
181+ where
182+ renderLower (Bound isClosed x) =
183+ bool "(" "[" isClosed <> maybe "" renderSQL x
184+ renderUpper (Bound isClosed x) =
185+ maybe "" renderSQL x <> bool ")" "]" isClosed
squeal-postgresql/src/Squeal/PostgreSQL/PG.hs+17−2
@@ -20,8 +20,9 @@ Stability: experimental
2020 , FunctionalDependencies
2121 , GADTs
2222 , LambdaCase
23− , OverloadedStrings
2423 , MultiParamTypeClasses
24+ , OverloadedStrings
25+ , PatternSynonyms
2526 , ScopedTypeVariables
2627 , TypeApplications
2728 , TypeFamilies
@@ -37,7 +38,7 @@ module Squeal.PostgreSQL.PG
3738 , NullPG
3839 , TuplePG
3940 , RowPG
40− -- * Storage newtypes
41+ -- * Storage types
4142 , Money (..)
4243 , Json (..)
4344 , Jsonb (..)
@@ -45,6 +46,9 @@ module Squeal.PostgreSQL.PG
4546 , Enumerated (..)
4647 , VarArray (..)
4748 , FixArray (..)
49+ , Bound (..)
50+ , pattern Closed
51+ , pattern Open
4852 -- * Type families
4953 , LabelsPG
5054 , DimPG
@@ -363,3 +367,14 @@ newtype FixArray arr = FixArray {getFixArray :: arr}
363367 deriving (Eq, Ord, Show, Read, GHC.Generic)
364368-- | `PGfixarray` @(@`DimPG` @hask) (@`FixPG` @hask)@
365369type instance PG (FixArray hask) = 'PGfixarray (DimPG hask) (FixPG hask)
370+
371+data Bound x = Bound
372+ { closedBound :: Bool
373+ , getBound :: Maybe x
374+ } deriving
375+ ( Eq, Ord, Show, Read, GHC.Generic
376+ , Functor, Foldable, Traversable )
377+pattern Closed, Open :: Maybe x -> Bound x
378+pattern Closed x = Bound True x
379+pattern Open x = Bound False x
380+type instance PG (Bound x, Bound x) = 'PGrange (PG x)