on 4e90af3 help sign in
rel8/src/Rel8/Type/Array1D.hs
132 lines 3.7 kB history raw
1{-# language DataKinds #-}
2{-# language DeriveTraversable #-}
3{-# language DerivingStrategies #-}
4{-# language GeneralizedNewtypeDeriving #-}
5{-# language NamedFieldPuns #-}
6{-# language StandaloneKindSignatures #-}
7{-# language TypeFamilies #-}
8{-# language UndecidableInstances #-}
10{-# options_ghc -fno-warn-redundant-constraints #-}
12module Rel8.Type.Array1D
13 ( Array1D( Array1D )
14 , getArray1D
15 )
16where
18-- aeson
19import Data.Aeson
20 ( ToJSON
21 , ToJSON1
22 , ToJSONKey
23 , FromJSON
24 , FromJSON1
25 , FromJSONKey
26 )
28-- base
29import Control.Applicative ( Alternative, (<|>) )
30import Control.Monad ( MonadPlus )
31import Data.Functor.Classes ( Eq1, Ord1, Read1, Show1 )
32import Data.Kind ( Type )
33import GHC.Exts ( IsList )
34import Prelude hiding ( null )
36-- hasql
37import qualified Hasql.Decoders as Hasql
39-- opaleye
40import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
42-- rel8
43import Rel8.Expr.Opaleye ( zipPrimExprsWith )
44import Rel8.Expr.Serialize ( litExpr )
45import Rel8.Schema.Null ( Unnullify, Nullity( Null, NotNull ), Sql, nullable )
46import Rel8.Type ( DBType, typeInformation )
47import Rel8.Type.Eq ( DBEq )
48import Rel8.Type.Information ( TypeInformation(..) )
49import Rel8.Type.Monoid ( DBMonoid, memptyExpr )
50import Rel8.Type.Ord ( DBMax, DBMin, DBOrd )
51import Rel8.Type.Semigroup ( DBSemigroup, (<>.) )
53-- semigroupoids
54import Data.Functor.Alt ( Alt, (<!>) )
55import Data.Functor.Apply ( Apply )
56import Data.Functor.Bind ( Bind )
57import Data.Functor.Plus ( Plus )
59-- semialign
60import Data.Align ( Align )
61import Data.Semialign ( Semialign )
62import Data.Zip ( Repeat, Unzip, Zip )
65-- | A one dimensional array.
66newtype Array1D a = Array1D [a]
67 deriving stock Traversable
68 deriving newtype
69 ( Eq, Ord, Read, Show, Semigroup, Monoid, IsList
70 , Functor, Foldable
71 , Eq1, Ord1, Read1, Show1
72 , FromJSON1, ToJSON1, FromJSON, FromJSONKey, ToJSON, ToJSONKey
73 , Apply, Applicative, Alternative, Plus, Bind, Monad, MonadPlus
74 , Align, Semialign, Repeat, Unzip, Zip
75 )
78instance Alt Array1D where
79 (<!>) = (<|>)
82getArray1D :: Array1D a -> [a]
83getArray1D (Array1D a) = a
86type IsArray1D :: Type -> Bool
87type family IsArray1D a where
88 IsArray1D (Array1D _) = 'True
89 IsArray1D _ = 'False
92array1DTypeInformation :: IsArray1D (Unnullify a) ~ 'False
93 => Nullity a
94 -> TypeInformation (Unnullify a)
95 -> TypeInformation (Array1D a)
96array1DTypeInformation nullity info =
97 case info of
98 TypeInformation{ encode, decode, typeName } -> TypeInformation
99 { decode = case nullity of
100 Null -> Array1D <$> Hasql.listArray (Hasql.nullable decode)
101 NotNull -> Array1D <$> Hasql.listArray (Hasql.nonNullable decode)
102 , encode = case nullity of
103 Null -> Opaleye.ArrayExpr . fmap (maybe null encode) . getArray1D
104 NotNull -> Opaleye.ArrayExpr . fmap encode . getArray1D
105 , typeName = typeName <> "[]"
106 }
107 where
108 null = Opaleye.ConstExpr Opaleye.NullLit
111instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBType (Array1D a) where
112 typeInformation = array1DTypeInformation nullable typeInformation
115instance (Sql DBEq a, IsArray1D (Unnullify a) ~ 'False) => DBEq (Array1D a)
118instance (Sql DBOrd a, IsArray1D (Unnullify a) ~ 'False) => DBOrd (Array1D a)
121instance (Sql DBMax a, IsArray1D (Unnullify a) ~ 'False) => DBMax (Array1D a)
124instance (Sql DBMin a, IsArray1D (Unnullify a) ~ 'False) => DBMin (Array1D a)
127instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBSemigroup (Array1D a) where
128 (<>.) = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:||))
131instance (Sql DBType a, IsArray1D (Unnullify a) ~ 'False) => DBMonoid (Array1D a) where
132 memptyExpr = litExpr mempty