1{-# language DataKinds #-}
2{-# language DerivingStrategies #-}
3{-# language FlexibleContexts #-}
5{-# language RoleAnnotations #-}
6{-# language ScopedTypeVariables #-}
7{-# language StandaloneKindSignatures #-}
8{-# language TypeApplications #-}
9{-# language UndecidableInstances #-}
17import Data.Kind ( Type )
18import Data.String ( IsString, fromString )
19import Prelude hiding ( null )
22import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
25import Rel8.Expr.Null ( liftOpNullable, nullify )
26import Rel8.Expr.Opaleye
32import Rel8.Expr.Serialize ( litExpr )
33import Rel8.Schema.Nullability
34 ( Nullability( Nullable, NonNullable )
35 , Sql, nullabilization
37import Rel8.Type ( DBType )
38import Rel8.Type.Monoid ( DBMonoid, memptyExpr )
39import Rel8.Type.Num ( DBFractional, DBNum )
40import Rel8.Type.Semigroup ( DBSemigroup, (<>.) )
43-- | Typed SQL expressions.
44type role Expr representational
45type Expr :: Type -> Type
46newtype Expr a = Expr Opaleye.PrimExpr
50instance Sql DBSemigroup a => Semigroup (Expr a) where
51 (<>) = case nullabilization @a of
52 Nullable -> liftOpNullable (<>.)
54 {-# INLINABLE (<>) #-}
57instance Sql DBMonoid a => Monoid (Expr a) where
58 mempty = case nullabilization @a of
59 Nullable -> nullify memptyExpr
60 NonNullable -> memptyExpr
61 {-# INLINABLE mempty #-}
64instance (Sql IsString a, Sql DBType a) => IsString (Expr a) where
65 fromString = litExpr . case nullabilization @a of
66 Nullable -> Just . fromString
67 NonNullable -> fromString
70instance Sql DBNum a => Num (Expr a) where
71 (+) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:+))
72 (*) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:*))
73 (-) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:-))
75 abs = mapPrimExpr (Opaleye.UnExpr Opaleye.OpAbs)
76 negate = mapPrimExpr (Opaleye.UnExpr Opaleye.OpNegate)
78 signum = castExpr . mapPrimExpr (Opaleye.UnExpr (Opaleye.UnOpOther "SIGN"))
80 fromInteger = castExpr . fromPrimExpr . Opaleye.ConstExpr . Opaleye.IntegerLit
83instance Sql DBFractional a => Fractional (Expr a) where
84 (/) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:/))
87 castExpr . Expr . Opaleye.ConstExpr . Opaleye.NumericLit . realToFrac
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list